@squiz/db-lib 1.2.1-alpha.105 → 1.2.1-alpha.107
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +16 -0
- package/lib/AbstractRepository.d.ts +10 -0
- package/lib/index.js +81 -10
- package/lib/index.js.map +3 -3
- package/package.json +6 -6
- package/src/AbstractRepository.ts +78 -3
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
@@ -3,6 +3,22 @@
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
5
|
|
6
|
+
## [1.2.1-alpha.107](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.107) (2022-09-21)
|
7
|
+
|
8
|
+
**Note:** Version bump only for package @squiz/db-lib
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
## [1.2.1-alpha.106](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.106) (2022-09-02)
|
15
|
+
|
16
|
+
**Note:** Version bump only for package @squiz/db-lib
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
6
22
|
## [1.2.1-alpha.105](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.105) (2022-08-26)
|
7
23
|
|
8
24
|
**Note:** Version bump only for package @squiz/db-lib
|
@@ -10,6 +10,13 @@ export interface Writer<T> {
|
|
10
10
|
delete(where: Partial<T>): Promise<number>;
|
11
11
|
}
|
12
12
|
export declare type Repository<T> = Reader<T> & Writer<T>;
|
13
|
+
export declare type PageResult<T> = {
|
14
|
+
items: T[];
|
15
|
+
totalCount: number;
|
16
|
+
pageSize: number;
|
17
|
+
};
|
18
|
+
export declare type SortDirection = 'desc' | 'asc';
|
19
|
+
export declare const DEFAULT_PAGE_SIZE = 20;
|
13
20
|
export declare abstract class AbstractRepository<T, ObjT extends T> implements Reader<T>, Writer<T> {
|
14
21
|
protected repositories: Repositories;
|
15
22
|
protected pool: Pool;
|
@@ -35,9 +42,12 @@ export declare abstract class AbstractRepository<T, ObjT extends T> implements R
|
|
35
42
|
update(where: Partial<T>, newValue: Partial<T>, transactionClient?: PoolClient | null): Promise<T[]>;
|
36
43
|
delete(where: Partial<T>, transactionClient?: PoolClient | null): Promise<number>;
|
37
44
|
protected createWhereStringFromPartialModel(values: Partial<T>, initialIndex?: number): string;
|
45
|
+
protected executeQueryRaw(query: string, values: any[], transactionClient?: PoolClient | null): Promise<any[]>;
|
38
46
|
protected executeQuery(query: string, values: any[], transactionClient?: PoolClient | null): Promise<T[]>;
|
39
47
|
protected createAndHydrateModel(row: any): T;
|
40
48
|
findOne(item: Partial<T>): Promise<T | undefined>;
|
41
49
|
find(item: Partial<T>): Promise<T[]>;
|
42
50
|
findAll(): Promise<T[]>;
|
51
|
+
getCount(item?: Partial<T> | null): Promise<number>;
|
52
|
+
getPage(pageNumber: number, sortBy?: (keyof T)[], direction?: SortDirection, pageSize?: number | null, item?: Partial<T> | null): Promise<PageResult<T>>;
|
43
53
|
}
|
package/lib/index.js
CHANGED
@@ -14845,6 +14845,31 @@ var require_winston = __commonJS({
|
|
14845
14845
|
}
|
14846
14846
|
});
|
14847
14847
|
|
14848
|
+
// ../dx-logger-lib/lib/formatters.js
|
14849
|
+
var require_formatters = __commonJS({
|
14850
|
+
"../dx-logger-lib/lib/formatters.js"(exports) {
|
14851
|
+
"use strict";
|
14852
|
+
var __importDefault = exports && exports.__importDefault || function(mod) {
|
14853
|
+
return mod && mod.__esModule ? mod : { "default": mod };
|
14854
|
+
};
|
14855
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
14856
|
+
exports.getHumanFormat = exports.errorFormatting = exports.getJsonFormat = void 0;
|
14857
|
+
var winston_1 = __importDefault(require_winston());
|
14858
|
+
function getJsonFormat() {
|
14859
|
+
return winston_1.default.format.combine(winston_1.default.format.errors(), winston_1.default.format.json(), winston_1.default.format.timestamp({ alias: "time" }));
|
14860
|
+
}
|
14861
|
+
exports.getJsonFormat = getJsonFormat;
|
14862
|
+
exports.errorFormatting = winston_1.default.format.printf((info) => {
|
14863
|
+
let message = `${info.timestamp} ${info.level}: ${info.stack || info.message}`;
|
14864
|
+
return message;
|
14865
|
+
});
|
14866
|
+
function getHumanFormat() {
|
14867
|
+
return winston_1.default.format.combine(winston_1.default.format.colorize(), winston_1.default.format.timestamp(), winston_1.default.format.errors({ stack: true }), exports.errorFormatting);
|
14868
|
+
}
|
14869
|
+
exports.getHumanFormat = getHumanFormat;
|
14870
|
+
}
|
14871
|
+
});
|
14872
|
+
|
14848
14873
|
// ../dx-logger-lib/lib/index.js
|
14849
14874
|
var require_lib3 = __commonJS({
|
14850
14875
|
"../dx-logger-lib/lib/index.js"(exports) {
|
@@ -14855,28 +14880,23 @@ var require_lib3 = __commonJS({
|
|
14855
14880
|
Object.defineProperty(exports, "__esModule", { value: true });
|
14856
14881
|
exports.getLogger = exports.Logger = void 0;
|
14857
14882
|
var winston_1 = __importDefault(require_winston());
|
14883
|
+
var formatters_1 = require_formatters();
|
14858
14884
|
var winston_2 = require_winston();
|
14859
14885
|
Object.defineProperty(exports, "Logger", { enumerable: true, get: function() {
|
14860
14886
|
return winston_2.Logger;
|
14861
14887
|
} });
|
14862
14888
|
function getLogger2(options) {
|
14863
|
-
const formatter = options.format === "human" ? getHumanFormat() : getJsonFormat();
|
14889
|
+
const formatter = options.format === "human" ? (0, formatters_1.getHumanFormat)() : (0, formatters_1.getJsonFormat)();
|
14864
14890
|
return winston_1.default.createLogger({
|
14865
14891
|
defaultMeta: {
|
14866
14892
|
...options.meta,
|
14867
14893
|
name: options.name
|
14868
14894
|
},
|
14869
14895
|
format: formatter,
|
14870
|
-
transports: [new winston_1.default.transports.Console()]
|
14896
|
+
transports: [new winston_1.default.transports.Console({ silent: options.silent })]
|
14871
14897
|
});
|
14872
14898
|
}
|
14873
14899
|
exports.getLogger = getLogger2;
|
14874
|
-
function getJsonFormat() {
|
14875
|
-
return winston_1.default.format.combine(winston_1.default.format.json(), winston_1.default.format.timestamp({ alias: "time" }));
|
14876
|
-
}
|
14877
|
-
function getHumanFormat() {
|
14878
|
-
return winston_1.default.format.combine(winston_1.default.format.timestamp(), winston_1.default.format.cli());
|
14879
|
-
}
|
14880
14900
|
}
|
14881
14901
|
});
|
14882
14902
|
|
@@ -30930,6 +30950,7 @@ var src_exports = {};
|
|
30930
30950
|
__export(src_exports, {
|
30931
30951
|
AbstractRepository: () => AbstractRepository,
|
30932
30952
|
ConnectionManager: () => ConnectionManager,
|
30953
|
+
DEFAULT_PAGE_SIZE: () => DEFAULT_PAGE_SIZE,
|
30933
30954
|
Migrator: () => Migrator,
|
30934
30955
|
Pool: () => import_pg2.Pool,
|
30935
30956
|
PostgresErrorCode: () => PostgresErrorCode,
|
@@ -30938,6 +30959,7 @@ __export(src_exports, {
|
|
30938
30959
|
module.exports = __toCommonJS(src_exports);
|
30939
30960
|
|
30940
30961
|
// src/AbstractRepository.ts
|
30962
|
+
var DEFAULT_PAGE_SIZE = 20;
|
30941
30963
|
var AbstractRepository = class {
|
30942
30964
|
constructor(repositories, pool, tableName, mapping, classRef) {
|
30943
30965
|
this.repositories = repositories;
|
@@ -31002,17 +31024,21 @@ var AbstractRepository = class {
|
|
31002
31024
|
}, "");
|
31003
31025
|
return sql;
|
31004
31026
|
}
|
31005
|
-
async
|
31027
|
+
async executeQueryRaw(query, values, transactionClient = null) {
|
31006
31028
|
const client = transactionClient ?? await this.getConnection();
|
31007
31029
|
try {
|
31008
31030
|
const result = await client.query(query, values);
|
31009
|
-
return result.rows
|
31031
|
+
return result.rows;
|
31010
31032
|
} finally {
|
31011
31033
|
if (client && !transactionClient) {
|
31012
31034
|
client.release();
|
31013
31035
|
}
|
31014
31036
|
}
|
31015
31037
|
}
|
31038
|
+
async executeQuery(query, values, transactionClient = null) {
|
31039
|
+
const rows = await this.executeQueryRaw(query, values, transactionClient);
|
31040
|
+
return rows.map((a) => this.createAndHydrateModel(a));
|
31041
|
+
}
|
31016
31042
|
createAndHydrateModel(row) {
|
31017
31043
|
const inputData = {};
|
31018
31044
|
for (const key of Object.keys(row)) {
|
@@ -31048,6 +31074,50 @@ var AbstractRepository = class {
|
|
31048
31074
|
);
|
31049
31075
|
return result;
|
31050
31076
|
}
|
31077
|
+
async getCount(item = null) {
|
31078
|
+
let whereClause = "";
|
31079
|
+
if (item) {
|
31080
|
+
whereClause = `WHERE ${this.createWhereStringFromPartialModel(item)}`;
|
31081
|
+
}
|
31082
|
+
const result = await this.executeQueryRaw(
|
31083
|
+
`SELECT count(*)
|
31084
|
+
FROM ${this.tableName} ${whereClause}`,
|
31085
|
+
item ? Object.values(item) : []
|
31086
|
+
);
|
31087
|
+
return parseInt(result[0].count);
|
31088
|
+
}
|
31089
|
+
async getPage(pageNumber, sortBy = [], direction = "asc", pageSize = null, item = null) {
|
31090
|
+
if (pageSize === null) {
|
31091
|
+
pageSize = DEFAULT_PAGE_SIZE;
|
31092
|
+
}
|
31093
|
+
if (pageNumber <= 0) {
|
31094
|
+
throw new Error(`Page number value cannot be less than 1`);
|
31095
|
+
}
|
31096
|
+
if (pageSize <= 0) {
|
31097
|
+
throw new Error(`Page size value cannot be less than 1`);
|
31098
|
+
}
|
31099
|
+
let whereClause = "";
|
31100
|
+
if (item) {
|
31101
|
+
whereClause = `WHERE ${this.createWhereStringFromPartialModel(item)}`;
|
31102
|
+
}
|
31103
|
+
let orderByClause = "";
|
31104
|
+
if (sortBy.length) {
|
31105
|
+
orderByClause = `ORDER BY ${sortBy.map((a) => this.modelPropertyToSqlColumn[a]).join(",")} ${direction}`;
|
31106
|
+
}
|
31107
|
+
const offset = (pageNumber - 1) * pageSize;
|
31108
|
+
const items = await this.executeQuery(
|
31109
|
+
`SELECT *
|
31110
|
+
FROM ${this.tableName} ${whereClause} ${orderByClause}
|
31111
|
+
OFFSET ${offset}
|
31112
|
+
LIMIT ${pageSize}`,
|
31113
|
+
item ? Object.values(item) : []
|
31114
|
+
);
|
31115
|
+
return {
|
31116
|
+
items,
|
31117
|
+
totalCount: await this.getCount(item),
|
31118
|
+
pageSize
|
31119
|
+
};
|
31120
|
+
}
|
31051
31121
|
};
|
31052
31122
|
|
31053
31123
|
// src/ConnectionManager.ts
|
@@ -31520,6 +31590,7 @@ var import_pg2 = __toESM(require_lib2());
|
|
31520
31590
|
0 && (module.exports = {
|
31521
31591
|
AbstractRepository,
|
31522
31592
|
ConnectionManager,
|
31593
|
+
DEFAULT_PAGE_SIZE,
|
31523
31594
|
Migrator,
|
31524
31595
|
Pool,
|
31525
31596
|
PostgresErrorCode,
|