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
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.
import { column, id, Entity } from 'orm-adapter';
@Entity()
class User {
@id()
username: string;
@column()
firstName: string;
@column()
lastName: string;
@column()
email: string;
}
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.
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);
}
}
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;
}
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))