sqlstack 1.0.3 → 1.0.5

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/dist/index.d.ts CHANGED
@@ -9,4 +9,5 @@ export * from './decorators/defaults';
9
9
  export * from './decorators/resultShape';
10
10
  export * from './decorators/validateResult';
11
11
  export * from './decorators/transform';
12
+ export * from './utils/dto';
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,0BAA0B,CAAC;AACzC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,0BAA0B,CAAC;AACzC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC"}
package/dist/index.js CHANGED
@@ -25,4 +25,5 @@ __exportStar(require("./decorators/defaults"), exports);
25
25
  __exportStar(require("./decorators/resultShape"), exports);
26
26
  __exportStar(require("./decorators/validateResult"), exports);
27
27
  __exportStar(require("./decorators/transform"), exports);
28
+ __exportStar(require("./utils/dto"), exports);
28
29
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,gDAA8B;AAC9B,mDAAiC;AACjC,6CAA2B;AAC3B,6CAA2B;AAC3B,2DAAyC;AACzC,qDAAmC;AACnC,wDAAsC;AACtC,2DAAyC;AACzC,8DAA4C;AAC5C,yDAAuC","sourcesContent":["export * from './binders/sqlBinder';\nexport * from './core/errors';\nexport * from './core/validator';\nexport * from './registry';\nexport * from './adapters';\nexport * from './decorators/queryBinder';\nexport * from './decorators/query';\nexport * from './decorators/defaults';\nexport * from './decorators/resultShape';\nexport * from './decorators/validateResult';\nexport * from './decorators/transform';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,gDAA8B;AAC9B,mDAAiC;AACjC,6CAA2B;AAC3B,6CAA2B;AAC3B,2DAAyC;AACzC,qDAAmC;AACnC,wDAAsC;AACtC,2DAAyC;AACzC,8DAA4C;AAC5C,yDAAuC;AACvC,8CAA4B","sourcesContent":["export * from './binders/sqlBinder';\nexport * from './core/errors';\nexport * from './core/validator';\nexport * from './registry';\nexport * from './adapters';\nexport * from './decorators/queryBinder';\nexport * from './decorators/query';\nexport * from './decorators/defaults';\nexport * from './decorators/resultShape';\nexport * from './decorators/validateResult';\nexport * from './decorators/transform';\nexport * from './utils/dto';\n"]}
@@ -0,0 +1,16 @@
1
+ type KV = Record<string, any>;
2
+ export type NodeSpec = {
3
+ id: string;
4
+ name?: string;
5
+ [collectionKey: string]: any;
6
+ };
7
+ type DTOOptions = {
8
+ /** If true, return a single root (or null). If false, return an array of roots. Default: true */
9
+ single?: boolean;
10
+ };
11
+ /**
12
+ * Build a rows→tree transformer from a nested spec.
13
+ */
14
+ export declare function DTO(spec: NodeSpec, opts?: DTOOptions): (rows: KV[]) => KV | KV[] | null;
15
+ export {};
16
+ //# sourceMappingURL=dto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dto.d.ts","sourceRoot":"","sources":["../../src/utils/dto.ts"],"names":[],"mappings":"AAAA,KAAK,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE9B,MAAM,MAAM,QAAQ,GAAG;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,CAAC,aAAa,EAAE,MAAM,GAAG,GAAG,CAAC;CAC9B,CAAC;AAEF,KAAK,UAAU,GAAG;IAChB,iGAAiG;IACjG,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AA6CF;;GAEG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,GAAE,UAAe,IA4D7B,MAAM,EAAE,EAAE,sBAerC"}
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DTO = DTO;
4
+ const CHILD_INDEX = Symbol('childIndex');
5
+ function setDeep(obj, path, value) {
6
+ const parts = path.split('.');
7
+ let cur = obj;
8
+ for (let i = 0; i < parts.length - 1; i++) {
9
+ const k = parts[i];
10
+ if (cur[k] == null || typeof cur[k] !== 'object')
11
+ cur[k] = {};
12
+ cur = cur[k];
13
+ }
14
+ cur[parts[parts.length - 1]] = value;
15
+ }
16
+ function childCollections(spec) {
17
+ const out = [];
18
+ for (const key of Object.keys(spec)) {
19
+ if (key === 'id' || key === 'name')
20
+ continue;
21
+ const val = spec[key];
22
+ if (val && typeof val === 'object' && 'id' in val)
23
+ out.push([key, val]);
24
+ }
25
+ return out;
26
+ }
27
+ function ensureChildInfra(node, collectionKey) {
28
+ if (!node[collectionKey])
29
+ node[collectionKey] = [];
30
+ if (!node[CHILD_INDEX])
31
+ node[CHILD_INDEX] = {};
32
+ const idxBag = node[CHILD_INDEX];
33
+ if (!idxBag[collectionKey])
34
+ idxBag[collectionKey] = new Map();
35
+ return { arr: node[collectionKey], index: idxBag[collectionKey] };
36
+ }
37
+ function stripMeta(node) {
38
+ if (node && typeof node === 'object') {
39
+ // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
40
+ delete node[CHILD_INDEX];
41
+ for (const k of Object.keys(node)) {
42
+ const v = node[k];
43
+ if (Array.isArray(v))
44
+ v.forEach(stripMeta);
45
+ }
46
+ }
47
+ return node;
48
+ }
49
+ /**
50
+ * Build a rows→tree transformer from a nested spec.
51
+ */
52
+ function DTO(spec, opts = {}) {
53
+ const single = opts.single ?? false;
54
+ const mappingsCache = new WeakMap();
55
+ function getMappings(levelSpec) {
56
+ const cached = mappingsCache.get(levelSpec);
57
+ if (cached)
58
+ return cached;
59
+ const out = [];
60
+ for (const [k, v] of Object.entries(levelSpec)) {
61
+ if (k === 'id' || k === 'name')
62
+ continue;
63
+ if (typeof v === 'string')
64
+ out.push([k, v]);
65
+ }
66
+ mappingsCache.set(levelSpec, out);
67
+ return out;
68
+ }
69
+ function upsertAtLevel(rowsIndex, rowsOut, levelSpec, row) {
70
+ const nodeId = row[levelSpec.id];
71
+ if (nodeId == null)
72
+ return null;
73
+ let node = rowsIndex.get(nodeId);
74
+ if (!node) {
75
+ node = {
76
+ id: nodeId,
77
+ ...(levelSpec.name ? { name: row[levelSpec.name] } : {}),
78
+ };
79
+ for (const [target, source] of getMappings(levelSpec)) {
80
+ if (source in row)
81
+ setDeep(node, target, row[source]);
82
+ }
83
+ rowsIndex.set(nodeId, node);
84
+ rowsOut.push(node);
85
+ }
86
+ return node;
87
+ }
88
+ function descend(parent, levelSpec, row) {
89
+ for (const [collectionKey, childSpec] of childCollections(levelSpec)) {
90
+ const childId = row[childSpec.id];
91
+ if (childId == null)
92
+ continue; // skip this branch for this row
93
+ const { arr, index } = ensureChildInfra(parent, collectionKey);
94
+ let child = index.get(childId);
95
+ if (!child) {
96
+ child = {
97
+ id: childId,
98
+ ...(childSpec.name ? { name: row[childSpec.name] } : {}),
99
+ };
100
+ for (const [target, source] of getMappings(childSpec)) {
101
+ if (source in row)
102
+ setDeep(child, target, row[source]);
103
+ }
104
+ index.set(childId, child);
105
+ arr.push(child);
106
+ }
107
+ // Recurse further for this child (it may host its own collections)
108
+ descend(child, childSpec, row);
109
+ }
110
+ }
111
+ return function transform(rows) {
112
+ if (!rows || rows.length === 0)
113
+ return single ? null : [];
114
+ const roots = [];
115
+ const rootIndex = new Map();
116
+ for (const row of rows) {
117
+ const root = upsertAtLevel(rootIndex, roots, spec, row);
118
+ if (!root)
119
+ continue;
120
+ descend(root, spec, row);
121
+ }
122
+ roots.forEach(stripMeta);
123
+ return single ? roots[0] ?? null : roots;
124
+ };
125
+ }
126
+ //# sourceMappingURL=dto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dto.js","sourceRoot":"","sources":["../../src/utils/dto.ts"],"names":[],"mappings":";;AA4DA,kBA2EC;AAzHD,MAAM,WAAW,GAAG,MAAM,CAAC,YAAY,CAAC,CAAC;AAEzC,SAAS,OAAO,CAAC,GAAO,EAAE,IAAY,EAAE,KAAU;IAChD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,GAAG,GAAQ,GAAG,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC1C,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;QACpB,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,IAAI,IAAI,OAAO,GAAG,CAAC,CAAC,CAAC,KAAK,QAAQ;YAAE,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC;QAC9D,GAAG,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IACf,CAAC;IACD,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG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EAAE,CAAC;iBACzD,CAAC;gBACF,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;oBACtD,IAAI,MAAM,IAAI,GAAG;wBAAE,OAAO,CAAC,KAAK,EAAE,MAAM,EAAG,GAAW,CAAC,MAAM,CAAC,CAAC,CAAC;gBAClE,CAAC;gBACD,KAAK,CAAC,GAAG,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;gBAC1B,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAClB,CAAC;YAED,mEAAmE;YACnE,OAAO,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC;QACjC,CAAC;IACH,CAAC;IAED,OAAO,SAAS,SAAS,CAAC,IAAU;QAClC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QAE1D,MAAM,KAAK,GAAS,EAAE,CAAC;QACvB,MAAM,SAAS,GAAa,IAAI,GAAG,EAAE,CAAC;QAEtC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,IAAI,GAAG,aAAa,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;YACxD,IAAI,CAAC,IAAI;gBAAE,SAAS;YACpB,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;QAC3B,CAAC;QAED,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACzB,OAAO,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IAC3C,CAAC,CAAC;AACJ,CAAC","sourcesContent":["type KV = Record<string, any>;\n\nexport type NodeSpec = {\n id: string;\n name?: string;\n // nested collections: { [collectionKey]: NodeSpec }\n [collectionKey: string]: any;\n};\n\ntype DTOOptions = {\n /** If true, return a single root (or null). If false, return an array of roots. Default: true */\n single?: boolean;\n};\n\nconst CHILD_INDEX = Symbol('childIndex');\n\nfunction setDeep(obj: KV, path: string, value: any) {\n const parts = path.split('.');\n let cur: any = obj;\n for (let i = 0; i < parts.length - 1; i++) {\n const k = parts[i]!;\n if (cur[k] == null || typeof cur[k] !== 'object') cur[k] = {};\n cur = cur[k];\n }\n cur[parts[parts.length - 1]!] = value;\n}\n\nfunction childCollections(spec: NodeSpec): Array<[string, NodeSpec]> {\n const out: Array<[string, NodeSpec]> = [];\n for (const key of Object.keys(spec)) {\n if (key === 'id' || key === 'name') continue;\n const val = (spec as any)[key];\n if (val && typeof val === 'object' && 'id' in val) out.push([key, val as NodeSpec]);\n }\n return out;\n}\n\nfunction ensureChildInfra(node: KV, collectionKey: string) {\n if (!node[collectionKey]) node[collectionKey] = [];\n if (!(node as any)[CHILD_INDEX]) (node as any)[CHILD_INDEX] = {} as Record<string, Map<any, KV>>;\n const idxBag = (node as any)[CHILD_INDEX] as Record<string, Map<any, KV>>;\n if (!idxBag[collectionKey]) idxBag[collectionKey] = new Map<any, KV>();\n return { arr: node[collectionKey] as KV[], index: idxBag[collectionKey] as Map<any, KV> };\n}\n\nfunction stripMeta(node: KV) {\n if (node && typeof node === 'object') {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete (node as any)[CHILD_INDEX];\n for (const k of Object.keys(node)) {\n const v = node[k];\n if (Array.isArray(v)) v.forEach(stripMeta);\n }\n }\n return node;\n}\n\n/**\n * Build a rows→tree transformer from a nested spec.\n */\nexport function DTO(spec: NodeSpec, opts: DTOOptions = {}) {\n const single = opts.single ?? false;\n type IndexMap = Map<any, KV>;\n const mappingsCache = new WeakMap<NodeSpec, Array<[string, string]>>();\n\n function getMappings(levelSpec: NodeSpec): Array<[string, string]> {\n const cached = mappingsCache.get(levelSpec);\n if (cached) return cached;\n const out: Array<[string, string]> = [];\n for (const [k, v] of Object.entries(levelSpec)) {\n if (k === 'id' || k === 'name') continue;\n if (typeof v === 'string') out.push([k, v]);\n }\n mappingsCache.set(levelSpec, out);\n return out;\n }\n\n function upsertAtLevel(rowsIndex: IndexMap, rowsOut: KV[], levelSpec: NodeSpec, row: KV) {\n const nodeId = row[levelSpec.id];\n if (nodeId == null) return null;\n\n let node = rowsIndex.get(nodeId);\n if (!node) {\n node = {\n id: nodeId,\n ...(levelSpec.name ? { name: row[levelSpec.name] } : {}),\n };\n for (const [target, source] of getMappings(levelSpec)) {\n if (source in row) setDeep(node, target, (row as any)[source]);\n }\n rowsIndex.set(nodeId, node);\n rowsOut.push(node);\n }\n return node;\n }\n\n function descend(parent: KV, levelSpec: NodeSpec, row: KV) {\n for (const [collectionKey, childSpec] of childCollections(levelSpec)) {\n const childId = row[childSpec.id];\n if (childId == null) continue; // skip this branch for this row\n\n const { arr, index } = ensureChildInfra(parent, collectionKey);\n let child = index.get(childId);\n if (!child) {\n child = {\n id: childId,\n ...(childSpec.name ? { name: row[childSpec.name] } : {}),\n };\n for (const [target, source] of getMappings(childSpec)) {\n if (source in row) setDeep(child, target, (row as any)[source]);\n }\n index.set(childId, child);\n arr.push(child);\n }\n\n // Recurse further for this child (it may host its own collections)\n descend(child, childSpec, row);\n }\n }\n\n return function transform(rows: KV[]) {\n if (!rows || rows.length === 0) return single ? null : [];\n\n const roots: KV[] = [];\n const rootIndex: IndexMap = new Map();\n\n for (const row of rows) {\n const root = upsertAtLevel(rootIndex, roots, spec, row);\n if (!root) continue;\n descend(root, spec, row);\n }\n\n roots.forEach(stripMeta);\n return single ? roots[0] ?? null : roots;\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqlstack",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "",
package/readme.md CHANGED
@@ -427,6 +427,62 @@ if (!Array.isArray(pgWrite) && pgWrite.returning) {
427
427
  }
428
428
  ```
429
429
 
430
+ ## DTO Helper
431
+
432
+ Turn flat SQL rows into nested objects using a simple spec and the `@Transform` decorator.
433
+
434
+ ```ts
435
+ import { DTO, Transform, Query, QueryBinder } from 'sqlstack';
436
+
437
+ // Define a nesting spec using column names
438
+ const toMuscleGroup = DTO(
439
+ {
440
+ id: 'group_id',
441
+ name: 'group_name',
442
+ muscles: {
443
+ id: 'muscle_id',
444
+ name: 'muscle_name',
445
+ exercises: {
446
+ id: 'exercise_id',
447
+ name: 'exercise_name',
448
+ },
449
+ },
450
+ },
451
+ { single: true } // default returns an array; set { single: true } to return a single root (or null)
452
+ );
453
+
454
+ @QueryBinder()
455
+ class Repo {
456
+ @Transform(toMuscleGroup)
457
+ @Query({ sql: `
458
+ SELECT
459
+ g.id AS group_id, g.name AS group_name,
460
+ m.id AS muscle_id, m.name AS muscle_name,
461
+ e.id AS exercise_id,e.name AS exercise_name
462
+ FROM groups g
463
+ LEFT JOIN muscles m ON m.group_id = g.id
464
+ LEFT JOIN exercises e ON e.muscle_id = m.id
465
+ WHERE g.id = :id
466
+ ORDER BY m.name, e.name
467
+ ` })
468
+ async getGroup(_a: { id: string }): Promise<any | null> { throw new Error('replaced'); }
469
+ }
470
+ ```
471
+
472
+ You can also project flat columns into nested properties directly in the spec:
473
+
474
+ ```ts
475
+ // If a row has: { id: '1', muscle: 'biceps', group: 'arms' }
476
+ const toSimple = DTO({ id: 'id', name: 'muscle', 'group.name': 'group' } as any, { single: true });
477
+ // Result root: { name: 'biceps', group: { name: 'arms' } }
478
+ ```
479
+
480
+ - Arbitrary depth: nest collections by adding objects under new keys.
481
+ - Dedupe: nodes are de-duplicated by their `id` at each level.
482
+ - Null-safe: branches with `NULL` child ids are skipped for that row.
483
+ - Order: sibling order follows your SQL `ORDER BY`.
484
+ - Mapping: put `'target.path': 'source_key'` entries directly in your spec to populate nested properties from flat columns.
485
+
430
486
  ## Underlying Connections
431
487
 
432
488
  Every `Database` exposes its driver via `conn` so you can drop down to the raw client when needed: