Resource accessor for ClayDB
$ npm install clay-resource --save
'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()
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()
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()
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 {
/* ... */
}
void async function () {
const driver = clayDriverMemory()
const userResource = UserResource.fromDriver(driver, 'User')
const user = await userResource.create({name: 'Taka Okunishi'})
/* ... */
}()
This software is released under the Apache-2.0 License.