ObjectionJS Repository is repository pattern implementation on top of KnexJS and ObjectionJS
$ npm i objectionjs-repository
// 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 })
conditions is object contains any column in this table.
options is IFindingOptions.
return selected row or undefined
conditions is object contains any column in this table.
options is IFindingOptions.
return selected rows or empty array.
data to be inserted.
options is ICreationOptions.
array to be inserted.
options is ICreationOptions.
conditions is object contains any column in this table.
data to be updated.
options is IUpdatingOptions.
conditions is object contains any column in this table.
options is IDeletionOptions.
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 {
// database Transaction
trx?: Knex.Transaction;
}
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 {
// 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[];
}
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
Feel free to open issues on github.