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

rudiment

v2.2.0

Published

CRUD resource manager

Downloads

14

Readme

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.