@the-/db
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
- execSqlite
- module:@the-/db
- module:@the-/db.cascadeMix
- module:@the-/db.cliMix
- module:@the-/db.helpers
- module:@the-/db.refreshMix
- module:@the-/db.refreshMix~RefreshMixed
- module:@the-/db.resourceMix
- module:@the-/db.resourceMix~ResourceMixed
- module:@the-/db.setup
- module:@the-/db.TheDB
- module:@the-/db~MigrateMixed
- TheDB
See API Guide for more detail
License
This software is released under the MIT License.