0.0.11 • Published 6 years ago
orm-adapter v0.0.11
orm-adapter
An npm package that adapts other orms into a decorator based orm. Currently supports:
1- DynamoDB
2- Js-data
3- BookShelf
4- Mongoose
5- Custom
Supported API fields:
1- @Entity() or @Entity(OrmType.<Select_Orm>, OrmConfig): Creates the database model using the defined Orm or in case of passing the parameters it will used the configurations passed.
2- @column() or @column(params): Creates a column in the model. The params are optional and the can veary depending on the orm used.
3- @id: Creates a column as well as identifies as id for quiries and updates.
4- @key: Creates a column as well as identifies as secondary id for quiries and updates.
5- @hasMany: creates a one to many relationship. Will be fully functional next release.
6- @belongsTo: creates a one to one relationship. Will be fully functional next release.
7- @beforeCreate: runs function before saving model to database.
8- @afterCreate: runs function after saving model to database.
9- @beforeUpdate: runs function before updating model in the database.
10- @afterUpdate: runs function after updating model in the database.
11- @beforeDelete: runs function before deleting model in the database.
12- @afterDelete: runs function after deleting model in the database.
Creating models:
import { column, id, Entity } from 'orm-adapter';
@Entity()
class User {
@id()
username: string;
@column()
firstName: string;
@column()
lastName: string;
@column()
email: string;
}
That will simply create a user model using any orm or custom database connector and connection.
You can also create different models using different orms:
User.ts
import { column, id, Entity } from 'orm-adapter';
@Entity()
class User {
@id()
username: string;
@column()
firstName: string;
@column()
lastName: string;
@column()
email: string;
}
Nerd.ts
import { Schema, Container } from 'js-data';
import { MongoDBAdapter } from 'js-data-mongodb';
import { column, id, Entity } from 'orm-adapter';
@Entity(JS_DATA,
<JsDataConfig>{ Schema: Schema,
Adapter: MongoDBAdapter,
adapterConfig: { uri: 'mongodb://localhost:27017' },
adapterName: "mongodb",
Container: Container })
class Nerd {
@id()
realName: string;
@column()
alterEgo: string;
@column()
cursh: string;
}
User will be created using the global orm and Nerd will be created using js-data.
Life hooks Support?:
import { column, id,
Entity,
beforeCreate,
afterCreate,
beforeUpdate,
afterUpdate,
beforeDelete,
afterDelete } from 'orm-adapter';
@Entity()
class Nerd {
@id()
realName: string;
@column()
alterEgo: string;
@column()
cursh: string;
// lets help our friend out.
@beforeCreate()
initialize() {
// Do some real work then joke a little:
console.log('You have no chance buddy.');
}
@afterCreate()
ready (data: any) {
// Do real job with the data.
console.log(data);
}
@beforeUpdate()
myMan() {
// Do some real work then joke a little:
console.log('You still have no chance buddy.')
}
@afterUpdate()
newMan (data: any) {
// Do real job with the data.
console.log(data);
}
@beforeDelete()
beforeRest() {
// Do some real work then joke a little:
console.log('High five');
}
@afterDelete()
afterRest (data: any) {
// Do real job with the data.
console.log(data);
}
}
The api also supports multiple helpful properties:
import { column, id, Entity,
key, passwordField,
ignore, hasMany,
belongsTo } from 'orm-adapter';
@Entity()
class User {
@id()
username: string;
@column()
firstName: string;
@column()
lastName: string;
@key()
email: string;
@ignore()
deepSecret: string;
@passwordField()
password: string;
@hasMany(Friend /*or 'Friend'*/)
friends: Friend[];
@belongsTo(Friend /*or 'Friend'*/)
bestFriend: Friend;
}
API usage:
import { getGlobalConnector } from 'orm-adapter';
// Getting all users.
getGlobalConnector()
.then(conn => conn.getRepository(User).findAll())
// Getting a user by id = 11.
getGlobalConnector()
.then(conn => conn.getRepository(User).findById(11))
// Getting a user by a param: firstName = 'sal'.
getGlobalConnector()
.then(conn => conn.getRepository(User).findByKey({firstName: 'sal'}))
// Save a user.
getGlobalConnector()
.then(conn => conn.getRepository(User).save(new User(/* pass your params*/))
// Update a user.
getGlobalConnector()
.then(conn => conn.getRepository(User).update(someUser))
// Update a user by id.
getGlobalConnector()
.then(conn => conn.getRepository(User).updateById(11,someUser))
// Delete a user.
getGlobalConnector()
.then(conn => conn.getRepository(User).delete(someUser))
// Delete a user by id.
getGlobalConnector()
.then(conn => conn.getRepository(User).deleteById(11))