jorm
Simple orm - just orm.
Init
Just init jorm parameters:
var jormParams = connectionString: "postgres://user:password@server.com:5432/base" // required logger: custom_logger // optional object with log, error, warn and info methods
db config:
var config = User: table: 'user' fields: id : pk: true // this is primary key to CRUD operations created : { return } // field allows auto init default values name : public: true // this field will be in public object by .getPublic() method with any parameter hpassword : {} email : public: 'lite' // this field will be in public object by .getPublic() and .getPublic('lite') method phone : public: 'lite' 'full' // this field will be in public object by .getPublic(), .getPublic('lite'), .getPublic('full') and .getPublic('lite','anyOther') methods is_alex : db: false { return paramsname == 'Alex' } // this field will be ignored in all db CRUD operations, but will be filled while user object created post_count_cache: sql: 'COALESCE(post_count_cache, 0)' // custom sql part for select column post_count_cache comments_count: db: 'demand' sql: 'COALESCE((SELECT count(*) FROM "comment" WHERE "comment"."user_id" = "user"."id"),0)' // this field including in query only by demand. All tables and columns must be with quotes (") current_geo_id: public:true alias:'GeoCurrent' birth_geo_id : public:true alias:'GeoBirth' // specify alias to separete fields in get request with joined objects (this is alternate method for 'alias' in 'join' params) link : db:false { return 'href://site.com/user/'+_thisid; } // getPublic - function for field, which calling while getPublic method for essence executing // Optional possible DB triggers - all combinations of 'select', 'insert', 'update', 'delete' commands, and 'after', 'before', 'error' events { ifparamsmake_ext_request // make_ext_request - just for example // ... make some action, incl. sql request with [client] ... else ; }; { ifparamsmake_ext_request // make_ext_request - just for example // ... make some action, incl. sql request with [client] ... ; else ; } { ifparamsmake_ext_request // make_ext_request - just for example // ... make some action, incl. sql request with [client] ... ; else ; } {thishpassword = } // called after object created ;
and init jorm:
var jorm = ;var dto = jormParams config;
CRUD
Create
var newUser = jormUser
Save
If object's primary key field is null or undefined, then make insert. Else make update
save and delete supports optional params, which will be transfered to handlers
newUser;// ornewUser;
Update
username = newName;user;
Delete
Just delete it
user// oruser;
Get it from DB
Simple get by field
jormUser
Custom get by fields
jormUser
Avaliable comparsions: 'not in', '=', '<>', '<', '>', '<=', '=<', '>=', '=>', 'is null', 'is not null' and full set from https://github.com/brianc/node-sql/blob/master/lib/node/valueExpression.js
Order, Limit and Offset
jormUser
Join essences
Simple join table with minimum options
jormPost;
Join same table twice (join to joined table) with WHERE cluase by two tables
jormPost;
CRUD Handlers
Each CRUD function can be wrapped with before and after handler.
Additionaly, parameter {transaction: true} for get, save and delete make handlers transaction safe.
var config = User: table: 'user' fields: { client; } ;// ...var user = dtoUser;user
Tests
See 'test' folder