better-sqlite3-model
Quick Note - I honestly did not expect more than a few people to download this, so I must clarify
that this is an active work in progress and will be updated frequently. This is a public version of in house software,
so we will be updating as we find and patch bugs, and improve efficiency. We are open to public suggestions and forks.
stone.info.labs@gmail.com
Email for suggestions:A fast and simple model based orm wrapping JoshuaWise's better-sqlite3
here
Checkout JoshuaWise's better-sqlite3 moduleThis 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
installed
This package requires your project to have Basic Usage
Connect to a database
const Connect = 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
static get tableName()
and static get jsonSchema()
Models are defined by extending the base Model class and implementing Any function can be added to the model as long as it does not override the base methods
const Model = Model static { return 'example_model' // this is how models refer to eachother in the database } static { // 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 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: SomeOtherModeltableName // 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 }
Models are initialized by getting thier dollar sign method
ExampleModel$
dispense
function, optionally passing any initial data for the instance
Instances are created by calling a models instances can be considered regular objects. Any props or functions can be added as long as they dont conflict with the base model
and they will be stripped when added to the database. note that added props and functions are not added back on database load
var instance = ExampleModel
instance.save
and instance.remove
respectively. Note the lack of parenthesis
Instances can be saved and removed by using find
function, passing any selectors
Instances can be loaded from the database by calling thier models var loadedInstance = ExampleModel
Instances can be linked (if a relationship is defined)
instancelink = loadedInstance
and unlinked
instanceunlink = loadedInstance
This is done using setter methods, so set link or unlink to the object you want to link/unlink to/from
Models can hook into slots provided by the base model
This can be useful if a boolean field is needed
... { thissomeBoolean ? thissomeBoolean = 1 : thissomeBoolean = 0; } { thissomeBoolean ? thissomeBoolean = 1 : thissomeBoolean = 0; } { thissomeBoolean > 0 ? thissomeBoolean = true : thissomeBoolean = false } { ... } { ... }