emmo-model
Flexible lightweight orm framework, supports Structure Mirroring.
Why?
- Organize your model/table definition in one place.
- Structure Mirroring feature, you can create multiple databases with same structure dynamically.
- Create/Migrate databases automatically for you, you don't need to write a single line of SQL
- Whenever you update your model, run
em migrate NAME
in your project folder, a SQL file will be created accordingly, you can customize it, late on it will be applied to all databases smartly - You can
spawn
ornew
a new EmmoModel instance to connect to other Data Server or hold different Model set. - Data output from Database will be converted to proper type automatically.
- Provide a very convenience way to accept input data from User.
- Provide multiple styles to manipulate databases.
- Support Incremental Update:
User.update({ 'age': em.o('age').plus(10) })
- Support Auto Join:
User.all({ field: [ '*', { department: 'Department.title' } ], join: 'Department' })
- Support GroupBy/Having/Order/Offset/Limit/Pagination!
- WELL TESTED!
Installation
Install emmo-model-cli to help you generate necessary files
$ sudo npm install emmo-model-cli -g
Enter your project folder and install emmo-model and database connector(only pg available currently)
$ cd myproject$ npm install emmo-model --save$ npm install pg --save$ em init
That would create em.json
file, models/
and migrations/
folders in myproject/
Sync database structure:
$ em sync
Rebase current model definition into database, this is useful when adapting to a legacy database, you can run this command whenever model definitions are created accordingly:
$ em rebase
Usage
Step 1: Define models
Assume we have users in our system.
myproject/models/user.js
var em = ; var User = moduleexports = em;
Assume we need a role base access control machanism.
myproject/models/role.js
var em = ; var Role = moduleexports = em
Then we need to make a Many-To-Mary relationship between user and role.
myproject/models/user-role.js
var em = ; var UserRole = moduleexports = em;
Assume we need to log down who and when created/disabled the UserRole relationship.
myproject/models/user-role-log.js
var em = ; var UserRoleLog = moduleexports = em;
Here we refer to a composite primary key, emmo-model assumes that all columns refer same table are a composite foreign key
Assume we need to track down user's relationship
myproject/models/relation.js
var em = ; var Relation = moduleexports = em;
As you can see, referName
is critical for tell whether it's a part of a composite key or not
either assign true
to tell it's not a composite key or specify same referName
for group of
columns to tell they are refering same composite primary key.
Step 2: Bootstrap
Modify myproject/bin/www to bootstrap emmo-model before server start.
var em = ; em;
sync
method will CREATE or MIGRATE database automatically.
Step 3: Here you go
** ASSUME WE NEED A RESTful API **
Suggested Easy Way
var em = ;var User = ; routeroute;route;route;route;
Low Level Way:
var _ = ; route; route; route; route;
Check out Session to know more about operation methods
Documentation
Check out emmo-model for more document.
Migration
Enter your project folder which had emmo-model installed then you can generate migration
$ cd myproject$ em migrate MIGRATION_NAME
You can specify configuration file by --config like:
$ em migration MIGRATION_NAME --config em.dev.json
That will create a migration sql script file smartly for you, but you can still do some customization(like convert data of some sort). and then, you can either migrate your databases during app boostrap automatically, or run following command to migrate databases immediately:
$ em sync