boco-knex-rdb
Installation
Installation is available via npm or github.
$ npm install boco-knex-rdb$ git clone https://github.com/bocodigitalmedia/boco-knex-rdb
Usage
BocoKnexRDB = require "boco-knex-rdb"knex = require"knex" client: "sqlite3"connection: "test.db"
Let's create a "users" table to use for our examples:
= tableuuid"id"primary tablestring"username" tablestring"full_name" tablejson"serialized_data" tableboolean"active"defaultTotrue = -> knexschemacreateTableIfNotExists"users"defineUsersTable createUsersPromise = createUsersTable
We'll also hold on to a "user" record for later use.
record = id: "aaeab0c8-201f-4a1b-85bd-f925a01d551c" username: "user@example.com" full_name: null serialized_data: JSONstringifyfoo: "bar" active: false
Make sure the table was created before continuing...
createUsersPromiseasCallback expecterror?toBe false ok
Table Gateway
gateway = knex: knextable: "users"
Modifying record construction
Since knex returns 1
or 0
for booleans, let's override the constructRecord
method
to return true
and false
on our active
property:
= record.active = Booleanrecordactive record
Inserting a record
Insert a new record by passing in the record data.
gatewayinsert record throw error if error? expectincrementIdtoBe 1 ok
Updating a record
Update a record by passing in the identifier, followed by a set of update parameters.
parameters = username: "john.doe@example.com" full_name: "John Doe" active: true gatewayupdate recordidparameters throw error if error? expectupdateCounttoEqual 1 ok
Reading a record
Read a record by passing in the identifier.
gatewayread recordid throw error if error? expectresultidtoEqual recordid expectresultusernametoEqual "john.doe@example.com" expectresultfull_nametoEqual "John Doe" expectresultserialized_datatoEqual '{"foo":"bar"}' expectresultactivetoEqual 1 ok
Reading all records
Just call all
to get a Cursor
for all records
cursor = gatewayallcursortoArray throw error if error? expectrecordslengthtoEqual1 expectrecords0idtoEqual recordid ok
Finding records with scopes
Define named scopes for your gateway, with each scope receiving a query pre-bound to select full records from the table.
The second argument of your scope method will be the value passed
to the scope via the find
method.
gatewaydefineScope "isActive" querywhere active: activeState gatewaydefineScope "withLastName" querywhere "full_name""like""% "
Call find
with scopes and their parameters to get a cursor.
cursor = gatewayfind isActive: truewithLastName: "Doe" cursortoArray throw error if error? expectresultslengthtoBe 1 ok
Transactions
Pass a knex
transaction as an option into any method as the last parameter
before the callback to run that method within the transaction:
gateway.insert record, {transaction}, done
gateway.update id, parameters, {transaction}, done
gateway.read id, transaction: {transaction}, done
gateway.remove id, {transaction}, done
gateway.all {transaction}
DataMapper
The DataMapper acts as a layer over a TableGateway.
usersGateway = knex: knextable: "users"mapper = tableGateway: usersGateway
Define the object source map for your records and the objects they represent. See boco-object-source-map for more information on object source maps.
Note that there are default resolvers on both the recordSourceMap
and objectSourceMap
for translating snakeCase
record keys to camelCase
object keys, and vice-versa.
mapperdefineObjectSourceMap id: null username: null : recordfull_namesplit" "0 : recordfull_namesplit" "1 data: "serialized_data"JSONparse active: null mapperdefineRecordSourceMap id: null username: null : userfirstNameuserlastNamejoin" " serialized_data: "data"JSONstringify active: null
Using the DataMapper
The methods of the DataMapper mimic the underlying TableGateway interface. The only difference being that your model objects are converted to and from records.
userId = recordid mapperread userId throw error if error? expectuseridtoEqual recordid expectuserusernametoEqual "john.doe@example.com" expectuserfirstNametoEqual "John" expectuserlastNametoEqual "Doe" expectuserdatafootoEqual "bar" ok
The MIT License (MIT)
Copyright (c) 2016 Christian Bradley + Boco Digital Media
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.