@squiz/db-lib 1.2.11 → 1.2.12

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,14 @@
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.12](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.12) (2022-11-24)
7
+
8
+ **Note:** Version bump only for package @squiz/db-lib
9
+
10
+
11
+
12
+
13
+
6
14
  ## [1.2.11](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.2...v1.2.11) (2022-11-11)
7
15
 
8
16
  **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 declare type Repository<T> = Reader<T> & Writer<T>;
13
- export declare type PageResult<T> = {
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 declare type SortDirection = 'desc' | 'asc';
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: {
@@ -11,7 +11,7 @@ export interface DbConnection {
11
11
  export interface ConnectionStringObj {
12
12
  connectionString: string;
13
13
  }
14
- export declare type TransactionClient = PoolClient;
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 applyMigration(migration: string, sql: string): Promise<void>;
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
  }
@@ -1,2 +1,2 @@
1
1
  import { Repository } from './AbstractRepository';
2
- export declare type Repositories = Record<string, Repository<any>>;
2
+ export type Repositories = Record<string, Repository<any>>;
package/lib/index.js CHANGED
@@ -31232,14 +31232,13 @@ var Migrator = class {
31232
31232
  return row.id;
31233
31233
  });
31234
31234
  }
31235
- async applyMigration(migration, sql) {
31235
+ async doSqlMigration(migration, sql) {
31236
31236
  try {
31237
31237
  const result = await this.pool.query(sql);
31238
31238
  logger.info("Applying " + migration);
31239
31239
  if (result.rowCount !== void 0) {
31240
31240
  logger.info("affected rows", result.rowCount);
31241
31241
  }
31242
- await this.pool.query("insert into __migrations__ (id) values ($1)", [migration]);
31243
31242
  } catch (e) {
31244
31243
  logger.info("error occurred running migration", migration, e);
31245
31244
  throw e;
@@ -31309,11 +31308,24 @@ var Migrator = class {
31309
31308
  await this.runMigration(migration);
31310
31309
  }
31311
31310
  }
31311
+ async doScriptMigration(migration) {
31312
+ const migrationScript = import_path.default.join(this.migrationDir, migration);
31313
+ const migrationFunc = require(migrationScript);
31314
+ let callable;
31315
+ if (migrationFunc instanceof Function) {
31316
+ callable = migrationFunc;
31317
+ } else if (migrationFunc.default instanceof Function) {
31318
+ callable = migrationFunc.default;
31319
+ } else {
31320
+ throw new Error(`${migrationScript} isn't callable`);
31321
+ }
31322
+ await callable(this.pool, logger);
31323
+ }
31312
31324
  async runMigration(migration) {
31313
31325
  try {
31314
31326
  await this.pool.query("BEGIN");
31315
- const sql = await this.getSql(migration);
31316
- await this.applyMigration(migration, sql);
31327
+ await this.doMigrationWork(migration);
31328
+ await this.pool.query("insert into __migrations__ (id) values ($1)", [migration]);
31317
31329
  await this.pool.query("COMMIT");
31318
31330
  } catch (e) {
31319
31331
  logger.error("migration failed", migration, e);
@@ -31321,6 +31333,16 @@ var Migrator = class {
31321
31333
  throw e;
31322
31334
  }
31323
31335
  }
31336
+ async doMigrationWork(migration) {
31337
+ if (migration.endsWith(".sql")) {
31338
+ const sql = await this.getSql(migration);
31339
+ await this.doSqlMigration(migration, sql);
31340
+ } else if (migration.endsWith(".js")) {
31341
+ await this.doScriptMigration(migration);
31342
+ } else {
31343
+ throw new Error(`${migration} as an invalid migration extension`);
31344
+ }
31345
+ }
31324
31346
  dispose() {
31325
31347
  return this.pool.release();
31326
31348
  }