core-services-sdk 1.3.32 → 1.3.34
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/mongodb/dsl-to-mongo.js +19 -0
- package/tests/mongodb/cast-iso-dates.test.js +67 -0
- package/types/ids/generators.d.ts +18 -0
- package/types/ids/prefixes.d.ts +36 -0
- package/types/mongodb/connect.d.ts +5 -2
- package/types/mongodb/index.d.ts +1 -0
- package/types/mongodb/paginate.d.ts +24 -0
- package/types/util/context.d.ts +55 -0
- package/types/util/index.d.ts +1 -0
package/package.json
CHANGED
|
@@ -97,3 +97,22 @@ export function toMongo(query = {}) {
|
|
|
97
97
|
}),
|
|
98
98
|
)
|
|
99
99
|
}
|
|
100
|
+
|
|
101
|
+
export function castIsoDates(obj) {
|
|
102
|
+
if (Array.isArray(obj)) {
|
|
103
|
+
return obj.map(castIsoDates)
|
|
104
|
+
}
|
|
105
|
+
if (obj && typeof obj === 'object') {
|
|
106
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
107
|
+
obj[key] = castIsoDates(value)
|
|
108
|
+
}
|
|
109
|
+
return obj
|
|
110
|
+
}
|
|
111
|
+
if (typeof obj === 'string' && /^\d{4}-\d{2}-\d{2}T/.test(obj)) {
|
|
112
|
+
const d = new Date(obj)
|
|
113
|
+
if (!isNaN(d)) {
|
|
114
|
+
return d
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return obj
|
|
118
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { castIsoDates } from '../../src/mongodb/dsl-to-mongo.js'
|
|
4
|
+
|
|
5
|
+
describe('castIsoDates', () => {
|
|
6
|
+
it('should convert ISO date strings to Date objects', () => {
|
|
7
|
+
const input = {
|
|
8
|
+
createdAt: {
|
|
9
|
+
$gte: '2025-10-08T00:00:00.000Z',
|
|
10
|
+
$lte: '2025-10-09T23:59:59.999Z',
|
|
11
|
+
},
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const output = castIsoDates(structuredClone(input))
|
|
15
|
+
|
|
16
|
+
expect(output.createdAt.$gte).toBeInstanceOf(Date)
|
|
17
|
+
expect(output.createdAt.$lte).toBeInstanceOf(Date)
|
|
18
|
+
expect(output.createdAt.$gte.toISOString()).toBe('2025-10-08T00:00:00.000Z')
|
|
19
|
+
expect(output.createdAt.$lte.toISOString()).toBe('2025-10-09T23:59:59.999Z')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('should recursively convert nested ISO strings', () => {
|
|
23
|
+
const input = {
|
|
24
|
+
filters: [
|
|
25
|
+
{ time: '2025-09-01T12:00:00.000Z' },
|
|
26
|
+
{ inner: { deep: '2025-09-02T00:00:00.000Z' } },
|
|
27
|
+
],
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const output = castIsoDates(structuredClone(input))
|
|
31
|
+
|
|
32
|
+
expect(output.filters[0].time).toBeInstanceOf(Date)
|
|
33
|
+
expect(output.filters[1].inner.deep).toBeInstanceOf(Date)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should not modify non-date strings or numbers', () => {
|
|
37
|
+
const input = {
|
|
38
|
+
name: 'hello',
|
|
39
|
+
value: 123,
|
|
40
|
+
notIso: '2025-09-01', // missing 'T' — not ISO format
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const output = castIsoDates(structuredClone(input))
|
|
44
|
+
|
|
45
|
+
expect(output.name).toBe('hello')
|
|
46
|
+
expect(output.value).toBe(123)
|
|
47
|
+
expect(output.notIso).toBe('2025-09-01')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('should handle arrays directly', () => {
|
|
51
|
+
const input = ['2025-10-01T10:00:00.000Z', 'not-a-date', 5]
|
|
52
|
+
|
|
53
|
+
const output = castIsoDates(structuredClone(input))
|
|
54
|
+
|
|
55
|
+
expect(output[0]).toBeInstanceOf(Date)
|
|
56
|
+
expect(output[1]).toBe('not-a-date')
|
|
57
|
+
expect(output[2]).toBe(5)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should leave null and undefined unchanged', () => {
|
|
61
|
+
const input = { a: null, b: undefined }
|
|
62
|
+
const output = castIsoDates(structuredClone(input))
|
|
63
|
+
|
|
64
|
+
expect(output.a).toBeNull()
|
|
65
|
+
expect(output.b).toBeUndefined()
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -8,3 +8,21 @@ export function generateVerificationId(): string;
|
|
|
8
8
|
export function generateRolePermissionsId(): string;
|
|
9
9
|
export function generateOnboardingId(): string;
|
|
10
10
|
export function generateSessionId(): string;
|
|
11
|
+
export function generateFileId(): string;
|
|
12
|
+
export function generateEventId(): string;
|
|
13
|
+
export function generateJobId(): string;
|
|
14
|
+
export function generateTaskId(): string;
|
|
15
|
+
export function generateQueueId(): string;
|
|
16
|
+
export function generateMessageId(): string;
|
|
17
|
+
export function generateNotificationId(): string;
|
|
18
|
+
export function generateLogId(): string;
|
|
19
|
+
export function generateAuditId(): string;
|
|
20
|
+
export function generateConfigId(): string;
|
|
21
|
+
export function generateKeyId(): string;
|
|
22
|
+
export function generateMetricId(): string;
|
|
23
|
+
export function generateTagId(): string;
|
|
24
|
+
export function generatePolicyId(): string;
|
|
25
|
+
export function generateProfileId(): string;
|
|
26
|
+
export function generateDeviceId(): string;
|
|
27
|
+
export function generateAlertId(): string;
|
|
28
|
+
export function generateResourceId(): string;
|
package/types/ids/prefixes.d.ts
CHANGED
|
@@ -31,4 +31,40 @@ export const ID_PREFIXES: Readonly<{
|
|
|
31
31
|
ONBOARDING: "onb";
|
|
32
32
|
/** Session mapping ID prefix */
|
|
33
33
|
SESSION: "sess";
|
|
34
|
+
/** File mapping ID prefix */
|
|
35
|
+
FILE: "fil";
|
|
36
|
+
/** Event entity ID prefix */
|
|
37
|
+
EVENT: "evt";
|
|
38
|
+
/** Job entity ID prefix */
|
|
39
|
+
JOB: "job";
|
|
40
|
+
/** Task entity ID prefix */
|
|
41
|
+
TASK: "task";
|
|
42
|
+
/** Queue entity ID prefix */
|
|
43
|
+
QUEUE: "que";
|
|
44
|
+
/** Message entity ID prefix */
|
|
45
|
+
MESSAGE: "msg";
|
|
46
|
+
/** Notification entity ID prefix */
|
|
47
|
+
NOTIFICATION: "ntf";
|
|
48
|
+
/** Log entity ID prefix */
|
|
49
|
+
LOG: "log";
|
|
50
|
+
/** Audit entity ID prefix */
|
|
51
|
+
AUDIT: "adt";
|
|
52
|
+
/** Config entity ID prefix */
|
|
53
|
+
CONFIG: "cfg";
|
|
54
|
+
/** Key entity ID prefix */
|
|
55
|
+
KEY: "key";
|
|
56
|
+
/** Metric entity ID prefix */
|
|
57
|
+
METRIC: "met";
|
|
58
|
+
/** Tag entity ID prefix */
|
|
59
|
+
TAG: "tag";
|
|
60
|
+
/** Policy entity ID prefix */
|
|
61
|
+
POLICY: "plc";
|
|
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 */
|
|
69
|
+
RESOURCE: "res";
|
|
34
70
|
}>;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export function mongoConnect({ uri, serverApi }: {
|
|
1
|
+
export function mongoConnect({ uri, serverApi, timeout, retries, }: {
|
|
2
2
|
uri: string;
|
|
3
3
|
serverApi?: object;
|
|
4
|
-
|
|
4
|
+
timeout?: number;
|
|
5
|
+
retries?: number;
|
|
6
|
+
}): Promise<MongoClient>;
|
|
7
|
+
import { MongoClient } from 'mongodb';
|
package/types/mongodb/index.d.ts
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pagination with SQL-like ascending/descending
|
|
3
|
+
*
|
|
4
|
+
* @param {import('mongodb').Collection} collection
|
|
5
|
+
* @param {Object} options
|
|
6
|
+
* @param {Object} [options.filter={}]
|
|
7
|
+
* @param {string} [options.cursorField='_id']
|
|
8
|
+
* @param {string|Date|ObjectId} [options.cursor]
|
|
9
|
+
* @param {'asc'|'desc'} [options.order='asc']
|
|
10
|
+
* @param {number} [options.limit=10]
|
|
11
|
+
*/
|
|
12
|
+
export function paginate(collection: import("mongodb").Collection, { limit, projection, filter, cursor, order, cursorField, }?: {
|
|
13
|
+
filter?: any;
|
|
14
|
+
cursorField?: string;
|
|
15
|
+
cursor?: string | Date | ObjectId;
|
|
16
|
+
order?: "asc" | "desc";
|
|
17
|
+
limit?: number;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
order: "desc" | "asc";
|
|
20
|
+
list: import("mongodb").WithId<import("bson").Document>[];
|
|
21
|
+
previous: any;
|
|
22
|
+
next: any;
|
|
23
|
+
}>;
|
|
24
|
+
import { ObjectId } from 'mongodb';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export namespace Context {
|
|
2
|
+
/**
|
|
3
|
+
* Run a callback within a given context store.
|
|
4
|
+
* Everything `await`ed or invoked inside this callback will have access
|
|
5
|
+
* to the provided store via {@link Context.get}, {@link Context.set}, or {@link Context.all}.
|
|
6
|
+
*
|
|
7
|
+
* @template T
|
|
8
|
+
* @param {Record<string, any>} store
|
|
9
|
+
* @param {() => T} callback - Function to execute inside the context.
|
|
10
|
+
* @returns {T} The return value of the callback (sync or async).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* Context.run(
|
|
14
|
+
* { correlationId: 'abc123' },
|
|
15
|
+
* async () => {
|
|
16
|
+
* console.log(Context.get('correlationId')) // "abc123"
|
|
17
|
+
* }
|
|
18
|
+
* )
|
|
19
|
+
*/
|
|
20
|
+
function run<T>(store: Record<string, any>, callback: () => T): T;
|
|
21
|
+
/**
|
|
22
|
+
* Retrieve a single value from the current async context store.
|
|
23
|
+
*
|
|
24
|
+
* @template T
|
|
25
|
+
* @param {string} key - The key of the value to retrieve.
|
|
26
|
+
* @returns {T|undefined} The stored value, or `undefined` if no store exists or key not found.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const userId = Context.get('userId')
|
|
30
|
+
*/
|
|
31
|
+
function get<T>(key: string): T | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Set a single key-value pair in the current async context store.
|
|
34
|
+
* If there is no active store (i.e. outside of a {@link Context.run}),
|
|
35
|
+
* this function does nothing.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} key - The key under which to store the value.
|
|
38
|
+
* @param {any} value - The value to store.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* Context.set('tenantId', 'tnt_1234')
|
|
42
|
+
*/
|
|
43
|
+
function set(key: string, value: any): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get the entire store object for the current async context.
|
|
46
|
+
*
|
|
47
|
+
* @returns {Record<string, any>} The current store object,
|
|
48
|
+
* or an empty object if no store exists.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const all = Context.all()
|
|
52
|
+
* console.log(all) // { correlationId: 'abc123', userId: 'usr_789' }
|
|
53
|
+
*/
|
|
54
|
+
function all(): Record<string, any>;
|
|
55
|
+
}
|
package/types/util/index.d.ts
CHANGED