core-services-sdk 1.3.69 → 1.3.71

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.3.69",
3
+ "version": "1.3.71",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "types": "types/index.d.ts",
@@ -160,6 +160,14 @@ export const generateInvoiceItemId = () =>
160
160
  export const generateInvoiceCorrectionId = () =>
161
161
  generatePrefixedId(ID_PREFIXES.INVOICE_CORRECTION)
162
162
 
163
+ /**
164
+ * Generates a counterparty ID.
165
+ *
166
+ * @returns {string}
167
+ */
168
+ export const generateCounterpartyId = () =>
169
+ generatePrefixedId(ID_PREFIXES.COUNTERPARTY)
170
+
163
171
  /* =========================
164
172
  * AI / document processing
165
173
  * ========================= */
@@ -57,7 +57,7 @@ export const ID_PREFIXES = Object.freeze({
57
57
  DELIVERY_NOTE: 'dln',
58
58
  ORDER: 'ord',
59
59
  QUOTE: 'qte',
60
-
60
+ COUNTERPARTY: 'ctp',
61
61
  //////////////////////////////////////////////////////
62
62
  // AI / document processing
63
63
  //////////////////////////////////////////////////////
@@ -1,22 +1,41 @@
1
1
  import knex from 'knex'
2
2
 
3
3
  /**
4
- * Creates and returns a Knex database connection instance.
4
+ * Creates and returns a Knex PostgreSQL connection instance
5
+ * with sane default pool and timeout configuration.
5
6
  *
6
- * This function initializes a Knex client configured for PostgreSQL
7
- * using the provided connection configuration.
7
+ * Defaults are optimized for typical API / service workloads
8
+ * and can be overridden by the caller if needed.
8
9
  *
9
10
  * @param {object|string} connection
10
- * Knex connection configuration.
11
- * Can be a connection object or a connection string.
11
+ * Knex connection configuration or connection string.
12
+ *
13
+ * @param {object} [options]
14
+ * Optional Knex overrides.
15
+ *
16
+ * @param {object} [options.pool]
17
+ * Knex pool configuration overrides.
12
18
  *
13
19
  * @returns {import('knex').Knex}
14
20
  * A configured Knex database instance.
15
21
  */
16
- export const connectToPg = (connection) => {
22
+ export const connectToPg = (connection, options = {}) => {
17
23
  const db = knex({
18
- connection,
19
24
  client: 'pg',
25
+ connection,
26
+
27
+ pool: {
28
+ min: 2,
29
+ max: 10,
30
+ acquireTimeoutMillis: 30000,
31
+ idleTimeoutMillis: 10000,
32
+ createTimeoutMillis: 30000,
33
+ reapIntervalMillis: 1000,
34
+ propagateCreateError: false,
35
+ ...(options.pool || {}),
36
+ },
37
+
38
+ ...options,
20
39
  })
21
40
 
22
41
  return db
@@ -72,6 +72,7 @@ import {
72
72
 
73
73
  // Bots
74
74
  generateBotFlowId,
75
+ generateCounterpartyId,
75
76
  } from '../../src/ids/generators.js'
76
77
 
77
78
  import { ID_PREFIXES } from '../../src/ids/prefixes.js'
@@ -143,6 +144,7 @@ describe('generate*Id functions', () => {
143
144
  it('accounting domain', () => {
144
145
  testPrefixFunction(generateSupplierId, ID_PREFIXES.SUPPLIER)
145
146
  testPrefixFunction(generateCustomerId, ID_PREFIXES.CUSTOMER)
147
+ testPrefixFunction(generateCounterpartyId, ID_PREFIXES.COUNTERPARTY)
146
148
  testPrefixFunction(generateInvoiceId, ID_PREFIXES.INVOICE)
147
149
  testPrefixFunction(generateInvoiceItemId, ID_PREFIXES.INVOICE_ITEM)
148
150
  testPrefixFunction(
@@ -199,3 +201,9 @@ describe('generate*Id functions', () => {
199
201
  testPrefixFunction(generateBotFlowId, ID_PREFIXES.BOT_FLOW)
200
202
  })
201
203
  })
204
+
205
+ it('all prefixes are lowercase and reasonably short', () => {
206
+ Object.values(ID_PREFIXES).forEach((prefix) => {
207
+ expect(prefix).toMatch(/^[a-z0-9]{2,6}$/)
208
+ })
209
+ })
@@ -29,10 +29,11 @@ describe('ID_PREFIXES', () => {
29
29
 
30
30
  // Accounting domain
31
31
  SUPPLIER: 'sup',
32
+ CUSTOMER: 'cust',
33
+ COUNTERPARTY: 'ctp',
32
34
  INVOICE: 'inv',
33
35
  INVOICE_ITEM: 'invi',
34
36
  INVOICE_CORRECTION: 'invc',
35
- CUSTOMER: 'cust',
36
37
  PAYMENT: 'pay',
37
38
  TRANSACTION: 'txn',
38
39
  RECEIPT: 'rcp',
@@ -0,0 +1,70 @@
1
+ // @ts-nocheck
2
+ import { describe, it, expect, afterEach } from 'vitest'
3
+ import { connectToPg } from '../../src/postgresql/connect-to-pg.js'
4
+
5
+ let db
6
+
7
+ afterEach(async () => {
8
+ if (db) {
9
+ await db.destroy()
10
+ db = null
11
+ }
12
+ })
13
+
14
+ describe('connectToPg', () => {
15
+ it('creates knex instance with default pool settings', () => {
16
+ db = connectToPg('postgres://user:pass@localhost:5432/test')
17
+
18
+ const pool = db.client.pool
19
+
20
+ expect(pool).toBeDefined()
21
+ expect(pool.min).toBe(2)
22
+ expect(pool.max).toBe(10)
23
+ expect(pool.acquireTimeoutMillis).toBeGreaterThan(0)
24
+ expect(pool.idleTimeoutMillis).toBe(10000)
25
+ })
26
+
27
+ it('allows overriding pool settings', () => {
28
+ db = connectToPg('postgres://user:pass@localhost:5432/test', {
29
+ pool: {
30
+ min: 1,
31
+ max: 20,
32
+ },
33
+ })
34
+
35
+ const pool = db.client.pool
36
+
37
+ expect(pool.min).toBe(1)
38
+ expect(pool.max).toBe(20)
39
+ expect(pool.acquireTimeoutMillis).toBeGreaterThan(0)
40
+ })
41
+
42
+ it('allows overriding non-pool knex options', () => {
43
+ db = connectToPg('postgres://user:pass@localhost:5432/test', {
44
+ debug: true,
45
+ })
46
+
47
+ expect(db.client.config.debug).toBe(true)
48
+ })
49
+
50
+ it('does not lose default pool values when partial override is provided', () => {
51
+ db = connectToPg('postgres://user:pass@localhost:5432/test', {
52
+ pool: {
53
+ max: 15,
54
+ },
55
+ })
56
+
57
+ const pool = db.client.pool
58
+
59
+ expect(pool.min).toBe(2)
60
+ expect(pool.max).toBe(15)
61
+ expect(pool.acquireTimeoutMillis).toBeGreaterThan(0)
62
+ })
63
+
64
+ it('returns a valid knex instance', () => {
65
+ db = connectToPg('postgres://user:pass@localhost:5432/test')
66
+
67
+ expect(typeof db.raw).toBe('function')
68
+ expect(typeof db.destroy).toBe('function')
69
+ })
70
+ })
@@ -26,6 +26,7 @@ export function generateSupplierId(): string
26
26
  export function generateInvoiceId(): string
27
27
  export function generateInvoiceItemId(): string
28
28
  export function generateInvoiceCorrectionId(): string
29
+ export function generateCounterpartyId(): string
29
30
  export function generateDocumentDataId(): string
30
31
  export function generateBotFlowId(): string
31
32
  export function generateCorrelationId(): string
@@ -50,6 +50,7 @@ export const ID_PREFIXES: Readonly<{
50
50
  DELIVERY_NOTE: 'dln'
51
51
  ORDER: 'ord'
52
52
  QUOTE: 'qte'
53
+ COUNTERPARTY: 'ctp'
53
54
  DOCUMENT_DATA: 'docd'
54
55
  CORRELATION: 'crln'
55
56
  EVENT: 'evt'
@@ -1 +1,6 @@
1
- export function connectToPg(connection: object | string): import('knex').Knex
1
+ export function connectToPg(
2
+ connection: object | string,
3
+ options?: {
4
+ pool?: object
5
+ },
6
+ ): import('knex').Knex