Miss any of our Open RFC calls?Watch the recordings here! »

@sharyn/db

3.0.2 • Public • Published

🌹 @sharyn/db

npm

This package provides database utilities and configs.

🌹 Install

yarn add @sharyn/db

🌹 Usage

knex-config.js

A knex-config.js knexfile is provided and is used automatically unless you have your own located at src/_db/knex-config.js

knex

import { knex } from '@sharyn/db'
 
export const findNoteById = (userId, id) =>
  knex('Note')
    .where({ id, userId })
    .first()

createQuery

createQuery is higher-level than knex, the created query already contains the table name, and can pass a userId as a .where({ userId }) clause.

import { createQuery } from '@sharyn/db'
 
const query = createQuery('Note')
 
export const createNote = input => query().insert(input)
 
export const findNoteById = (userId, id) =>
  query(userId)
    .where({ id })
    .first()

createQuery with a transaction

You can pass a transaction to createQuery as the second parameter:

import { createQuery, knex } from '@sharyn/db'
 
const tableAQuery = createQuery('tableA')
const tableBQuery = createQuery('tableB')
 
export const somethingWithATransaction = userId =>
  knex.transaction(async trx => {
    await tableAQuery(userId, trx).something()
    await tableBQuery(null, trx).something()
  })

Keywords

none

Install

npm i @sharyn/db

DownloadsWeekly Downloads

9

Version

3.0.2

License

MIT

Unpacked Size

10.1 kB

Total Files

14

Last publish

Collaborators

  • avatar