21.0.5 • Published 3 years ago

@the-/db v21.0.5

Weekly downloads
574
License
MIT
Repository
github
Last release
3 years ago

@the-/db

npm Version

DB for the-framework

Installation

$ npm install @the-/db --save

Usage

'use strict'

const { TheDB } = require('@the-/db')
const {
  DataTypes: { STRING },
} = require('@the-/resource')

const db = new TheDB({
  dialect: 'sqlite', // Uses "clay-driver-sqlite" package
  storage: 'var/my-app.db', // File path to save
})

// Define factory method for resource
// See https://github.com/realglobe-Inc/clay-resource for more detail
const User = db.defineResource(
  'User',
  // Resource policy'
  // https://github.com/realglobe-Inc/clay-policy#usage
  {
    password: { type: STRING },
    username: { type: STRING },
  },
  {
    interceptors: {
      // Convert entity attributes on inbound
      inbound(attributes) {
        const digest = () => {
          /* ... */
        }
        attributes.passwordHash = digest(attributes.password)
        return attributes
      },
      // Convert entity attributes on outbound
      outbound(attributes) {
        delete attributes.password
        return attributes
      },
    },
    // Enhance entity class
    entityClass(ResourceEntity) {
      return class UserResourceEntity extends ResourceEntity {
        get fullName() {
          const { firstName, lastName } = this
          return [firstName, lastName].filter(Boolean).join(' ')
        }
      }
    },
    // Create index to enable filter/sort with nested attributes
    indices() {
      return ['profile.name', 'profile.email']
    },
    schema() {
      return {}
    },
  },
)

async function tryExample() {
  const user = await User.create({
    password: 'Super Cool',
    username: 'Black Fire',
  })
  console.log(user)
  /* ... */
}

tryExample().catch((err) => console.error(err))

API Guide

See API Guide for more detail

License

This software is released under the MIT License.

Links

21.0.4

3 years ago

21.0.5

3 years ago

21.0.3

3 years ago

21.0.2

4 years ago

21.0.1

4 years ago

21.0.0

4 years ago

20.3.7

4 years ago

20.3.6

4 years ago

20.3.4

4 years ago

20.3.3

4 years ago

20.3.2

4 years ago

20.2.1

4 years ago

20.1.4

4 years ago

20.1.3

4 years ago

20.1.2

4 years ago

20.1.1

4 years ago

20.0.1

4 years ago

20.0.0

4 years ago

19.0.3

4 years ago

19.0.2

4 years ago

18.4.12

4 years ago

18.4.11

4 years ago

18.4.9

4 years ago

18.4.7

4 years ago

18.4.6

4 years ago

18.4.5

4 years ago

18.4.4

4 years ago

18.4.3

4 years ago

18.4.2

4 years ago

18.4.1

4 years ago

18.2.4

4 years ago

18.3.1

4 years ago

18.3.0

4 years ago

18.2.1

4 years ago

18.2.0

4 years ago

18.1.0

4 years ago

17.3.3

4 years ago

17.3.2

4 years ago

17.3.0

4 years ago

17.2.4

4 years ago

17.2.3

4 years ago

17.2.2

4 years ago

17.2.1

4 years ago

17.1.14

4 years ago

17.1.12

4 years ago

17.1.11

4 years ago

17.1.10

4 years ago

17.1.9

4 years ago

17.1.7

4 years ago

17.1.6

4 years ago

17.1.5

4 years ago

17.1.4

4 years ago

17.1.3

4 years ago

17.1.0

4 years ago

17.0.20

5 years ago

17.0.19

5 years ago

17.0.15

5 years ago

17.0.12

5 years ago

17.0.11

5 years ago

17.0.9

5 years ago

17.0.7

5 years ago

17.0.6

5 years ago

17.0.4

5 years ago

17.0.2

5 years ago

17.0.5

5 years ago

17.0.3

5 years ago

17.0.1

5 years ago

17.0.0-beta.2

5 years ago

16.0.3

5 years ago

16.0.2

5 years ago

16.0.1

5 years ago

15.4.15

5 years ago

15.4.14

5 years ago

15.4.13

5 years ago

15.4.10

5 years ago

15.4.8

5 years ago

15.4.7

5 years ago

15.4.6

5 years ago

15.4.3

5 years ago

15.4.2

5 years ago

15.4.1

5 years ago

15.2.6

5 years ago

15.2.5

5 years ago

15.2.3

5 years ago

15.2.2

5 years ago

15.2.1

5 years ago

15.2.0

5 years ago

15.1.5

5 years ago

15.1.4

5 years ago

15.1.3

5 years ago

15.1.2

5 years ago

15.1.0

5 years ago

15.0.3

5 years ago

15.0.2

5 years ago