npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

final-orm

v1.0.9

Published

> 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

Downloads

45

Readme

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(_id) // get model by id
Model.add(obj) // add model by description obj
Model.remove(model) // remove model
Model.restore(model) // restore removed model
Model.save(model) // save modified model to db
Model.update(model) // update modified model from db
Model.find({ where: { key: value }, skip: 0, limit: 10 }) // find models by where obj
Model.findAndCount({ where: { key: value }, skip: 0, limit: 10 }) // find models by where obj and return as { data: [data], meta: { count: 123 } }
Model.findOne({ where: { key: value } }) // find one model by selector obj
Model.count(selector) // return count models matches of selector
Model.have(selector) // returns true if there is at least one model suitable for selector

instance:

Model.prototype.save() // alias  Model.save(this)
Model.prototype.update() // alias  Model.update(this)
Model.prototype.remove() // alias  Model.remove(this)
Model.prototype.restore() // alias  Model.remove(this)

Basic usage:

create init class model.js

var orm = require('final-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.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 = {
    // 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
    }
  }
}

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 () {

  // adding user to db
  var user = await User.add({
    name: 'Ашот',
    age: 24,
    })

  user._id // 'User/434370324723'
  user._removed // false
  user.name // 'Ашот'
  user.age // 24

  // change field
  user.name = 'Ololo'
  console.log(user.name) // 'Ololo' field is changed

  // reset changes
  await user.update() // load state from db
  user.name // 'Ашот'

  // saving changes
  user.name = 'Ololo' // change field
  await user.save() // save changes to db
  await user.update() // load state from db
  user.name // 'Ololo' because we save

  // like via edge collection
  const rose = await User.findOne({ where: { name: 'Rose' } })
  // in edge collections, the usage is Edge.add(from, to, data)
  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 // [user, user]  two itself =)

  await user.removeAllFriends()
  await user.friends // []

  await user.friends === await user.friends // true

  user.name = 22
  await user.save() // ValidationError: Field `name` must be String, but have Number

  await user.removeAllFriends() // since this method uses this.update, you must do user.save() first

})()

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 // 236


  var sector2 = await Sector.add({
    size: 1004
  })
  user.sector = sector2
  await user.save()

  (await user.sector).size // 1004 because this another sector ^__^


})()

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
  }


  // convert to db document
  toJSON() {
    return {
      r: this.r,
      g: this.g,
      b: this.b
    }
  }


  // restore from db document
  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 //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']}
}