Streaming MySQL ORM
This is shorthand for establishing a mysql connection and connecting to it. See the linked page for the connection options.
Registers a table against the internal table cache. Note, this does not create the table in the database (nor does it run any SQL at all).
localName is the name the table will be registered under. You can use this later with
connection.table() to get a handle for the table.
primaryKey: the primary key for the table. Defaults to
tableName: the name of the table in the actual database. Defaults to
fields: an array representing all the fields this table has. Example:
['id', 'first_name', 'last_name', 'created_at']
methods: methods to add to a row object as it gets emitted from the database.
thisin the function context will be a reference to the row. Example:
Return a previously registered table. If the table is not in the internal cache,
db.table will throw an error.
Inserts or updates a single row.
An insert will always be attempted first. If the insert fails with an
ER_DUP_ENTRY and the row contains the table's primaryKey, an update will be attempted
callback will receive two arguments:
result. Result should have three properties,
insertId. If the result of a
put() is an update, the result will have
affectedRows instead of
Gets some a single row from the table.
conditions can be in one of two forms: simple or explicit
You can also mix and match the types and use arrays:
(NOTE: arrays not yet implemented)
Currently all of the conditions are inclusive – the where statement is joined with
AND – so the row must match all of the parameters to be included.
sort: TODO: implement
limit: TODO: implement
page: TODO: implement
Deletes rows from the database.
Be careful – you can truncate an entire table with this command.
conditions: see above
limit: maximum number of rows to delete
Create a ReadStream for the table.
conditions: see above
resume() will operate on the underlying connection and you are guaranteed to not receive anymore
data events after calling
pause() (according to the documentation)
data: Receives one argument,
error: If there is an error, it will be emitted here.
end: When the stream is complete.
You can define relationships on the data coming out of the stream. This will translate to
JOINs at the SQL layer, so you can (potentially) see some performance gains versus populating manually.
options.relationships is an object, keyed by property. The property name will be used when attaching the foreign rows to the main row. This will also be used as the source of the foreign key relationship unless a
from property is defined.
table: the name of the foreign table. This should be a string that can be used with
db.table()to look up the table cache.
foreign: The foreign column to match against.
from: If the key name doesn't correspond to a local column,
fromshould be used to specify it. Optional
optional: Whether or not the relationship is optional (INNER vs LEFT join). Defaults to
pivot: This is necessary as a hint to help properly aggregate "hasMany" relationships. Foreign rows will be stored as an array on the current main row until
pivotcolumn on the main row changes. At that point the foreign rows will start aggregating against the new main row.
This would emit two rows:
// row 1id: 1handle: 'brianloveswords'name: 'brian'location: 'brooklyn'food:id: 1 user_id 1 text: 'tacos'id: 2 user_id 1 text: 'pizza'id: 5 user_id 1 text: 'salmon'// row 2id: 2handle: 'mozilla'name: 'mozilla'location: 'the internet'food:id: 3 user_id 2 text: 'burittos'id: 4 user_id 2 text: 'fries'
data event for each row with just the primary key of that row.
See above for definition of
Creates a WriteStream to the table.
write() method on the stream takes row data. When a row is successfully written, a
meta event is emitted and passed a
meta object containing
An internal buffer is not kept, so all calls to
write()s will return
false to signal a ReadStream to
pause(). Once a row has been succesfully stored, a
drain event will be emitted.
options.ignoreDupes, any duplicate key errors will be ignored instead of emitting an
error event. Ignored rows will be emitted as
const ws = bandwswswswswsws