Skip to content

AhmedAdelFahim/objectionjs-repository

Repository files navigation

ObjectionJS Repository - A Repository Pattern for KnexJS & ObjectionJS

ObjectionJS Repository is a lightweight library that implements the repository pattern on top of KnexJS and ObjectionJS, making it easy to manage database queries in your Node.js applications. It simplifies CRUD operations while supporting transactions, filtering, and query optimization.

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

Why Use ObjectionJS Repository?

  • Simplifies CRUD operations with KnexJS and ObjectionJS.
  • Implements the Repository Pattern, making code maintainable.
  • Supports transactions, filtering, and query optimizations.
  • Designed for Node.js applications that need efficient database access.

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.

About

Repository pattern implementation on top of KnexJS and ObjectionJS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published