2017-05-21 19:48:08 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { createConnection, getConnection } from 'ionic-typeorm'
|
|
|
|
import { Connection } from 'ionic-typeorm'
|
2017-05-23 07:01:18 +00:00
|
|
|
|
2017-05-21 22:00:38 +00:00
|
|
|
export { Repository } from 'ionic-typeorm'
|
2017-05-23 07:01:18 +00:00
|
|
|
export { QueryBuilder } from 'ionic-typeorm'
|
2017-05-21 19:48:08 +00:00
|
|
|
|
|
|
|
import { Consulta } from '../entities/Consulta';
|
|
|
|
import { Necessidade } from '../entities/Necessidade';
|
|
|
|
import { Planejamento } from '../entities/Planejamento';
|
|
|
|
import { Produto } from '../entities/Produto';
|
|
|
|
import { Supermercado } from '../entities/Supermercado';
|
|
|
|
import { UnidadeMedida } from '../entities/UnidadeMedida';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class OrmDatabase{
|
|
|
|
getConnection():Promise<Connection>{
|
|
|
|
return new Promise<Connection>((resolve, reject)=>{
|
|
|
|
try{
|
|
|
|
return resolve(getConnection());
|
|
|
|
}catch(e){
|
|
|
|
createConnection({
|
|
|
|
driver: {
|
|
|
|
type: "cordova-sqlite",
|
|
|
|
database: "masterdb",
|
|
|
|
extra: {
|
|
|
|
version: "1.0",
|
|
|
|
description: "master database"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
entities: [
|
|
|
|
UnidadeMedida,
|
|
|
|
Produto,
|
|
|
|
Necessidade,
|
|
|
|
Planejamento,
|
|
|
|
Consulta,
|
|
|
|
Supermercado,
|
|
|
|
],
|
|
|
|
logging: {
|
|
|
|
logFailedQueryError: true,
|
|
|
|
logQueries: true,
|
|
|
|
logSchemaCreation: true,
|
|
|
|
logOnlyFailedQueries: true
|
|
|
|
},
|
|
|
|
autoSchemaSync: true,
|
|
|
|
}).then(connection => {
|
|
|
|
return resolve(connection);
|
|
|
|
}, () => {
|
|
|
|
createConnection({
|
|
|
|
driver: {
|
|
|
|
type: "websql",
|
|
|
|
database: "masterdb",
|
|
|
|
extra: {
|
|
|
|
version: "1.0",
|
|
|
|
description: "master database",
|
|
|
|
size: 2 * 1024 * 1024
|
|
|
|
}
|
|
|
|
},
|
|
|
|
entities: [
|
|
|
|
UnidadeMedida,
|
|
|
|
Produto,
|
|
|
|
Necessidade,
|
|
|
|
Planejamento,
|
|
|
|
Consulta,
|
|
|
|
Supermercado,
|
|
|
|
],
|
|
|
|
logging: {
|
|
|
|
logFailedQueryError: true,
|
|
|
|
logQueries: true,
|
|
|
|
logSchemaCreation: true,
|
|
|
|
logOnlyFailedQueries: true
|
|
|
|
},
|
|
|
|
autoSchemaSync: true,
|
|
|
|
}).then(connection => {
|
|
|
|
return resolve(connection);
|
|
|
|
}, reject)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-05-28 22:15:08 +00:00
|
|
|
dropDatabase(){
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
this.getConnection().then(connection =>{
|
|
|
|
connection.dropDatabase().then(()=>{
|
|
|
|
connection.close().then(resolve,reject)
|
|
|
|
}, reject)
|
|
|
|
},reject)
|
|
|
|
})
|
|
|
|
}
|
2017-05-21 19:48:08 +00:00
|
|
|
}
|