sweets-nougat

0.11.0 • Public • Published

Sweets

Flexible declarative web framework for real-time projects

Nougat

Rethinkdb adapter

Concept:

  • table is a box
  • slug is changeable
  • model can be a part of other models
  • model can be stored inside other models
  • path to resource item is parent-item/another-item/this-item

Usage

  1. Add module to your Sweets application
  2. Add nougat settings to your application settings file
  3. Add db unit to your application sample

Settings

db = {
    sweet: "nougat",
    options: {
        modelPathSeparator: "/", //separator string that will be used to split model path
 
        host: "localhost", // host
        port: 28015, // port
        db: "dbname",
 
        cursor: false, // if true returns raw cursor instead of array.
        pool: true, // use connection pool
        buffer: 50, // minimum number of connections available in the pool
        max: 1000, // maximum number of connections in the pool
        timeout: 20, // number of seconds for a connection to be opened
        timeoutError: 1000, // wait time before reconnecting in case of an error (in ms)
        timeoutGb: 60*60*1000, // how long the pool keep a connection that hasn't been used (in ms)
        maxExponent: 6, // the maximum timeout before trying to reconnect is 2^maxExponent*timeoutError, default 6 (~60 seconds for the longest wait)
        silent: false // console.error errors
    }
};

Methods

rethnikdbdash object is always available as db.r

Sugar

table(...)

r.table command wrapper, returns table selector

updateOnPath(r, path, func)

Runs func on the item by path, returns query.

insert(box, [item], data)

Inserts new item in box (table). If data ommited, creates new data item on item path. Returns array of inserted items ids.

get(box, id, [without])

Gets item from box (table) by id. You can pass array to without function (excludes fields from the document)

update(box, id, to)

Updates the document with id by to object

remove(box, id)

Removes the document by id.

addToSet(query, add)

Adds value or values to the set. If the set does not exist — creates it

add {
    set: "name",
    value: "value"
}
//or
add {
    set: "name",
    values: ["array", "of", "values"]
}

removeFromSet(query, add)

Removes value or values from the set

remove {
    set: "name",
    value: "value"
}
//or
remove {
    set: "name",
    values: ["array", "of", "values"]
}

Sugar to work with slugs

Common arguments:

  • box — table name
  • item — full item path. Ex. "main-menu/company/about"
  • options — options
options = {
    index: "index name" // slug by default
}

getSlug(box, slug, options)

Returns one item by slug from box

renameSlug(box, item, newName, options)

Renames item's slug

removeSlug(box, item, options)

Removes item by slug

Uniqueness for the other fields than id

Rethinkdb does not support unique secondary indexes. To achieve that behavior you have to use another table to store unque field as id there and check it on every operation with that field. Nougat provides some sugar for that.

In your controller scheme you can define unique array of the additional table names.

Controller.prototype.scheme = {
  indexes: [ 'name'],
  unique: [ 'name' ]
};

This will create aditional table name to store your name index.

ensureUnique(box, id)

inserts id in the box as id and returns promise.

removeUnique(box, id)

removes id from the box and returns promise.

renameUnique(box, oldId, newId)

tries to insert newId as id if succeed removes oldId and returns promise.

Example:

// create
let person = {
  name: "John Dou",
  title: "Mr"
}
 
db.ensureUnique('name', person.name)
  .then(function(res) {
    return db.table('people').insert(person).run();
  })
  .then(function(res) {
    console.log('Done', res);
  })
  .catch(function(err) {
    console.log('Error:', err);
  });
 
// remove
db.table(this.box)
  .get(id)
  .delete({ returnChanges: true })
  .run()
  .then(function(res) {
    if (res.deleted) {
      return db.removeUnique('name', res.changes[0].old_val.name);
    }
  })
  .then(function(res) {
    console.log('Done', res);
  })
  .catch(function(err) {
    console.log('Error:', err);
  });

License MIT

Package Sidebar

Install

npm i sweets-nougat

Weekly Downloads

40

Version

0.11.0

License

MIT

Last publish

Collaborators

  • velocityzen