objectionjs-repository
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

ObjectionJS Repository

ObjectionJS Repository is repository pattern implementation on top of KnexJS and ObjectionJS

Latest Stable Version License NPM Downloads NPM Downloads

Content

  1. Installation
  2. Usage
  3. API
    1. getOne
    2. getAll
    3. create
    4. createMany
    5. update
    6. delete
  4. Options
  5. Tests
  6. Support

Installation

$ npm i objectionjs-repository

Usage

// Define Interface
export interface IUser {
  id: number;
  age: number;
  name: string;
}

// Define Model
export default class User extends Model {
  static get tableName() {
    return TABLES.USER;
  }
}

// Define Repository
import { BaseRepository } from 'objectionjs-repository';

export class UserRepository extends BaseRepository<IUser> {
  constructor(knexInstance: Knex) {
    super(User, knexInstance);
  }
}

then you can use defined repository

   const userRepo = new UserRepository(knexInstance);
   const user = await userRepo.getOne({ age: 25 })

API

getOne(conditions, [options])

conditions is object contains any column in this table. options is IFindingOptions. return selected row or undefined

getAll(conditions, [options])

conditions is object contains any column in this table. options is IFindingOptions. return selected rows or empty array.

create(data, [options])

data to be inserted. options is ICreationOptions.

createMany(data, [options])

array to be inserted. options is ICreationOptions.

update(conditions, data, [options])

conditions is object contains any column in this table. data to be updated. options is IUpdatingOptions.

delete(conditions, [options])

conditions is object contains any column in this table. options is IDeletionOptions.

Options

IFindingOptions

IFindingOptions {
  // select specific columns
  select?: string[];
  // database Transaction
  trx?: Knex.Transaction;
  // lock selected rows or not
  forUpdate?: boolean;
  // select where column not in array
  whereNotIn?: {
    field: string;
    values: any;
  }[];
  // select where column in array
  whereIn?: {
    field: string;
    values: any;
  }[];
  // select where columns is null
  whereNull?: string[];
  // select where columns is not null
  whereNotNull?: string[];
}

ICreationOptions

ICreationOptions {
  // database Transaction
  trx?: Knex.Transaction;
}

IUpdatingOptions

IUpdatingOptions {
  // database Transaction
  trx?: Knex.Transaction;
  // select where column not in array
  whereNotIn?: {
    field: string;
    values: any;
  }[];
  // select where column in array
  whereIn?: {
    field: string;
    values: any;
  }[];

  // select where columns is null
  whereNull?: string[];
  // select where columns is not null
  whereNotNull?: string[];
}

IDeletionOptions

IDeletionOptions {
  // database Transaction
  trx?: Knex.Transaction;
  // select where column not in array
  whereNotIn?: {
    field: string;
    values: any;
  }[];
  // select where column in array
  whereIn?: {
    field: string;
    values: any;
  }[];
  // select where columns is null
  whereNull?: string[];
  // select where columns is not null
  whereNotNull?: string[];
}

Tests

To run the test suite, first install the dependencies and rename .env.sample to .env and set connection url for postgres database in .env then run npm test:

$ npm install
$ npm test

Support

Feel free to open issues on github.

Package Sidebar

Install

npm i objectionjs-repository

Weekly Downloads

16

Version

1.0.9

License

MIT

Unpacked Size

30.2 kB

Total Files

28

Last publish

Collaborators

  • ahmedadelfahim