Final ORM
Please check out https://github.com/oknoah/final and https://github.com/oknoah/final/packages/arangolize for similar projects that MAY be more up to date
This is a javascript OOP interface for ArangoDB
Conception: Using the ES7 operator await
, getters
and promises
, we can navigate the graph objects tree.
It is recommended for use with the ES7 (async await) For a more beautiful syntax.
Never heard of ArangoDB? Check out these benchmarks: https://www.arangodb.com/2015/10/benchmark-postgresql-mongodb-arangodb/
Please see ./index.test.js
for most up-to-date examples.
API
static:
Model // get model by idModel // add model by description objModel // remove modelModel // restore removed modelModel // save modified model to dbModel // update modified model from dbModel // find models by where objModel // find models by where obj and return as { data: [data], meta: { count: 123 } }Model // find one model by selector objModel // return count models matches of selectorModel // returns true if there is at least one model suitable for selector
instance:
Modelprototype // alias Model.save(this)Modelprototype // alias Model.update(this)Modelprototype // alias Model.remove(this)Modelprototype // alias Model.remove(this)
Basic usage:
create init class model.js
var orm = var options = database: 'test' // db name // You can initialize the database using just a url. url: 'http://root:@localhost:8529' // Or supply each of these values. You do not need both. host: 'localhost' port: '8529' username: 'root' password: '' // You can also supply a protocol. If localhost, it's `http` by default, otherwise `https` protocol: 'tcp' var Model Edge = orm
orm.connect()
returns Model
and Edge
classes, and you need export and extend it
Define collection User (class name will be collection name), and edge collection "Like"
static schema = // basic types name: String male: Boolean age: Number birth: Date tags: Set // like array but items are unique // structures messages: String // array of types prop1: prop2: tags: String // sub schemas // relations with other (or self) db collections bestFriend: User // link to model friends: User // link to array models // field options name: String name: $type: String name: $type: String test: /^\w+$/ status: $type: String enum: 'sleep' 'eat' // enum optional: true // allows null value static schema = date: Date
Example 0:
static schema = name: String age: Number
Usage:
{ // adding user to db var user = await User user_id // 'User/434370324723' user_removed // false username // 'Ашот' userage // 24 // change field username = 'Ololo' console // 'Ololo' field is changed // reset changes await user // load state from db username // 'Ашот' // saving changes username = 'Ololo' // change field await user // save changes to db await user // load state from db username // 'Ololo' because we save // like via edge collection const rose = await User // in edge collections, the usage is Edge.add(from, to, data) Like }
Example 1: Instance methods
static schema = name: String age: Number friends: User async { var friends = await thisfriends friends await this } async { thisfriends = await this }
Usage:
{ var user = await User await user await user await userfriends // [user, user] two itself =) await user await userfriends // [] await userfriends === await userfriends // true username = 22 await user // ValidationError: Field `name` must be String, but have Number await user // since this method uses this.update, you must do user.save() first }
Example 2:
static schema = size: Number static schema = name: String sector: Sector
Usage:
{ var sector = await Sector var user = await User await usersectorsize // 236 var sector2 = await Sector usersector = sector2 await user await usersectorsize // 1004 because this another sector ^__^ }
Custom types:
System types is: String, Number, Boolean, Data, Set Actually we can use custom types:
{ thisr = r thisg = g thisb = b } // convert to db document { return r: thisr g: thisg b: thisb } // restore from db document static { return jsonr jsong jsonb } static schema = name: String color: Color
Usage:
{ var user = await User usercolor instanceof Color //true }
Schemas
Number
schema = age: Number age: $type: Number age: $type: Number min:0 max:100
======= String
schema = name: String name: $type: String name: $type: String min:3 max:20 test:/^\w+$/
======= Set
schema = tags: Set tags: $type: Set tags: $type: Set set: 'soviet' 'movies'