@rawsql-ts/ztd-cli 0.15.0 → 0.17.0

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.
Files changed (153) hide show
  1. package/README.md +38 -25
  2. package/dist/commands/init.d.ts +13 -0
  3. package/dist/commands/init.js +372 -118
  4. package/dist/commands/init.js.map +1 -1
  5. package/dist/commands/lint.d.ts +4 -4
  6. package/dist/commands/lint.js +60 -40
  7. package/dist/commands/lint.js.map +1 -1
  8. package/dist/commands/ztdConfig.d.ts +2 -2
  9. package/dist/commands/ztdConfig.js +26 -12
  10. package/dist/commands/ztdConfig.js.map +1 -1
  11. package/dist/utils/optionalDependencies.d.ts +35 -0
  12. package/dist/utils/optionalDependencies.js +96 -0
  13. package/dist/utils/optionalDependencies.js.map +1 -0
  14. package/package.json +18 -10
  15. package/templates/AGENTS.md +36 -296
  16. package/templates/README.md +12 -237
  17. package/templates/dist/drivers/pg-testkit/src/driver/PgTestkitClient.d.ts +38 -0
  18. package/templates/dist/drivers/pg-testkit/src/driver/PgTestkitClient.js +117 -0
  19. package/templates/dist/drivers/pg-testkit/src/driver/PgTestkitClient.js.map +1 -0
  20. package/templates/dist/drivers/pg-testkit/src/driver/createPgTestkitPool.d.ts +4 -0
  21. package/templates/dist/drivers/pg-testkit/src/driver/createPgTestkitPool.js +71 -0
  22. package/templates/dist/drivers/pg-testkit/src/driver/createPgTestkitPool.js.map +1 -0
  23. package/templates/dist/drivers/pg-testkit/src/index.d.ts +5 -0
  24. package/templates/dist/drivers/pg-testkit/src/index.js +11 -0
  25. package/templates/dist/drivers/pg-testkit/src/index.js.map +1 -0
  26. package/templates/dist/drivers/pg-testkit/src/proxy/wrapPgClient.d.ts +3 -0
  27. package/templates/dist/drivers/pg-testkit/src/proxy/wrapPgClient.js +79 -0
  28. package/templates/dist/drivers/pg-testkit/src/proxy/wrapPgClient.js.map +1 -0
  29. package/templates/dist/drivers/pg-testkit/src/types.d.ts +69 -0
  30. package/templates/dist/drivers/pg-testkit/src/types.js +3 -0
  31. package/templates/dist/drivers/pg-testkit/src/types.js.map +1 -0
  32. package/templates/dist/drivers/pg-testkit/src/utils/fixtureState.d.ts +15 -0
  33. package/templates/dist/drivers/pg-testkit/src/utils/fixtureState.js +34 -0
  34. package/templates/dist/drivers/pg-testkit/src/utils/fixtureState.js.map +1 -0
  35. package/templates/dist/drivers/pg-testkit/src/utils/fixtureValidation.d.ts +12 -0
  36. package/templates/dist/drivers/pg-testkit/src/utils/fixtureValidation.js +53 -0
  37. package/templates/dist/drivers/pg-testkit/src/utils/fixtureValidation.js.map +1 -0
  38. package/templates/dist/mapper-core/src/index.d.ts +160 -0
  39. package/templates/dist/mapper-core/src/index.js +637 -0
  40. package/templates/dist/mapper-core/src/index.js.map +1 -0
  41. package/templates/dist/testkit-core/src/errors/index.d.ts +49 -0
  42. package/templates/dist/testkit-core/src/errors/index.js +111 -0
  43. package/templates/dist/testkit-core/src/errors/index.js.map +1 -0
  44. package/templates/dist/testkit-core/src/fixtures/ColumnAffinity.d.ts +5 -0
  45. package/templates/dist/testkit-core/src/fixtures/ColumnAffinity.js +29 -0
  46. package/templates/dist/testkit-core/src/fixtures/ColumnAffinity.js.map +1 -0
  47. package/templates/dist/testkit-core/src/fixtures/DdlFixtureLoader.d.ts +37 -0
  48. package/templates/dist/testkit-core/src/fixtures/DdlFixtureLoader.js +182 -0
  49. package/templates/dist/testkit-core/src/fixtures/DdlFixtureLoader.js.map +1 -0
  50. package/templates/dist/testkit-core/src/fixtures/FixtureProvider.d.ts +20 -0
  51. package/templates/dist/testkit-core/src/fixtures/FixtureProvider.js +121 -0
  52. package/templates/dist/testkit-core/src/fixtures/FixtureProvider.js.map +1 -0
  53. package/templates/dist/testkit-core/src/fixtures/FixtureStore.d.ts +51 -0
  54. package/templates/dist/testkit-core/src/fixtures/FixtureStore.js +199 -0
  55. package/templates/dist/testkit-core/src/fixtures/FixtureStore.js.map +1 -0
  56. package/templates/dist/testkit-core/src/fixtures/TableDefinitionSchemaRegistry.d.ts +10 -0
  57. package/templates/dist/testkit-core/src/fixtures/TableDefinitionSchemaRegistry.js +28 -0
  58. package/templates/dist/testkit-core/src/fixtures/TableDefinitionSchemaRegistry.js.map +1 -0
  59. package/templates/dist/testkit-core/src/fixtures/TableNameResolver.d.ts +18 -0
  60. package/templates/dist/testkit-core/src/fixtures/TableNameResolver.js +80 -0
  61. package/templates/dist/testkit-core/src/fixtures/TableNameResolver.js.map +1 -0
  62. package/templates/dist/testkit-core/src/fixtures/ddlLint.d.ts +59 -0
  63. package/templates/dist/testkit-core/src/fixtures/ddlLint.js +489 -0
  64. package/templates/dist/testkit-core/src/fixtures/ddlLint.js.map +1 -0
  65. package/templates/dist/testkit-core/src/fixtures/naming.d.ts +1 -0
  66. package/templates/dist/testkit-core/src/fixtures/naming.js +6 -0
  67. package/templates/dist/testkit-core/src/fixtures/naming.js.map +1 -0
  68. package/templates/dist/testkit-core/src/index.d.ts +17 -0
  69. package/templates/dist/testkit-core/src/index.js +47 -0
  70. package/templates/dist/testkit-core/src/index.js.map +1 -0
  71. package/templates/dist/testkit-core/src/logger/NoopLogger.d.ts +8 -0
  72. package/templates/dist/testkit-core/src/logger/NoopLogger.js +16 -0
  73. package/templates/dist/testkit-core/src/logger/NoopLogger.js.map +1 -0
  74. package/templates/dist/testkit-core/src/provider/TestkitProvider.d.ts +57 -0
  75. package/templates/dist/testkit-core/src/provider/TestkitProvider.js +149 -0
  76. package/templates/dist/testkit-core/src/provider/TestkitProvider.js.map +1 -0
  77. package/templates/dist/testkit-core/src/rewriter/ResultSelectRewriter.d.ts +43 -0
  78. package/templates/dist/testkit-core/src/rewriter/ResultSelectRewriter.js +473 -0
  79. package/templates/dist/testkit-core/src/rewriter/ResultSelectRewriter.js.map +1 -0
  80. package/templates/dist/testkit-core/src/rewriter/SelectAnalyzer.d.ts +9 -0
  81. package/templates/dist/testkit-core/src/rewriter/SelectAnalyzer.js +38 -0
  82. package/templates/dist/testkit-core/src/rewriter/SelectAnalyzer.js.map +1 -0
  83. package/templates/dist/testkit-core/src/rewriter/SelectFixtureRewriter.d.ts +42 -0
  84. package/templates/dist/testkit-core/src/rewriter/SelectFixtureRewriter.js +298 -0
  85. package/templates/dist/testkit-core/src/rewriter/SelectFixtureRewriter.js.map +1 -0
  86. package/templates/dist/testkit-core/src/sql/SqliteValuesBuilder.d.ts +12 -0
  87. package/templates/dist/testkit-core/src/sql/SqliteValuesBuilder.js +63 -0
  88. package/templates/dist/testkit-core/src/sql/SqliteValuesBuilder.js.map +1 -0
  89. package/templates/dist/testkit-core/src/types/index.d.ts +69 -0
  90. package/templates/dist/testkit-core/src/types/index.js +3 -0
  91. package/templates/dist/testkit-core/src/types/index.js.map +1 -0
  92. package/templates/dist/testkit-core/src/utils/queryHelpers.d.ts +28 -0
  93. package/templates/dist/testkit-core/src/utils/queryHelpers.js +81 -0
  94. package/templates/dist/testkit-core/src/utils/queryHelpers.js.map +1 -0
  95. package/templates/dist/writer-core/src/index.d.ts +34 -0
  96. package/templates/dist/writer-core/src/index.js +115 -0
  97. package/templates/dist/writer-core/src/index.js.map +1 -0
  98. package/templates/dist/ztd-cli/templates/src/db/sql-client.d.ts +20 -0
  99. package/templates/dist/ztd-cli/templates/src/db/sql-client.js +3 -0
  100. package/templates/dist/ztd-cli/templates/src/db/sql-client.js.map +1 -0
  101. package/templates/dist/ztd-cli/templates/src/db/sql-client.ts +24 -0
  102. package/templates/dist/ztd-cli/templates/src/repositories/user-accounts.d.ts +36 -0
  103. package/templates/dist/ztd-cli/templates/src/repositories/user-accounts.js +85 -0
  104. package/templates/dist/ztd-cli/templates/src/repositories/user-accounts.js.map +1 -0
  105. package/templates/dist/ztd-cli/templates/tests/generated/ztd-row-map.generated.d.ts +20 -0
  106. package/templates/dist/ztd-cli/templates/tests/generated/ztd-row-map.generated.js +33 -0
  107. package/templates/dist/ztd-cli/templates/tests/generated/ztd-row-map.generated.js.map +1 -0
  108. package/templates/dist/ztd-cli/templates/tests/support/global-setup.d.ts +10 -0
  109. package/templates/dist/ztd-cli/templates/tests/support/global-setup.js +29 -0
  110. package/templates/dist/ztd-cli/templates/tests/support/global-setup.js.map +1 -0
  111. package/templates/dist/ztd-cli/templates/tests/support/testkit-client.d.ts +66 -0
  112. package/templates/dist/ztd-cli/templates/tests/support/testkit-client.js +552 -0
  113. package/templates/dist/ztd-cli/templates/tests/support/testkit-client.js.map +1 -0
  114. package/templates/dist/ztd-cli/templates/tests/user-profiles.test.d.ts +1 -0
  115. package/templates/dist/ztd-cli/templates/tests/user-profiles.test.js +82 -0
  116. package/templates/dist/ztd-cli/templates/tests/user-profiles.test.js.map +1 -0
  117. package/templates/dist/ztd-cli/templates/tests/writer-constraints.test.d.ts +1 -0
  118. package/templates/dist/ztd-cli/templates/tests/writer-constraints.test.js +29 -0
  119. package/templates/dist/ztd-cli/templates/tests/writer-constraints.test.js.map +1 -0
  120. package/templates/dist/ztd-cli/templates/tests/ztd-layout.generated.d.ts +7 -0
  121. package/templates/dist/ztd-cli/templates/tests/ztd-layout.generated.js +10 -0
  122. package/templates/dist/ztd-cli/templates/tests/ztd-layout.generated.js.map +1 -0
  123. package/templates/src/AGENTS.md +26 -0
  124. package/templates/src/catalog/AGENTS.md +37 -0
  125. package/templates/src/catalog/runtime/AGENTS.md +75 -0
  126. package/templates/src/catalog/runtime/_coercions.ts +1 -0
  127. package/templates/src/catalog/runtime/_smoke.runtime.ts +21 -0
  128. package/templates/src/catalog/specs/AGENTS.md +48 -0
  129. package/templates/src/catalog/specs/_smoke.spec.arktype.ts +21 -0
  130. package/templates/src/catalog/specs/_smoke.spec.zod.ts +20 -0
  131. package/templates/src/db/sql-client.ts +5 -5
  132. package/templates/src/jobs/AGENTS.md +26 -0
  133. package/templates/src/jobs/README.md +3 -0
  134. package/templates/src/repositories/AGENTS.md +118 -0
  135. package/templates/src/repositories/tables/AGENTS.md +94 -0
  136. package/templates/src/repositories/tables/README.md +3 -0
  137. package/templates/src/repositories/views/AGENTS.md +25 -0
  138. package/templates/src/repositories/views/README.md +3 -0
  139. package/templates/src/sql/AGENTS.md +77 -0
  140. package/templates/src/sql/README.md +6 -0
  141. package/templates/tests/AGENTS.md +43 -129
  142. package/templates/tests/generated/AGENTS.md +16 -0
  143. package/templates/tests/smoke.test.ts +5 -0
  144. package/templates/tests/smoke.validation.test.ts +34 -0
  145. package/templates/tests/support/AGENTS.md +26 -0
  146. package/templates/tests/support/global-setup.ts +8 -23
  147. package/templates/tests/support/testkit-client.ts +13 -741
  148. package/templates/tests/ztd-layout.generated.ts +0 -2
  149. package/templates/tsconfig.json +9 -3
  150. package/templates/ztd/AGENTS.md +11 -142
  151. package/templates/ztd/README.md +4 -82
  152. package/templates/ztd/ddl/AGENTS.md +34 -0
  153. package/templates/ztd/ddl/demo.sql +74 -0
@@ -0,0 +1,69 @@
1
+ import type { QueryConfig, QueryResult, QueryResultRow } from 'pg';
2
+ import type { FixtureTableDefinition, MissingFixtureStrategy, SqlFormatterOptions, TableDefinitionModel, TableDefinitionRegistry } from 'rawsql-ts';
3
+ export type { TableDefinitionModel } from 'rawsql-ts';
4
+ import type { TableRowsFixture } from '@rawsql-ts/testkit-core';
5
+ import type { DdlFixtureLoaderOptions } from '@rawsql-ts/testkit-core';
6
+ interface SchemaResolutionOptions {
7
+ defaultSchema?: string;
8
+ searchPath?: string[];
9
+ }
10
+ /** Rows-only fixtures authored by pg-testkit consumers. */
11
+ export type PgTableRowsFixture = TableRowsFixture;
12
+ /** Table definitions shared across testkits and derived from DDL. */
13
+ export type PgTableDefinition = TableDefinitionModel;
14
+ /** Snapshot returned after fixture resolution, containing the tables, resolver-generated registry, and the applied fixture names. */
15
+ export interface PgFixtureSnapshot {
16
+ fixtureTables: FixtureTableDefinition[];
17
+ tableDefinitions: TableDefinitionRegistry;
18
+ fixturesApplied: string[];
19
+ }
20
+ /** Resolves fixture overrides and exposes the most recently applied snapshot. */
21
+ export interface PgFixtureProvider {
22
+ resolve(overrides?: TableRowsFixture[]): PgFixtureSnapshot;
23
+ }
24
+ /** Input accepted by driver query helpers (raw SQL or pg `QueryConfig`). */
25
+ export type PgQueryInput = string | QueryConfig<unknown[]>;
26
+ /** Represents a Postgres client that can execute queries during testing. */
27
+ export interface PgQueryable {
28
+ query<T extends QueryResultRow = QueryResultRow>(queryTextOrConfig: PgQueryInput, values?: unknown[]): Promise<QueryResult<T>>;
29
+ end?(): Promise<void> | void;
30
+ release?(): void;
31
+ }
32
+ /**
33
+ * Options accepted when creating a PgTestkit client, including the
34
+ * connection factory and fixture resolution hooks.
35
+ */
36
+ export interface CreatePgTestkitClientOptions extends SchemaResolutionOptions {
37
+ connectionFactory: () => PgQueryable | Promise<PgQueryable>;
38
+ tableDefinitions?: TableDefinitionModel[];
39
+ tableRows?: TableRowsFixture[];
40
+ formatterOptions?: SqlFormatterOptions;
41
+ missingFixtureStrategy?: MissingFixtureStrategy;
42
+ onExecute?(sql: string, params?: unknown[] | QueryConfig['values'], fixtures?: string[]): void;
43
+ /** Directories whose SQL files are converted into fixtures automatically. */
44
+ ddl?: DdlFixtureLoaderOptions;
45
+ }
46
+ /**
47
+ * Options accepted when wrapping an existing Postgres client, mirroring
48
+ * creation options but omitting the connection factory.
49
+ */
50
+ export interface WrapPgClientOptions extends SchemaResolutionOptions {
51
+ tableDefinitions?: TableDefinitionModel[];
52
+ tableRows?: TableRowsFixture[];
53
+ formatterOptions?: SqlFormatterOptions;
54
+ missingFixtureStrategy?: MissingFixtureStrategy;
55
+ onExecute?(sql: string, params?: unknown[] | QueryConfig['values'], fixtures?: string[]): void;
56
+ /** Shared DDL directory configuration used to infer fixtures for wrapped clients. */
57
+ ddl?: DdlFixtureLoaderOptions;
58
+ }
59
+ /** Extends any Pg client instance with fixture-aware helper methods. */
60
+ export type WrappedPgClient<T> = T & {
61
+ withFixtures(fixtures: TableRowsFixture[]): WrappedPgClient<T>;
62
+ };
63
+ export interface CreatePgTestkitPoolOptions extends SchemaResolutionOptions {
64
+ tableDefinitions?: TableDefinitionModel[];
65
+ tableRows?: TableRowsFixture[];
66
+ /** DDL paths that should populate the base fixture set for the pool. */
67
+ ddl?: DdlFixtureLoaderOptions;
68
+ }
69
+ export type { TableRowsFixture } from '@rawsql-ts/testkit-core';
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../drivers/pg-testkit/src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,15 @@
1
+ import type { DdlFixtureLoaderOptions, DdlProcessedFixture, TableRowsFixture } from '@rawsql-ts/testkit-core';
2
+ import type { TableDefinitionModel } from 'rawsql-ts';
3
+ import type { TableNameResolver } from '@rawsql-ts/testkit-core';
4
+ export interface FixtureResolutionOptions {
5
+ tableDefinitions?: TableDefinitionModel[];
6
+ tableRows?: TableRowsFixture[];
7
+ ddl?: DdlFixtureLoaderOptions;
8
+ }
9
+ export interface ResolvedFixtureState {
10
+ ddlFixtures: DdlProcessedFixture[];
11
+ tableDefinitions: TableDefinitionModel[];
12
+ tableRows: TableRowsFixture[];
13
+ }
14
+ /** Produces the merged fixture metadata for any pg-testkit entry point. */
15
+ export declare function resolveFixtureState(options: FixtureResolutionOptions, tableNameResolver: TableNameResolver): ResolvedFixtureState;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveFixtureState = resolveFixtureState;
4
+ const testkit_core_1 = require("@rawsql-ts/testkit-core");
5
+ /** Produces the merged fixture metadata for any pg-testkit entry point. */
6
+ function resolveFixtureState(options, tableNameResolver) {
7
+ var _a, _b, _c, _d, _e;
8
+ // Load DDL-derived fixtures when directories were supplied so every consumer shares the same resolver rules.
9
+ const loader = ((_b = (_a = options.ddl) === null || _a === void 0 ? void 0 : _a.directories) === null || _b === void 0 ? void 0 : _b.length)
10
+ ? new testkit_core_1.DdlFixtureLoader({
11
+ ...options.ddl,
12
+ tableNameResolver,
13
+ })
14
+ : undefined;
15
+ const ddlFixtures = (_c = loader === null || loader === void 0 ? void 0 : loader.getFixtures()) !== null && _c !== void 0 ? _c : [];
16
+ // Combine generated definitions with any explicit metadata the caller provided.
17
+ const tableDefinitions = [
18
+ ...ddlFixtures.map((fixture) => fixture.tableDefinition),
19
+ ...((_d = options.tableDefinitions) !== null && _d !== void 0 ? _d : []),
20
+ ];
21
+ // Merge DDL-sourced rows ahead of caller-supplied fixtures so overrides take precedence.
22
+ const tableRows = [
23
+ ...ddlFixtures.flatMap((fixture) => fixture.rows && fixture.rows.length
24
+ ? [{ tableName: fixture.tableDefinition.name, rows: fixture.rows }]
25
+ : []),
26
+ ...((_e = options.tableRows) !== null && _e !== void 0 ? _e : []),
27
+ ];
28
+ return {
29
+ ddlFixtures,
30
+ tableDefinitions,
31
+ tableRows,
32
+ };
33
+ }
34
+ //# sourceMappingURL=fixtureState.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fixtureState.js","sourceRoot":"","sources":["../../../../../../../drivers/pg-testkit/src/utils/fixtureState.ts"],"names":[],"mappings":";;AAkBA,kDAkCC;AAnDD,0DAA2D;AAgB3D,2EAA2E;AAC3E,SAAgB,mBAAmB,CACjC,OAAiC,EACjC,iBAAoC;;IAEpC,6GAA6G;IAC7G,MAAM,MAAM,GAAG,CAAA,MAAA,MAAA,OAAO,CAAC,GAAG,0CAAE,WAAW,0CAAE,MAAM;QAC7C,CAAC,CAAC,IAAI,+BAAgB,CAAC;YACnB,GAAG,OAAO,CAAC,GAAG;YACd,iBAAiB;SAClB,CAAC;QACJ,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,WAAW,GAAG,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,WAAW,EAAE,mCAAI,EAAE,CAAC;IAEhD,gFAAgF;IAChF,MAAM,gBAAgB,GAAG;QACvB,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC,OAA4B,EAAE,EAAE,CAAC,OAAO,CAAC,eAAe,CAAC;QAC7E,GAAG,CAAC,MAAA,OAAO,CAAC,gBAAgB,mCAAI,EAAE,CAAC;KACpC,CAAC;IAEF,yFAAyF;IACzF,MAAM,SAAS,GAAuB;QACpC,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,OAA4B,EAAE,EAAE,CACtD,OAAO,CAAC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,MAAM;YACjC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC;YACnE,CAAC,CAAC,EAAE,CACP;QACD,GAAG,CAAC,MAAA,OAAO,CAAC,SAAS,mCAAI,EAAE,CAAC;KAC7B,CAAC;IAEF,OAAO;QACL,WAAW;QACX,gBAAgB;QAChB,SAAS;KACV,CAAC;AACJ,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { TableDefinitionModel, TableRowsFixture } from '../types';
2
+ import type { TableNameResolver } from '@rawsql-ts/testkit-core';
3
+ /**
4
+ * Validates fixture rows against table definitions, ensuring every referenced
5
+ * table and column exists in the configured DDL or explicit definitions.
6
+ *
7
+ * @param tableRows - Fixtures to validate; undefined or empty arrays are no-ops.
8
+ * @param tableDefinitions - Table metadata to validate against.
9
+ * @param contextLabel - Optional label prefixed to thrown error messages for clarity.
10
+ * @throws Error when a fixture references a missing table or column.
11
+ */
12
+ export declare const validateFixtureRowsAgainstTableDefinitions: (tableRows: TableRowsFixture[] | undefined, tableDefinitions: TableDefinitionModel[], contextLabel?: string, tableNameResolver?: TableNameResolver) => void;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateFixtureRowsAgainstTableDefinitions = void 0;
4
+ const rawsql_ts_1 = require("rawsql-ts");
5
+ const normalizeColumnName = (value) => value.toLowerCase();
6
+ /**
7
+ * Validates fixture rows against table definitions, ensuring every referenced
8
+ * table and column exists in the configured DDL or explicit definitions.
9
+ *
10
+ * @param tableRows - Fixtures to validate; undefined or empty arrays are no-ops.
11
+ * @param tableDefinitions - Table metadata to validate against.
12
+ * @param contextLabel - Optional label prefixed to thrown error messages for clarity.
13
+ * @throws Error when a fixture references a missing table or column.
14
+ */
15
+ const validateFixtureRowsAgainstTableDefinitions = (tableRows, tableDefinitions, contextLabel, tableNameResolver) => {
16
+ var _a;
17
+ if (!(tableRows === null || tableRows === void 0 ? void 0 : tableRows.length)) {
18
+ return;
19
+ }
20
+ const prefix = contextLabel ? `${contextLabel}: ` : '';
21
+ // Build a lookup keyed by resolved table names so definitions can be resolved quickly.
22
+ const definitionLookup = new Map();
23
+ for (const definition of tableDefinitions) {
24
+ const key = (_a = tableNameResolver === null || tableNameResolver === void 0 ? void 0 : tableNameResolver.resolve(definition.name)) !== null && _a !== void 0 ? _a : (0, rawsql_ts_1.normalizeTableName)(definition.name);
25
+ definitionLookup.set(key, definition);
26
+ }
27
+ const columnCache = new Map();
28
+ for (const fixture of tableRows) {
29
+ const resolvedTableName = tableNameResolver
30
+ ? tableNameResolver.resolve(fixture.tableName, (candidate) => definitionLookup.has(candidate))
31
+ : (0, rawsql_ts_1.normalizeTableName)(fixture.tableName);
32
+ const tableDefinition = definitionLookup.get(resolvedTableName);
33
+ if (!tableDefinition) {
34
+ throw new Error(`${prefix}Table '${fixture.tableName}' is not defined by the configured DDL or explicit table definitions.`);
35
+ }
36
+ // Memoize column metadata for each resolved table to avoid repeated work.
37
+ let columnNames = columnCache.get(resolvedTableName);
38
+ if (!columnNames) {
39
+ columnNames = new Set(tableDefinition.columns.map((column) => normalizeColumnName(column.name)));
40
+ columnCache.set(resolvedTableName, columnNames);
41
+ }
42
+ // Validate each row so mistyped columns surface immediately during setup.
43
+ for (const row of fixture.rows) {
44
+ for (const column of Object.keys(row)) {
45
+ if (!columnNames.has(normalizeColumnName(column))) {
46
+ throw new Error(`${prefix}Column '${column}' not found in table '${fixture.tableName}'. Please fix the fixture to match the DDL.`);
47
+ }
48
+ }
49
+ }
50
+ }
51
+ };
52
+ exports.validateFixtureRowsAgainstTableDefinitions = validateFixtureRowsAgainstTableDefinitions;
53
+ //# sourceMappingURL=fixtureValidation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fixtureValidation.js","sourceRoot":"","sources":["../../../../../../../drivers/pg-testkit/src/utils/fixtureValidation.ts"],"names":[],"mappings":";;;AAAA,yCAA+C;AAI/C,MAAM,mBAAmB,GAAG,CAAC,KAAa,EAAU,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC;AAE3E;;;;;;;;GAQG;AACI,MAAM,0CAA0C,GAAG,CACxD,SAAyC,EACzC,gBAAwC,EACxC,YAAqB,EACrB,iBAAqC,EAC/B,EAAE;;IACR,IAAI,CAAC,CAAA,SAAS,aAAT,SAAS,uBAAT,SAAS,CAAE,MAAM,CAAA,EAAE,CAAC;QACvB,OAAO;IACT,CAAC;IAED,MAAM,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IAEvD,uFAAuF;IACvF,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAgC,CAAC;IACjE,KAAK,MAAM,UAAU,IAAI,gBAAgB,EAAE,CAAC;QAC1C,MAAM,GAAG,GAAG,MAAA,iBAAiB,aAAjB,iBAAiB,uBAAjB,iBAAiB,CAAE,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,mCAAI,IAAA,8BAAkB,EAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC/F,gBAAgB,CAAC,GAAG,CAAC,GAAG,EAAE,UAAU,CAAC,CAAC;IACxC,CAAC;IAED,MAAM,WAAW,GAAG,IAAI,GAAG,EAAuB,CAAC;IAEnD,KAAK,MAAM,OAAO,IAAI,SAAS,EAAE,CAAC;QAChC,MAAM,iBAAiB,GAAG,iBAAiB;YACzC,CAAC,CAAC,iBAAiB,CAAC,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,gBAAgB,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC9F,CAAC,CAAC,IAAA,8BAAkB,EAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAC1C,MAAM,eAAe,GAAG,gBAAgB,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QAChE,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CACb,GAAG,MAAM,UAAU,OAAO,CAAC,SAAS,uEAAuE,CAC5G,CAAC;QACJ,CAAC;QAED,0EAA0E;QAC1E,IAAI,WAAW,GAAG,WAAW,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QACrD,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,WAAW,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,mBAAmB,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACjG,WAAW,CAAC,GAAG,CAAC,iBAAiB,EAAE,WAAW,CAAC,CAAC;QAClD,CAAC;QAED,0EAA0E;QAC1E,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;YAC/B,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBACtC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;oBAClD,MAAM,IAAI,KAAK,CACb,GAAG,MAAM,WAAW,MAAM,yBAAyB,OAAO,CAAC,SAAS,6CAA6C,CAClH,CAAC;gBACJ,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAlDW,QAAA,0CAA0C,8CAkDrD"}
@@ -0,0 +1,160 @@
1
+ /**
2
+ * A single database row returned by a SQL driver.
3
+ * Row keys are SQL column names, which must be strings; symbol keys are not supported.
4
+ */
5
+ export type Row = Record<string, unknown>;
6
+ /**
7
+ * Executes SQL and returns the resulting rows.
8
+ *
9
+ * The mapper keeps this layer DBMS/driver agnostic; callers inject the concrete
10
+ * executor that speaks to the desired database.
11
+ */
12
+ export type QueryExecutor = (sql: string, params: unknown[]) => Promise<Row[]>;
13
+ /**
14
+ * Defines how a column prefix, key, and optional overrides map to an entity.
15
+ */
16
+ export interface EntityOptions<T, K extends Extract<keyof T, string>> {
17
+ name: string;
18
+ key: K;
19
+ prefix?: string;
20
+ columnMap?: Partial<Record<Extract<keyof T, string>, string>>;
21
+ coerce?: boolean;
22
+ coerceFn?: (value: unknown) => unknown;
23
+ }
24
+ /**
25
+ * Describes how a child entity references a parent mapping.
26
+ */
27
+ export interface BelongsToOptions {
28
+ optional?: boolean;
29
+ }
30
+ /**
31
+ * Controls how raw column names are normalized for simple mapping.
32
+ */
33
+ export type KeyTransform = 'snake_to_camel' | 'none' | ((column: string) => string);
34
+ /**
35
+ * Supported type hints for `mapSimpleRows`, controlling how primitive values are coerced before identifier normalization.
36
+ */
37
+ export type SimpleMapTypeHint = 'string' | 'number' | 'boolean' | 'date' | 'bigint';
38
+ /**
39
+ * Options that influence simple (duck-typed) row mapping.
40
+ */
41
+ export interface SimpleMapOptions {
42
+ keyTransform?: KeyTransform;
43
+ idKeysAsString?: boolean;
44
+ typeHints?: Record<string, SimpleMapTypeHint>;
45
+ coerceDates?: boolean;
46
+ coerceFn?: (args: {
47
+ key: string;
48
+ sourceKey: string;
49
+ value: unknown;
50
+ }) => unknown;
51
+ }
52
+ /**
53
+ * Named presets for simple mapping that avoid implicit inference.
54
+ */
55
+ export declare const simpleMapPresets: {
56
+ safe(): SimpleMapOptions;
57
+ pgLike(): SimpleMapOptions;
58
+ };
59
+ type ParentLink<T> = {
60
+ propertyName: string;
61
+ parent: EntityMapping<any>;
62
+ localKey: string;
63
+ optional: boolean;
64
+ };
65
+ type RowContext = {
66
+ row: Row;
67
+ normalizedColumns: Map<string, string>;
68
+ };
69
+ /**
70
+ * Builds an entity mapping that can be consumed by {@link Mapper#query} or {@link mapRows}.
71
+ */
72
+ export declare class EntityMapping<T, K extends Extract<keyof T, string> = Extract<keyof T, string>> {
73
+ readonly name: string;
74
+ readonly key: K;
75
+ readonly prefix: string;
76
+ readonly parents: ParentLink<T>[];
77
+ private readonly columnMap;
78
+ private readonly overrideLookup;
79
+ private readonly prefixNormalized;
80
+ private readonly prefixLength;
81
+ private readonly shouldCoerce;
82
+ private readonly coerceFn;
83
+ constructor(options: EntityOptions<T, K>);
84
+ /**
85
+ * Registers a parent relationship that will be attached after the current row is mapped.
86
+ */
87
+ belongsTo<P, PK extends Extract<keyof P, string>>(propertyName: Extract<keyof T, string>, parent: EntityMapping<P, PK>, localKey: Extract<keyof T, string>, options?: BelongsToOptions): this;
88
+ /**
89
+ * Registers a parent relationship with an explicit local key.
90
+ */
91
+ belongsToWithLocalKey<P, PK extends Extract<keyof P, string>>(propertyName: Extract<keyof T, string>, parent: EntityMapping<P, PK>, localKey: Extract<keyof T, string>): this;
92
+ /**
93
+ * Registers an optional parent relationship with an explicit local key.
94
+ */
95
+ belongsToOptional<P, PK extends Extract<keyof P, string>>(propertyName: Extract<keyof T, string>, parent: EntityMapping<P, PK>, localKey?: Extract<keyof T, string>): this;
96
+ matchColumn(columnName: string): string | undefined;
97
+ resolveColumnName(propertyName: string): string;
98
+ readKeyValue(ctx: RowContext): unknown;
99
+ assignFields(target: Record<string, unknown>, ctx: RowContext): void;
100
+ private normalizeColumnValue;
101
+ }
102
+ /**
103
+ * Creates a new entity mapping from the provided options.
104
+ */
105
+ export declare function entity<T, K extends Extract<keyof T, string> = Extract<keyof T, string>>(options: EntityOptions<T, K>): EntityMapping<T, K>;
106
+ /**
107
+ * Builds a column map by prefixing each property with the provided prefix and
108
+ * converting property names to snake_case.
109
+ */
110
+ export declare function columnMapFromPrefix<K extends string>(prefix: string, properties: readonly K[]): Record<K, string>;
111
+ /**
112
+ * Executes SQL via the provided executor and maps the rows using the supplied mapping.
113
+ */
114
+ export declare class Mapper {
115
+ private readonly executor;
116
+ private readonly defaults;
117
+ constructor(executor: QueryExecutor, defaults?: SimpleMapOptions | undefined);
118
+ query<T>(sql: string, params: unknown[], mapping: EntityMapping<T>): Promise<T[]>;
119
+ query<T>(sql: string, params?: unknown[], options?: SimpleMapOptions): Promise<T[]>;
120
+ queryOne<T>(sql: string, params: unknown[], mapping: EntityMapping<T>): Promise<T | undefined>;
121
+ queryOne<T>(sql: string, params?: unknown[], options?: SimpleMapOptions): Promise<T | undefined>;
122
+ }
123
+ /**
124
+ * This package maps rows and does not manage DB drivers.
125
+ * Inject a query executor rather than wiring connections inside the mapper.
126
+ */
127
+ export declare function createMapper(executor: QueryExecutor, defaults?: SimpleMapOptions): Mapper;
128
+ /**
129
+ * Creates a mapper using the supplied executor and user defaults.
130
+ * This helper is the recommended entry point when wiring an executor because
131
+ * it clearly signals where defaults are configured.
132
+ */
133
+ export declare function createMapperFromExecutor(executor: QueryExecutor, defaults?: SimpleMapOptions): Mapper;
134
+ /**
135
+ * Normalizes an executor returning `{ rows }` so it can be consumed by the mapper.
136
+ */
137
+ export declare function toRowsExecutor(executorOrTarget: ((sql: string, params: unknown[]) => Promise<{
138
+ rows: Row[];
139
+ } | {
140
+ rows: Row[];
141
+ rowCount?: number;
142
+ } | Row[]>) | {
143
+ [key: string]: (...args: unknown[]) => Promise<unknown>;
144
+ }, methodName?: string): QueryExecutor;
145
+ /**
146
+ * Maps a pre-fetched row array into typed objects defined by an entity mapping.
147
+ * Row values remain `unknown`, and the mapper only applies the general-purpose
148
+ * coercion rules declared in `coerceColumnValue`.
149
+ */
150
+ export declare function mapRows<T>(rows: Row[], mapping: EntityMapping<T>): T[];
151
+ /**
152
+ * Maps pre-fetched rows into typed DTOs using the simple map preset, honoring key transforms, type hints, and optional coercion settings.
153
+ *
154
+ * @template T Target DTO shape.
155
+ * @param rows Rows produced by the SQL executor.
156
+ * @param options Optional overrides that control key normalization, coercion, and type hints.
157
+ * @returns An array of `T` instances synthesized from `rows`.
158
+ */
159
+ export declare function mapSimpleRows<T>(rows: Row[], options?: SimpleMapOptions): T[];
160
+ export {};