Skip to content
This repository was archived by the owner on Jun 22, 2021. It is now read-only.
/ knex Public archive

A concrete implementation of js-entity-repos for knex.

License

Notifications You must be signed in to change notification settings

js-entity-repos/knex

Folders and files

NameName
Last commit message
Last commit date
Sep 21, 2019
May 11, 2018
Dec 26, 2017
Dec 26, 2017
Dec 26, 2017
Dec 26, 2017
Dec 26, 2017
Dec 26, 2017
Sep 27, 2019
Sep 27, 2019
May 11, 2018
Dec 26, 2017
Dec 26, 2017
Dec 26, 2017

Repository files navigation

knex

A concrete implementation of js-entity-repos for knex.

Usage

  1. Install it with npm i @js-entity-repos/knex.
  2. For each entity you will need to do the following.
    1. Create an Entity interface.
    2. Construct the facade.
    3. Use the facade.

Entity Interface

import Entity from '@js-entity-repos/core/dist/types/Entity';

export interface TodoEntity extends Entity {
  readonly description: string;
  readonly completed: boolean;
}

Construct the Facade

import factory from '@js-entity-repos/knex/dist/factory';
import connectToDb from '@js-entity-repos/knex/dist/utils/connectToDb';

const todosFacade = factory<TodoEntity>({
  // Optional property to convert an entity to a DB document. Defaults to the function below.
  constructDocument: (patch) => {
    return patch;
  },
  // Optional property to convert a DB document to an entity. Defaults to the function below.
  constructEntity: (document) => {
    return document;
  },
  // Optional property to convert an entity filter to a DB filter. Defaults to the function below.
  constructFilter: (filter) => {
    return filter;
  },
  // Optional property to construct an initial database query. Defaults to the function below.
  constructQuery: (db) => {
    return db.table('todos');
  },
  // Optional property to convert an entity sort to a DB sort. Defaults to the function below.
  constructSort: (sort) => {
    return sort;
  },
  db: connectToDb({
    client: 'mysql',
    connection: {
      database: 'todoapp',
      host: '127.0.0.1',
      password: 'pword',
      user: 'todouser',
    },
  }),
  // Optional property. Defaults to 10.
  defaultPaginationLimit: 10,
  entityName: 'todo',
  // Optional property. Defaults to the entityName.
  tableName: 'todos',
});