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

sweets-nougat

v0.11.0

Published

Rethinkdb database adapter

Downloads

19

Readme

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