2.2.0 • Published 7 years ago

rudiment v2.2.0

Weekly downloads
8
License
MIT
Repository
github
Last release
7 years ago

rudiment

A resource manager providing simplified database methods for CRUD operations.

Installation

$ npm install rudiment

Usage

var Rudiment = require('rudiment');
var db = require('rethinkdbdash')();
var schema = require('js-schema');

var users = new Rudiment({
  db: db.table('users'),
  schema: schema({
    username: String,
    name: String,
  }),

  key: 'username',
  index: 'uid'
});

Options

db: Existing database table object to use. For rethinkdb, the suggested module is rethinkdbdash.

schema: Optional schema function (or an array of schema functions). These may be custom predicate functions, or functions returned by a schema library like js-schema.

props: Optional array of whitelisted property names to keep in proposed documents before creating or updating. If using js-schema, this option is not needed, as the properties will be extracted from the schema.

key: Optional property name to be used as unique key.

index: Optional property name to use as an auto-index, starting at 0.

uniq: Optional array of property names to be considered unique (database ID, key and index are automatically included).

in: Optional map function for documents being created or updated.

out: Optional map function for documents being read.

path: Optional REST path.

Methods

getSupportedDbTypes

Return an array of all supported database types.

Rudiment.getSupportedDbTypes();
// ['rethinkdb']

Prototype Methods

All methods may be overridden by the constructor.

getDbType

Get the detected database type.

users.getDbType();
// 'rethinkdb'

getNextIndex

If using auto-indexing (index), generate a unique numeric index (starting at 0) to use a pseudo-key.

users.getNextIndex().then(function(index) {
  // ...
});

clean

Remove extraneous properties from a proposed document. This method only works if the first defined schema is a js-schema object, or if the props option is provided.

users.clean({
  username: 'foo',
  name: 'Foo',
  color: 'blue' // this property is not part of the defined schema
});

// { username: 'foo', name: 'Foo' }

isValid

Check if a proposed document is valid by comparing it to the defined schema(s).

users.isValid({ username: 'foo', name: 'Foo' });
// true

// `name` should be a String
users.isValid({ username: 'foo', name: 200 });
// false

// Extraneous properties are ignored here
users.isValid({ username: 'foo', name: 'Foo', color: 'green' });
// true

isAdmissible

Check if a proposed document is admissible into the database. An admissible document should pass isValid and not have any unique properties with values that are already present in the database.

users.isAdmissible({
  username: 'foo'
  name: 'Foo'
}).then(function(ok) {
  // `ok` is true if admissible, false otherwise
});

create

Create and insert a new document into the database.

users.create({
  username: 'foo',
  name: 'Foo'
}).then(function(doc) {
  // `doc` is newly inserted document
});

find

Get all documents from the database with matching properties.

users.find({
  name: 'Foo'
}).then(function(docs) {
  // `docs` is an array of matching documents
});

read

Get a document from the database by database ID.

users.read('foo-db-id').then(function(doc) {
  // `doc` is matching document
});

readByKey

Get a document from the database by key.

users.readByKey('foo').then(function(doc) {
  // `doc` is matching document
});

readByIndex

Get a document from the database by auto-index.

users.readByIndex(0).then(function(doc) {
  // `doc` is matching document
});

readAll

Get all documents from the database.

users.readAll().then(function(docs) {
  // `docs` is an array of all documents
});

update

Update a document in the database by database ID.

users.update('foo-db-id', {
  name: 'Dr. Foo'
}).then(function(doc) {
  // `doc` is updated document
});

updateByKey

Update a document in the database by key.

users.updateByKey('foo', {
  name: 'Dr. Foo'
}).then(function(doc) {
  // `doc` is updated document
});

updateByIndex

Update a document in the database by auto-index.

users.updateByIndex(0, {
  name: 'Dr. Foo'
}).then(function(doc) {
  // `doc` is updated document
});

delete

Delete a document from the database by database ID.

users.delete('foo-db-id').then(function() {
  // document was deleted
});

deleteByKey

Delete a document from the database by key.

users.deleteByKey('foo').then(function() {
  // document was deleted
});

deleteByIndex

Delete a document from the database by auto-index.

users.deleteByIndex(0).then(function() {
  // document was deleted
});

rest

Middleware REST handler for CRUD operations.

api.post('/users', function(req, res) {
  users.rest(users.create(req.body), res);
});

License

This software is released under the terms of the MIT license. See LICENSE.

2.2.0

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.0.0

8 years ago

0.6.1

8 years ago

0.6.0

8 years ago

0.5.4

8 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago