A fast and simple model based orm wrapping JoshuaWise's better-sqlite
Homepage Repository npm Download
npm install better-sqlite3-model@2.1.1
A fast and simple model based orm wrapping JoshuaWise's better-sqlite3
This was built from the need for an orm, and not wanting to switch from sqlite3 and better-sqlite3 (amazing package)
This was pulled from private projects and made public in the hopes of helping those on embedded systems or those who just prefer sqlite3
better-sqlite3 and uuid installedconst Connect = require('better-sqlite3-model').Connect
// the connect function connects to a database file and caches the connection for the orm
// the database can be switched at any time but models will have to be re-initialized after
// the function is a wrapper for 'better-sqlite3's Database contructor and takes the same props
// the function returns a better-sqlite3 Database object. Calling without props will return the last connection used
Connect(path,options)
static get tableName() and static get jsonSchema()
const Model = require('better-sqlite3-model').Model
class ExampleModel extends Model {
static get tableName() {
return 'example_model' // this is how models refer to eachother in the database
}
static get jsonSchema() { // this defines how the model looks
// the model is already provided with an id field, and a uuid, createdAt, and lastUpdated fields are added to objects automatically
return {
type: this.tableName, // this field is required but soon to be deprecated
required: ['name','someData'], // an array of required fields that an object must have before it can be saved
index: [], //this is experimental and far from ready but is an array of fields to index in a dedicated index table for faster lookups
json: ['someData','someArrayData'], // this is an array of fields that will be passed through JSON.parse and JSON.stringify on save and load, respectively
properties: { // this is where the models instance properties and types are declared
aStringProperty: {type: 'string' , unique: true , allowNull: false},
aNumberProperty: {type: 'integer'},
someData: {type: 'object'},
someArrayData: {type: 'array'},
// relationships are defined as follows:
aRelationship: {
manyHasMany: SomeOtherModel.tableName
}
// A relaship can be one of manyHasMany , oneHasMany , oneHasOne , hasOne
// - note that the name of the field does not affect the name given to the property on an object. They will be merged according to below:
// manyHasMany - loads children and adds them to instance as an array as the childs tableName with a 's' added. e.g. example_models
// oneHasMany - similar to manyHasMany
// oneHasOne - allows only one of this type of child to be linked, adding an object instead of an array and no 's' appended to the tableName
// hasOne - similar to oneHasOne, but instead of using a lookup table for the child, the childs uuid is stored on the parent in the database,
// - and the child is loaded and added to the parent on parent load
// also note that if multiple relationships arre found in a property, the highest one on the list above takes precedence
}
}
}
}
ExampleModel.$
dispense function, optionally passing any initial data for the instancevar instance = ExampleModel.dispense()
instance.save and instance.remove respectively. Note the lack of parenthesis
find function, passing any selectorsvar loadedInstance = ExampleModel.find({name: 'example' , uuid: 'hghuhgshthkjhtw4-45234523c46-45c24636c2'})
instance.link = loadedInstance
instance.unlink = loadedInstance
class ExampleModel extends Model {
...
preInsert() {
this.someBoolean ? this.someBoolean = 1 : this.someBoolean = 0;
}
preUpdate() {
this.someBoolean ? this.someBoolean = 1 : this.someBoolean = 0;
}
preLoad() {
this.someBoolean > 0 ? this.someBoolean = true : this.someBoolean = false
}
preRemove() {
...
}
preCheckout() {
...
}
}