core-services-sdk 1.3.52 → 1.3.54
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.
- package/package.json +1 -1
- package/src/postgresql/validate-schema.js +26 -7
- package/tests/postgresql/validate-schema.integration.test.js +11 -3
- package/types/ids/generators.d.ts +20 -11
- package/types/ids/prefixes.d.ts +33 -47
- package/types/index.d.ts +1 -0
- package/types/postgresql/connect-to-pg.d.ts +1 -0
- package/types/postgresql/index.d.ts +3 -0
- package/types/postgresql/start-stop-postgres-docker.d.ts +116 -0
- package/types/postgresql/validate-schema.d.ts +42 -0
package/package.json
CHANGED
|
@@ -6,19 +6,35 @@ import { connectToPg } from './connect-to-pg.js'
|
|
|
6
6
|
* Checks all provided table names and collects any missing tables.
|
|
7
7
|
* Throws a single error listing all missing tables after validation completes.
|
|
8
8
|
*
|
|
9
|
-
* @param {
|
|
9
|
+
* @param {Object} params
|
|
10
|
+
* Parameters object.
|
|
11
|
+
*
|
|
12
|
+
* @param {string|Object} params.connection
|
|
10
13
|
* Database connection configuration.
|
|
11
14
|
* Can be a database connection URI or a Knex connection object.
|
|
12
15
|
*
|
|
13
|
-
* @param {string[]} tables
|
|
16
|
+
* @param {string[]} params.tables
|
|
14
17
|
* List of required table names to validate.
|
|
15
18
|
*
|
|
19
|
+
* @param {Object} [params.log]
|
|
20
|
+
* Optional logger object.
|
|
21
|
+
*
|
|
22
|
+
* @param {Function} [params.log.info]
|
|
23
|
+
* Logger function for informational messages.
|
|
24
|
+
*
|
|
25
|
+
* @param {Function} [params.log.error]
|
|
26
|
+
* Logger function for error messages.
|
|
27
|
+
*
|
|
16
28
|
* @returns {Promise<void>}
|
|
17
29
|
*
|
|
18
30
|
* @throws {Error}
|
|
19
31
|
* Throws an error if one or more required tables are missing.
|
|
20
32
|
*/
|
|
21
|
-
export async function validateSchema(
|
|
33
|
+
export async function validateSchema({
|
|
34
|
+
tables,
|
|
35
|
+
connection,
|
|
36
|
+
log = { error: console.error, info: console.info },
|
|
37
|
+
}) {
|
|
22
38
|
const db = connectToPg(connection)
|
|
23
39
|
|
|
24
40
|
const missingTables = []
|
|
@@ -31,9 +47,12 @@ export async function validateSchema(connection, tables) {
|
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
if (missingTables.length > 0) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
50
|
+
const errorMessage = `Missing the following tables: ${missingTables.join(', ')}. Did you run migrations?`
|
|
51
|
+
log.error(errorMessage)
|
|
52
|
+
throw new Error(errorMessage)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (tables.length) {
|
|
56
|
+
log.info(`All required tables are exists: ${tables.join(', ')}`)
|
|
38
57
|
}
|
|
39
58
|
}
|
|
@@ -37,18 +37,26 @@ describe('validateSchema', () => {
|
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
it('does not throw when all required tables exist', async () => {
|
|
40
|
-
await expect(
|
|
40
|
+
await expect(
|
|
41
|
+
validateSchema({ connection: DATABASE_URI, tables: ['files'] }),
|
|
42
|
+
).resolves.not.toThrow()
|
|
41
43
|
})
|
|
42
44
|
|
|
43
45
|
it('throws a single error listing missing tables', async () => {
|
|
44
46
|
await expect(
|
|
45
|
-
validateSchema(
|
|
47
|
+
validateSchema({
|
|
48
|
+
connection: DATABASE_URI,
|
|
49
|
+
tables: ['files', 'documents', 'attachments'],
|
|
50
|
+
}),
|
|
46
51
|
).rejects.toThrow('Missing the following tables: documents, attachments')
|
|
47
52
|
})
|
|
48
53
|
|
|
49
54
|
it('throws when all tables are missing', async () => {
|
|
50
55
|
await expect(
|
|
51
|
-
validateSchema(
|
|
56
|
+
validateSchema({
|
|
57
|
+
connection: DATABASE_URI,
|
|
58
|
+
tables: ['missing_a', 'missing_b'],
|
|
59
|
+
}),
|
|
52
60
|
).rejects.toThrow('Missing the following tables: missing_a, missing_b')
|
|
53
61
|
})
|
|
54
62
|
})
|
|
@@ -2,30 +2,39 @@ export function generateId(): string
|
|
|
2
2
|
export function generatePrefixedId(prefix: string): string
|
|
3
3
|
export function generateUserId(): string
|
|
4
4
|
export function generateTenantId(): string
|
|
5
|
+
export function generateSessionId(): string
|
|
6
|
+
export function generateOnboardingId(): string
|
|
5
7
|
export function generatePermissionId(): string
|
|
6
|
-
export function
|
|
7
|
-
export function generateVerificationId(): string
|
|
8
|
+
export function generateRoleId(): string
|
|
8
9
|
export function generateRolePermissionsId(): string
|
|
9
|
-
export function
|
|
10
|
-
export function
|
|
10
|
+
export function generateVerificationId(): string
|
|
11
|
+
export function generateAssetId(): string
|
|
12
|
+
export function generateAssetUploadId(): string
|
|
11
13
|
export function generateFileId(): string
|
|
14
|
+
export function generateSupplierId(): string
|
|
15
|
+
export function generateInvoiceId(): string
|
|
16
|
+
export function generateInvoiceItemId(): string
|
|
17
|
+
export function generateInvoiceCorrectionId(): string
|
|
18
|
+
export function generateDocumentDataId(): string
|
|
19
|
+
export function generateBotFlowId(): string
|
|
20
|
+
export function generateCorrelationId(): string
|
|
12
21
|
export function generateEventId(): string
|
|
13
22
|
export function generateJobId(): string
|
|
14
23
|
export function generateTaskId(): string
|
|
15
24
|
export function generateQueueId(): string
|
|
16
25
|
export function generateMessageId(): string
|
|
26
|
+
export function generateEmailId(): string
|
|
27
|
+
export function generateIncomingEmailId(): string
|
|
28
|
+
export function generateImId(): string
|
|
17
29
|
export function generateNotificationId(): string
|
|
18
|
-
export function generateLogId(): string
|
|
19
30
|
export function generateAuditId(): string
|
|
20
|
-
export function
|
|
21
|
-
export function generateKeyId(): string
|
|
31
|
+
export function generateLogId(): string
|
|
22
32
|
export function generateMetricId(): string
|
|
23
|
-
export function
|
|
33
|
+
export function generateKeyId(): string
|
|
24
34
|
export function generatePolicyId(): string
|
|
25
35
|
export function generateProfileId(): string
|
|
26
36
|
export function generateDeviceId(): string
|
|
27
37
|
export function generateAlertId(): string
|
|
28
38
|
export function generateResourceId(): string
|
|
29
|
-
export function
|
|
30
|
-
export function
|
|
31
|
-
export function generateImId(): string
|
|
39
|
+
export function generateTagId(): string
|
|
40
|
+
export function generateConfigId(): string
|
package/types/ids/prefixes.d.ts
CHANGED
|
@@ -1,76 +1,62 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Mapping of entity types to their unique ID prefixes.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Prefixes are prepended to ULIDs to create readable,
|
|
5
|
+
* sortable, and easily identifiable IDs across the system.
|
|
6
|
+
*
|
|
7
|
+
* Example:
|
|
8
|
+
* ast_01HZY3M7K4FJ9A8Q4Y1ZB5NX3T
|
|
6
9
|
*/
|
|
7
10
|
export type ID_PREFIXES = string
|
|
8
11
|
/**
|
|
9
12
|
* Mapping of entity types to their unique ID prefixes.
|
|
10
13
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
14
|
+
* Prefixes are prepended to ULIDs to create readable,
|
|
15
|
+
* sortable, and easily identifiable IDs across the system.
|
|
16
|
+
*
|
|
17
|
+
* Example:
|
|
18
|
+
* ast_01HZY3M7K4FJ9A8Q4Y1ZB5NX3T
|
|
13
19
|
*
|
|
14
20
|
* @readonly
|
|
15
21
|
* @enum {string}
|
|
16
22
|
*/
|
|
17
23
|
export const ID_PREFIXES: Readonly<{
|
|
18
|
-
/** User entity ID prefix */
|
|
19
24
|
USER: 'usr'
|
|
20
|
-
/** Tenant entity ID prefix */
|
|
21
25
|
TENANT: 'tnt'
|
|
22
|
-
|
|
26
|
+
SESSION: 'sess'
|
|
27
|
+
ONBOARDING: 'onb'
|
|
28
|
+
ROLE: 'role'
|
|
23
29
|
PERMISSION: 'prm'
|
|
24
|
-
|
|
25
|
-
CORRELATION: 'crln'
|
|
26
|
-
/** Verification entity ID prefix (e.g., email/phone code) */
|
|
30
|
+
ROLE_PERMISSION: 'rprm'
|
|
27
31
|
VERIFICATION: 'vrf'
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
SESSION: 'sess'
|
|
34
|
-
/** File mapping ID prefix */
|
|
32
|
+
POLICY: 'plc'
|
|
33
|
+
PROFILE: 'prf'
|
|
34
|
+
DEVICE: 'dev'
|
|
35
|
+
ASSET: 'ast'
|
|
36
|
+
ASSET_UPLOAD: 'aupl'
|
|
35
37
|
FILE: 'fil'
|
|
36
|
-
|
|
38
|
+
SUPPLIER: 'sup'
|
|
39
|
+
INVOICE: 'inv'
|
|
40
|
+
INVOICE_ITEM: 'invi'
|
|
41
|
+
INVOICE_CORRECTION: 'invc'
|
|
42
|
+
DOCUMENT_DATA: 'docd'
|
|
43
|
+
CORRELATION: 'crln'
|
|
37
44
|
EVENT: 'evt'
|
|
38
|
-
/** Job entity ID prefix */
|
|
39
45
|
JOB: 'job'
|
|
40
|
-
/** Task entity ID prefix */
|
|
41
46
|
TASK: 'task'
|
|
42
|
-
/** Queue entity ID prefix */
|
|
43
47
|
QUEUE: 'que'
|
|
44
|
-
/** Message entity ID prefix */
|
|
45
48
|
MESSAGE: 'msg'
|
|
46
|
-
/** Notification entity ID prefix */
|
|
47
49
|
NOTIFICATION: 'ntf'
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
EMAIL: 'eml'
|
|
51
|
+
INCOMING_EMAIL: 'ieml'
|
|
52
|
+
IM: 'im'
|
|
51
53
|
AUDIT: 'adt'
|
|
52
|
-
|
|
53
|
-
CONFIG: 'cfg'
|
|
54
|
-
/** Key entity ID prefix */
|
|
55
|
-
KEY: 'key'
|
|
56
|
-
/** Metric entity ID prefix */
|
|
54
|
+
LOG: 'log'
|
|
57
55
|
METRIC: 'met'
|
|
58
|
-
/** Tag entity ID prefix */
|
|
59
56
|
TAG: 'tag'
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
/** Profile entity ID prefix */
|
|
63
|
-
PROFILE: 'prf'
|
|
64
|
-
/** Device entity ID prefix */
|
|
65
|
-
DEVICE: 'dev'
|
|
66
|
-
/** Alert entity ID prefix */
|
|
67
|
-
ALERT: 'alr'
|
|
68
|
-
/** Resource entity ID prefix */
|
|
57
|
+
CONFIG: 'cfg'
|
|
58
|
+
KEY: 'key'
|
|
69
59
|
RESOURCE: 'res'
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
/** Email ID prefix */
|
|
73
|
-
EMAIL: 'eml'
|
|
74
|
-
/** Instant Message ID prefix */
|
|
75
|
-
IM: 'im'
|
|
60
|
+
ALERT: 'alr'
|
|
61
|
+
BOT_FLOW: 'botf'
|
|
76
62
|
}>
|
package/types/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function connectToPg(connection: object | string): import('knex').Knex
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts a PostgreSQL Docker container for testing purposes.
|
|
3
|
+
*
|
|
4
|
+
* If a container with the same name already exists, it will be stopped and removed first.
|
|
5
|
+
* The function blocks until PostgreSQL is ready to accept connections.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} options
|
|
8
|
+
* @param {number} [options.port=5433]
|
|
9
|
+
* Host port to bind PostgreSQL to.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} [options.containerName='postgres-test']
|
|
12
|
+
* Docker container name.
|
|
13
|
+
*
|
|
14
|
+
* @param {string} [options.user='testuser']
|
|
15
|
+
* Database user name.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} [options.pass='testpass']
|
|
18
|
+
* Database user password.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} [options.db='testdb']
|
|
21
|
+
* Database name.
|
|
22
|
+
*
|
|
23
|
+
* @returns {void}
|
|
24
|
+
*
|
|
25
|
+
* @throws {Error}
|
|
26
|
+
* Throws if PostgreSQL does not become ready within the expected time.
|
|
27
|
+
*/
|
|
28
|
+
export function startPostgres({
|
|
29
|
+
port,
|
|
30
|
+
containerName,
|
|
31
|
+
user,
|
|
32
|
+
pass,
|
|
33
|
+
db,
|
|
34
|
+
}: {
|
|
35
|
+
port?: number
|
|
36
|
+
containerName?: string
|
|
37
|
+
user?: string
|
|
38
|
+
pass?: string
|
|
39
|
+
db?: string
|
|
40
|
+
}): void
|
|
41
|
+
/**
|
|
42
|
+
* Stops and removes a PostgreSQL Docker container used for testing.
|
|
43
|
+
*
|
|
44
|
+
* If the container does not exist, the function exits silently.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} [containerName='postgres-test']
|
|
47
|
+
* Docker container name.
|
|
48
|
+
*
|
|
49
|
+
* @returns {void}
|
|
50
|
+
*/
|
|
51
|
+
export function stopPostgres(containerName?: string): void
|
|
52
|
+
/**
|
|
53
|
+
* Checks whether PostgreSQL inside the Docker container
|
|
54
|
+
* is ready to accept connections.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} containerName
|
|
57
|
+
* Docker container name.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} user
|
|
60
|
+
* Database user name.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} db
|
|
63
|
+
* Database name.
|
|
64
|
+
*
|
|
65
|
+
* @returns {boolean}
|
|
66
|
+
* Returns true if PostgreSQL is ready, otherwise false.
|
|
67
|
+
*/
|
|
68
|
+
export function isPostgresReady(
|
|
69
|
+
containerName: string,
|
|
70
|
+
user: string,
|
|
71
|
+
db: string,
|
|
72
|
+
): boolean
|
|
73
|
+
/**
|
|
74
|
+
* Waits until PostgreSQL inside the Docker container
|
|
75
|
+
* is ready to accept connections.
|
|
76
|
+
*
|
|
77
|
+
* Retries for a fixed number of attempts before failing.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} containerName
|
|
80
|
+
* Docker container name.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} user
|
|
83
|
+
* Database user name.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} db
|
|
86
|
+
* Database name.
|
|
87
|
+
*
|
|
88
|
+
* @returns {void}
|
|
89
|
+
*
|
|
90
|
+
* @throws {Error}
|
|
91
|
+
* Throws if PostgreSQL does not become ready within the timeout.
|
|
92
|
+
*/
|
|
93
|
+
export function waitForPostgres(
|
|
94
|
+
containerName: string,
|
|
95
|
+
user: string,
|
|
96
|
+
db: string,
|
|
97
|
+
): void
|
|
98
|
+
/**
|
|
99
|
+
* Build a PostgreSQL connection URI
|
|
100
|
+
* @param {object} opts
|
|
101
|
+
* @param {string} opts.user
|
|
102
|
+
* @param {string} opts.pass
|
|
103
|
+
* @param {number} opts.port
|
|
104
|
+
* @param {string} opts.db
|
|
105
|
+
*/
|
|
106
|
+
export function buildPostgresUri({
|
|
107
|
+
user,
|
|
108
|
+
pass,
|
|
109
|
+
port,
|
|
110
|
+
db,
|
|
111
|
+
}: {
|
|
112
|
+
user: string
|
|
113
|
+
pass: string
|
|
114
|
+
port: number
|
|
115
|
+
db: string
|
|
116
|
+
}): string
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that the specified database tables exist.
|
|
3
|
+
*
|
|
4
|
+
* Checks all provided table names and collects any missing tables.
|
|
5
|
+
* Throws a single error listing all missing tables after validation completes.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} params
|
|
8
|
+
* Parameters object.
|
|
9
|
+
*
|
|
10
|
+
* @param {string|Object} params.connection
|
|
11
|
+
* Database connection configuration.
|
|
12
|
+
* Can be a database connection URI or a Knex connection object.
|
|
13
|
+
*
|
|
14
|
+
* @param {string[]} params.tables
|
|
15
|
+
* List of required table names to validate.
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} [params.log]
|
|
18
|
+
* Optional logger object.
|
|
19
|
+
*
|
|
20
|
+
* @param {Function} [params.log.info]
|
|
21
|
+
* Logger function for informational messages.
|
|
22
|
+
*
|
|
23
|
+
* @param {Function} [params.log.error]
|
|
24
|
+
* Logger function for error messages.
|
|
25
|
+
*
|
|
26
|
+
* @returns {Promise<void>}
|
|
27
|
+
*
|
|
28
|
+
* @throws {Error}
|
|
29
|
+
* Throws an error if one or more required tables are missing.
|
|
30
|
+
*/
|
|
31
|
+
export function validateSchema({
|
|
32
|
+
tables,
|
|
33
|
+
connection,
|
|
34
|
+
log,
|
|
35
|
+
}: {
|
|
36
|
+
connection: string | any
|
|
37
|
+
tables: string[]
|
|
38
|
+
log?: {
|
|
39
|
+
info?: Function
|
|
40
|
+
error?: Function
|
|
41
|
+
}
|
|
42
|
+
}): Promise<void>
|