npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

boco-knex-rdb

v0.2.2

Published

Relational Database classes for Knex: TableGateway, DataMapper, etc...

Downloads

14

Readme

boco-knex-rdb

npm version npm license dependencies

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:

defineUsersTable = (table) ->
  table.uuid("id").primary()
  table.string("username")
  table.string("full_name")
  table.json("serialized_data")
  table.boolean("active").defaultTo(true)

createUsersTable = ->
  knex.schema.createTableIfNotExists("users", defineUsersTable)

createUsersPromise = createUsersTable()

We'll also hold on to a "user" record for later use.

record =
  id: "aaeab0c8-201f-4a1b-85bd-f925a01d551c"
  username: "[email protected]"
  full_name: null
  serialized_data: JSON.stringify(foo: "bar")
  active: false

Make sure the table was created before continuing...

createUsersPromise.asCallback (error, result) ->
  expect(error?).toBe false
  ok()

Table Gateway

gateway = new BocoKnexRDB.TableGateway knex: knex, table: "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:

gateway.constructRecord = (record) ->
  record.active = Boolean(record.active)
  record

Inserting a record

Insert a new record by passing in the record data.

gateway.insert record, (error, incrementId) ->
  throw error if error?
  expect(incrementId).toBe 1
  ok()

Updating a record

Update a record by passing in the identifier, followed by a set of update parameters.

parameters =
  username: "[email protected]"
  full_name: "John Doe"
  active: true

gateway.update record.id, parameters, (error, updateCount) ->
  throw error if error?
  expect(updateCount).toEqual 1
  ok()

Reading a record

Read a record by passing in the identifier.

gateway.read record.id, (error, result) ->
  throw error if error?
  expect(result.id).toEqual record.id
  expect(result.username).toEqual "[email protected]"
  expect(result.full_name).toEqual "John Doe"
  expect(result.serialized_data).toEqual '{"foo":"bar"}'
  expect(result.active).toEqual 1
  ok()

Reading all records

Just call all to get a Cursor for all records

cursor = gateway.all()
cursor.toArray (error, records) ->
  throw error if error?
  expect(records.length).toEqual(1)
  expect(records[0].id).toEqual record.id
  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.

gateway.defineScope "isActive", (query, activeState) ->
  query.where active: activeState

gateway.defineScope "withLastName", (query, last) ->
  query.where "full_name", "like", "% #{last}"

Call find with scopes and their parameters to get a cursor.

cursor = gateway.find isActive: true, withLastName: "Doe"

cursor.toArray (error, results) ->
  throw error if error?
  expect(results.length).toBe 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 = new BocoKnexRDB.TableGateway knex: knex, table: "users"
mapper = new BocoKnexRDB.DataMapper 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.

mapper.defineObjectSourceMap
  id: null
  username: null
  firstName: (record) -> record.full_name.split(" ")[0]
  lastName: (record) -> record.full_name.split(" ")[1]
  data: ["serialized_data", JSON.parse]
  active: null

mapper.defineRecordSourceMap
  id: null
  username: null
  full_name: (user) -> [user.firstName, user.lastName].join(" ")
  serialized_data: ["data", JSON.stringify]
  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 = record.id

mapper.read userId, (error, user) ->
  throw error if error?
  expect(user.id).toEqual record.id
  expect(user.username).toEqual "[email protected]"
  expect(user.firstName).toEqual "John"
  expect(user.lastName).toEqual "Doe"
  expect(user.data.foo).toEqual "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.