BirchTree
BirchTree is mainly a helper library for knexjs. It takes an instance of knex and adds two helper functions: grow and nest. These functions make it easy to get nested objects as results when running join queries.
BirchTree is also a repository library built on knexjs. It provides base Repo
and Model
classes that can be extended to create a basic model system.
Install
npm install --save birchtree
TypeScript
BirchTree is written in typescript and should just work. You can import the type for birchtree like this
Helper Functions
Basic Usage
;; const knex = ; const birch = ; const songs = 'albums:songs';const author = 'albums:songs:author';const select = await birch;const results = await ;const nestedResults = birch;
Results will look like
id: 1 name: 'The Beatles' albums: id: 1 name: 'Rubber Soul' songs: id: 1 name: 'Drive My Car' author: id: 2 name: 'Paul McCartney' id: 2 name: 'Norwegian Wood (This Bird has Flown)' author: id: 1 name: 'John Lennon' id: 2 name: 'Revolver' songs: id: 3 name: 'Taxman' author: id: 3 name: 'George Harrison' id: 4 name: 'Eleanor Rigby' author: id: 2 name: 'Paul McCartney'
Important
There are some important things to note from the example.
- In order for things more than 1 layer deep to nest they must be aliased in a nested way. The delimiter is
:
- If a name or alias is plural than the results will form an array correctly
- If the name or alias is singular than it will not form a collection
- You will only get 1 result if you use a singular alias for something you want multiple results for
grow
Asynchronous: Takes any number of table names with or without aliases as arguments and returns an array for knex's select function that will make sure columns don't get overwritten. It will select every column in each table given.
nest
Takes the results from a select query and nests them using treeize
Repository System
Basic Setup
; // let's just pretend we set knex upconst birch = ; // let's make a user repoconst userRepo = birch; static tableName = 'users'; private id: number; private email: string; private username: string; { super; Object; } { return id: thisid email: thisemail username: thisusername } <User> { superUser birch; } async { return this ; }
Important
- Models
- The models must have
static tableName
property equal to the name of the corresponding database table - models must have a
toJSON
function. This function is used to get the object to persist inrepo.create
,repo.update
andrepo.save
- models must have an
id
property
- The models must have
- Repos
- Must pass the model class and birch to
super
in the constructor - can access birch with
this.birch
in extended repos
- Must pass the model class and birch to
Create
const user = email: 'fry@gmail.com' username: 'fry';await userRepo;
Update
userusername = 'bender';await userRepo;
Save
useremail = 'bender@gmail.com';await userRepo; const leela = email: 'leela@gmail.com' username: 'leela';await userRepo;
findOneById
const bender = await userRepo;
findByIds
const benderAndLeela = await userRepo;
exterminate
await userRepo;
findOne
const leelaAgain = await userRepo;
find
const leelaAsAnArray = await userRepo;
createQuery
This returns a birchtree/knex querybuilder.
const results = await userRepo;
Transactions
birch;