An orm for both sqlite and websql usable in Angular 2+ and Ionic 2+
An orm for both sqlite and websql usable in Angular 2+ and Ionic 2+. Follow us On Github
npm i sqlite-websql-orm
🚧 This package is being updated to be compatible with angular 6. By the way many optimisations are done to improve the conceptual idea.
npm i [email protected]
npm i [email protected]
⚠️ You will probably need to install manually some peer dependencies.
If you get an issue please look first to the know issue or add an issue to our repository. docs/issues.md.
// under vervion 0.2.2
imports: [
// ...,
OrmModule.init({
name: 'my_custom_database.db',
location: 'default',
options: {
useFakeDatabase: true,
webname: 'my_custom_database',
description: 'SQLite/WebSQL database for browser',
version: '1.0',
maxsize: 2 * 1024 * 1024
}
})
],
// above vervion 1.0.0, replace useFakeDatabase by adapter
import { SqliteWebsqlOrmModule, SchemaFactory, Manager, ADAPTERS } from 'sqlite-websql-orm';
imports: [
// ...,
SqliteWebsqlOrmModule.init({
name: 'my_custom_database.db',
location: 'default',
options: {
adapter: ADAPTERS.auto,
webname: 'my_custom_database',
description: 'SQLite/WebSQL database for browser',
version: '1.0',
maxsize: 2 * 1024 * 1024
}
})
],
RepositoryStore.getSchemaSources()
anymore// under vervion 0.2.2
export class AppModule {
constructor(
private schema: SchemaFactory,
private platform: Platform,
) {
if ( this.platform.is('mobile') ) {
this.platform.ready().then(() => {
this.generateSchema();
}).catch(() => {
console.log('mobile platform is not ready');
});
} else {
this.generateSchema();
}
}
generateSchema() {
this.schema.generateSchema(RepositoryStore.getSchemaSources()) // <-- here
.then((db) => {
console.log('Succeed to create the database');
this.eventService.publish(EVENTS.database_schema_create);
}).catch(() => {
console.log('Failed to create the database');
this.eventService.publish(EVENTS.database_schema_failure);
});
}
}
// above vervion 1.0.0, generateSchema doesnt take parameters anymore
generateSchema() {
this.schema.generateSchema() // <-- here
.then(async (connectionHandler) => {
console.log('Succeed to create the database');
this.events.publish('create');
}).catch(() => {
console.log('Failed to create the database');
this.events.publish('failure');
});
}
mapArrayToObject()
is not mandatory anymore.@Injectable()
@Repository(Personne)
export class PersonneRepository extends EntityRepository {
constructor(public manager: Manager) {
super(manager);
}
/**
* In versions above 1.0.0,
* this functions overrides the internal one.
*/
mapArrayToObject(array: any): Personne {
const personne: Personne = new Personne();
personne.id = array.id;
personne.name = array.name;
return personne;
}
}
The internal function looks like the function below. An instance of the class managed by the repository is created and all the fields are copied from the database array to the instance.
mapArrayToObject(array: Array<any>): EntityInterface {
const instance = new (this.getClassToken());
const schema = this.getSchema();
for (const key in schema) {
if (key) {
const fieldMetadata = schema[key];
instance[fieldMetadata.propertyKey] = array[fieldMetadata.name];
}
}
return instance;
}