TypeORM with Nativescript how to use a existing database?











up vote
0
down vote

favorite












Hey i'm developing an app with Nativescript-Angular and TypeORM and i already have a SQLite database created with data, so what i'm trying to do it's link my typeorm connection to the existing SQLite database. But it doesn't work i don't know why.



This is my code for the typeorm connection.



@Injectable()
export class ConnectionService {
constructor() {}
initConnection() {

(async () => {
try {
const connection = await createConnection({
name: "database.db",
database: "database.db",
type: "nativescript",
driver,
entities: [
//"~/entities/*.ts"
Answer,
AnswerType,
Consumption,
Institution,
Profile,
Question,
Questionnaire,
Section,
Session,
Solution,
Substance,
Unit,
User
],
logging: true
});

console.log("Connection Created");

// setting true will drop tables and recreate
await connection.synchronize(false);

console.log("Synchronized");

//Create a repository to load from the database
let answerRepository = connection.getRepository(Answer);
//Select all from Answer
let allAnswers = await answerRepository.find();
console.log("All the answers are: ", allAnswers);
} catch (err) {
console.log("Error");
console.error(err);
}
})();
}
}


This is my app.component.ts



import { Component } from "@angular/core";
import { ConnectionService } from
"./services/local/connection.service";
const SQLite = require("nativescript-sqlite");

@Component({
selector: "ns-app",
templateUrl: "app.component.html"
})
export class AppComponent {
constructor() {
if (!SQLite.exists("database.db")) {
SQLite.copyDatabase("database.db");
console.log("Database copied");
} else {
console.log("Database has been already copied!");
}
new SQLite("database.db", function(err, db) {
db.get("select * from answers where id=?", [1],
function(err, row) {
console.log("Row of data was: ", row);
});
});
//TypeORM
let connection = new ConnectionService();
connection.initConnection();
}
}


And for last, this is my output



JS: Database has been already copied!
JS: Row of data was: [1, Hombre, 2018-06-07 18:35:16, 2018-06-07
18:35:16]
JS: Angular is running in the development mode. Call
enableProdMode() to enable the production mode.
JS: Connection Created
JS: query: BEGIN TRANSACTION
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'table'
AND "name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'index'
AND "tbl_name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: PRAGMA table_info("answer")
JS: query: PRAGMA index_list("answer")
JS: query: PRAGMA foreign_key_list("answer")
JS: query: PRAGMA table_info("answer_type")
JS: query: PRAGMA index_list("answer_type")
JS: query: PRAGMA foreign_key_list("answer_type")
JS: query: PRAGMA table_info("consumption")
JS: query: PRAGMA index_list("consumption")
JS: query: PRAGMA foreign_key_list("consumption")
JS: query: PRAGMA table_info("institution")
JS: query: PRAGMA index_list("institution")
JS: query: PRAGMA foreign_key_list("institution")
JS: query: PRAGMA table_info("profile")
JS: query: PRAGMA index_list("profile")
JS: query: PRAGMA foreign_key_list("profile")
JS: query: PRAGMA table_info("question")
JS: query: PRAGMA index_list("question")
JS: query: PRAGMA foreign_key_list("question")
JS: query: PRAGMA table_info("questionnaire")
JS: query: PRAGMA index_list("questionnaire")
JS: query: PRAGMA foreign_key_list("questionnaire")
JS: query: PRAGMA table_info("section")
JS: query: PRAGMA index_list("section")
JS: query: PRAGMA foreign_key_list("section")
JS: query: PRAGMA table_info("session")
JS: query: PRAGMA index_list("session")
JS: query: PRAGMA foreign_key_list("session")
JS: query: PRAGMA table_info("solution")
JS: query: PRAGMA index_list("solution")
JS: query: PRAGMA foreign_key_list("solution")
JS: query: PRAGMA table_info("substance")
JS: query: PRAGMA index_list("substance")
JS: query: PRAGMA foreign_key_list("substance")
JS: query: PRAGMA table_info("unit")
JS: query: PRAGMA index_list("unit")
JS: query: PRAGMA foreign_key_list("unit")
JS: query: PRAGMA table_info("user")
JS: query: PRAGMA index_list("user")
JS: query: PRAGMA foreign_key_list("user")
JS: query: COMMIT
JS: Synchronized
JS: query: SELECT "Answer"."id" AS "Answer_id",
"Answer"."description" AS "Answer_description",
"Answer"."created_at"AS "Answer_created_at",
"Answer"."updated_at" AS "Answer_updated_at" FROM "answer" "
Answer"
JS: todas las answers de la bd:


So the result for the query with typeorm its empty, but if i execute the query just with sqlite it works, i think typeorm its not linking my existing database to the connection.
Any solution?










share|improve this question






















  • Are you using the commercial version of plugin, transactions are not supported in the free version.
    – Manoj
    Nov 9 at 18:26















up vote
0
down vote

favorite












Hey i'm developing an app with Nativescript-Angular and TypeORM and i already have a SQLite database created with data, so what i'm trying to do it's link my typeorm connection to the existing SQLite database. But it doesn't work i don't know why.



This is my code for the typeorm connection.



@Injectable()
export class ConnectionService {
constructor() {}
initConnection() {

(async () => {
try {
const connection = await createConnection({
name: "database.db",
database: "database.db",
type: "nativescript",
driver,
entities: [
//"~/entities/*.ts"
Answer,
AnswerType,
Consumption,
Institution,
Profile,
Question,
Questionnaire,
Section,
Session,
Solution,
Substance,
Unit,
User
],
logging: true
});

console.log("Connection Created");

// setting true will drop tables and recreate
await connection.synchronize(false);

console.log("Synchronized");

//Create a repository to load from the database
let answerRepository = connection.getRepository(Answer);
//Select all from Answer
let allAnswers = await answerRepository.find();
console.log("All the answers are: ", allAnswers);
} catch (err) {
console.log("Error");
console.error(err);
}
})();
}
}


This is my app.component.ts



import { Component } from "@angular/core";
import { ConnectionService } from
"./services/local/connection.service";
const SQLite = require("nativescript-sqlite");

@Component({
selector: "ns-app",
templateUrl: "app.component.html"
})
export class AppComponent {
constructor() {
if (!SQLite.exists("database.db")) {
SQLite.copyDatabase("database.db");
console.log("Database copied");
} else {
console.log("Database has been already copied!");
}
new SQLite("database.db", function(err, db) {
db.get("select * from answers where id=?", [1],
function(err, row) {
console.log("Row of data was: ", row);
});
});
//TypeORM
let connection = new ConnectionService();
connection.initConnection();
}
}


And for last, this is my output



JS: Database has been already copied!
JS: Row of data was: [1, Hombre, 2018-06-07 18:35:16, 2018-06-07
18:35:16]
JS: Angular is running in the development mode. Call
enableProdMode() to enable the production mode.
JS: Connection Created
JS: query: BEGIN TRANSACTION
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'table'
AND "name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'index'
AND "tbl_name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: PRAGMA table_info("answer")
JS: query: PRAGMA index_list("answer")
JS: query: PRAGMA foreign_key_list("answer")
JS: query: PRAGMA table_info("answer_type")
JS: query: PRAGMA index_list("answer_type")
JS: query: PRAGMA foreign_key_list("answer_type")
JS: query: PRAGMA table_info("consumption")
JS: query: PRAGMA index_list("consumption")
JS: query: PRAGMA foreign_key_list("consumption")
JS: query: PRAGMA table_info("institution")
JS: query: PRAGMA index_list("institution")
JS: query: PRAGMA foreign_key_list("institution")
JS: query: PRAGMA table_info("profile")
JS: query: PRAGMA index_list("profile")
JS: query: PRAGMA foreign_key_list("profile")
JS: query: PRAGMA table_info("question")
JS: query: PRAGMA index_list("question")
JS: query: PRAGMA foreign_key_list("question")
JS: query: PRAGMA table_info("questionnaire")
JS: query: PRAGMA index_list("questionnaire")
JS: query: PRAGMA foreign_key_list("questionnaire")
JS: query: PRAGMA table_info("section")
JS: query: PRAGMA index_list("section")
JS: query: PRAGMA foreign_key_list("section")
JS: query: PRAGMA table_info("session")
JS: query: PRAGMA index_list("session")
JS: query: PRAGMA foreign_key_list("session")
JS: query: PRAGMA table_info("solution")
JS: query: PRAGMA index_list("solution")
JS: query: PRAGMA foreign_key_list("solution")
JS: query: PRAGMA table_info("substance")
JS: query: PRAGMA index_list("substance")
JS: query: PRAGMA foreign_key_list("substance")
JS: query: PRAGMA table_info("unit")
JS: query: PRAGMA index_list("unit")
JS: query: PRAGMA foreign_key_list("unit")
JS: query: PRAGMA table_info("user")
JS: query: PRAGMA index_list("user")
JS: query: PRAGMA foreign_key_list("user")
JS: query: COMMIT
JS: Synchronized
JS: query: SELECT "Answer"."id" AS "Answer_id",
"Answer"."description" AS "Answer_description",
"Answer"."created_at"AS "Answer_created_at",
"Answer"."updated_at" AS "Answer_updated_at" FROM "answer" "
Answer"
JS: todas las answers de la bd:


So the result for the query with typeorm its empty, but if i execute the query just with sqlite it works, i think typeorm its not linking my existing database to the connection.
Any solution?










share|improve this question






















  • Are you using the commercial version of plugin, transactions are not supported in the free version.
    – Manoj
    Nov 9 at 18:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hey i'm developing an app with Nativescript-Angular and TypeORM and i already have a SQLite database created with data, so what i'm trying to do it's link my typeorm connection to the existing SQLite database. But it doesn't work i don't know why.



This is my code for the typeorm connection.



@Injectable()
export class ConnectionService {
constructor() {}
initConnection() {

(async () => {
try {
const connection = await createConnection({
name: "database.db",
database: "database.db",
type: "nativescript",
driver,
entities: [
//"~/entities/*.ts"
Answer,
AnswerType,
Consumption,
Institution,
Profile,
Question,
Questionnaire,
Section,
Session,
Solution,
Substance,
Unit,
User
],
logging: true
});

console.log("Connection Created");

// setting true will drop tables and recreate
await connection.synchronize(false);

console.log("Synchronized");

//Create a repository to load from the database
let answerRepository = connection.getRepository(Answer);
//Select all from Answer
let allAnswers = await answerRepository.find();
console.log("All the answers are: ", allAnswers);
} catch (err) {
console.log("Error");
console.error(err);
}
})();
}
}


This is my app.component.ts



import { Component } from "@angular/core";
import { ConnectionService } from
"./services/local/connection.service";
const SQLite = require("nativescript-sqlite");

@Component({
selector: "ns-app",
templateUrl: "app.component.html"
})
export class AppComponent {
constructor() {
if (!SQLite.exists("database.db")) {
SQLite.copyDatabase("database.db");
console.log("Database copied");
} else {
console.log("Database has been already copied!");
}
new SQLite("database.db", function(err, db) {
db.get("select * from answers where id=?", [1],
function(err, row) {
console.log("Row of data was: ", row);
});
});
//TypeORM
let connection = new ConnectionService();
connection.initConnection();
}
}


And for last, this is my output



JS: Database has been already copied!
JS: Row of data was: [1, Hombre, 2018-06-07 18:35:16, 2018-06-07
18:35:16]
JS: Angular is running in the development mode. Call
enableProdMode() to enable the production mode.
JS: Connection Created
JS: query: BEGIN TRANSACTION
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'table'
AND "name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'index'
AND "tbl_name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: PRAGMA table_info("answer")
JS: query: PRAGMA index_list("answer")
JS: query: PRAGMA foreign_key_list("answer")
JS: query: PRAGMA table_info("answer_type")
JS: query: PRAGMA index_list("answer_type")
JS: query: PRAGMA foreign_key_list("answer_type")
JS: query: PRAGMA table_info("consumption")
JS: query: PRAGMA index_list("consumption")
JS: query: PRAGMA foreign_key_list("consumption")
JS: query: PRAGMA table_info("institution")
JS: query: PRAGMA index_list("institution")
JS: query: PRAGMA foreign_key_list("institution")
JS: query: PRAGMA table_info("profile")
JS: query: PRAGMA index_list("profile")
JS: query: PRAGMA foreign_key_list("profile")
JS: query: PRAGMA table_info("question")
JS: query: PRAGMA index_list("question")
JS: query: PRAGMA foreign_key_list("question")
JS: query: PRAGMA table_info("questionnaire")
JS: query: PRAGMA index_list("questionnaire")
JS: query: PRAGMA foreign_key_list("questionnaire")
JS: query: PRAGMA table_info("section")
JS: query: PRAGMA index_list("section")
JS: query: PRAGMA foreign_key_list("section")
JS: query: PRAGMA table_info("session")
JS: query: PRAGMA index_list("session")
JS: query: PRAGMA foreign_key_list("session")
JS: query: PRAGMA table_info("solution")
JS: query: PRAGMA index_list("solution")
JS: query: PRAGMA foreign_key_list("solution")
JS: query: PRAGMA table_info("substance")
JS: query: PRAGMA index_list("substance")
JS: query: PRAGMA foreign_key_list("substance")
JS: query: PRAGMA table_info("unit")
JS: query: PRAGMA index_list("unit")
JS: query: PRAGMA foreign_key_list("unit")
JS: query: PRAGMA table_info("user")
JS: query: PRAGMA index_list("user")
JS: query: PRAGMA foreign_key_list("user")
JS: query: COMMIT
JS: Synchronized
JS: query: SELECT "Answer"."id" AS "Answer_id",
"Answer"."description" AS "Answer_description",
"Answer"."created_at"AS "Answer_created_at",
"Answer"."updated_at" AS "Answer_updated_at" FROM "answer" "
Answer"
JS: todas las answers de la bd:


So the result for the query with typeorm its empty, but if i execute the query just with sqlite it works, i think typeorm its not linking my existing database to the connection.
Any solution?










share|improve this question













Hey i'm developing an app with Nativescript-Angular and TypeORM and i already have a SQLite database created with data, so what i'm trying to do it's link my typeorm connection to the existing SQLite database. But it doesn't work i don't know why.



This is my code for the typeorm connection.



@Injectable()
export class ConnectionService {
constructor() {}
initConnection() {

(async () => {
try {
const connection = await createConnection({
name: "database.db",
database: "database.db",
type: "nativescript",
driver,
entities: [
//"~/entities/*.ts"
Answer,
AnswerType,
Consumption,
Institution,
Profile,
Question,
Questionnaire,
Section,
Session,
Solution,
Substance,
Unit,
User
],
logging: true
});

console.log("Connection Created");

// setting true will drop tables and recreate
await connection.synchronize(false);

console.log("Synchronized");

//Create a repository to load from the database
let answerRepository = connection.getRepository(Answer);
//Select all from Answer
let allAnswers = await answerRepository.find();
console.log("All the answers are: ", allAnswers);
} catch (err) {
console.log("Error");
console.error(err);
}
})();
}
}


This is my app.component.ts



import { Component } from "@angular/core";
import { ConnectionService } from
"./services/local/connection.service";
const SQLite = require("nativescript-sqlite");

@Component({
selector: "ns-app",
templateUrl: "app.component.html"
})
export class AppComponent {
constructor() {
if (!SQLite.exists("database.db")) {
SQLite.copyDatabase("database.db");
console.log("Database copied");
} else {
console.log("Database has been already copied!");
}
new SQLite("database.db", function(err, db) {
db.get("select * from answers where id=?", [1],
function(err, row) {
console.log("Row of data was: ", row);
});
});
//TypeORM
let connection = new ConnectionService();
connection.initConnection();
}
}


And for last, this is my output



JS: Database has been already copied!
JS: Row of data was: [1, Hombre, 2018-06-07 18:35:16, 2018-06-07
18:35:16]
JS: Angular is running in the development mode. Call
enableProdMode() to enable the production mode.
JS: Connection Created
JS: query: BEGIN TRANSACTION
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'table'
AND "name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: SELECT * FROM "sqlite_master" WHERE "type" = 'index'
AND "tbl_name" IN ('answer', 'answer_type', 'consumption',
'institution', 'profile', 'question', 'questionnaire', 'section',
'session', 'solution', 'substance', 'unit', 'user')
JS: query: PRAGMA table_info("answer")
JS: query: PRAGMA index_list("answer")
JS: query: PRAGMA foreign_key_list("answer")
JS: query: PRAGMA table_info("answer_type")
JS: query: PRAGMA index_list("answer_type")
JS: query: PRAGMA foreign_key_list("answer_type")
JS: query: PRAGMA table_info("consumption")
JS: query: PRAGMA index_list("consumption")
JS: query: PRAGMA foreign_key_list("consumption")
JS: query: PRAGMA table_info("institution")
JS: query: PRAGMA index_list("institution")
JS: query: PRAGMA foreign_key_list("institution")
JS: query: PRAGMA table_info("profile")
JS: query: PRAGMA index_list("profile")
JS: query: PRAGMA foreign_key_list("profile")
JS: query: PRAGMA table_info("question")
JS: query: PRAGMA index_list("question")
JS: query: PRAGMA foreign_key_list("question")
JS: query: PRAGMA table_info("questionnaire")
JS: query: PRAGMA index_list("questionnaire")
JS: query: PRAGMA foreign_key_list("questionnaire")
JS: query: PRAGMA table_info("section")
JS: query: PRAGMA index_list("section")
JS: query: PRAGMA foreign_key_list("section")
JS: query: PRAGMA table_info("session")
JS: query: PRAGMA index_list("session")
JS: query: PRAGMA foreign_key_list("session")
JS: query: PRAGMA table_info("solution")
JS: query: PRAGMA index_list("solution")
JS: query: PRAGMA foreign_key_list("solution")
JS: query: PRAGMA table_info("substance")
JS: query: PRAGMA index_list("substance")
JS: query: PRAGMA foreign_key_list("substance")
JS: query: PRAGMA table_info("unit")
JS: query: PRAGMA index_list("unit")
JS: query: PRAGMA foreign_key_list("unit")
JS: query: PRAGMA table_info("user")
JS: query: PRAGMA index_list("user")
JS: query: PRAGMA foreign_key_list("user")
JS: query: COMMIT
JS: Synchronized
JS: query: SELECT "Answer"."id" AS "Answer_id",
"Answer"."description" AS "Answer_description",
"Answer"."created_at"AS "Answer_created_at",
"Answer"."updated_at" AS "Answer_updated_at" FROM "answer" "
Answer"
JS: todas las answers de la bd:


So the result for the query with typeorm its empty, but if i execute the query just with sqlite it works, i think typeorm its not linking my existing database to the connection.
Any solution?







angular sqlite nativescript typeorm nativescript-angular






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 18:11









Humberto Villalpando

1




1












  • Are you using the commercial version of plugin, transactions are not supported in the free version.
    – Manoj
    Nov 9 at 18:26


















  • Are you using the commercial version of plugin, transactions are not supported in the free version.
    – Manoj
    Nov 9 at 18:26
















Are you using the commercial version of plugin, transactions are not supported in the free version.
– Manoj
Nov 9 at 18:26




Are you using the commercial version of plugin, transactions are not supported in the free version.
– Manoj
Nov 9 at 18:26

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231228%2ftypeorm-with-nativescript-how-to-use-a-existing-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231228%2ftypeorm-with-nativescript-how-to-use-a-existing-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()