level-model

3.1.1 • Public • Published

level-model

A higher-level module for creating content models using leveldb.

npm travis standard conduct

About

level-model is a wrapper around leveldb that provides validation and indexing.

Validation is provided using the is-my-json-valid module.

Indexing is achieved using the level-simple-indexes module, which in turn relies on level-indexer.

Install

npm install --save level-model

Usage

var level = require('level')
var Model = require('level-model')
var db = level('db')
 
var posts = Model(db, {
  modelName: 'example',
  indexKeys: ['test', 'ok'],
  properties: {
    title: { type: 'string' },
    content: { type: 'string' },
  },
  required: ['title']
})
 
var data = {
  title: 'first post!',
  content: 'this is some text.'
}
 
posts.create(data, function (err, post) {
  console.log(err, post)
})

Contributing

Contributions are welcome! Please read the contributing guidelines first.

Conduct

It is important that this project contributes to a friendly, safe, and welcoming environment for all. Read this project's code of conduct

Changelog

Read about the changes to this project in CHANGELOG.md. The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

API

Create a model by calling the function exported from level-model

var posts = Model(db, options)

Options:

{
  modelName: 'Example',
  indexKeys: [],
  properties: {},
  required: []
}

The options object can accept anything that json-schema accepts.

posts.create(data, callback)

posts.get(key, options, callback)

posts.update(key, data, callback)

posts.delete(key, callback)

posts.createReadStream(options)

posts.find(index, options)

Format data before create & update

Add beforeCreate and beforeUpdate methods to options.hooks to format data before it is saved to the db:

var posts = Model(db, {
  modelName: 'posts',
  hooks: {
    beforeCreate: function (data) {
      data.slug = slugify(data.title)
      return data
    },
    beforeUpdate: function (data) {
      return data
    }
  }
})

Events

example.on('create', function (model) {})

example.on('update', function (model) {})

example.on('delete', function () {})

Contact

License

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i level-model

Weekly Downloads

33

Version

3.1.1

License

none

Unpacked Size

26 kB

Total Files

13

Last publish

Collaborators

  • ajoslin
  • sethvincent