New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

clay-resource

Package Overview
Dependencies
Maintainers
2
Versions
124
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clay-resource

Resource accessor for ClayDB

latest
Source
npmnpm
Version
5.7.6
Version published
Weekly downloads
167
882.35%
Maintainers
2
Weekly downloads
 
Created
Source

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 {
  /* ... */
}

void async function () {
  const driver = clayDriverMemory()
  const userResource = UserResource.fromDriver(driver, 'User')

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


API Guide

License

This software is released under the Apache-2.0 License.

Keywords

ClayDB

FAQs

Package last updated on 20 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts