@squiz/db-lib 1.71.1 → 1.71.2

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.71.2
4
+
5
+ ### Patch Changes
6
+
7
+ - d9a8506: fix up publish compilation
8
+
3
9
  ## 1.71.1
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,55 @@
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 type Repository<T> = Reader<T> & Writer<T>;
13
+ export type PageResult<T> = {
14
+ items: T[];
15
+ totalCount: number;
16
+ pageSize: number;
17
+ };
18
+ export type SortDirection = 'desc' | 'asc';
19
+ export declare const DEFAULT_PAGE_SIZE = 20;
20
+ export declare abstract class AbstractRepository<SHAPE extends object, DATA_CLASS extends SHAPE> implements Reader<SHAPE>, Writer<SHAPE> {
21
+ protected repositories: Repositories;
22
+ protected pool: Pool;
23
+ protected classRef: {
24
+ new (data?: Record<string, unknown>): DATA_CLASS;
25
+ };
26
+ protected tableName: string;
27
+ /** object where the key is the model property name amd the value is sql column name */
28
+ protected modelPropertyToSqlColumn: {
29
+ [key in keyof SHAPE]: string;
30
+ };
31
+ /** object where the key is the sql column name and the value is the model property name */
32
+ protected sqlColumnToModelProperty: {
33
+ [key: string]: string;
34
+ };
35
+ constructor(repositories: Repositories, pool: Pool, tableName: string, mapping: {
36
+ [key in keyof SHAPE]: string;
37
+ }, classRef: {
38
+ new (data?: Record<string, unknown>): DATA_CLASS;
39
+ });
40
+ protected getConnection(): Promise<PoolClient>;
41
+ create(value: SHAPE, transactionClient?: PoolClient | null): Promise<SHAPE>;
42
+ update(where: Partial<SHAPE>, newValue: Exclude<Partial<SHAPE>, Record<string, never>>, 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
+ protected executeQueryRaw(query: string, values: any[], transactionClient?: PoolClient | null): Promise<any[]>;
46
+ executeQuery(query: string, values: any[], transactionClient?: PoolClient | null): Promise<SHAPE[]>;
47
+ protected createAndHydrateModel(row: any): SHAPE;
48
+ findOne(item: Partial<SHAPE>, transactionClient?: PoolClient): 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
+ getCountRaw(whereClause?: string, values?: any[], tableRef?: string): Promise<number>;
54
+ getPageRaw(pageNumber: number, sortBy?: (keyof SHAPE)[], direction?: SortDirection, whereClause?: string, tableRef?: string, values?: any[], pageSize?: number | null, searchFields?: Partial<SHAPE> | null): Promise<PageResult<SHAPE>>;
55
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -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 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,24 @@
1
+ import { PoolClient } from 'pg';
2
+ import { Logger } from '@squiz/dx-logger-lib';
3
+ export type Migration = {
4
+ (db: PoolClient, logger: Logger): Promise<void>;
5
+ };
6
+ export declare class Migrator {
7
+ protected migrationDir: string;
8
+ protected migrationList: string[];
9
+ protected pool: PoolClient;
10
+ constructor(migrationDir: string, migrationList: string[], pool: PoolClient);
11
+ protected ensureMigrationTableExists(): Promise<import("pg").QueryResult<any>>;
12
+ protected getAppliedMigrations(): Promise<any[]>;
13
+ protected doSqlMigration(migration: string, sql: string): Promise<void>;
14
+ protected getPending(migrationsList: string[], appliedMigrations: string[]): Promise<string[]>;
15
+ protected getSql(migration: string): Promise<string>;
16
+ protected tryToObtainLock(): Promise<boolean>;
17
+ protected releaseLock(): Promise<void>;
18
+ migrate(): Promise<any>;
19
+ protected runMigrations(): Promise<void>;
20
+ protected doScriptMigration(migration: string): Promise<void>;
21
+ protected runMigration(migration: string): Promise<void>;
22
+ protected doMigrationWork(migration: string): Promise<void>;
23
+ protected dispose(): void;
24
+ }
@@ -0,0 +1,268 @@
1
+ export declare enum PostgresErrorCode {
2
+ SUCCESSFUL_COMPLETION = "00000",
3
+ WARNING = "01000",
4
+ WARNING_DYNAMIC_RESULT_SETS_RETURNED = "0100C",
5
+ WARNING_IMPLICIT_ZERO_BIT_PADDING = "01008",
6
+ WARNING_NULL_VALUE_ELIMINATED_IN_SET_FUNCTION = "01003",
7
+ WARNING_PRIVILEGE_NOT_GRANTED = "01007",
8
+ WARNING_PRIVILEGE_NOT_REVOKED = "01006",
9
+ WARNING_STRING_DATA_RIGHT_TRUNCATION = "01004",
10
+ WARNING_DEPRECATED_FEATURE = "01P01",
11
+ NO_DATA = "02000",
12
+ NO_ADDITIONAL_DYNAMIC_RESULT_SETS_RETURNED = "02001",
13
+ SQL_STATEMENT_NOT_YET_COMPLETE = "03000",
14
+ CONNECTION_EXCEPTION = "08000",
15
+ CONNECTION_DOES_NOT_EXIST = "08003",
16
+ CONNECTION_FAILURE = "08006",
17
+ SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION = "08001",
18
+ SQLSERVER_REJECTED_ESTABLISHMENT_OF_SQLCONNECTION = "08004",
19
+ TRANSACTION_RESOLUTION_UNKNOWN = "08007",
20
+ PROTOCOL_VIOLATION = "08P01",
21
+ TRIGGERED_ACTION_EXCEPTION = "09000",
22
+ FEATURE_NOT_SUPPORTED = "0A000",
23
+ INVALID_TRANSACTION_INITIATION = "0B000",
24
+ LOCATOR_EXCEPTION = "0F000",
25
+ L_E_INVALID_SPECIFICATION = "0F001",
26
+ INVALID_GRANTOR = "0L000",
27
+ INVALID_GRANT_OPERATION = "0LP01",
28
+ INVALID_ROLE_SPECIFICATION = "0P000",
29
+ DIAGNOSTICS_EXCEPTION = "0Z000",
30
+ STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER = "0Z002",
31
+ CASE_NOT_FOUND = "20000",
32
+ CARDINALITY_VIOLATION = "21000",
33
+ DATA_EXCEPTION = "22000",
34
+ ARRAY_ELEMENT_ERROR = "2202E",
35
+ ARRAY_SUBSCRIPT_ERROR = "2202E",
36
+ CHARACTER_NOT_IN_REPERTOIRE = "22021",
37
+ DATETIME_FIELD_OVERFLOW = "22008",
38
+ DATETIME_VALUE_OUT_OF_RANGE = "22008",
39
+ DIVISION_BY_ZERO = "22012",
40
+ ERROR_IN_ASSIGNMENT = "22005",
41
+ ESCAPE_CHARACTER_CONFLICT = "2200B",
42
+ INDICATOR_OVERFLOW = "22022",
43
+ INTERVAL_FIELD_OVERFLOW = "22015",
44
+ INVALID_ARGUMENT_FOR_LOG = "2201E",
45
+ INVALID_ARGUMENT_FOR_NTILE = "22014",
46
+ INVALID_ARGUMENT_FOR_NTH_VALUE = "22016",
47
+ INVALID_ARGUMENT_FOR_POWER_FUNCTION = "2201F",
48
+ INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION = "2201G",
49
+ INVALID_CHARACTER_VALUE_FOR_CAST = "22018",
50
+ INVALID_DATETIME_FORMAT = "22007",
51
+ INVALID_ESCAPE_CHARACTER = "22019",
52
+ INVALID_ESCAPE_OCTET = "2200D",
53
+ INVALID_ESCAPE_SEQUENCE = "22025",
54
+ NONSTANDARD_USE_OF_ESCAPE_CHARACTER = "22P06",
55
+ INVALID_INDICATOR_PARAMETER_VALUE = "22010",
56
+ INVALID_PARAMETER_VALUE = "22023",
57
+ INVALID_PRECEDING_OR_FOLLOWING_SIZE = "22013",
58
+ INVALID_REGULAR_EXPRESSION = "2201B",
59
+ INVALID_ROW_COUNT_IN_LIMIT_CLAUSE = "2201W",
60
+ INVALID_ROW_COUNT_IN_RESULT_OFFSET_CLAUSE = "2201X",
61
+ INVALID_TABLESAMPLE_ARGUMENT = "2202H",
62
+ INVALID_TABLESAMPLE_REPEAT = "2202G",
63
+ INVALID_TIME_ZONE_DISPLACEMENT_VALUE = "22009",
64
+ INVALID_USE_OF_ESCAPE_CHARACTER = "2200C",
65
+ MOST_SPECIFIC_TYPE_MISMATCH = "2200G",
66
+ NULL_VALUE_NOT_ALLOWED = "22004",
67
+ NULL_VALUE_NO_INDICATOR_PARAMETER = "22002",
68
+ NUMERIC_VALUE_OUT_OF_RANGE = "22003",
69
+ SEQUENCE_GENERATOR_LIMIT_EXCEEDED = "2200H",
70
+ STRING_DATA_LENGTH_MISMATCH = "22026",
71
+ STRING_DATA_RIGHT_TRUNCATION = "22001",
72
+ SUBSTRING_ERROR = "22011",
73
+ TRIM_ERROR = "22027",
74
+ UNTERMINATED_C_STRING = "22024",
75
+ ZERO_LENGTH_CHARACTER_STRING = "2200F",
76
+ FLOATING_POINT_EXCEPTION = "22P01",
77
+ INVALID_TEXT_REPRESENTATION = "22P02",
78
+ INVALID_BINARY_REPRESENTATION = "22P03",
79
+ BAD_COPY_FILE_FORMAT = "22P04",
80
+ UNTRANSLATABLE_CHARACTER = "22P05",
81
+ NOT_AN_XML_DOCUMENT = "2200L",
82
+ INVALID_XML_DOCUMENT = "2200M",
83
+ INVALID_XML_CONTENT = "2200N",
84
+ INVALID_XML_COMMENT = "2200S",
85
+ INVALID_XML_PROCESSING_INSTRUCTION = "2200T",
86
+ DUPLICATE_JSON_OBJECT_KEY_VALUE = "22030",
87
+ INVALID_ARGUMENT_FOR_SQL_JSON_DATETIME_FUNCTION = "22031",
88
+ INVALID_JSON_TEXT = "22032",
89
+ INVALID_SQL_JSON_SUBSCRIPT = "22033",
90
+ MORE_THAN_ONE_SQL_JSON_ITEM = "22034",
91
+ NO_SQL_JSON_ITEM = "22035",
92
+ NON_NUMERIC_SQL_JSON_ITEM = "22036",
93
+ NON_UNIQUE_KEYS_IN_A_JSON_OBJECT = "22037",
94
+ SINGLETON_SQL_JSON_ITEM_REQUIRED = "22038",
95
+ SQL_JSON_ARRAY_NOT_FOUND = "22039",
96
+ SQL_JSON_MEMBER_NOT_FOUND = "2203A",
97
+ SQL_JSON_NUMBER_NOT_FOUND = "2203B",
98
+ SQL_JSON_OBJECT_NOT_FOUND = "2203C",
99
+ TOO_MANY_JSON_ARRAY_ELEMENTS = "2203D",
100
+ TOO_MANY_JSON_OBJECT_MEMBERS = "2203E",
101
+ SQL_JSON_SCALAR_REQUIRED = "2203F",
102
+ SQL_JSON_ITEM_CANNOT_BE_CAST_TO_TARGET_TYPE = "2203G",
103
+ INTEGRITY_CONSTRAINT_VIOLATION = "23000",
104
+ RESTRICT_VIOLATION = "23001",
105
+ NOT_NULL_VIOLATION = "23502",
106
+ FOREIGN_KEY_VIOLATION = "23503",
107
+ UNIQUE_VIOLATION = "23505",
108
+ CHECK_VIOLATION = "23514",
109
+ EXCLUSION_VIOLATION = "23P01",
110
+ INVALID_CURSOR_STATE = "24000",
111
+ INVALID_TRANSACTION_STATE = "25000",
112
+ ACTIVE_SQL_TRANSACTION = "25001",
113
+ BRANCH_TRANSACTION_ALREADY_ACTIVE = "25002",
114
+ HELD_CURSOR_REQUIRES_SAME_ISOLATION_LEVEL = "25008",
115
+ INAPPROPRIATE_ACCESS_MODE_FOR_BRANCH_TRANSACTION = "25003",
116
+ INAPPROPRIATE_ISOLATION_LEVEL_FOR_BRANCH_TRANSACTION = "25004",
117
+ NO_ACTIVE_SQL_TRANSACTION_FOR_BRANCH_TRANSACTION = "25005",
118
+ READ_ONLY_SQL_TRANSACTION = "25006",
119
+ SCHEMA_AND_DATA_STATEMENT_MIXING_NOT_SUPPORTED = "25007",
120
+ NO_ACTIVE_SQL_TRANSACTION = "25P01",
121
+ IN_FAILED_SQL_TRANSACTION = "25P02",
122
+ IDLE_IN_TRANSACTION_SESSION_TIMEOUT = "25P03",
123
+ INVALID_SQL_STATEMENT_NAME = "26000",
124
+ TRIGGERED_DATA_CHANGE_VIOLATION = "27000",
125
+ INVALID_AUTHORIZATION_SPECIFICATION = "28000",
126
+ INVALID_PASSWORD = "28P01",
127
+ DEPENDENT_PRIVILEGE_DESCRIPTORS_STILL_EXIST = "2B000",
128
+ DEPENDENT_OBJECTS_STILL_EXIST = "2BP01",
129
+ INVALID_TRANSACTION_TERMINATION = "2D000",
130
+ SQL_ROUTINE_EXCEPTION = "2F000",
131
+ S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT = "2F005",
132
+ S_R_E_MODIFYING_SQL_DATA_NOT_PERMITTED = "2F002",
133
+ S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED = "2F003",
134
+ S_R_E_READING_SQL_DATA_NOT_PERMITTED = "2F004",
135
+ INVALID_CURSOR_NAME = "34000",
136
+ EXTERNAL_ROUTINE_EXCEPTION = "38000",
137
+ E_R_E_CONTAINING_SQL_NOT_PERMITTED = "38001",
138
+ E_R_E_MODIFYING_SQL_DATA_NOT_PERMITTED = "38002",
139
+ E_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED = "38003",
140
+ E_R_E_READING_SQL_DATA_NOT_PERMITTED = "38004",
141
+ EXTERNAL_ROUTINE_INVOCATION_EXCEPTION = "39000",
142
+ E_R_I_E_INVALID_SQLSTATE_RETURNED = "39001",
143
+ E_R_I_E_NULL_VALUE_NOT_ALLOWED = "39004",
144
+ E_R_I_E_TRIGGER_PROTOCOL_VIOLATED = "39P01",
145
+ E_R_I_E_SRF_PROTOCOL_VIOLATED = "39P02",
146
+ E_R_I_E_EVENT_TRIGGER_PROTOCOL_VIOLATED = "39P03",
147
+ SAVEPOINT_EXCEPTION = "3B000",
148
+ S_E_INVALID_SPECIFICATION = "3B001",
149
+ INVALID_CATALOG_NAME = "3D000",
150
+ INVALID_SCHEMA_NAME = "3F000",
151
+ TRANSACTION_ROLLBACK = "40000",
152
+ T_R_INTEGRITY_CONSTRAINT_VIOLATION = "40002",
153
+ T_R_SERIALIZATION_FAILURE = "40001",
154
+ T_R_STATEMENT_COMPLETION_UNKNOWN = "40003",
155
+ T_R_DEADLOCK_DETECTED = "40P01",
156
+ SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION = "42000",
157
+ SYNTAX_ERROR = "42601",
158
+ INSUFFICIENT_PRIVILEGE = "42501",
159
+ CANNOT_COERCE = "42846",
160
+ GROUPING_ERROR = "42803",
161
+ WINDOWING_ERROR = "42P20",
162
+ INVALID_RECURSION = "42P19",
163
+ INVALID_FOREIGN_KEY = "42830",
164
+ INVALID_NAME = "42602",
165
+ NAME_TOO_LONG = "42622",
166
+ RESERVED_NAME = "42939",
167
+ DATATYPE_MISMATCH = "42804",
168
+ INDETERMINATE_DATATYPE = "42P18",
169
+ COLLATION_MISMATCH = "42P21",
170
+ INDETERMINATE_COLLATION = "42P22",
171
+ WRONG_OBJECT_TYPE = "42809",
172
+ GENERATED_ALWAYS = "428C9",
173
+ UNDEFINED_COLUMN = "42703",
174
+ UNDEFINED_CURSOR = "34000",
175
+ UNDEFINED_DATABASE = "3D000",
176
+ UNDEFINED_FUNCTION = "42883",
177
+ UNDEFINED_PSTATEMENT = "26000",
178
+ UNDEFINED_SCHEMA = "3F000",
179
+ UNDEFINED_TABLE = "42P01",
180
+ UNDEFINED_PARAMETER = "42P02",
181
+ UNDEFINED_OBJECT = "42704",
182
+ DUPLICATE_COLUMN = "42701",
183
+ DUPLICATE_CURSOR = "42P03",
184
+ DUPLICATE_DATABASE = "42P04",
185
+ DUPLICATE_FUNCTION = "42723",
186
+ DUPLICATE_PSTATEMENT = "42P05",
187
+ DUPLICATE_SCHEMA = "42P06",
188
+ DUPLICATE_TABLE = "42P07",
189
+ DUPLICATE_ALIAS = "42712",
190
+ DUPLICATE_OBJECT = "42710",
191
+ AMBIGUOUS_COLUMN = "42702",
192
+ AMBIGUOUS_FUNCTION = "42725",
193
+ AMBIGUOUS_PARAMETER = "42P08",
194
+ AMBIGUOUS_ALIAS = "42P09",
195
+ INVALID_COLUMN_REFERENCE = "42P10",
196
+ INVALID_COLUMN_DEFINITION = "42611",
197
+ INVALID_CURSOR_DEFINITION = "42P11",
198
+ INVALID_DATABASE_DEFINITION = "42P12",
199
+ INVALID_FUNCTION_DEFINITION = "42P13",
200
+ INVALID_PSTATEMENT_DEFINITION = "42P14",
201
+ INVALID_SCHEMA_DEFINITION = "42P15",
202
+ INVALID_TABLE_DEFINITION = "42P16",
203
+ INVALID_OBJECT_DEFINITION = "42P17",
204
+ WITH_CHECK_OPTION_VIOLATION = "44000",
205
+ INSUFFICIENT_RESOURCES = "53000",
206
+ DISK_FULL = "53100",
207
+ OUT_OF_MEMORY = "53200",
208
+ TOO_MANY_CONNECTIONS = "53300",
209
+ CONFIGURATION_LIMIT_EXCEEDED = "53400",
210
+ PROGRAM_LIMIT_EXCEEDED = "54000",
211
+ STATEMENT_TOO_COMPLEX = "54001",
212
+ TOO_MANY_COLUMNS = "54011",
213
+ TOO_MANY_ARGUMENTS = "54023",
214
+ OBJECT_NOT_IN_PREREQUISITE_STATE = "55000",
215
+ OBJECT_IN_USE = "55006",
216
+ CANT_CHANGE_RUNTIME_PARAM = "55P02",
217
+ LOCK_NOT_AVAILABLE = "55P03",
218
+ UNSAFE_NEW_ENUM_VALUE_USAGE = "55P04",
219
+ OPERATOR_INTERVENTION = "57000",
220
+ QUERY_CANCELED = "57014",
221
+ ADMIN_SHUTDOWN = "57P01",
222
+ CRASH_SHUTDOWN = "57P02",
223
+ CANNOT_CONNECT_NOW = "57P03",
224
+ DATABASE_DROPPED = "57P04",
225
+ IDLE_SESSION_TIMEOUT = "57P05",
226
+ SYSTEM_ERROR = "58000",
227
+ IO_ERROR = "58030",
228
+ UNDEFINED_FILE = "58P01",
229
+ DUPLICATE_FILE = "58P02",
230
+ SNAPSHOT_TOO_OLD = "72000",
231
+ CONFIG_FILE_ERROR = "F0000",
232
+ LOCK_FILE_EXISTS = "F0001",
233
+ FDW_ERROR = "HV000",
234
+ FDW_COLUMN_NAME_NOT_FOUND = "HV005",
235
+ FDW_DYNAMIC_PARAMETER_VALUE_NEEDED = "HV002",
236
+ FDW_FUNCTION_SEQUENCE_ERROR = "HV010",
237
+ FDW_INCONSISTENT_DESCRIPTOR_INFORMATION = "HV021",
238
+ FDW_INVALID_ATTRIBUTE_VALUE = "HV024",
239
+ FDW_INVALID_COLUMN_NAME = "HV007",
240
+ FDW_INVALID_COLUMN_NUMBER = "HV008",
241
+ FDW_INVALID_DATA_TYPE = "HV004",
242
+ FDW_INVALID_DATA_TYPE_DESCRIPTORS = "HV006",
243
+ FDW_INVALID_DESCRIPTOR_FIELD_IDENTIFIER = "HV091",
244
+ FDW_INVALID_HANDLE = "HV00B",
245
+ FDW_INVALID_OPTION_INDEX = "HV00C",
246
+ FDW_INVALID_OPTION_NAME = "HV00D",
247
+ FDW_INVALID_STRING_LENGTH_OR_BUFFER_LENGTH = "HV090",
248
+ FDW_INVALID_STRING_FORMAT = "HV00A",
249
+ FDW_INVALID_USE_OF_NULL_POINTER = "HV009",
250
+ FDW_TOO_MANY_HANDLES = "HV014",
251
+ FDW_OUT_OF_MEMORY = "HV001",
252
+ FDW_NO_SCHEMAS = "HV00P",
253
+ FDW_OPTION_NAME_NOT_FOUND = "HV00J",
254
+ FDW_REPLY_HANDLE = "HV00K",
255
+ FDW_SCHEMA_NOT_FOUND = "HV00Q",
256
+ FDW_TABLE_NOT_FOUND = "HV00R",
257
+ FDW_UNABLE_TO_CREATE_EXECUTION = "HV00L",
258
+ FDW_UNABLE_TO_CREATE_REPLY = "HV00M",
259
+ FDW_UNABLE_TO_ESTABLISH_CONNECTION = "HV00N",
260
+ PLPGSQL_ERROR = "P0000",
261
+ RAISE_EXCEPTION = "P0001",
262
+ NO_DATA_FOUND = "P0002",
263
+ TOO_MANY_ROWS = "P0003",
264
+ ASSERT_FAILURE = "P0004",
265
+ INTERNAL_ERROR = "XX000",
266
+ DATA_CORRUPTED = "XX001",
267
+ INDEX_CORRUPTED = "XX002"
268
+ }
@@ -0,0 +1,2 @@
1
+ import { Repository } from './AbstractRepository';
2
+ export type Repositories = Record<string, Repository<any>>;
@@ -0,0 +1,161 @@
1
+ import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
2
+ import { Transaction, DynamoDbManager } from '..';
3
+ interface Reader<T> {
4
+ queryItems(partialItem: Partial<T>, useSortKey?: boolean, index?: keyof TableIndexes): Promise<T[]>;
5
+ getItem(id: string | Partial<T>): Promise<T | undefined>;
6
+ }
7
+ interface Writer<T> {
8
+ createItem(item: Partial<T>): Promise<T>;
9
+ updateItem(partialItem: Partial<T>, newValue: Partial<T>): Promise<T | undefined>;
10
+ deleteItem(partialItem: Partial<T>): Promise<number>;
11
+ }
12
+ type Repository<T> = Reader<T> & Writer<T>;
13
+ type Repositories = Record<string, Repository<any>>;
14
+ export type TableKeys = {
15
+ pk: {
16
+ attributeName: string;
17
+ format: string;
18
+ };
19
+ sk: {
20
+ attributeName: string;
21
+ format: string;
22
+ };
23
+ };
24
+ export type TableIndexes = Record<string, TableKeys>;
25
+ export type KeysFormat = Record<keyof TableKeys | keyof TableIndexes, string>;
26
+ export type EntityDefinition = {
27
+ keys: TableKeys;
28
+ indexes: TableIndexes;
29
+ fieldsAsJsonString: string[];
30
+ };
31
+ export declare abstract class AbstractDynamoDbRepository<SHAPE extends object, DATA_CLASS extends SHAPE> implements Reader<SHAPE>, Writer<SHAPE> {
32
+ protected tableName: string;
33
+ protected dbManager: DynamoDbManager<Repositories>;
34
+ protected entityName: string;
35
+ protected entityDefinition: EntityDefinition;
36
+ protected classRef: {
37
+ new (data?: Record<string, unknown>): DATA_CLASS;
38
+ };
39
+ protected client: DynamoDBDocument;
40
+ protected keys: TableKeys;
41
+ protected indexes: TableIndexes;
42
+ protected keysFormat: KeysFormat;
43
+ protected fieldsAsJsonString: string[];
44
+ constructor(tableName: string, dbManager: DynamoDbManager<Repositories>, entityName: string, entityDefinition: EntityDefinition, classRef: {
45
+ new (data?: Record<string, unknown>): DATA_CLASS;
46
+ });
47
+ /**
48
+ * Get the single item matching the key fields value in the given
49
+ * partial item. Will throw MissingKeyValuesError if key field values
50
+ * are missing
51
+ *
52
+ * @param item
53
+ *
54
+ * @throws MissingKeyValuesError
55
+ */
56
+ getItem(item: Partial<SHAPE>): Promise<DATA_CLASS | undefined>;
57
+ /**
58
+ * Finds all the items matching the partition key or
59
+ * the gsi key (when gsi index name is specified)
60
+ *
61
+ * @param item
62
+ * @param useSortKey
63
+ * @param index
64
+ * @throws MissingKeyValuesError
65
+ */
66
+ queryItems(item: Partial<SHAPE>, useSortKey?: boolean, index?: keyof TableIndexes): Promise<DATA_CLASS[]>;
67
+ /**
68
+ * Update the existing item matching the key fields value
69
+ * in the passed in partialItem
70
+ * @param partialItem
71
+ * @param newValue
72
+ * @param transaction
73
+ *
74
+ * @returns Promise<SHAPE | undefined>
75
+ * @throws MissingKeyValuesError
76
+ */
77
+ updateItem(partialItem: Partial<SHAPE>, newValue: Exclude<Partial<SHAPE>, Record<string, never>>, transaction?: Transaction): Promise<DATA_CLASS | undefined>;
78
+ /**
79
+ * Adds new item to the table
80
+ *
81
+ * @param value
82
+ * @param transaction
83
+ *
84
+ * @returns Promise<SHAPE>
85
+ * @throws DuplicateItemError
86
+ * @throws MissingKeyValuesError
87
+ */
88
+ createItem(value: DATA_CLASS, transaction?: Transaction): Promise<DATA_CLASS>;
89
+ /**
90
+ * Deletes an item from the table
91
+ *
92
+ * @param partialItem
93
+ * @param transaction
94
+ * @returns number
95
+ * @throw MissingKeyValuesError
96
+ */
97
+ deleteItem(partialItem: Partial<SHAPE>, transaction?: Transaction): Promise<number>;
98
+ /**
99
+ * Return repo model object from the db value
100
+ * @param item
101
+ * @returns
102
+ */
103
+ protected hydrateItem(item: Record<string, unknown>): DATA_CLASS;
104
+ protected convertSelectedValuesToJsonString(item: Record<string, unknown>): void;
105
+ /**
106
+ * Evaluate the partition key value from the partial item
107
+ * @param item
108
+ * @returns string
109
+ * @throw MissingKeyValuesError
110
+ */
111
+ protected getPk(item: Partial<SHAPE>): string;
112
+ /**
113
+ * Evaluate the sort key value from the partial item
114
+ * @param item
115
+ * @returns string
116
+ *
117
+ * @throw MissingKeyValuesError
118
+ */
119
+ protected getSk(item: Partial<SHAPE>): string;
120
+ /**
121
+ * Evaluate the key value from the
122
+ *
123
+ * Example 1:
124
+ * Input:
125
+ * - item: {id: foo, name: 'some-name' }
126
+ * - attributeName: pk
127
+ * - this.keysFormat = { pk: 'item#{id}', 'sk': '#meta', ... }
128
+ * Output:
129
+ * - 'item#foo'
130
+ *
131
+ * Example 2:
132
+ * Input:
133
+ * - item: {id: foo, name: 'some-name', itemType: 'A' }
134
+ * - attributeName: sk
135
+ * - this.keysFormat = { pk: 'item#{id}', 'sk': 'type#{itemType}', ... }
136
+ * Output:
137
+ * - 'type#A'
138
+ *
139
+ * Example 3:
140
+ * Input:
141
+ * - item: {id: foo, name: 'some-name' }
142
+ * - attributeName: sk
143
+ * - this.keysFormat = { pk: 'item#{id}', 'sk': 'name-type#{itemType}{name}', ... }
144
+ * Output:
145
+ * - Error: "Key field "itemType" must be specified in the input item"
146
+ *
147
+ * @param item
148
+ * @param attributeName
149
+ *
150
+ * @returns string
151
+ * @throw MissingKeyValuesError
152
+ */
153
+ protected getKey(item: Partial<SHAPE>, attributeName: keyof KeysFormat): string;
154
+ /**
155
+ * Validate the data matches with "DATA_MODEL"
156
+ * @param value
157
+ * @return void
158
+ */
159
+ private assertValueMatchesModel;
160
+ }
161
+ export {};
@@ -0,0 +1,26 @@
1
+ import { AbstractDynamoDbRepository } from './AbstractDynamoDbRepository';
2
+ import { DynamoDbManager } from './DynamoDbManager';
3
+ import 'aws-sdk-client-mock-jest';
4
+ interface ITestItem {
5
+ name: string;
6
+ age: number;
7
+ country: string;
8
+ data?: object;
9
+ data2?: object;
10
+ }
11
+ declare class TestItem implements ITestItem {
12
+ name: string;
13
+ age: number;
14
+ country: string;
15
+ data: object;
16
+ data2?: object;
17
+ constructor(data?: Partial<ITestItem>);
18
+ }
19
+ export type TestRepositories = {
20
+ testItem: TestItemRepository;
21
+ };
22
+ export type TestDbManager = DynamoDbManager<TestRepositories>;
23
+ declare class TestItemRepository extends AbstractDynamoDbRepository<ITestItem, TestItem> {
24
+ constructor(tableName: string, dbManager: TestDbManager);
25
+ }
26
+ export {};
@@ -0,0 +1,18 @@
1
+ import { DynamoDBDocument, TransactWriteCommandInput } from '@aws-sdk/lib-dynamodb';
2
+ export type Transaction = {
3
+ id?: string;
4
+ };
5
+ type TransactionItems = TransactWriteCommandInput['TransactItems'];
6
+ export type TransactionItem = NonNullable<TransactionItems>[number];
7
+ export declare class DynamoDbManager<TRepositories> {
8
+ client: DynamoDBDocument;
9
+ private transactionItems;
10
+ repositories: TRepositories;
11
+ constructor(client: DynamoDBDocument, repositoryCreator: (dbManager: DynamoDbManager<TRepositories>) => TRepositories);
12
+ executeInTransaction<T>(func: (transaction: Transaction) => Promise<T>): Promise<T>;
13
+ addWriteTransactionItem(transactionId: string, item: TransactionItem): void;
14
+ private executeTransaction;
15
+ private startTransaction;
16
+ private closeTransaction;
17
+ }
18
+ export {};
@@ -0,0 +1,12 @@
1
+ export declare const getDynamoDbOptions: (awsRegion: string, nodeEnv: 'production' | 'development') => {
2
+ region: string;
3
+ credentials?: undefined;
4
+ endpoint?: undefined;
5
+ } | {
6
+ credentials: {
7
+ accessKeyId: string;
8
+ secretAccessKey: string;
9
+ };
10
+ endpoint: string;
11
+ region?: undefined;
12
+ };
@@ -0,0 +1,5 @@
1
+ import { InternalServerError } from '@squiz/dx-common-lib';
2
+ export declare class DuplicateItemError extends InternalServerError {
3
+ name: string;
4
+ constructor(message: string);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { InternalServerError } from '@squiz/dx-common-lib';
2
+ export declare class InvalidDataFormatError extends InternalServerError {
3
+ name: string;
4
+ constructor(message: string);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { BadRequestError } from '@squiz/dx-common-lib';
2
+ export declare class InvalidDbSchemaError extends BadRequestError {
3
+ name: string;
4
+ constructor(message: string);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { InternalServerError } from '@squiz/dx-common-lib';
2
+ export declare class MissingKeyValuesError extends InternalServerError {
3
+ name: string;
4
+ constructor(message: string);
5
+ }
@@ -0,0 +1,5 @@
1
+ import { InternalServerError } from '@squiz/dx-common-lib';
2
+ export declare class TransactionError extends InternalServerError {
3
+ name: string;
4
+ constructor(message: string);
5
+ }
@@ -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,14 @@
1
+ export * from './AbstractRepository';
2
+ export * from './ConnectionManager';
3
+ export * from './dynamodb/DynamoDbManager';
4
+ export * from './dynamodb/AbstractDynamoDbRepository';
5
+ export * from './dynamodb/getDynamoDbOptions';
6
+ export * from './Migrator';
7
+ export * from './Repositories';
8
+ export * from './getConnectionInfo';
9
+ export * from './error/DuplicateItemError';
10
+ export * from './error/TransactionError';
11
+ export * from './error/MissingKeyValuesError';
12
+ export * from './error/InvalidDbSchemaError';
13
+ export * from './PostgresErrorCodes';
14
+ export { Pool, PoolClient } from 'pg';