Final ORM
Is a javascript OOP interface for ArangoDB
Based off of part of the ui-js code found here: https://github.com/uMaxmaxmaximus/ui-js/tree/master/server/core/orm
Conception: Using the ES7 operator await
, getters
and promises
, we can navigate the graph objects tree.
It recommended for use with the ES7 (async await) For a more beautiful syntax. BUT you can use ES5.
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(_id)
Model.add(obj)
Model.remove(model)
Model.restore(model)
Model.save(model)
Model.update(model)
Model.find({ where: { key: value }, skip: 0, limit: 10 })
Model.findOne({ where: { key: value } })
Model.count(selector)
Model.have(selector)
instance:
Model.prototype.save()
Model.prototype.update()
Model.prototype.remove()
Model.prototype.restore()
Basic usage:
create init class model.js
var orm = require('final-orm')
var options = {
database: 'test',
url: 'http://root:@localhost:8529',
host: 'localhost',
port: '8529',
username: 'root',
password: ''
}
var { Model, Edge } = orm.connect(options)
export default Model
export { Model, Edge }
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"
import { Model, Edge } from './model.js'
class User extends Model {
static schema = {
name: String,
male: Boolean,
age: Number,
birth: Date,
tags: Set,
messages: [String],
prop1: {prop2: [{tags: [String]}] },
bestFriend: User,
friends: [User],
name: String,
name: {$type: String},
name: {$type: String, test: /^\w+$/},
status: {
$type: String,
enum: ['sleep', 'eat'],
optional: true
}
}
}
class Like extends Edge {
static schema = {
date: Date
}
}
Example 0:
import Model from './model.js'
class User extends Model {
static schema = {
name: String,
age: Number,
}
}
Usage:
(async function () {
var user = await User.add({
name: 'Ашот',
age: 24,
})
user._id
user._removed
user.name
user.age
user.name = 'Ololo'
console.log(user.name)
await user.update()
user.name
user.name = 'Ololo'
await user.save()
await user.update()
user.name
const rose = await User.findOne({ where: { name: 'Rose' } })
Like.add(rose, user, { date: new Date() })
}())
Example 1: Instance methods
import Model from './model.js'
class User extends Model {
static schema = {
name: String,
age: Number,
friends: [User]
}
async addFriend(user) {
var friends = await this.friends
friends.push(user)
await this.save()
}
async removeAllFriends(){
this.friends = []
await this.save()
}
}
Usage:
(async function(){
var user = await User.add({
name: 'Ivan',
age: 24,
friends: []
})
await user.addFriend(user)
await user.addFriend(user)
await user.friends
await user.removeAllFriends()
await user.friends
await user.friends === await user.friends
user.name = 22
await user.save()
await user.removeAllFriends()
})()
Example 2:
import Model from './model.js'
class Sector extends Model {
static schema = {
size: Number
}
}
class User extends Model {
static schema = {
name: String,
sector: Sector,
}
}
Usage:
(async function () {
var sector = await Sector.add({
size: 236
})
var user = await User.add({
name: 'Ашот',
sector: sector
})
(await user.sector).size
var sector2 = await Sector.add({
size: 1004
})
user.sector = sector2
await user.save()
(await user.sector).size
})()
Custom types:
System types is: String, Number, Boolean, Data, Set
Actually we can use custom types:
import Model from './model.js'
class Color {
constructor(r, g, b) {
this.r = r
this.g = g
this.b = b
}
toJSON() {
return {
r: this.r,
g: this.g,
b: this.b
}
}
static fromJSON(json) {
return new Color(json.r, json.g, json.b)
}
}
class User extends Model {
static schema = {
name: String,
color: Color
}
}
Usage:
(async function () {
var user = await User.add({
name: 'Ашот',
color: new Color(0, 255, 0)
})
user.color instanceof Color
}())
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: ['animals', 'porn', 'movie']}
}