clay-resource
Resource accessor for ClayDB
Last updated 17 days ago by realglobe .
Apache-2.0 · Repository · Bugs · Original npm · Tarball
$ cnpm install clay-resource 
SYNC missed versions from official npm registry.

Title Banner

Build Status npm Version JS Standard

Resource accessor for ClayDB

Table of Contents

Installation

$ npm install clay-resource --save

Usage

'use strict'

const {fromDriver} = require('clay-resource')
const clayDriverMemory = require('clay-driver-memory')

async function tryExample () {
  const driver = clayDriverMemory()
  const Product = fromDriver(driver, 'Product')

  {
    const product01 = await Product.create({name: 'Awesome Box', price: 100})
    const {id} = product01
    await Product.update(id, {price: 200})

    const {meta, entities} = await Product.list({
      filter: {price: {$gt: 100}}, // Supported operators: "$gt", "$gte", "$lt", "$lte", "$like", "$in" ...etc
      page: {size: 25, number: 1}
    })
    console.log('Found products:', entities, 'total:', meta.total)
    await Product.destroy(id)
  }
}

tryExample()

Advanced Usage

Listening to Events

Resources are instances of EventEmitter and fires events. See ResourceEvents to know what you can listen.

'use strict'

const { fromDriver, ResourceEvents } = require('clay-resource')
const clayDriverMemory = require('clay-driver-memory')

// Events fired from resource
const {
  ENTITY_CREATE,
  ENTITY_CREATE_BULK,
  ENTITY_UPDATE,
  ENTITY_UPDATE_BULK,
  ENTITY_DESTROY,
  ENTITY_DESTROY_BULK,
  ENTITY_DROP
} = ResourceEvents

async function tryEvents () {
  let driver = clayDriverMemory()
  let Product = fromDriver(driver, 'Product')

  Product.on(ENTITY_CREATE, ({ created }) => { /* ... */ })
  Product.on(ENTITY_CREATE_BULK, ({ created }) => { /* ... */ })
  Product.on(ENTITY_UPDATE, ({ id, updated }) => { /* ... */ })
  Product.on(ENTITY_UPDATE_BULK, ({ ids, updated }) => { /* ... */ })
  Product.on(ENTITY_DESTROY, ({ id, destroyed }) => { /* ... */ })
  Product.on(ENTITY_DESTROY_BULK, ({ ids, destroyed }) => { /* ... */ })
  Product.on(ENTITY_DROP, ({ dropped }) => { /* ... */ })
  {
    let product01 = await Product.create({ name: 'Awesome Box', price: 100 })
    /* ... */
  }
}

tryEvents()

Decorating Resource Method

To add some custom logic before/after resource handling, use .decorate(methodName, decorator).

'use strict'

const { fromDriver } = require('clay-resource')
const clayDriverMemory = require('clay-driver-memory')

async function tryDecoration () {
  let driver = clayDriverMemory()
  let Product = fromDriver(driver, 'Product')

  // Decorate resource method
  Product.decorate('create', (create) => async function decoratedCreate (attributes) {
    // Add custom check before create
    {
      let ok = /^[a-zA-Z\d].$/.test(attributes.name)
      if (ok) {
        throw new Error('Invalid name!')
      }
    }
    let created = await create(attributes) // Call the original method

    // Add custom logging after created
    {
      let logMsg = '[product] New entity created:' + created.id
      console.log(logMsg)
    }
    return created
  })

  let created = await Product.create({ name: 'foo' })
  /* ... */
}

tryDecoration()

Define Custom Resource class

To define custom resource, extends ClayResource class and use .fromDriver() method to create new instance

'use strict'

const { ClayResource } = require('clay-resource')
const clayDriverMemory = require('clay-driver-memory')

// Extends ClayResource class
class UserResource extends ClayResource {
  /* ... */
}

{
  let driver = clayDriverMemory()
  let userResource = UserResource.fromDriver(driver, 'User')

  let user = yield userResource.create({ name: 'Taka Okunishi' })
  /* ... */
}


API Guide

License

This software is released under the Apache-2.0 License.

Links

Downloads
Today 0
This Week 0
This Month 93
Last Day 0
Last Week 1
Last Month 146
Dependencies (15)
Dev Dependencies (25)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |