@squiz/db-lib 1.2.1-alpha.89 → 1.2.1-alpha.90

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.1-alpha.90](https://gitlab.squiz.net/developer-experience/cmp/compare/v1.2.1-alpha.89...v1.2.1-alpha.90) (2022-08-05)
7
+
8
+ **Note:** Version bump only for package @squiz/db-lib
9
+
10
+
11
+
12
+
13
+
6
14
  ## [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)
7
15
 
8
16
  **Note:** Version bump only for package @squiz/db-lib
@@ -0,0 +1,41 @@
1
+ import { PoolClient, Pool } from 'pg';
2
+ import { Repositories } from './Repositories';
3
+ export interface Reader<T> {
4
+ find(item: Partial<T>): Promise<T[]>;
5
+ findOne(id: string | Partial<T>): Promise<T | undefined>;
6
+ }
7
+ export interface Writer<T> {
8
+ create(value: Partial<T>): Promise<T>;
9
+ update(where: Partial<T>, newValue: Partial<T>): Promise<T[]>;
10
+ delete(where: Partial<T>): Promise<number>;
11
+ }
12
+ export declare type Repository<T> = Reader<T> & Writer<T>;
13
+ export declare abstract class AbstractRepository<T> implements Reader<T>, Writer<T> {
14
+ protected repositories: Repositories;
15
+ protected pool: Pool;
16
+ protected classRef: {
17
+ new (): T;
18
+ };
19
+ protected tableName: string;
20
+ protected modelPropertyToSqlColumn: {
21
+ [key in keyof T]: string;
22
+ };
23
+ protected sqlColumnToModelProperty: {
24
+ [key: string]: keyof T;
25
+ };
26
+ constructor(repositories: Repositories, pool: Pool, tableName: string, mapping: {
27
+ [key in keyof T]: string;
28
+ }, classRef: {
29
+ new (): T;
30
+ });
31
+ protected getConnection(): Promise<PoolClient>;
32
+ create(value: Partial<T>, transactionClient?: PoolClient | null): Promise<T>;
33
+ update(where: Partial<T>, newValue: Partial<T>, transactionClient?: PoolClient | null): Promise<T[]>;
34
+ delete(where: Partial<T>, transactionClient?: PoolClient | null): Promise<number>;
35
+ protected createWhereStringFromPartialModel(values: Partial<T>, initialIndex?: number): string;
36
+ protected executeQuery(query: string, values: any[], transactionClient?: PoolClient | null): Promise<T[]>;
37
+ protected createAndHydrateModel(row: any): T;
38
+ findOne(item: Partial<T>): Promise<T | undefined>;
39
+ find(item: Partial<T>): Promise<T[]>;
40
+ findAll(): Promise<T[]>;
41
+ }
@@ -0,0 +1,25 @@
1
+ import { Pool } from 'pg';
2
+ import { PoolClient } from 'pg';
3
+ import { Repositories } from './Repositories';
4
+ export interface DbConnection {
5
+ user: string;
6
+ password: string;
7
+ host: string;
8
+ port: number;
9
+ database: string;
10
+ }
11
+ export interface ConnectionStringObj {
12
+ connectionString: string;
13
+ }
14
+ export declare type TransactionClient = PoolClient;
15
+ export declare class ConnectionManager<T extends Repositories> {
16
+ protected applicationName: string;
17
+ protected migrationDirectory: string;
18
+ protected migrationList: string[];
19
+ readonly pool: Pool;
20
+ readonly repositories: T;
21
+ constructor(applicationName: string, connection: string | DbConnection, migrationDirectory: string, migrationList: string[], repositoryCreator: (dbManager: ConnectionManager<T>) => T);
22
+ applyMigrations(): Promise<void>;
23
+ close(): Promise<void>;
24
+ executeInTransaction<T>(func: (client: TransactionClient) => Promise<T>): Promise<T>;
25
+ }
@@ -0,0 +1,18 @@
1
+ import { PoolClient } from 'pg';
2
+ export declare class Migrator {
3
+ protected migrationDir: string;
4
+ protected migrationList: string[];
5
+ protected pool: PoolClient;
6
+ constructor(migrationDir: string, migrationList: string[], pool: PoolClient);
7
+ protected ensureMigrationTableExists(): Promise<import("pg").QueryResult<any>>;
8
+ protected getAppliedMigrations(): Promise<any[]>;
9
+ protected applyMigration(migration: string, sql: string): Promise<void>;
10
+ protected getPending(migrationsList: string[], appliedMigrations: string[]): Promise<string[]>;
11
+ protected getSql(migration: string): Promise<string>;
12
+ protected tryToObtainLock(): Promise<boolean>;
13
+ protected releaseLock(): Promise<void>;
14
+ migrate(): Promise<any>;
15
+ protected runMigrations(): Promise<void>;
16
+ protected runMigration(migration: string): Promise<void>;
17
+ protected dispose(): void;
18
+ }
@@ -0,0 +1,2 @@
1
+ import { Repository } from './AbstractRepository';
2
+ export declare type Repositories = Record<string, Repository<any>>;
@@ -0,0 +1,5 @@
1
+ import { DbConnection } from './ConnectionManager';
2
+ export declare function getConnectionInfo(config: {
3
+ databaseConnectionSecret?: string | false;
4
+ databaseConnectionString?: string | false;
5
+ }): Promise<DbConnection | string>;
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './AbstractRepository';
2
+ export * from './ConnectionManager';
3
+ export * from './Migrator';
4
+ export * from './Repositories';
5
+ export * from './getConnectionInfo';