knexed
Adds some neatness to Knex library. Utilities for tables, joins, transactions and other.
why?
There're three ways to work with relational DB on an application side: raw SQL, query builders, ORMs.
I prefer query builders over other two and find it a perfect balance.
- why not raw SQL: I love SQL and plain queries, but it lacks two main features from application's view: composability and type control. By composability I mean that I can create query with multiple variative clauses and not bother with string glueing, putting spaces and commas here and there. By type control I mean bi-directional type conversions and data escaping. Both theese issues are perfectly solved by query builders.
- why not ORM: I dislike ORMs because they compel me for specific application structure, to use special type-wrappers and stateful data workflow. They often have ugly verbose API, poor performance, crazy output queries and bring no benefits comparing to query builders. On the other hand query builders work with native language types and encourage functional data-flow instead of «statefulness».
The lead query builder in JS ecosystem is Knex. I don't like it much for poor code style & architecture, not so good API. Yes, Knex is not perfect, however this is the leading project in this area and it tends to stay so. It working, it will receive updates, fixes and improves. I decided to build a better abstractions over it with composability and much more JS/Lispy-crazy-science-style in mind. The only restriction is not to fall into ORM-ish style. So this library contains only simple helpers and composable abstractions. Feel free to use another good solutions (like Bluebird's Promise extensions which is built-in for Knex, Lodash/fp, Ramda & lenses etc) along the way.
API
Import / require modules you need explicitly. Most of functionality is accessible from the root of the package, like require('knexed/one')
, require('knexed/exists')
. Some utilities are bundled into groups by usage, like require('knexed/table/table')
, require('knexed/table/join')
. The group of utility is mentioned in the braces below.
./
)
dataset helpers (/* build query … */ /* then use one of utilities: */ // pick exact 1 row // pick 0..1 rows // true/false depending on rows existence // negated true/false on existence // pick rows count // compose object with rows by ids
table/table
)
table helpers (/* create init point for this table queries: */var accounts = /* `accounts()` creates new query at every invocation */ /* as a part of transaction `trx` */accounts /* with alias */accounts/* … then build query … */
tx/method
)
transaction helpers (method
helper allows function to both be initial point in transaction, or consecutive.
If no transaction is passed as first argument, transaction well be initialized automatically.
If transaction is passed, method will accept it.
/* create method */var create = /* then use it */ /* as a part of transaction `trx` */ /* new transaction will be started */ /* if you don't need transaction at all */ /* method can also be curried */var m = var create =
table/join
)
join helpers (Use join
helper for symmetric-looking joins. table
is used as basis.
/* prepare two tables: */var accounts = var messages = /* join by accounts.id = messages.user_id: */var accounts$messages = /* `accounts$messages()` creates new query at every invocation */ /* then use as simple table */ /* as a part of transaction `trx` */ /* supported join types: */joinleftaccounts messages 'id' 'user_id' joinrightmessages accounts 'id' 'user_id' joinjoin /* pick predicate: */joinleftaccounts messages 'id' '<>' 'user_id' /* join by accounts.id = messages.id, like NATURAL JOIN: */ /* join with aliases *//* accounts as A, messages as M, this will also pick proper aliases on join predicate */ /* * multi-stage join is possible, by passing * join as the left argument in join(). * different types of joins are supported */var messages_read = var messages$read = joinleftmessages messages_read 'id'var accounts$messages$read =
query
)
query helpers (This helpers transform generated query.
var accounts = var q = /* `query/count` transforms query into COUNT() one */var qc = // returning `number` /* `query/exists` transforms query into EXISTS(SELECT …) */var qe = // returning `boolean`
Note that count
& exists
from dataset helpers works on returning
dataset, which means streaming potentially large amount of redundant data from
database driver to client. In contrast query/count
& query/exists
works on driver's side, sending to client only simple scalars.
catch/constraint
)
catch constraints (Catch constraints and rethrow them as your model-specific constraint errors.
var accounts = { return 'account_already_exists'}
var accounts = { return error: 'account_already_exists' conflict: data } var id = 1
updated
)
guarantee proper updates & deletes (Will throw is zero or more than one row had been updated.
var accounts = /* preventing too many (or zero) rows deletion */
var accounts = /* guarantee one and only one row modification */
flow
We're providing built-in Flow type definitions.
license
MIT. © Strider, 2016 — 2019.