Event-driven, agnostic, sane ORM for the rest of us.
Please Note: This is still a WIP and does not contain working code as of yet
This is really just a dispatcher that sends everything around, making your database more event-driven. It abstracts away the database making a SQL system resemble more of a document-based store. It has some small opinions about the final schema, but they can be overridden. This system also enables the use of caching engines to squeeze better performance out of your fetch operations.
While this is modeled around the constraints of a RDBMS, it is entirely possible to use it for any sort of data store, you just have to write the driver for it. Types are abstracted to be very granular and can easily be implemented with polyfills.
No need for janky workarounds, you have access to the data at every step pre and post processing to do additional validation, model validation, or whatever you wish. It will be easier to pick up if you all you know is running SQL against a database.
In the Defining Models section, we go through the items that are created automatically for you, but can be easily enabled or disabled at the local, Model Level, or at the global Application Level. Field names are not changed for you on the DB's side, making custom queries easy to work with.
Hitting the DB is expensive, you can enable caching engines and tweak any setting.
- Dates are stored as unix timestamps
- The first
err
parameter will either be an instance ofdbx.Error
or Booleanfalse
if no error happened. No null, no undefined, just a normal false (See The Worst Mistake of Computer Science).
npm install dbx --save
// contains all the classes
var dbx = require('dbx');
// create a new connection
var db = new dbx.Connection({
driver: require('dbx-mysql'),
dbuser: 'root',
dbpass: 'passw0rd',
dbname: 'mydb'
});
// define caching options (Todo)
db.cache();
// connect!
db.connect(function (err) {
console.log('connected!');
});
You might also want to use this connection in another file where db might not necessarily be in scope. You can use the singleton getter to accomplish just that:
var dbx = require('dbx');
var db = dbx.Connection.get();
Connecting to multiple servers? not to worry, you can name your connection:
var anotherDb = new dbx.Connection('myservice', {
driver: require('dbx-mysql'),
host: '10.23.44.44',
port: 64324
dbuser: 'root',
dbpass: 'passw0rd',
dbname: 'myservicedb'
});
anotherDb.connect(function (err) {
console.log('connected!')
});
Getting the connection back in other files is just as easy:
var dbx = requre('dbx');
var db = dbx.Connection.get(); // get the main connection
var anotherDb = dbx.Connection.get('myservice'); // get a named connection!
Models are nothing more than a representation of a table that they owe their namesake to. We abstract types here to very high-level ones, but you may also use the lower level ones if it suites your needs better.
// 'db' is a connected database object
var User = db.define('User', {
name: { type: 'string' },
email: { type: 'string', key: 'unique' },
password: { type: 'string' },
type: { type: 'flag', key: 'index', values: { '0': 'Normal', '1': 'Admin' } },
dateLastLogin: { type: 'timestamp' },
social: { type: 'json' },
bio: { type: 'text' }
});
var Post = db.define('Post', {
authorId: { type: 'fk', model: 'User', field: 'id' },
isVisible: { type 'boolean' },
title: { type: 'string' },
body: { type: 'text' }
});
// Using shorthand types
var Comment = db.define('Comment', {
postId: 'fk.Post.id',
authorId: 'fk.User.id',
body: 'text',
});
A few things are going on here:
- Passing in the first parameter (string "User", in the user-model's case) gives you both a way to fetch the model later, but it also identifies the table name in the database.
- The second parameter is a key/value object that contains field information, with the field's name being key. It is advisable to keep the field names valid JS identifiers for simplicity, however they can be any quoted string.
- An auto-incrementing "id" filed was omitted, it is given to you by default.
- Most date fields are absent.
dateCreated
anddateUpdated
are given to you by default. - Shorthand notation is used in the Comment model, where you simply define the type instead of a field object.
type
: (Required) Field type, use one of the following:id
: Auto-incrementing ID, unsigned. Indexed (as Primary Key) automatically.fk
,fk.TABLE.FIELD
: foreign key, same as ID except not auto-incrementing. Indexed automatically.int
: Signed, 8bit int, -2147483648 to 2147483647uint
: Unsigned, 8bit int, 0 to 4294967295flag
: Integer ranging from -128 to 127, typically a signed 8bit int. Indexed automatically.timestamp
: For unix timestamps, typicallyuint
. Can only represent dates from Jan 1st 1970 00:00:00 to Feb 7th 2106 06:28:15 UTC.string
: Single-line string, no bigger than 255 bytes (UTF-8 warning! 1 byte is not always 1 char)char
,char.X
: Byte of maximumX
length. Do not use for any UGC as per the UTF-8 warning.text
: Block of text no bigger than 65,535 bytes (See UTF-8 warning above)json
: Same basic type astext
, but converted to and from JSON automatically by the model
size
: (Optional) Only honored on fields that accept field size: (char
,string
)def
: (Optional) Default value for the field.- Default:
''
or0
depending on field type
- Default:
notnull
: (Optional) Does this field require some data in it?- Default: true
key
: (Optional) Can beprimary
,unique
,index
,text
. Note thatprimary
cannot be used if you're using theid
field, built-into models by default.- Default: Depends on
type
- Default: Depends on
values
: (Optional) Useful for theflag
type only. Key/value Object (with quoted numbers as keys) containing possible values.model
: (Optional) Useful for thefk
type only. May use the foreign key shorthand instead.field
: (Optional) Useful for thefk
type only. May use the foreign key shorthand instead.
var User = db.model('User');
User.create({
'name': 'John Doe',
'email': 'john@doe.com',
'password': 'hunter2'
}, function (err, record) {
if (err) throw err;
console.log('User exists:', record.exists);
console.log(record.name + ' <' + record.email + '>');
});
var User = db.model('User');
User.get(5, function (err, user) {
console.log('Hello, ' + user.name);
});
var User = db.model('User');
User.get(5, function (err, user) {
user.email = 'john.doe@gmail.com';
user.save(function (err) {
if (err) throw err;
console.log('New email saved!');
});
});
var User = db.model('User');
User.get(5, function (err, user) {
user.delete(function (err) {
if (err) throw err;
console.log('User exists? ', user.exists);
});
});
In the previous User.create
example, you can see the password is sent to the database with what looks like pain text. We can use an event hook for the User model to transform the password into a hashed version of whatever the password is.
var bcrypt = require('bcryptjs');
User.on('preSave', function (record, callback) {
// preSave is NOT a validation hook, it may be possible that we do have/want
// a password to update an incoming record with.
if (!record.password.length) {
return callback();
}
bcrypt.hash(record.password, function (err, hashed) {
if (err) return callback(err);
record.password = hashed;
return callback();
});
});
The API is still a WIP and should be considered VOLATILE. It is not to be relied upon until the first major release.
Constructor to create a new connection, defines the database driver and other driver-specific options. See Database Drivers for more information.
dbx.Connection([identifier], options)
identifier
- 'String' (optional): Omit to have returned the default connectionoptions
- 'Object': With the following options:driver
- Driver implementingdbx
, userequire('dbx-mysql')
for mysqlhost
- Hostname to connect toport
- Port to connect todbname
- Name of DBdbuser
- Username to connect to DB withdbpass
- password to authenticate the user with
This is an EventEmitter
with the following events:
Emitted upon successful connection
function (err, connection) { }
err
instance ofdbx.Error
,false
otherwise
Emitted upon disconnect.
function (err, reason) { }
err
instance ofdbx.Error
,false
otherwisereason
int (enum)
Emitted each time an error occurs.
function (err) { }
err
instance ofdbx.Error
Emitted when a query is ran
function (err, query)
(Static access) get the connection
identifier
string
Returns instance of dbx.Connection.
Boolean - returns the state of the connection
Boolean - returns whether or not a connection-level error has occurred.
String - Information about the last connection-level error
Define a caching engine. See Caching Engines.
Connect to the DB server
callback
function
Define a new model. See Defining Models.
name
String - Name of the modeloptions
Object - Object containing the model definition
Returns an instance of dbx.Model.
Get the model by its name.
name
String - Name of the model defined previously
Returns an instance of dbx.Model.
Directly run an SQL query.
sql
stringfromCache
boolean (optional) attempt to load from cachecallback
callback function accepting the following parameterserr
instance ofdbx.Error
if error,false
otherwiserecords
Array, instances ofdbx.Record
The base class for all data models. Note that this is only the API documentation, for a more practical introduction to models, see Defining Models. Note that it cannot be used directly, rather only through defining a model, which will have an instance returned to you. The consequence of this is that all events listed here are local to individual models.
This is an EventEmitter
with the following events:
Emitted after a successful write is issued for a new record.
function (record, [callback]) { }
record
instance ofdbx.Record
callback
callback function accepting no parameters
Emitted after a successful delete has been issued.
See Event 'postCreate' for callback details
Emitted after a fetch operation is issued to the DB.
function (query, [callback]) { }
query
instance ofdbx.Query
records
Array ofdbx.Record
callback
callback function accepting no parameters
Emitted after a successful write is issued for either a new or an existing record.
See Event 'postCreate' for callback details
Emitted after a successful write is issued for an existing record.
See Event 'postCreate' for callback details
Emitted before a write is issued for a new record. Use this hook for formatting data or injecting data into the query before it's run. use the 'validate' event for data validation.
function (record, [callback]) { }
record
instance ofdbx.Record
callback
callback function accepting the following parameters:err
instance ofdbx.Error
if error, false otherwise. Errors will abort the write and it will bubble up
Emitted before a delete is issued to an existing record.
function (record, [callback]) { }
record
instance ofdbx.Record
callback
callback function accepting the following parameters:err
instance ofdbx.Error
if error, false otherwise. Errors will abort the delete and it will bubble up
Emitted before a fetch operation is issued to the DB
function (query, [callback]) { }
query
instance ofdbx.Query
callback
callback function accepting the following parameters:err
instance ofdbx.Error
if error, false otherwise. Errors will abort the delete and it will bubble up
Emitted before a write is issued for either a new or an existing record.
See Event 'preCreate' for callback details
Emitted before a write is issued for an existing record.
See Event 'preCreate' for callback details
Emitted before any db operations have taken place, this is where you can validate information and error out if needed.
function (record, [callback]) { }
record
instance ofdbx.Record
callback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwise. Errors will abort the write and it will bubble up
String - name the model is identified by
Object - Key/value pairs containing the fields that this model can have.
Count the number of records in a query.
query
instance ofdbx.Query
callback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwisecount
Number
Create a new record, does not issue a create in the DB yet.
obj
key/value hash of fields OR instance ofdbx.Record
(to clone an object)callback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwiserecord
instance ofdbx.Record
Fetch one by the primary key.
id
primary key identifier as defined in the modelfromCache
boolean (optional) attempt to load from cachecallback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwiserecord
instance ofdbx.Record
Fetch many by an array of primary keys.
ids
Array of primary key identifier as defined in the modelfromCache
boolean (optional) attempt to load from cachecallback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwiserecords
Array, instances ofdbx.Record
Perform a search on a model.
query
instance ofdbx.Query
fromCache
boolean (optional) attempt to load from cachecallback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwiserecords
Array, instances ofdbx.Record
Alias of connection.query()
Delete the instance record.
callback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwise
Saves the current state of the model (updates or creates).
callback
callback function accepting the following parameterserr
instance ofdbx.Error
if error, false otherwise
Number - Unix timestamp from when the record was originally created
Number - Unix timestamp from when the record was last updated
Boolean - Whether the record exists in the DB or not
Number|String - Primary key of the record, falsey if this is not used as the main ID.
Boolean - Whether the record exists in the cache or not. Always false if no caching engine is defined.
Number - Time in seconds
Boolean - Has the query been executed yet
Boolean - Were the results in cache?
Number - Number of results
Array - Raw results
String - raw SQL query
(Static access) Array of valid error levels.
(Static access) Array of valid error types
Number - see Error.LEVELS for possible values
Number - See Error.TYPES for possible values
String - Message that is safe to surface to the end user
String - Message with information about the error itself that is only meant to be viewed by an elevated user.