@squiz/db-lib 1.2.11 → 1.2.13-alpha.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +8 -82
- package/lib/AbstractRepository.d.ts +6 -4
- package/lib/ConnectionManager.d.ts +1 -1
- package/lib/Migrator.d.ts +3 -1
- package/lib/Repositories.d.ts +1 -1
- package/lib/index.js +62 -10
- package/lib/index.js.map +2 -2
- package/package.json +8 -8
- package/src/AbstractRepository.ts +55 -7
- package/src/Migrator.ts +32 -5
- package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
@@ -3,174 +3,100 @@
|
|
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.
|
7
|
-
|
8
|
-
**Note:** Version bump only for package @squiz/db-lib
|
6
|
+
## [1.2.13-alpha.0](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.13-alpha.0) (2023-01-13)
|
9
7
|
|
8
|
+
### Bug Fixes
|
10
9
|
|
10
|
+
- **deps:** update dependency constructs to v10.1.183 ([def2040](https://gitlab.squiz.net/developer-experience/cmp/commit/def204076fd709282067ccaa09f73f08824bdbf4))
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
## [1.2.10](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.10) (2022-11-09)
|
12
|
+
## [1.2.12](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.12) (2022-11-24)
|
15
13
|
|
16
14
|
**Note:** Version bump only for package @squiz/db-lib
|
17
15
|
|
16
|
+
## [1.2.11](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.11) (2022-11-11)
|
18
17
|
|
18
|
+
**Note:** Version bump only for package @squiz/db-lib
|
19
19
|
|
20
|
+
## [1.2.10](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.10) (2022-11-09)
|
20
21
|
|
22
|
+
**Note:** Version bump only for package @squiz/db-lib
|
21
23
|
|
22
24
|
## [1.2.9](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.9) (2022-11-04)
|
23
25
|
|
24
26
|
**Note:** Version bump only for package @squiz/db-lib
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
28
|
## [1.2.2](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1...v1.2.2) (2022-09-26)
|
31
29
|
|
32
30
|
**Note:** Version bump only for package @squiz/db-lib
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
32
|
## [1.2.1](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1) (2022-09-23)
|
39
33
|
|
40
34
|
**Note:** Version bump only for package @squiz/db-lib
|
41
35
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
36
|
## [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)
|
47
37
|
|
48
38
|
**Note:** Version bump only for package @squiz/db-lib
|
49
39
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
40
|
## [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)
|
55
41
|
|
56
42
|
**Note:** Version bump only for package @squiz/db-lib
|
57
43
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
44
|
## [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)
|
63
45
|
|
64
46
|
**Note:** Version bump only for package @squiz/db-lib
|
65
47
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
48
|
## [1.2.1-alpha.104](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.104) (2022-08-23)
|
71
49
|
|
72
50
|
**Note:** Version bump only for package @squiz/db-lib
|
73
51
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
52
|
## [1.2.1-alpha.103](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.103) (2022-08-23)
|
79
53
|
|
80
54
|
**Note:** Version bump only for package @squiz/db-lib
|
81
55
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
56
|
## [1.2.1-alpha.102](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.102) (2022-08-23)
|
87
57
|
|
88
58
|
**Note:** Version bump only for package @squiz/db-lib
|
89
59
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
60
|
## [1.2.1-alpha.101](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.101) (2022-08-19)
|
95
61
|
|
96
62
|
**Note:** Version bump only for package @squiz/db-lib
|
97
63
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
64
|
## [1.2.1-alpha.100](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.100) (2022-08-18)
|
103
65
|
|
104
66
|
**Note:** Version bump only for package @squiz/db-lib
|
105
67
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
68
|
## [1.2.1-alpha.99](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.99) (2022-08-18)
|
111
69
|
|
112
70
|
**Note:** Version bump only for package @squiz/db-lib
|
113
71
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
72
|
## [1.2.1-alpha.98](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.98) (2022-08-17)
|
119
73
|
|
120
74
|
**Note:** Version bump only for package @squiz/db-lib
|
121
75
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
76
|
## [1.2.1-alpha.97](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.97) (2022-08-17)
|
127
77
|
|
128
78
|
**Note:** Version bump only for package @squiz/db-lib
|
129
79
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
80
|
## [1.2.1-alpha.96](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.96) (2022-08-12)
|
135
81
|
|
136
82
|
**Note:** Version bump only for package @squiz/db-lib
|
137
83
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
84
|
## [1.2.1-alpha.95](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.90...v1.2.1-alpha.95) (2022-08-10)
|
143
85
|
|
144
86
|
**Note:** Version bump only for package @squiz/db-lib
|
145
87
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
88
|
## [1.2.1-alpha.90](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.89...v1.2.1-alpha.90) (2022-08-05)
|
151
89
|
|
152
90
|
**Note:** Version bump only for package @squiz/db-lib
|
153
91
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
92
|
## [1.2.1-alpha.89](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.88...v1.2.1-alpha.89) (2022-08-05)
|
159
93
|
|
160
94
|
**Note:** Version bump only for package @squiz/db-lib
|
161
95
|
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
96
|
## [1.2.1-alpha.88](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.67...v1.2.1-alpha.88) (2022-08-05)
|
167
97
|
|
168
98
|
**Note:** Version bump only for package @squiz/db-lib
|
169
99
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
100
|
## [1.2.1-alpha.87](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.67...v1.2.1-alpha.87) (2022-08-05)
|
175
101
|
|
176
102
|
**Note:** Version bump only for package @squiz/db-lib
|
@@ -9,15 +9,15 @@ export interface Writer<T> {
|
|
9
9
|
update(where: Partial<T>, newValue: Partial<T>): Promise<T[]>;
|
10
10
|
delete(where: Partial<T>): Promise<number>;
|
11
11
|
}
|
12
|
-
export
|
13
|
-
export
|
12
|
+
export type Repository<T> = Reader<T> & Writer<T>;
|
13
|
+
export type PageResult<T> = {
|
14
14
|
items: T[];
|
15
15
|
totalCount: number;
|
16
16
|
pageSize: number;
|
17
17
|
};
|
18
|
-
export
|
18
|
+
export type SortDirection = 'desc' | 'asc';
|
19
19
|
export declare const DEFAULT_PAGE_SIZE = 20;
|
20
|
-
export declare abstract class AbstractRepository<T, ObjT extends T> implements Reader<T>, Writer<T> {
|
20
|
+
export declare abstract class AbstractRepository<T extends object, ObjT extends T> implements Reader<T>, Writer<T> {
|
21
21
|
protected repositories: Repositories;
|
22
22
|
protected pool: Pool;
|
23
23
|
protected classRef: {
|
@@ -50,4 +50,6 @@ export declare abstract class AbstractRepository<T, ObjT extends T> implements R
|
|
50
50
|
findAll(): Promise<T[]>;
|
51
51
|
getCount(item?: Partial<T> | null): Promise<number>;
|
52
52
|
getPage(pageNumber: number, sortBy?: (keyof T)[], direction?: SortDirection, pageSize?: number | null, item?: Partial<T> | null): Promise<PageResult<T>>;
|
53
|
+
getCountRaw(whereClause?: string, values?: any[], tableRef?: string): Promise<number>;
|
54
|
+
getPageRaw(pageNumber: number, sortBy?: (keyof T)[], direction?: SortDirection, whereClause?: string, tableRef?: string, values?: any[], pageSize?: number | null): Promise<PageResult<T>>;
|
53
55
|
}
|
@@ -11,7 +11,7 @@ export interface DbConnection {
|
|
11
11
|
export interface ConnectionStringObj {
|
12
12
|
connectionString: string;
|
13
13
|
}
|
14
|
-
export
|
14
|
+
export type TransactionClient = PoolClient;
|
15
15
|
export declare class ConnectionManager<T extends Repositories> {
|
16
16
|
protected applicationName: string;
|
17
17
|
protected migrationDirectory: string;
|
package/lib/Migrator.d.ts
CHANGED
@@ -6,13 +6,15 @@ export declare class Migrator {
|
|
6
6
|
constructor(migrationDir: string, migrationList: string[], pool: PoolClient);
|
7
7
|
protected ensureMigrationTableExists(): Promise<import("pg").QueryResult<any>>;
|
8
8
|
protected getAppliedMigrations(): Promise<any[]>;
|
9
|
-
protected
|
9
|
+
protected doSqlMigration(migration: string, sql: string): Promise<void>;
|
10
10
|
protected getPending(migrationsList: string[], appliedMigrations: string[]): Promise<string[]>;
|
11
11
|
protected getSql(migration: string): Promise<string>;
|
12
12
|
protected tryToObtainLock(): Promise<boolean>;
|
13
13
|
protected releaseLock(): Promise<void>;
|
14
14
|
migrate(): Promise<any>;
|
15
15
|
protected runMigrations(): Promise<void>;
|
16
|
+
protected doScriptMigration(migration: string): Promise<void>;
|
16
17
|
protected runMigration(migration: string): Promise<void>;
|
18
|
+
protected doMigrationWork(migration: string): Promise<void>;
|
17
19
|
protected dispose(): void;
|
18
20
|
}
|
package/lib/Repositories.d.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
import { Repository } from './AbstractRepository';
|
2
|
-
export
|
2
|
+
export type Repositories = Record<string, Repository<any>>;
|
package/lib/index.js
CHANGED
@@ -31165,12 +31165,7 @@ var AbstractRepository = class {
|
|
31165
31165
|
if (item) {
|
31166
31166
|
whereClause = `WHERE ${this.createWhereStringFromPartialModel(item)}`;
|
31167
31167
|
}
|
31168
|
-
|
31169
|
-
`SELECT count(*)
|
31170
|
-
FROM ${this.tableName} ${whereClause}`,
|
31171
|
-
item ? Object.values(item) : []
|
31172
|
-
);
|
31173
|
-
return parseInt(result[0].count);
|
31168
|
+
return this.getCountRaw(whereClause, item ? Object.values(item) : []);
|
31174
31169
|
}
|
31175
31170
|
async getPage(pageNumber, sortBy = [], direction = "asc", pageSize = null, item = null) {
|
31176
31171
|
if (pageSize === null) {
|
@@ -31204,6 +31199,41 @@ var AbstractRepository = class {
|
|
31204
31199
|
pageSize
|
31205
31200
|
};
|
31206
31201
|
}
|
31202
|
+
async getCountRaw(whereClause = "", values = [], tableRef = "") {
|
31203
|
+
const result = await this.executeQueryRaw(
|
31204
|
+
`SELECT COUNT(*) FROM ${this.tableName} ${tableRef} ${whereClause}`,
|
31205
|
+
values
|
31206
|
+
);
|
31207
|
+
return parseInt(result[0].count);
|
31208
|
+
}
|
31209
|
+
async getPageRaw(pageNumber, sortBy = [], direction = "asc", whereClause = "", tableRef = "", values = [], pageSize = null) {
|
31210
|
+
if (pageSize === null) {
|
31211
|
+
pageSize = DEFAULT_PAGE_SIZE;
|
31212
|
+
}
|
31213
|
+
if (pageNumber <= 0) {
|
31214
|
+
throw new Error(`Page number value cannot be less than 1`);
|
31215
|
+
}
|
31216
|
+
if (pageSize <= 0) {
|
31217
|
+
throw new Error(`Page size value cannot be less than 1`);
|
31218
|
+
}
|
31219
|
+
let orderByClause = "";
|
31220
|
+
if (sortBy.length) {
|
31221
|
+
orderByClause = `ORDER BY ${sortBy.join(",")} ${direction}`;
|
31222
|
+
}
|
31223
|
+
const offset = (pageNumber - 1) * pageSize;
|
31224
|
+
const query = `
|
31225
|
+
SELECT *
|
31226
|
+
FROM ${this.tableName} ${tableRef} ${whereClause} ${orderByClause}
|
31227
|
+
OFFSET ${offset}
|
31228
|
+
LIMIT ${pageSize}
|
31229
|
+
`;
|
31230
|
+
const items = await this.executeQuery(query, values);
|
31231
|
+
return {
|
31232
|
+
items,
|
31233
|
+
totalCount: await this.getCountRaw(whereClause, values, tableRef),
|
31234
|
+
pageSize
|
31235
|
+
};
|
31236
|
+
}
|
31207
31237
|
};
|
31208
31238
|
|
31209
31239
|
// src/ConnectionManager.ts
|
@@ -31232,14 +31262,13 @@ var Migrator = class {
|
|
31232
31262
|
return row.id;
|
31233
31263
|
});
|
31234
31264
|
}
|
31235
|
-
async
|
31265
|
+
async doSqlMigration(migration, sql) {
|
31236
31266
|
try {
|
31237
31267
|
const result = await this.pool.query(sql);
|
31238
31268
|
logger.info("Applying " + migration);
|
31239
31269
|
if (result.rowCount !== void 0) {
|
31240
31270
|
logger.info("affected rows", result.rowCount);
|
31241
31271
|
}
|
31242
|
-
await this.pool.query("insert into __migrations__ (id) values ($1)", [migration]);
|
31243
31272
|
} catch (e) {
|
31244
31273
|
logger.info("error occurred running migration", migration, e);
|
31245
31274
|
throw e;
|
@@ -31309,11 +31338,24 @@ var Migrator = class {
|
|
31309
31338
|
await this.runMigration(migration);
|
31310
31339
|
}
|
31311
31340
|
}
|
31341
|
+
async doScriptMigration(migration) {
|
31342
|
+
const migrationScript = import_path.default.join(this.migrationDir, migration);
|
31343
|
+
const migrationFunc = require(migrationScript);
|
31344
|
+
let callable;
|
31345
|
+
if (migrationFunc instanceof Function) {
|
31346
|
+
callable = migrationFunc;
|
31347
|
+
} else if (migrationFunc.default instanceof Function) {
|
31348
|
+
callable = migrationFunc.default;
|
31349
|
+
} else {
|
31350
|
+
throw new Error(`${migrationScript} isn't callable`);
|
31351
|
+
}
|
31352
|
+
await callable(this.pool, logger);
|
31353
|
+
}
|
31312
31354
|
async runMigration(migration) {
|
31313
31355
|
try {
|
31314
31356
|
await this.pool.query("BEGIN");
|
31315
|
-
|
31316
|
-
await this.
|
31357
|
+
await this.doMigrationWork(migration);
|
31358
|
+
await this.pool.query("insert into __migrations__ (id) values ($1)", [migration]);
|
31317
31359
|
await this.pool.query("COMMIT");
|
31318
31360
|
} catch (e) {
|
31319
31361
|
logger.error("migration failed", migration, e);
|
@@ -31321,6 +31363,16 @@ var Migrator = class {
|
|
31321
31363
|
throw e;
|
31322
31364
|
}
|
31323
31365
|
}
|
31366
|
+
async doMigrationWork(migration) {
|
31367
|
+
if (migration.endsWith(".sql")) {
|
31368
|
+
const sql = await this.getSql(migration);
|
31369
|
+
await this.doSqlMigration(migration, sql);
|
31370
|
+
} else if (migration.endsWith(".js")) {
|
31371
|
+
await this.doScriptMigration(migration);
|
31372
|
+
} else {
|
31373
|
+
throw new Error(`${migration} as an invalid migration extension`);
|
31374
|
+
}
|
31375
|
+
}
|
31324
31376
|
dispose() {
|
31325
31377
|
return this.pool.release();
|
31326
31378
|
}
|