@growsari/models
v1.2.2
Published
Models class. Extends from `@growsari/dbal`.
Downloads
415
Keywords
Readme
Models
Models class. Extends from @growsari/dbal.
Usage
Extend class
const {createModelClass} = require('@growsari/models')
module.exports = {
Model: createModelClass('Model', {properties: {fieldName: { ... }}}
)
}Create record
const {Model} = require('../models')
await Model.create(...)model = new Model(...)
await model.save()Find and Update record
model = await Model.find('some-uuid-value-for-id')
model.fieldName = 'newFieldValue'
await model.save()model = await Model.findBy('some-uuid-value-for-id')
model.fieldName = 'newFieldValue'
await model.save()Note that findBy field name must use a field configured as unique in order to work, i.e.
{properties: { uniqueField: { unique: true }}}Delete record
model = await Model.find('some-uuid-value-for-id')
await model.destroy()Errors
| Code | Message | | --- | --- | | MODELS-001 | Record not found for 'Entity' model | | MODELS-002 | Validation error | | MODELS-003 | Not supported | | MODELS-004 | Model not validated | | MODELS-005 | Schema not set |
