5.7.6 • Published 4 years ago

clay-resource v5.7.6

Weekly downloads
586
License
Apache-2.0
Repository
github
Last release
4 years ago

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.

Links

5.7.6

4 years ago

5.7.5

4 years ago

5.7.4

4 years ago

5.7.2

4 years ago

5.6.8

4 years ago

5.6.7

4 years ago

5.6.6

4 years ago

5.6.5

4 years ago

5.6.4

5 years ago

5.6.3

5 years ago

5.6.2

5 years ago

5.6.1

5 years ago

5.5.32

5 years ago

5.5.31

6 years ago

5.5.30

6 years ago

5.5.29

6 years ago

5.5.28

6 years ago

5.5.27

6 years ago

5.5.26

6 years ago

5.5.25

6 years ago

5.5.24

6 years ago

5.5.23

6 years ago

5.5.22

6 years ago

5.5.21

6 years ago

5.5.20

6 years ago

5.5.19

6 years ago

5.5.18

6 years ago

5.5.17

6 years ago

5.5.16

6 years ago

5.5.15

6 years ago

5.5.14

6 years ago

5.5.13

6 years ago

5.5.12

6 years ago

5.5.11

6 years ago

5.5.10

6 years ago

5.5.9

6 years ago

5.5.8

6 years ago

5.5.7

6 years ago

5.5.6

6 years ago

5.5.5

6 years ago

5.5.4

6 years ago

5.5.3

6 years ago

5.5.2

6 years ago

5.5.1

6 years ago

5.4.17

6 years ago

5.4.16

6 years ago

5.4.15

6 years ago

5.4.14

6 years ago

5.4.13

6 years ago

5.4.12

6 years ago

5.4.11

6 years ago

5.4.10

6 years ago

5.4.9

7 years ago

5.4.8

7 years ago

5.4.7

7 years ago

5.4.6

7 years ago

5.4.5

7 years ago

5.4.4

7 years ago

5.4.3

7 years ago

5.4.2

7 years ago

5.4.1

7 years ago

5.3.2

7 years ago

5.3.1

7 years ago

5.2.2

7 years ago

5.2.1

7 years ago

5.1.12

7 years ago

5.1.11

7 years ago

5.1.10

7 years ago

5.1.9

7 years ago

5.1.8

7 years ago

5.1.7

7 years ago

5.1.6

7 years ago

5.1.5

7 years ago

5.1.4

7 years ago

5.1.3

7 years ago

5.1.2

7 years ago

5.1.1

7 years ago

5.0.5

7 years ago

5.0.4

7 years ago

5.0.3

7 years ago

5.0.2

7 years ago

5.0.1

7 years ago

4.4.2

7 years ago

4.4.1

7 years ago

4.2.2

7 years ago

4.2.1

7 years ago

4.1.5

7 years ago

4.1.4

7 years ago

4.1.3

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

3.1.20

7 years ago

3.1.19

7 years ago

3.1.18

7 years ago

3.1.17

7 years ago

3.1.16

7 years ago

3.1.15

7 years ago

3.1.14

7 years ago

3.1.13

7 years ago

3.1.12

7 years ago

3.1.11

7 years ago

3.1.10

7 years ago

3.1.9

7 years ago

3.1.8

7 years ago

3.1.7

7 years ago

3.1.6

7 years ago

3.1.5

7 years ago

3.1.4

7 years ago

3.1.3

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.0.10

7 years ago

3.0.9

7 years ago

3.0.8

7 years ago

3.0.7

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago