@squiz/db-lib 1.12.0-alpha.8 → 1.12.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ 0 verbose cli /usr/local/bin/node /usr/local/bin/npm
2
+ 1 info using npm@8.19.3
3
+ 2 info using node@v16.19.0
4
+ 3 timing npm:load:whichnode Completed in 1ms
5
+ 4 timing config:load:defaults Completed in 2ms
6
+ 5 timing config:load:file:/usr/local/lib/node_modules/npm/npmrc Completed in 1ms
7
+ 6 timing config:load:builtin Completed in 1ms
8
+ 7 timing config:load:cli Completed in 2ms
9
+ 8 timing config:load:env Completed in 1ms
10
+ 9 info found workspace root at /builds/developer-experience/cmp
11
+ 10 timing config:load:file:/builds/developer-experience/cmp/.npmrc Completed in 1ms
12
+ 11 timing config:load:project Completed in 24ms
13
+ 12 timing config:load:file:/root/.npmrc Completed in 1ms
14
+ 13 timing config:load:user Completed in 1ms
15
+ 14 timing config:load:file:/usr/local/etc/npmrc Completed in 0ms
16
+ 15 timing config:load:global Completed in 0ms
17
+ 16 timing config:load:validate Completed in 1ms
18
+ 17 timing config:load:credentials Completed in 2ms
19
+ 18 timing config:load:setEnvs Completed in 1ms
20
+ 19 timing config:load Completed in 35ms
21
+ 20 timing npm:load:configload Completed in 35ms
22
+ 21 timing npm:load:mkdirpcache Completed in 1ms
23
+ 22 timing npm:load:mkdirplogs Completed in 1ms
24
+ 23 verbose title npm run compile
25
+ 24 verbose argv "run" "compile" "--"
26
+ 25 timing npm:load:setTitle Completed in 1ms
27
+ 26 timing config:load:flatten Completed in 4ms
28
+ 27 timing npm:load:display Completed in 5ms
29
+ 28 verbose logfile logs-max:10 dir:/builds/developer-experience/cmp/packages/db-lib/.npm/_logs
30
+ 29 verbose logfile /builds/developer-experience/cmp/packages/db-lib/.npm/_logs/2023-02-27T04_50_30_941Z-debug-0.log
31
+ 30 timing npm:load:logFile Completed in 4ms
32
+ 31 timing npm:load:timers Completed in 0ms
33
+ 32 timing npm:load:configScope Completed in 0ms
34
+ 33 timing npm:load Completed in 48ms
35
+ 34 silly logfile done cleaning log files
36
+ 35 timing command:run Completed in 3261ms
37
+ 36 verbose exit 0
38
+ 37 timing npm Completed in 3317ms
39
+ 38 info ok
package/jest.config.ts CHANGED
@@ -1,10 +1,13 @@
1
- import type { Config } from 'jest';
2
-
3
1
  import { config as configFunc } from 'dotenv';
4
- configFunc();
2
+ if ('error' in configFunc()) {
3
+ configFunc({ path: 'test.env' });
4
+ }
5
5
 
6
6
  // Sync object
7
- const config: Config = {
7
+ /**
8
+ * @type {import('jest').Config}
9
+ */
10
+ const config: import('jest').Config = {
8
11
  preset: 'ts-jest',
9
12
  testTimeout: 60_000,
10
13
  testEnvironment: 'node',
@@ -17,39 +17,39 @@ export type PageResult<T> = {
17
17
  };
18
18
  export type SortDirection = 'desc' | 'asc';
19
19
  export declare const DEFAULT_PAGE_SIZE = 20;
20
- export declare abstract class AbstractRepository<T extends object, ObjT extends T> implements Reader<T>, Writer<T> {
20
+ export declare abstract class AbstractRepository<SHAPE extends object, DATA_CLASS extends SHAPE> implements Reader<SHAPE>, Writer<SHAPE> {
21
21
  protected repositories: Repositories;
22
22
  protected pool: Pool;
23
23
  protected classRef: {
24
- new (data?: Record<string, unknown>): ObjT;
24
+ new (data?: Record<string, unknown>): DATA_CLASS;
25
25
  };
26
26
  protected tableName: string;
27
27
  /** object where the key is the model property name amd the value is sql column name */
28
28
  protected modelPropertyToSqlColumn: {
29
- [key in keyof T]: string;
29
+ [key in keyof SHAPE]: string;
30
30
  };
31
31
  /** object where the key is the sql column name and the value is the model property name */
32
32
  protected sqlColumnToModelProperty: {
33
33
  [key: string]: string;
34
34
  };
35
35
  constructor(repositories: Repositories, pool: Pool, tableName: string, mapping: {
36
- [key in keyof T]: string;
36
+ [key in keyof SHAPE]: string;
37
37
  }, classRef: {
38
- new (data?: Record<string, unknown>): ObjT;
38
+ new (data?: Record<string, unknown>): DATA_CLASS;
39
39
  });
40
40
  protected getConnection(): Promise<PoolClient>;
41
- create(value: ObjT, transactionClient?: PoolClient | null): Promise<T>;
42
- update(where: Partial<T>, newValue: Partial<T>, transactionClient?: PoolClient | null): Promise<T[]>;
43
- delete(where: Partial<T>, transactionClient?: PoolClient | null): Promise<number>;
44
- protected createWhereStringFromPartialModel(values: Partial<T>, initialIndex?: number): string;
41
+ create(value: DATA_CLASS, transactionClient?: PoolClient | null): Promise<SHAPE>;
42
+ update(where: Partial<SHAPE>, newValue: Partial<SHAPE>, transactionClient?: PoolClient | null): Promise<SHAPE[]>;
43
+ delete(where: Partial<SHAPE>, transactionClient?: PoolClient | null): Promise<number>;
44
+ protected createWhereStringFromPartialModel(values: Partial<SHAPE>, initialIndex?: number): string;
45
45
  protected executeQueryRaw(query: string, values: any[], transactionClient?: PoolClient | null): Promise<any[]>;
46
- protected executeQuery(query: string, values: any[], transactionClient?: PoolClient | null): Promise<T[]>;
47
- protected createAndHydrateModel(row: any): T;
48
- findOne(item: Partial<T>): Promise<T | undefined>;
49
- find(item: Partial<T>): Promise<T[]>;
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>>;
46
+ protected executeQuery(query: string, values: any[], transactionClient?: PoolClient | null): Promise<SHAPE[]>;
47
+ protected createAndHydrateModel(row: any): SHAPE;
48
+ findOne(item: Partial<SHAPE>): Promise<SHAPE | undefined>;
49
+ find(item: Partial<SHAPE>): Promise<SHAPE[]>;
50
+ findAll(): Promise<SHAPE[]>;
51
+ getCount(item?: Partial<SHAPE> | null): Promise<number>;
52
+ getPage(pageNumber: number, sortBy?: (keyof SHAPE)[], direction?: SortDirection, pageSize?: number | null, item?: Partial<SHAPE> | null): Promise<PageResult<SHAPE>>;
53
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>>;
54
+ getPageRaw(pageNumber: number, sortBy?: (keyof SHAPE)[], direction?: SortDirection, whereClause?: string, tableRef?: string, values?: any[], pageSize?: number | null): Promise<PageResult<SHAPE>>;
55
55
  }
@@ -0,0 +1 @@
1
+ export {};
package/lib/index.js CHANGED
@@ -35405,36 +35405,19 @@ var AbstractRepository = class {
35405
35405
  return this.getCountRaw(whereClause, item ? Object.values(item) : []);
35406
35406
  }
35407
35407
  async getPage(pageNumber, sortBy = [], direction = "asc", pageSize = null, item = null) {
35408
- if (pageSize === null) {
35409
- pageSize = DEFAULT_PAGE_SIZE;
35410
- }
35411
- if (pageNumber <= 0) {
35412
- throw new Error(`Page number value cannot be less than 1`);
35413
- }
35414
- if (pageSize <= 0) {
35415
- throw new Error(`Page size value cannot be less than 1`);
35416
- }
35417
35408
  let whereClause = "";
35418
35409
  if (item) {
35419
35410
  whereClause = `WHERE ${this.createWhereStringFromPartialModel(item)}`;
35420
35411
  }
35421
- let orderByClause = "";
35422
- if (sortBy.length) {
35423
- orderByClause = `ORDER BY ${sortBy.map((a) => this.modelPropertyToSqlColumn[a]).join(",")} ${direction}`;
35424
- }
35425
- const offset = (pageNumber - 1) * pageSize;
35426
- const items = await this.executeQuery(
35427
- `SELECT *
35428
- FROM ${this.tableName} ${whereClause} ${orderByClause}
35429
- OFFSET ${offset}
35430
- LIMIT ${pageSize}`,
35431
- item ? Object.values(item) : []
35432
- );
35433
- return {
35434
- items,
35435
- totalCount: await this.getCount(item),
35412
+ return this.getPageRaw(
35413
+ pageNumber,
35414
+ sortBy,
35415
+ direction,
35416
+ whereClause,
35417
+ this.tableName,
35418
+ Object.values(item ?? {}),
35436
35419
  pageSize
35437
- };
35420
+ );
35438
35421
  }
35439
35422
  async getCountRaw(whereClause = "", values = [], tableRef = "") {
35440
35423
  const result = await this.executeQueryRaw(
@@ -35455,7 +35438,7 @@ var AbstractRepository = class {
35455
35438
  }
35456
35439
  let orderByClause = "";
35457
35440
  if (sortBy.length) {
35458
- orderByClause = `ORDER BY ${sortBy.join(",")} ${direction}`;
35441
+ orderByClause = `ORDER BY ${sortBy.map((a) => this.modelPropertyToSqlColumn[a]).join(",")} ${direction}`;
35459
35442
  }
35460
35443
  const offset = (pageNumber - 1) * pageSize;
35461
35444
  const query = `