@leapfrogtechnology/db-model
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
# Change Log | ||
## [@leapfrogtechnology/db-model@1.0.0-beta.3](https://github.com/leapfrogtechnology/js-util/tree/@leapfrogtechnology/db-model@1.0.0-beta.3) (2020-05-05) | ||
[Full Changelog](https://github.com/leapfrogtechnology/js-util/compare/@leapfrogtechnology/db-model@1.0.0-beta.2...@leapfrogtechnology/db-model@1.0.0-beta.3) | ||
**Changes** | ||
- Refactor model.ts and db.ts [\#7](https://github.com/leapfrogtechnology/js-util/pull/7) ([FriedDust](https://github.com/FriedDust)) | ||
## [@leapfrogtechnology/db-model@1.0.0-beta.2](https://github.com/leapfrogtechnology/js-util/tree/@leapfrogtechnology/db-model@1.0.0-beta.2) (2020-04-25) | ||
@@ -4,0 +12,0 @@ |
@@ -40,3 +40,2 @@ import * as Knex from 'knex'; | ||
* Finds a record based on the params. | ||
* Returns null if no results were found. | ||
* | ||
@@ -46,38 +45,19 @@ * @param {Knex} connection | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {(Promise<T | null>)} | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
export declare function get<T>(connection: Knex, table: string, params?: object, trx?: Knex.Transaction): Promise<T | null>; | ||
export declare function find<T>(connection: Knex, table: string, params?: object, callback?: any, trx?: Knex.Transaction): Knex.QueryBuilder; | ||
/** | ||
* Find record by it's id. | ||
* Returns null if not found. | ||
* Finds a single record based on the params. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {number} id | ||
* @returns {(Promise<T | null>)} | ||
*/ | ||
export declare function getById<T>(connection: Knex, table: string, id: number, trx?: Knex.Transaction): Promise<T | null>; | ||
/** | ||
* Finds a record based on the params. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} [trx] | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
* @returns {(Promise<T | null>)} | ||
*/ | ||
export declare function find<T>(connection: Knex, table: string, params?: object, trx?: Knex.Transaction): Promise<T>; | ||
export declare function findFirst<T>(connection: Knex, table: string, params?: object, callback?: any, trx?: Knex.Transaction): Promise<T | null>; | ||
/** | ||
* Find all records based on the params. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {object} [params={}] | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
export declare function findAll<T>(connection: Knex, table: string, params?: object, trx?: Knex.Transaction): Promise<T[]>; | ||
/** | ||
* Insert all records sent in data object. | ||
@@ -89,17 +69,6 @@ * | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
export declare function insert<T>(connection: Knex, table: string, data: object | object[], trx?: Knex.Transaction): Promise<T[]>; | ||
export declare function insert<T>(connection: Knex, table: string, data: object | object[], trx?: Knex.Transaction): Knex.QueryBuilder; | ||
/** | ||
* Update records by id. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {(number | number[])} id | ||
* @param {object} params | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
export declare function updateById<T>(connection: Knex, table: string, id: number | number[], params: object, trx?: Knex.Transaction): Promise<T[]>; | ||
/** | ||
* Update records by where condition. | ||
@@ -112,3 +81,3 @@ * | ||
*/ | ||
export declare function updateWhere<T>(connection: Knex, table: string, where: object, params: object, trx?: Knex.Transaction): Promise<T[]>; | ||
export declare function update<T>(connection: Knex, table: string, where: object, params: object, trx?: Knex.Transaction): Promise<T[]>; | ||
/** | ||
@@ -115,0 +84,0 @@ * Delete row in table. |
218
dist/db.js
@@ -54,3 +54,2 @@ "use strict"; | ||
var object = require("./utils/object"); | ||
var ModelNotFoundError_1 = require("./ModelNotFoundError"); | ||
var log = debug(constants_1.NS_DB); | ||
@@ -137,5 +136,5 @@ /** | ||
if (!exists || (exists && params.updatedAt)) { | ||
return [2 /*return*/, object.toSnakeCase(params)]; | ||
return [2 /*return*/, params]; | ||
} | ||
return [2 /*return*/, __assign(__assign({}, object.toSnakeCase(params)), { updated_at: connection.fn.now() })]; | ||
return [2 /*return*/, __assign(__assign({}, params), { updated_at: connection.fn.now() })]; | ||
} | ||
@@ -147,3 +146,2 @@ }); | ||
* Finds a record based on the params. | ||
* Returns null if no results were found. | ||
* | ||
@@ -153,70 +151,16 @@ * @param {Knex} connection | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {(Promise<T | null>)} | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
function get(connection, table, params, trx) { | ||
function find(connection, table, params, callback, trx) { | ||
if (params === void 0) { params = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, queryBuilder(connection, trx) | ||
.select('*') | ||
.from(table) | ||
.where(object.toSnakeCase(params)) | ||
.limit(1)]; | ||
case 1: | ||
result = (_a.sent())[0]; | ||
if (!result) { | ||
return [2 /*return*/, null]; | ||
} | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
}); | ||
}); | ||
var qb = queryBuilder(connection, trx).select('*').from(table).where(params); | ||
if (callback) | ||
callback(qb); | ||
return qb; | ||
} | ||
exports.get = get; | ||
/** | ||
* Find record by it's id. | ||
* Returns null if not found. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {number} id | ||
* @returns {(Promise<T | null>)} | ||
*/ | ||
function getById(connection, table, id, trx) { | ||
return get(connection, table, { id: id }, trx); | ||
} | ||
exports.getById = getById; | ||
/** | ||
* Finds a record based on the params. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {object} [params={}] | ||
* @param {Knex.Transaction} [trx] | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
*/ | ||
function find(connection, table, params, trx) { | ||
if (params === void 0) { params = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, get(connection, table, params, trx)]; | ||
case 1: | ||
result = _a.sent(); | ||
if (!result) { | ||
throw new ModelNotFoundError_1.default('Model not found'); | ||
} | ||
return [2 /*return*/, result]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.find = find; | ||
/** | ||
* Find all records based on the params. | ||
* Finds a single record based on the params. | ||
* | ||
@@ -226,20 +170,22 @@ * @param {Knex} connection | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
* @returns {(Promise<T | null>)} | ||
*/ | ||
function findAll(connection, table, params, trx) { | ||
function findFirst(connection, table, params, callback, trx) { | ||
if (params === void 0) { params = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var rows; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, queryBuilder(connection, trx).select('*').from(table).where(object.toSnakeCase(params))]; | ||
case 1: | ||
rows = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(rows)]; | ||
} | ||
}); | ||
var qb = queryBuilder(connection, trx) | ||
.select('*') | ||
.from(table) | ||
.where(params) | ||
.limit(1) | ||
.then(function (_a) { | ||
var result = _a[0]; | ||
return result ? result : null; | ||
}); | ||
if (callback) | ||
callback(qb); | ||
return qb; | ||
} | ||
exports.findAll = findAll; | ||
exports.findFirst = findFirst; | ||
/** | ||
@@ -252,54 +198,9 @@ * Insert all records sent in data object. | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
function insert(connection, table, data, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var qb, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
qb = queryBuilder(connection, trx); | ||
return [4 /*yield*/, qb.insert(object.toSnakeCase(data)).into(table).returning('*')]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
}); | ||
}); | ||
return queryBuilder(connection, trx).insert(data).into(table).returning('*'); | ||
} | ||
exports.insert = insert; | ||
/** | ||
* Update records by id. | ||
* | ||
* @param {Knex} connection | ||
* @param {string} table | ||
* @param {(number | number[])} id | ||
* @param {object} params | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
function updateById(connection, table, id, params, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var qb, updateParams, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
qb = queryBuilder(connection, trx); | ||
return [4 /*yield*/, withTimestamp(connection, table, params)]; | ||
case 1: | ||
updateParams = _a.sent(); | ||
return [4 /*yield*/, qb | ||
.update(updateParams) | ||
.table(table) | ||
.whereIn('id', Array.isArray(id) ? id : [id]) | ||
.returning('*')]; | ||
case 2: | ||
result = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
}); | ||
}); | ||
} | ||
exports.updateById = updateById; | ||
/** | ||
* Update records by where condition. | ||
@@ -312,16 +213,11 @@ * | ||
*/ | ||
function updateWhere(connection, table, where, params, trx) { | ||
function update(connection, table, where, params, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var qb, updateParams, result; | ||
var updateParams; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
qb = queryBuilder(connection, trx); | ||
return [4 /*yield*/, withTimestamp(connection, table, params)]; | ||
case 0: return [4 /*yield*/, withTimestamp(connection, table, params)]; | ||
case 1: | ||
updateParams = _a.sent(); | ||
return [4 /*yield*/, qb.update(updateParams).table(table).where(object.toSnakeCase(where)).returning('*')]; | ||
case 2: | ||
result = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
return [2 /*return*/, queryBuilder(connection, trx).update(updateParams).table(table).where(where).returning('*')]; | ||
} | ||
@@ -331,3 +227,3 @@ }); | ||
} | ||
exports.updateWhere = updateWhere; | ||
exports.update = update; | ||
/** | ||
@@ -343,15 +239,3 @@ * Delete row in table. | ||
function remove(connection, table, params, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var qb, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
qb = queryBuilder(connection, trx); | ||
return [4 /*yield*/, qb.where(object.toSnakeCase(params)).from(table).del().returning('*')]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
}); | ||
}); | ||
return queryBuilder(connection, trx).where(params).from(table).del().returning('*'); | ||
} | ||
@@ -369,23 +253,3 @@ exports.remove = remove; | ||
function query(connection, sql, params, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var conn, result, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
conn = queryBuilder(connection, trx); | ||
if (!params) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, conn.raw(sql, params)]; | ||
case 1: | ||
_a = _b.sent(); | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, conn.raw(sql)]; | ||
case 3: | ||
_a = _b.sent(); | ||
_b.label = 4; | ||
case 4: | ||
result = _a; | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
}); | ||
}); | ||
return params ? queryBuilder(connection, trx).raw(sql, params) : queryBuilder(connection, trx).raw(sql); | ||
} | ||
@@ -404,15 +268,3 @@ exports.query = query; | ||
function batchInsert(connection, table, data, chunkSize, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var qb, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
qb = queryBuilder(connection, trx); | ||
return [4 /*yield*/, qb.batchInsert(table, object.toSnakeCase(data), chunkSize).returning('*')]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
}); | ||
}); | ||
return queryBuilder(connection, trx).batchInsert(table, data, chunkSize).returning('*'); | ||
} | ||
@@ -419,0 +271,0 @@ exports.batchInsert = batchInsert; |
/** | ||
* Parameters required for Paginator util module while calling it. | ||
* Parameters required for pagination. | ||
*/ | ||
interface PaginationParams { | ||
maxRows: number; | ||
boundParams?: any; | ||
currentPage: number; | ||
hasOrderBy?: boolean; | ||
totalCountQuery: string; | ||
pageSize: number; | ||
page: number; | ||
} | ||
export default PaginationParams; |
/** | ||
* Output interface for final response of Paginator util module. | ||
* Output interface for final response of pagination. | ||
*/ | ||
@@ -7,11 +7,4 @@ interface PaginationResult<T> { | ||
maxRows: number; | ||
pages: { | ||
first: number; | ||
prev: number | null; | ||
current: number; | ||
next: number | null; | ||
last: number; | ||
}; | ||
results: T[]; | ||
records: T[]; | ||
} | ||
export default PaginationResult; |
import * as Knex from 'knex'; | ||
import OrderBy from './domain/OrderBy'; | ||
import RawBindingParams, { ValueMap } from './domain/RawBindingParams'; | ||
import PaginationParams from './domain/PaginationParams'; | ||
import PaginationResult from './domain/PaginationResult'; | ||
import RawBindingParams, { ValueMap } from './domain/RawBindingParams'; | ||
export declare type ConnectionResolver = () => Knex; | ||
@@ -15,2 +15,4 @@ /** | ||
table: string; | ||
defaultOrderBy: OrderBy[]; | ||
id: string; | ||
connection?: Knex<any, unknown[]> | undefined; | ||
@@ -42,34 +44,34 @@ /** | ||
* | ||
* @param {(qb: Knex | Transaction) => QueryBuilder} callback | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
* @returns {(Knex.Transaction | Knex)} | ||
*/ | ||
buildQuery<T>(callback: (qb: Knex<any, unknown[]> | Knex.Transaction<any, any>) => Knex.QueryBuilder<any, any>, trx?: Knex.Transaction<any, any> | undefined): Promise<T[]>; | ||
queryBuilder(trx?: Knex.Transaction<any, any> | undefined): Knex<any, unknown[]> | Knex.Transaction<any, any>; | ||
/** | ||
* Finds a record based on the params. | ||
* Returns null if no results were found. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
get<T_1>(params?: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_1 | null>; | ||
find<T>(params?: object, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Knex.QueryBuilder<any, any>; | ||
/** | ||
* Find record by it's id. | ||
* Returns null if not found. | ||
* Finds the first record based on the params. | ||
* | ||
* @param {number} id | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
getById<T_2>(id: number, trx?: Knex.Transaction<any, any> | undefined): Promise<T_2 | null>; | ||
findFirst<T_1>(params?: object, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_1 | null>; | ||
/** | ||
* Finds a record based on the params. | ||
* Find record by it's id. | ||
* Throws an exception if not found. | ||
* | ||
* @param {object} [params={}] | ||
* @param {any} id | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
* @returns {Promise<T | null>} | ||
*/ | ||
find<T_3>(params?: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_3>; | ||
findById<T_2>(id: any, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_2 | null>; | ||
/** | ||
@@ -79,17 +81,30 @@ * Find record by it's id. | ||
* | ||
* @param {number} id | ||
* @param {any} id | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
* @returns {Promise<T | null>} | ||
*/ | ||
findById<T_4>(id: number, trx?: Knex.Transaction<any, any> | undefined): Promise<T_4>; | ||
findByIdOrFail<T_3>(id: any, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_3 | null>; | ||
/** | ||
* Find all records based on the params. | ||
* Finds records based on the params with records limit. | ||
* | ||
* @param {object} [params={}] | ||
* @param {PaginationParams} pageParams | ||
* @param {OrderBy[]} sortParams | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T[]>} | ||
* @param {Function} callback | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
findAll<T_5>(params?: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_5[]>; | ||
findWithPageAndSort(params: any, pageParams: PaginationParams, sortParams: OrderBy[], callback?: any, trx?: Knex.Transaction<any, any> | undefined): Knex.QueryBuilder<any, any>; | ||
/** | ||
* Count the total records. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
count(params?: any, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<any>; | ||
/** | ||
* Insert all records sent in data object. | ||
@@ -101,3 +116,3 @@ * | ||
*/ | ||
insert<T_6>(data: object | object[], trx?: Knex.Transaction<any, any> | undefined): Promise<T_6[]>; | ||
insert<T_4>(data: object | object[], trx?: Knex.Transaction<any, any> | undefined): Promise<T_4[]>; | ||
/** | ||
@@ -111,3 +126,3 @@ * Update records by id. | ||
*/ | ||
updateById<T_7>(id: number | number[], params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_7[]>; | ||
updateById<T_5>(id: any, params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_5[]>; | ||
/** | ||
@@ -121,3 +136,3 @@ * Update records by where condition. | ||
*/ | ||
updateWhere<T_8>(where: object, params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_8[]>; | ||
update<T_6>(where: object, params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_6[]>; | ||
/** | ||
@@ -130,4 +145,12 @@ * Delete row in table. | ||
*/ | ||
delete<T_9>(params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_9[]>; | ||
deleteById<T_7>(id: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_7[]>; | ||
/** | ||
* Delete row in table. | ||
* | ||
* @param {object} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
delete<T_8>(params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_8[]>; | ||
/** | ||
* Execute SQL raw query and return results. | ||
@@ -140,19 +163,11 @@ * | ||
*/ | ||
query<T_10>(sql: string, params?: ValueMap | RawBindingParams | undefined, trx?: Knex.Transaction<any, any> | undefined): Promise<T_10[]>; | ||
query<T_9>(sql: string, params?: ValueMap | RawBindingParams | undefined, trx?: Knex.Transaction<any, any> | undefined): Promise<T_9[]>; | ||
/** | ||
* Execute a query with pagination and return the paginated results. | ||
* TODO: Accept both raw sql query and knex query builder instance. | ||
* | ||
* @param {string} query | ||
* @param {PaginationParams} params | ||
* @returns {Promise<PaginationResult<T>>} | ||
*/ | ||
paginate<T_11>(query: string, params: PaginationParams): Promise<PaginationResult<T_11>>; | ||
/** | ||
* Method to perform a transactional query execution. | ||
* | ||
* @param {(trx: Transaction) => any} callback | ||
* @param {Transaction} trx | ||
* @returns {any} | ||
*/ | ||
transaction<T_12>(callback: (trx: Knex.Transaction<any, any>) => any): any; | ||
transaction<T_10>(callback: (trx: Knex.Transaction<any, any>) => any, trx?: Knex.Transaction<any, any> | undefined): any; | ||
/** | ||
@@ -166,3 +181,3 @@ * Batch insert rows of data. | ||
*/ | ||
batchInsert<T_13>(data: object[], trx?: Knex.Transaction<any, any> | undefined, chunksize?: number): Promise<T_13[]>; | ||
batchInsert<T_11>(data: object[], trx?: Knex.Transaction<any, any> | undefined, chunksize?: number): Promise<T_11[]>; | ||
/** | ||
@@ -176,3 +191,3 @@ * Execute SQL raw query and return scalar value. | ||
*/ | ||
getValue<T_14>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_14 | null>; | ||
getValue<T_12>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_12 | null>; | ||
/** | ||
@@ -196,3 +211,3 @@ * Execute SQL raw query returning a boolean result. | ||
*/ | ||
getJson<T_15>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_15 | null>; | ||
getJson<T_13>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_13 | null>; | ||
/** | ||
@@ -213,3 +228,3 @@ * Invoke a scalar-valued function and return results. | ||
*/ | ||
invoke<T_16>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_16 | null>; | ||
invoke<T_14>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_14 | null>; | ||
/** | ||
@@ -230,3 +245,10 @@ * Execute a procedure and return the results returned (if any). | ||
*/ | ||
exec<T_17>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_17[]>; | ||
exec<T_15>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_15[]>; | ||
/** | ||
* Build param object for given id. | ||
* | ||
* @param {any} id | ||
* @returns {object} | ||
*/ | ||
buildIdParams(id: any): any; | ||
}; | ||
@@ -236,2 +258,4 @@ export declare const Model: { | ||
table: string; | ||
defaultOrderBy: OrderBy[]; | ||
id: string; | ||
connection?: Knex<any, unknown[]> | undefined; | ||
@@ -263,34 +287,34 @@ /** | ||
* | ||
* @param {(qb: Knex | Transaction) => QueryBuilder} callback | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
* @returns {(Knex.Transaction | Knex)} | ||
*/ | ||
buildQuery<T>(callback: (qb: Knex<any, unknown[]> | Knex.Transaction<any, any>) => Knex.QueryBuilder<any, any>, trx?: Knex.Transaction<any, any> | undefined): Promise<T[]>; | ||
queryBuilder(trx?: Knex.Transaction<any, any> | undefined): Knex<any, unknown[]> | Knex.Transaction<any, any>; | ||
/** | ||
* Finds a record based on the params. | ||
* Returns null if no results were found. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
get<T_1>(params?: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_1 | null>; | ||
find<T>(params?: object, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Knex.QueryBuilder<any, any>; | ||
/** | ||
* Find record by it's id. | ||
* Returns null if not found. | ||
* Finds the first record based on the params. | ||
* | ||
* @param {number} id | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
getById<T_2>(id: number, trx?: Knex.Transaction<any, any> | undefined): Promise<T_2 | null>; | ||
findFirst<T_1>(params?: object, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_1 | null>; | ||
/** | ||
* Finds a record based on the params. | ||
* Find record by it's id. | ||
* Throws an exception if not found. | ||
* | ||
* @param {object} [params={}] | ||
* @param {any} id | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
* @returns {Promise<T | null>} | ||
*/ | ||
find<T_3>(params?: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_3>; | ||
findById<T_2>(id: any, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_2 | null>; | ||
/** | ||
@@ -300,17 +324,30 @@ * Find record by it's id. | ||
* | ||
* @param {number} id | ||
* @param {any} id | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
* @returns {Promise<T | null>} | ||
*/ | ||
findById<T_4>(id: number, trx?: Knex.Transaction<any, any> | undefined): Promise<T_4>; | ||
findByIdOrFail<T_3>(id: any, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_3 | null>; | ||
/** | ||
* Find all records based on the params. | ||
* Finds records based on the params with records limit. | ||
* | ||
* @param {object} [params={}] | ||
* @param {PaginationParams} pageParams | ||
* @param {OrderBy[]} sortParams | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T[]>} | ||
* @param {Function} callback | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
findAll<T_5>(params?: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_5[]>; | ||
findWithPageAndSort(params: any, pageParams: PaginationParams, sortParams: OrderBy[], callback?: any, trx?: Knex.Transaction<any, any> | undefined): Knex.QueryBuilder<any, any>; | ||
/** | ||
* Count the total records. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
count(params?: any, callback?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<any>; | ||
/** | ||
* Insert all records sent in data object. | ||
@@ -322,3 +359,3 @@ * | ||
*/ | ||
insert<T_6>(data: object | object[], trx?: Knex.Transaction<any, any> | undefined): Promise<T_6[]>; | ||
insert<T_4>(data: object | object[], trx?: Knex.Transaction<any, any> | undefined): Promise<T_4[]>; | ||
/** | ||
@@ -332,3 +369,3 @@ * Update records by id. | ||
*/ | ||
updateById<T_7>(id: number | number[], params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_7[]>; | ||
updateById<T_5>(id: any, params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_5[]>; | ||
/** | ||
@@ -342,3 +379,3 @@ * Update records by where condition. | ||
*/ | ||
updateWhere<T_8>(where: object, params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_8[]>; | ||
update<T_6>(where: object, params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_6[]>; | ||
/** | ||
@@ -351,4 +388,12 @@ * Delete row in table. | ||
*/ | ||
delete<T_9>(params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_9[]>; | ||
deleteById<T_7>(id: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_7[]>; | ||
/** | ||
* Delete row in table. | ||
* | ||
* @param {object} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
delete<T_8>(params: object, trx?: Knex.Transaction<any, any> | undefined): Promise<T_8[]>; | ||
/** | ||
* Execute SQL raw query and return results. | ||
@@ -361,19 +406,11 @@ * | ||
*/ | ||
query<T_10>(sql: string, params?: ValueMap | RawBindingParams | undefined, trx?: Knex.Transaction<any, any> | undefined): Promise<T_10[]>; | ||
query<T_9>(sql: string, params?: ValueMap | RawBindingParams | undefined, trx?: Knex.Transaction<any, any> | undefined): Promise<T_9[]>; | ||
/** | ||
* Execute a query with pagination and return the paginated results. | ||
* TODO: Accept both raw sql query and knex query builder instance. | ||
* | ||
* @param {string} query | ||
* @param {PaginationParams} params | ||
* @returns {Promise<PaginationResult<T>>} | ||
*/ | ||
paginate<T_11>(query: string, params: PaginationParams): Promise<PaginationResult<T_11>>; | ||
/** | ||
* Method to perform a transactional query execution. | ||
* | ||
* @param {(trx: Transaction) => any} callback | ||
* @param {Transaction} trx | ||
* @returns {any} | ||
*/ | ||
transaction<T_12>(callback: (trx: Knex.Transaction<any, any>) => any): any; | ||
transaction<T_10>(callback: (trx: Knex.Transaction<any, any>) => any, trx?: Knex.Transaction<any, any> | undefined): any; | ||
/** | ||
@@ -387,3 +424,3 @@ * Batch insert rows of data. | ||
*/ | ||
batchInsert<T_13>(data: object[], trx?: Knex.Transaction<any, any> | undefined, chunksize?: number): Promise<T_13[]>; | ||
batchInsert<T_11>(data: object[], trx?: Knex.Transaction<any, any> | undefined, chunksize?: number): Promise<T_11[]>; | ||
/** | ||
@@ -397,3 +434,3 @@ * Execute SQL raw query and return scalar value. | ||
*/ | ||
getValue<T_14>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_14 | null>; | ||
getValue<T_12>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_12 | null>; | ||
/** | ||
@@ -417,3 +454,3 @@ * Execute SQL raw query returning a boolean result. | ||
*/ | ||
getJson<T_15>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_15 | null>; | ||
getJson<T_13>(sql: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_13 | null>; | ||
/** | ||
@@ -434,3 +471,3 @@ * Invoke a scalar-valued function and return results. | ||
*/ | ||
invoke<T_16>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_16 | null>; | ||
invoke<T_14>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_14 | null>; | ||
/** | ||
@@ -451,3 +488,10 @@ * Execute a procedure and return the results returned (if any). | ||
*/ | ||
exec<T_17>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_17[]>; | ||
exec<T_15>(objectName: string, params?: any, trx?: Knex.Transaction<any, any> | undefined): Promise<T_15[]>; | ||
/** | ||
* Build param object for given id. | ||
* | ||
* @param {any} id | ||
* @returns {object} | ||
*/ | ||
buildIdParams(id: any): any; | ||
}; |
@@ -42,5 +42,4 @@ "use strict"; | ||
var constants_1 = require("./constants"); | ||
var object = require("./utils/object"); | ||
var paginator = require("./utils/paginator"); | ||
var ModelNotFoundError_1 = require("./ModelNotFoundError"); | ||
var ramda_1 = require("ramda"); | ||
var log = debug(constants_1.NS_MODEL); | ||
@@ -54,296 +53,333 @@ /** | ||
function createBaseModel(resolver) { | ||
return /** @class */ (function () { | ||
function BaseModel() { | ||
} | ||
/** | ||
* Binds a database connection to the model. | ||
* | ||
* @param {Knex} connection | ||
* @returns {void} | ||
*/ | ||
BaseModel.bindConnection = function (connection) { | ||
log('Binding database connection to the model (Lazy)'); | ||
this.connection = connection; | ||
}; | ||
/** | ||
* Binds a database connection to the model (chainable version of bindConnection()). | ||
* | ||
* @param {Knex} connection | ||
* @returns {any} | ||
*/ | ||
BaseModel.bind = function (connection) { | ||
this.bindConnection(connection); | ||
return this; | ||
}; | ||
/** | ||
* Resolves a database connection. | ||
* | ||
* Note: It would throw an Error on the run time if it couldn't resolve the | ||
* connection by the time any DB methods are invoked on it. | ||
* @returns {Knex} | ||
*/ | ||
BaseModel.getConnection = function () { | ||
if (this.connection) { | ||
return this.connection; | ||
var _a; | ||
return _a = /** @class */ (function () { | ||
function BaseModel() { | ||
} | ||
// Note: We need to resolve db connection everytime. | ||
if (resolver) { | ||
return resolver(); | ||
} | ||
throw new Error('Cannot resolve the database connection.'); | ||
}; | ||
/** | ||
* Generic query builder. | ||
* | ||
* @param {(qb: Knex | Transaction) => QueryBuilder} callback | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.buildQuery = function (callback, trx) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var qb, result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
qb = db.queryBuilder(this.getConnection(), trx); | ||
return [4 /*yield*/, callback(qb)]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, object.toCamelCase(result)]; | ||
} | ||
/** | ||
* Binds a database connection to the model. | ||
* | ||
* @param {Knex} connection | ||
* @returns {void} | ||
*/ | ||
BaseModel.bindConnection = function (connection) { | ||
log('Binding database connection to the model (Lazy)'); | ||
this.connection = connection; | ||
}; | ||
/** | ||
* Binds a database connection to the model (chainable version of bindConnection()). | ||
* | ||
* @param {Knex} connection | ||
* @returns {any} | ||
*/ | ||
BaseModel.bind = function (connection) { | ||
this.bindConnection(connection); | ||
return this; | ||
}; | ||
/** | ||
* Resolves a database connection. | ||
* | ||
* Note: It would throw an Error on the run time if it couldn't resolve the | ||
* connection by the time any DB methods are invoked on it. | ||
* @returns {Knex} | ||
*/ | ||
BaseModel.getConnection = function () { | ||
if (this.connection) { | ||
return this.connection; | ||
} | ||
// Note: We need to resolve db connection everytime. | ||
if (resolver) { | ||
return resolver(); | ||
} | ||
throw new Error('Cannot resolve the database connection.'); | ||
}; | ||
/** | ||
* Generic query builder. | ||
* | ||
* @param {Transaction} [trx] | ||
* @returns {(Knex.Transaction | Knex)} | ||
*/ | ||
BaseModel.queryBuilder = function (trx) { | ||
return db.queryBuilder(this.getConnection(), trx); | ||
}; | ||
/** | ||
* Finds a record based on the params. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
BaseModel.find = function (params, callback, trx) { | ||
if (params === void 0) { params = {}; } | ||
return db.find(this.getConnection(), this.table, params, callback, trx); | ||
}; | ||
/** | ||
* Finds the first record based on the params. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.findFirst = function (params, callback, trx) { | ||
if (params === void 0) { params = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, db.findFirst(this.getConnection(), this.table, params, ramda_1.any, trx)]; | ||
}); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Finds a record based on the params. | ||
* Returns null if no results were found. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.get = function (params, trx) { | ||
if (params === void 0) { params = {}; } | ||
return db.get(this.getConnection(), this.table, params, trx); | ||
}; | ||
/** | ||
* Find record by it's id. | ||
* Returns null if not found. | ||
* | ||
* @param {number} id | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.getById = function (id, trx) { | ||
return db.getById(this.getConnection(), this.table, id, trx); | ||
}; | ||
/** | ||
* Finds a record based on the params. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
*/ | ||
BaseModel.find = function (params, trx) { | ||
if (params === void 0) { params = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.get(params, trx)]; | ||
case 1: | ||
result = _a.sent(); | ||
if (!result) { | ||
throw new ModelNotFoundError_1.default(this.name + ' not found'); | ||
} | ||
return [2 /*return*/, result]; | ||
}; | ||
/** | ||
* Find record by it's id. | ||
* Throws an exception if not found. | ||
* | ||
* @param {any} id | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.findById = function (id, callback, trx) { | ||
var idParams = this.buildIdParams(id); | ||
return db.findFirst(this.getConnection(), this.table, idParams, callback, trx); | ||
}; | ||
/** | ||
* Find record by it's id. | ||
* Throws an exception if not found. | ||
* | ||
* @param {any} id | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.findByIdOrFail = function (id, callback, trx) { | ||
var _this = this; | ||
var idParams = this.buildIdParams(id); | ||
return db.findFirst(this.getConnection(), this.table, idParams, callback, trx).then(function (result) { | ||
if (!result) { | ||
throw new ModelNotFoundError_1.default(_this.name + ' not found'); | ||
} | ||
return result; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Find record by it's id. | ||
* Throws an exception if not found. | ||
* | ||
* @param {number} id | ||
* @param {Knex.Transaction} trx | ||
* @throws {ModelNotFoundError} | ||
* @returns {Promise<T>} | ||
*/ | ||
BaseModel.findById = function (id, trx) { | ||
return this.find({ id: id }, trx); | ||
}; | ||
/** | ||
* Find all records based on the params. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Knex.Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.findAll = function (params, trx) { | ||
if (params === void 0) { params = {}; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, db.findAll(this.getConnection(), this.table, params, trx)]; | ||
}; | ||
/** | ||
* Finds records based on the params with records limit. | ||
* | ||
* @param {object} [params={}] | ||
* @param {PaginationParams} pageParams | ||
* @param {OrderBy[]} sortParams | ||
* @param {Knex.Transaction} trx | ||
* @param {Function} callback | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
BaseModel.findWithPageAndSort = function (params, pageParams, sortParams, callback, trx) { | ||
if (params === void 0) { params = {}; } | ||
var offset = (pageParams.page - 1) * pageParams.pageSize; | ||
var qb = this.find(params, trx).offset(offset).limit(pageParams.pageSize); | ||
if (sortParams && sortParams.length > 0) { | ||
qb.clearOrder(); | ||
sortParams.forEach(function (item) { | ||
qb.orderBy(item.field, item.direction); | ||
}); | ||
} | ||
if (callback) | ||
callback(qb); | ||
return qb; | ||
}; | ||
/** | ||
* Count the total records. | ||
* | ||
* @param {object} [params={}] | ||
* @param {Function} callback | ||
* @param {Knex.Transaction} trx | ||
* @returns {Knex.QueryBuilder} | ||
*/ | ||
BaseModel.count = function (params, callback, trx) { | ||
if (params === void 0) { params = {}; } | ||
var qb = this.find(params, trx).clearSelect().count('*').clearOrder(); | ||
if (callback) | ||
callback(qb); | ||
return qb.then(function (_a) { | ||
var result = _a[0]; | ||
return result.count; | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Insert all records sent in data object. | ||
* | ||
* @param {(object | object[])} data | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.insert = function (data, trx) { | ||
return db.insert(this.getConnection(), this.table, data, trx); | ||
}; | ||
/** | ||
* Update records by id. | ||
* | ||
* @param {number} id | ||
* @param {object} params | ||
* @param {Transaction} transaction | ||
* @returns {Promise<object>} | ||
*/ | ||
BaseModel.updateById = function (id, params, trx) { | ||
return db.updateById(this.getConnection(), this.table, id, params, trx); | ||
}; | ||
/** | ||
* Update records by where condition. | ||
* | ||
* @param {object} where | ||
* @param {object} params | ||
* @param {Transaction} transaction | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.updateWhere = function (where, params, trx) { | ||
return db.updateWhere(this.getConnection(), this.table, where, params, trx); | ||
}; | ||
/** | ||
* Delete row in table. | ||
* | ||
* @param {object} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.delete = function (params, trx) { | ||
return db.remove(this.getConnection(), this.table, params, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query and return results. | ||
* | ||
* @param {string} sql | ||
* @param {RawBindingParams | ValueMap} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.query = function (sql, params, trx) { | ||
return db.query(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Execute a query with pagination and return the paginated results. | ||
* TODO: Accept both raw sql query and knex query builder instance. | ||
* | ||
* @param {string} query | ||
* @param {PaginationParams} params | ||
* @returns {Promise<PaginationResult<T>>} | ||
*/ | ||
BaseModel.paginate = function (query, params) { | ||
return paginator.paginate(this.getConnection(), query, params); | ||
}; | ||
/** | ||
* Method to perform a transactional query execution. | ||
* | ||
* @param {(trx: Transaction) => any} callback | ||
* @returns {any} | ||
*/ | ||
BaseModel.transaction = function (callback) { | ||
return this.getConnection().transaction(callback); | ||
}; | ||
/** | ||
* Batch insert rows of data. | ||
* | ||
* @param {object[]} data | ||
* @param {Transaction} [trx] | ||
* @param {number} [chunksize=30] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.batchInsert = function (data, trx, chunksize) { | ||
if (chunksize === void 0) { chunksize = 30; } | ||
return db.batchInsert(this.getConnection(), this.table, data, chunksize, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query and return scalar value. | ||
* | ||
* @param {string} sql | ||
* @param {any} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.getValue = function (sql, params, trx) { | ||
return db.getValue(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query returning a boolean result. | ||
* | ||
* @param {string} sql | ||
* @param {*} [params] | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<boolean>} | ||
*/ | ||
BaseModel.check = function (sql, params, trx) { | ||
return db.check(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query returning a JSON encoded result | ||
* and produce the parsed object. | ||
* | ||
* @param {string} sql | ||
* @param {*} [params] | ||
* @param {Transaction} [trx] | ||
* @returns {(Promise<T | null>)} | ||
*/ | ||
BaseModel.getJson = function (sql, params, trx) { | ||
return db.getJson(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Invoke a scalar-valued function and return results. | ||
* | ||
* Example usage: | ||
* | ||
* const username = await User.invoke<string>('dbo.can_user_access_object', { userId: 10, objectId: 15 }); | ||
* | ||
* // => Runs SQL: SELECT dbo.can_user_access_object(:userId, :objectId) | ||
* // => Binds params: { userId: 10, objectId: 15 } | ||
* | ||
* @param {string} objectName | ||
* @param {RawBindingParams | ValueMap} [params] | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.invoke = function (objectName, params, trx) { | ||
return db.invoke(trx || this.getConnection(), objectName, params); | ||
}; | ||
/** | ||
* Execute a procedure and return the results returned (if any). | ||
* | ||
* Example usage: | ||
* | ||
* await Recommendation.exec<string>('dbo.update_top_recommendations', { userId: 10, type }); | ||
* | ||
* // => Runs SQL: EXEC dbo.update_top_recommendations :userId, :type | ||
* // => Binds params: { userId: 10, type } | ||
* | ||
* @param {string} objectName | ||
* @param {RawBindingParams | ValueMap} [params] | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.exec = function (objectName, params, trx) { | ||
return db.exec(trx || this.getConnection(), objectName, params); | ||
}; | ||
return BaseModel; | ||
}()); | ||
}; | ||
/** | ||
* Insert all records sent in data object. | ||
* | ||
* @param {(object | object[])} data | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.insert = function (data, trx) { | ||
return db.insert(this.getConnection(), this.table, data, trx); | ||
}; | ||
/** | ||
* Update records by id. | ||
* | ||
* @param {number} id | ||
* @param {object} params | ||
* @param {Transaction} transaction | ||
* @returns {Promise<object>} | ||
*/ | ||
BaseModel.updateById = function (id, params, trx) { | ||
var idParams = this.buildIdParams(id); | ||
return db.update(this.getConnection(), this.table, idParams, params, trx); | ||
}; | ||
/** | ||
* Update records by where condition. | ||
* | ||
* @param {object} where | ||
* @param {object} params | ||
* @param {Transaction} transaction | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.update = function (where, params, trx) { | ||
return db.update(this.getConnection(), this.table, where, params, trx); | ||
}; | ||
/** | ||
* Delete row in table. | ||
* | ||
* @param {object} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.deleteById = function (id, trx) { | ||
var idParams = this.buildIdParams(id); | ||
return db.remove(this.getConnection(), this.table, idParams, trx); | ||
}; | ||
/** | ||
* Delete row in table. | ||
* | ||
* @param {object} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.delete = function (params, trx) { | ||
return db.remove(this.getConnection(), this.table, params, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query and return results. | ||
* | ||
* @param {string} sql | ||
* @param {RawBindingParams | ValueMap} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.query = function (sql, params, trx) { | ||
return db.query(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Method to perform a transactional query execution. | ||
* | ||
* @param {(trx: Transaction) => any} callback | ||
* @param {Transaction} trx | ||
* @returns {any} | ||
*/ | ||
BaseModel.transaction = function (callback, trx) { | ||
if (trx) { | ||
return callback(trx); | ||
} | ||
return this.getConnection().transaction(callback); | ||
}; | ||
/** | ||
* Batch insert rows of data. | ||
* | ||
* @param {object[]} data | ||
* @param {Transaction} [trx] | ||
* @param {number} [chunksize=30] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.batchInsert = function (data, trx, chunksize) { | ||
if (chunksize === void 0) { chunksize = 30; } | ||
return db.batchInsert(this.getConnection(), this.table, data, chunksize, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query and return scalar value. | ||
* | ||
* @param {string} sql | ||
* @param {any} params | ||
* @param {Transaction} trx | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.getValue = function (sql, params, trx) { | ||
return db.getValue(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query returning a boolean result. | ||
* | ||
* @param {string} sql | ||
* @param {*} [params] | ||
* @param {Transaction} [trx] | ||
* @returns {Promise<boolean>} | ||
*/ | ||
BaseModel.check = function (sql, params, trx) { | ||
return db.check(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Execute SQL raw query returning a JSON encoded result | ||
* and produce the parsed object. | ||
* | ||
* @param {string} sql | ||
* @param {*} [params] | ||
* @param {Transaction} [trx] | ||
* @returns {(Promise<T | null>)} | ||
*/ | ||
BaseModel.getJson = function (sql, params, trx) { | ||
return db.getJson(this.getConnection(), sql, params, trx); | ||
}; | ||
/** | ||
* Invoke a scalar-valued function and return results. | ||
* | ||
* Example usage: | ||
* | ||
* const username = await User.invoke<string>('dbo.can_user_access_object', { userId: 10, objectId: 15 }); | ||
* | ||
* // => Runs SQL: SELECT dbo.can_user_access_object(:userId, :objectId) | ||
* // => Binds params: { userId: 10, objectId: 15 } | ||
* | ||
* @param {string} objectName | ||
* @param {RawBindingParams | ValueMap} [params] | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T | null>} | ||
*/ | ||
BaseModel.invoke = function (objectName, params, trx) { | ||
return db.invoke(trx || this.getConnection(), objectName, params); | ||
}; | ||
/** | ||
* Execute a procedure and return the results returned (if any). | ||
* | ||
* Example usage: | ||
* | ||
* await Recommendation.exec<string>('dbo.update_top_recommendations', { userId: 10, type }); | ||
* | ||
* // => Runs SQL: EXEC dbo.update_top_recommendations :userId, :type | ||
* // => Binds params: { userId: 10, type } | ||
* | ||
* @param {string} objectName | ||
* @param {RawBindingParams | ValueMap} [params] | ||
* @param {Knex.Transaction} [trx] | ||
* @returns {Promise<T[]>} | ||
*/ | ||
BaseModel.exec = function (objectName, params, trx) { | ||
return db.exec(trx || this.getConnection(), objectName, params); | ||
}; | ||
/** | ||
* Build param object for given id. | ||
* | ||
* @param {any} id | ||
* @returns {object} | ||
*/ | ||
BaseModel.buildIdParams = function (id) { | ||
var _a; | ||
if (typeof id === 'number' || typeof id === 'string') { | ||
return _a = {}, _a[this.id] = id, _a; | ||
} | ||
return id; | ||
}; | ||
return BaseModel; | ||
}()), | ||
_a.defaultOrderBy = [{ field: 'id', direction: 'asc' }], | ||
_a.id = 'id', | ||
_a; | ||
} | ||
@@ -350,0 +386,0 @@ exports.createBaseModel = createBaseModel; |
{ | ||
"name": "@leapfrogtechnology/db-model", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"description": "Low-footprint database abstraction layer and model built on top of Knex.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
86665
1828