@ftschopp/dynatable-core 1.0.0 → 1.2.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 (85) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +528 -2
  3. package/dist/builders/delete/types.d.ts +6 -1
  4. package/dist/builders/delete/types.d.ts.map +1 -1
  5. package/dist/builders/get/types.d.ts +6 -1
  6. package/dist/builders/get/types.d.ts.map +1 -1
  7. package/dist/builders/put/types.d.ts +6 -1
  8. package/dist/builders/put/types.d.ts.map +1 -1
  9. package/dist/builders/query/create-query-builder.d.ts +1 -1
  10. package/dist/builders/query/create-query-builder.d.ts.map +1 -1
  11. package/dist/builders/query/types.d.ts +9 -10
  12. package/dist/builders/query/types.d.ts.map +1 -1
  13. package/dist/builders/transact-write/create-transact-write-builder.d.ts.map +1 -1
  14. package/dist/builders/transact-write/create-transact-write-builder.js +1 -1
  15. package/dist/builders/transact-write/types.d.ts +33 -6
  16. package/dist/builders/transact-write/types.d.ts.map +1 -1
  17. package/dist/builders/update/create-update-builder.d.ts.map +1 -1
  18. package/dist/builders/update/create-update-builder.js +30 -9
  19. package/dist/builders/update/types.d.ts +8 -2
  20. package/dist/builders/update/types.d.ts.map +1 -1
  21. package/dist/core/types.d.ts +96 -11
  22. package/dist/core/types.d.ts.map +1 -1
  23. package/dist/entity/create-entity-api.d.ts +15 -0
  24. package/dist/entity/create-entity-api.d.ts.map +1 -0
  25. package/dist/entity/create-entity-api.js +124 -0
  26. package/dist/entity/index.d.ts +12 -0
  27. package/dist/entity/index.d.ts.map +1 -0
  28. package/dist/entity/index.js +18 -0
  29. package/dist/entity/middleware/factories.d.ts +6 -0
  30. package/dist/entity/middleware/factories.d.ts.map +1 -0
  31. package/dist/entity/middleware/factories.js +15 -0
  32. package/dist/entity/middleware/types.d.ts +8 -0
  33. package/dist/entity/middleware/types.d.ts.map +1 -0
  34. package/dist/entity/middleware/types.js +2 -0
  35. package/dist/entity/middleware/with-middleware.d.ts +8 -0
  36. package/dist/entity/middleware/with-middleware.d.ts.map +1 -0
  37. package/dist/entity/middleware/with-middleware.js +29 -0
  38. package/dist/{entity.d.ts → entity/types.d.ts} +6 -17
  39. package/dist/entity/types.d.ts.map +1 -0
  40. package/dist/entity/types.js +2 -0
  41. package/dist/entity/validation/key-validation.d.ts +7 -0
  42. package/dist/entity/validation/key-validation.d.ts.map +1 -0
  43. package/dist/entity/validation/key-validation.js +25 -0
  44. package/dist/index.d.ts +1 -1
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/table.d.ts +8 -1
  47. package/dist/table.d.ts.map +1 -1
  48. package/dist/table.js +1 -0
  49. package/dist/utils/model-utils.d.ts +7 -1
  50. package/dist/utils/model-utils.d.ts.map +1 -1
  51. package/dist/utils/model-utils.js +32 -3
  52. package/dist/utils/zod-utils.d.ts +3 -2
  53. package/dist/utils/zod-utils.d.ts.map +1 -1
  54. package/dist/utils/zod-utils.js +33 -11
  55. package/package.json +3 -2
  56. package/src/builders/README.md +68 -1
  57. package/src/builders/delete/types.ts +7 -1
  58. package/src/builders/get/types.ts +7 -1
  59. package/src/builders/put/types.ts +7 -1
  60. package/src/builders/query/create-query-builder.ts +4 -6
  61. package/src/builders/query/types.ts +9 -12
  62. package/src/builders/transact-write/README.md +37 -1
  63. package/src/builders/transact-write/create-transact-write-builder.ts +20 -14
  64. package/src/builders/transact-write/types.ts +44 -6
  65. package/src/builders/update/create-update-builder.test.ts +43 -0
  66. package/src/builders/update/create-update-builder.ts +47 -9
  67. package/src/builders/update/types.ts +9 -2
  68. package/src/core/types.test.ts +137 -0
  69. package/src/core/types.ts +97 -20
  70. package/src/entity/create-entity-api.ts +212 -0
  71. package/src/entity/index.ts +19 -0
  72. package/src/entity/middleware/factories.ts +15 -0
  73. package/src/entity/middleware/types.ts +7 -0
  74. package/src/entity/middleware/with-middleware.ts +37 -0
  75. package/src/entity/types.ts +79 -0
  76. package/src/entity/validation/key-validation.ts +34 -0
  77. package/src/index.ts +1 -0
  78. package/src/table.ts +10 -3
  79. package/src/utils/model-utils.test.ts +131 -1
  80. package/src/utils/model-utils.ts +35 -2
  81. package/src/utils/zod-utils.test.ts +97 -2
  82. package/src/utils/zod-utils.ts +31 -12
  83. package/dist/entity.d.ts.map +0 -1
  84. package/dist/entity.js +0 -161
  85. package/src/entity.ts +0 -337
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCleanKeysMiddleware = createCleanKeysMiddleware;
4
+ const model_utils_1 = require("../../utils/model-utils");
5
+ /**
6
+ * Creates middleware configuration for cleaning internal keys
7
+ */
8
+ function createCleanKeysMiddleware(shouldClean) {
9
+ if (!shouldClean) {
10
+ return {};
11
+ }
12
+ return {
13
+ after: (result) => (0, model_utils_1.stripInternalKeys)(result),
14
+ };
15
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Middleware hooks for builder execution
3
+ */
4
+ export type ExecutionMiddleware<T = any> = {
5
+ before?: () => void | Promise<void>;
6
+ after?: (result: T) => T | Promise<T>;
7
+ };
8
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/entity/middleware/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,mBAAmB,CAAC,CAAC,GAAG,GAAG,IAAI;IACzC,MAAM,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACvC,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { ExecutionMiddleware } from './types';
2
+ /**
3
+ * Applies middleware hooks to a builder's execute method
4
+ */
5
+ export declare function withMiddleware<B extends {
6
+ execute: () => Promise<any>;
7
+ }>(builder: B, middleware: ExecutionMiddleware): B;
8
+ //# sourceMappingURL=with-middleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-middleware.d.ts","sourceRoot":"","sources":["../../../src/entity/middleware/with-middleware.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAE9C;;GAEG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS;IAAE,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,CAAA;CAAE,EACtE,OAAO,EAAE,CAAC,EACV,UAAU,EAAE,mBAAmB,GAC9B,CAAC,CA2BH"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withMiddleware = withMiddleware;
4
+ /**
5
+ * Applies middleware hooks to a builder's execute method
6
+ */
7
+ function withMiddleware(builder, middleware) {
8
+ const { before, after } = middleware;
9
+ // If no middleware hooks provided, return builder as-is
10
+ if (!before && !after) {
11
+ return builder;
12
+ }
13
+ return {
14
+ ...builder,
15
+ execute: async () => {
16
+ // Before execution hook
17
+ if (before) {
18
+ await before();
19
+ }
20
+ // Execute original builder
21
+ const result = await builder.execute();
22
+ // After execution hook
23
+ if (after) {
24
+ return await after(result);
25
+ }
26
+ return result;
27
+ },
28
+ };
29
+ }
@@ -1,18 +1,17 @@
1
- import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
2
- import { InferInput, InferKeyInput, InferModel, ModelDefinition } from './core/types';
3
- import { GetBuilder, PutBuilder, QueryBuilder, UpdateBuilder, DeleteBuilder, ScanBuilder, BatchGetBuilder, BatchWriteBuilder } from './builders';
4
- import { DynamoDBLogger } from './utils/dynamodb-logger';
1
+ import { DynamoDBLogger } from '../utils/dynamodb-logger';
2
+ import { GetBuilder, PutBuilder, QueryBuilder, UpdateBuilder, DeleteBuilder, ScanBuilder, BatchGetBuilder, BatchWriteBuilder } from '../builders';
5
3
  /**
6
4
  * Options for creating the Entity API
7
5
  */
8
6
  export type EntityAPIOptions = {
9
7
  logger?: DynamoDBLogger;
10
8
  timestamps?: boolean;
9
+ cleanInternalKeys?: boolean;
11
10
  };
12
11
  /**
13
12
  * Entity API interface for a model
14
13
  */
15
- export type EntityAPI<Model, Input, KeyInput, ModelDef extends ModelDefinition = any> = {
14
+ export type EntityAPI<Model, Input, KeyInput> = {
16
15
  /**
17
16
  * Retrieves an item by its key.
18
17
  * @param key - Partial or full key object to identify the item
@@ -29,7 +28,7 @@ export type EntityAPI<Model, Input, KeyInput, ModelDef extends ModelDefinition =
29
28
  * Queries items using key conditions.
30
29
  * @returns QueryBuilder for building and executing the query
31
30
  */
32
- query: () => QueryBuilder<Model, ModelDef>;
31
+ query: () => QueryBuilder<Model>;
33
32
  /**
34
33
  * Scans the entire table or index without key conditions.
35
34
  * @returns ScanBuilder for building and executing the scan
@@ -60,14 +59,4 @@ export type EntityAPI<Model, Input, KeyInput, ModelDef extends ModelDefinition =
60
59
  */
61
60
  batchWrite: (items: Input[]) => BatchWriteBuilder;
62
61
  };
63
- /**
64
- * Creates an entity API instance with validation, key resolution, and builder creation.
65
- *
66
- * @param modelName - The name of the model/entity
67
- * @param model - The model definition
68
- * @param client - DynamoDB client instance
69
- * @param options - Optional configuration (logger, timestamps)
70
- * @returns EntityAPI with get and put methods
71
- */
72
- export declare const createEntityAPI: <Model extends ModelDefinition>(tableName: string, modelName: string, model: Model, client: DynamoDBClient, options?: EntityAPIOptions) => EntityAPI<InferModel<Model>, InferInput<Model>, InferKeyInput<Model>, Model>;
73
- //# sourceMappingURL=entity.d.ts.map
62
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/entity/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EACL,UAAU,EACV,UAAU,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,WAAW,EACX,eAAe,EACf,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,IAAI;IAC9C;;;;OAIG;IACH,GAAG,EAAE,CAAC,GAAG,EAAE,QAAQ,KAAK,UAAU,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAEpD;;;;OAIG;IACH,GAAG,EAAE,CAAC,IAAI,EAAE,KAAK,KAAK,UAAU,CAAC,KAAK,CAAC,CAAC;IAExC;;;OAGG;IACH,KAAK,EAAE,MAAM,YAAY,CAAC,KAAK,CAAC,CAAC;IAEjC;;;OAGG;IACH,IAAI,EAAE,MAAM,WAAW,CAAC,KAAK,CAAC,CAAC;IAE/B;;;;OAIG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,QAAQ,KAAK,aAAa,CAAC,KAAK,CAAC,CAAC;IAEhD;;;;OAIG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,QAAQ,KAAK,aAAa,CAAC,KAAK,CAAC,CAAC;IAEhD;;;;OAIG;IACH,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,eAAe,CAAC,KAAK,CAAC,CAAC;IAEvD;;;;OAIG;IACH,UAAU,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,iBAAiB,CAAC;CACnD,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ import { ModelDefinition } from '../../core/types';
2
+ /**
3
+ * Validates that all required key fields are present in the key object
4
+ * @throws Error if any required key fields are missing
5
+ */
6
+ export declare function validateKeyFields(modelName: string, model: ModelDefinition, key: Record<string, unknown>, operation?: string): void;
7
+ //# sourceMappingURL=key-validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-validation.d.ts","sourceRoot":"","sources":["../../../src/entity/validation/key-validation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG/C;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,eAAe,EACtB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,SAAS,CAAC,EAAE,MAAM,GACjB,IAAI,CAmBN"}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.validateKeyFields = validateKeyFields;
5
+ const model_utils_1 = require("../../utils/model-utils");
6
+ /**
7
+ * Validates that all required key fields are present in the key object
8
+ * @throws Error if any required key fields are missing
9
+ */
10
+ function validateKeyFields(modelName, model, key, operation) {
11
+ // Extract required fields from key templates
12
+ const requiredFields = new Set();
13
+ if (model.key) {
14
+ for (const keyDef of Object.values(model.key)) {
15
+ (0, model_utils_1.extractTemplateVars)(keyDef.value).forEach((field) => requiredFields.add(field));
16
+ }
17
+ }
18
+ // Check if all required fields are present
19
+ const missingFields = Array.from(requiredFields).filter((field) => key[field] === undefined);
20
+ if (missingFields.length > 0) {
21
+ const operationSuffix = operation ? ` for ${operation}` : '';
22
+ throw new Error(`[${modelName}] Missing required key field(s)${operationSuffix}: ${missingFields.join(', ')}. ` +
23
+ `Required fields: ${Array.from(requiredFields).join(', ')}`);
24
+ }
25
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './table';
2
- export { type SchemaDefinition, type ModelDefinition, type PrimaryKeyDefinition, type KeyDefinition, type AttributeDefinition, type IndexDefinition, type IndexesDefinition, type SchemaParams, type InferInput, type InferModel, type InferKeyInput, type InferModelFromSchema, type InferInputFromSchema, type TimestampFields, } from './core/types';
2
+ export { type SchemaDefinition, type ModelDefinition, type PrimaryKeyDefinition, type KeyDefinition, type AttributeDefinition, type IndexDefinition, type IndexesDefinition, type SchemaParams, type InferInput, type InferModel, type InferKeyInput, type InferModelFromSchema, type InferInputFromSchema, type TimestampFields, type ArrayItem, } from './core/types';
3
3
  export { createDynamoDBLogger, type DynamoDBLogger, type DynamoDBLoggerConfig, } from './utils/dynamodb-logger';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,EACzB,KAAK,eAAe,GACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,oBAAoB,GAC1B,MAAM,yBAAyB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,KAAK,eAAe,EACpB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,SAAS,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,oBAAoB,EACpB,KAAK,cAAc,EACnB,KAAK,oBAAoB,GAC1B,MAAM,yBAAyB,CAAC"}
package/dist/table.d.ts CHANGED
@@ -17,11 +17,18 @@ export type TableConfig<S extends SchemaDefinition> = {
17
17
  /** The schema definition for all models in the table */
18
18
  schema: S;
19
19
  };
20
+ /**
21
+ * Helper type to force TypeScript to expand/simplify a type.
22
+ * This helps with type inference in callbacks by making the type "concrete".
23
+ */
24
+ type Simplify<T> = {
25
+ [K in keyof T]: T[K];
26
+ } & {};
20
27
  /**
21
28
  * Internal helper type to infer all entity APIs from schema definition
22
29
  */
23
30
  type EntityMap<S extends SchemaDefinition> = {
24
- [K in keyof S['models']]: EntityAPI<InferModelFromSchema<S, K>, InferInputFromSchema<S, K>, InferKeyInput<S['models'][K]>>;
31
+ [K in keyof S['models']]: EntityAPI<Simplify<InferModelFromSchema<S, K>>, Simplify<InferInputFromSchema<S, K>>, Simplify<InferKeyInput<S['models'][K]>>>;
25
32
  };
26
33
  /**
27
34
  * Represents a typed DynamoDB Table with entity APIs
@@ -1 +1 @@
1
- {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,oBAAoB,EACpB,gBAAgB,EACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAmB,SAAS,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAA8B,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAC7F,OAAO,EAA4B,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,gBAAgB,IAAI;IACpD,8BAA8B;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,mCAAmC;IACnC,MAAM,EAAE,cAAc,CAAC;IACvB,uDAAuD;IACvD,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,wDAAwD;IACxD,MAAM,EAAE,CAAC,CAAC;CACX,CAAC;AAEF;;GAEG;AACH,KAAK,SAAS,CAAC,CAAC,SAAS,gBAAgB,IAAI;KAC1C,CAAC,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,SAAS,CACjC,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,EAC1B,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,EAC1B,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAC9B;CACF,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,KAAK,CAAC,CAAC,SAAS,gBAAgB;IAC3C,4BAA4B;IAC5B,SAAgB,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;IAEvC,sBAAsB;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;gBAE5B,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IAsBlC;;;;;;;;;;;;;OAaG;IACH,aAAa,IAAI,oBAAoB;IAIrC;;;;;;;;;;;;;OAaG;IACH,WAAW,IAAI,kBAAkB;CAGlC"}
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,oBAAoB,EACpB,gBAAgB,EACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAmB,SAAS,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAA8B,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAC7F,OAAO,EAA4B,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AACvF,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD;;GAEG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,gBAAgB,IAAI;IACpD,8BAA8B;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,mCAAmC;IACnC,MAAM,EAAE,cAAc,CAAC;IACvB,uDAAuD;IACvD,MAAM,CAAC,EAAE,cAAc,CAAC;IACxB,wDAAwD;IACxD,MAAM,EAAE,CAAC,CAAC;CACX,CAAC;AAEF;;;GAGG;AACH,KAAK,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAEjD;;GAEG;AACH,KAAK,SAAS,CAAC,CAAC,SAAS,gBAAgB,IAAI;KAC1C,CAAC,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,GAAG,SAAS,CACjC,QAAQ,CAAC,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EACpC,QAAQ,CAAC,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EACpC,QAAQ,CAAC,aAAa,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CACxC;CACF,CAAC;AAEF;;;;;GAKG;AACH,qBAAa,KAAK,CAAC,CAAC,SAAS,gBAAgB;IAC3C,4BAA4B;IAC5B,SAAgB,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;IAEvC,sBAAsB;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;gBAE5B,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IAuBlC;;;;;;;;;;;;;OAaG;IACH,aAAa,IAAI,oBAAoB;IAIrC;;;;;;;;;;;;;OAaG;IACH,WAAW,IAAI,kBAAkB;CAGlC"}
package/dist/table.js CHANGED
@@ -27,6 +27,7 @@ class Table {
27
27
  rawEntities[modelName] = (0, entity_1.createEntityAPI)(tableName, modelName, model, client, {
28
28
  logger,
29
29
  timestamps: schema.params?.timestamps ?? false,
30
+ cleanInternalKeys: schema.params?.cleanInternalKeys ?? false,
30
31
  });
31
32
  }
32
33
  this.entities = rawEntities;
@@ -1,4 +1,4 @@
1
- import { InferModel, ModelDefinition } from '@/core/types';
1
+ import { InferModel, ModelDefinition } from '../core/types';
2
2
  /**
3
3
  * Extracts all variable names from a template string
4
4
  * @param template - Template string with ${variable} syntax
@@ -21,4 +21,10 @@ export declare const applyPostDefaults: <M extends ModelDefinition>(model: M, va
21
21
  isUpdate?: boolean;
22
22
  timestamps?: boolean;
23
23
  }) => InferModel<M>;
24
+ /**
25
+ * Removes internal DynamoDB keys from an item or array of items
26
+ * @param data - Single item or array of items from DynamoDB
27
+ * @returns Data with internal keys removed
28
+ */
29
+ export declare const stripInternalKeys: <T>(data: T | T[] | undefined) => T | T[] | undefined;
24
30
  //# sourceMappingURL=model-utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"model-utils.d.ts","sourceRoot":"","sources":["../../src/utils/model-utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAiB,eAAe,EAAE,MAAM,cAAc,CAAC;AAG1E;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,MAAM,EAG5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAG,MAoB7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,eAAe,EACnD,OAAO,CAAC,EACR,OAAO,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,OAAM,KAAK,GAAG,OAAO,GAAG,MAAc,KACrC,MAAM,CAAC,MAAM,EAAE,MAAM,CAWvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,EACzD,OAAO,CAAC,EACR,eAAe,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAClC,UAAU;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,KACrD,UAAU,CAAC,CAAC,CAgCd,CAAC"}
1
+ {"version":3,"file":"model-utils.d.ts","sourceRoot":"","sources":["../../src/utils/model-utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAiB,eAAe,EAAE,MAAM,cAAc,CAAC;AAG1E;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAAI,UAAU,MAAM,KAAG,MAAM,EAG5D,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,eAAe,GAAI,UAAU,MAAM,EAAE,MAAM,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAG,MAoB7E,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,eAAe,EACnD,OAAO,CAAC,EACR,OAAO,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC1B,OAAM,KAAK,GAAG,OAAO,GAAG,MAAc,KACrC,MAAM,CAAC,MAAM,EAAE,MAAM,CAWvB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,EACzD,OAAO,CAAC,EACR,eAAe,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAClC,UAAU;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAA;CAAE,KACrD,UAAU,CAAC,CAAC,CAiCd,CAAC;AAOF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,SAAS,KAAG,CAAC,GAAG,CAAC,EAAE,GAAG,SAoB1E,CAAC"}
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.applyPostDefaults = exports.resolveKeys = exports.resolveTemplate = exports.extractTemplateVars = void 0;
4
+ exports.stripInternalKeys = exports.applyPostDefaults = exports.resolveKeys = exports.resolveTemplate = exports.extractTemplateVars = void 0;
5
5
  const ulid_1 = require("ulid");
6
6
  /**
7
7
  * Extracts all variable names from a template string
@@ -60,10 +60,11 @@ const applyPostDefaults = (model, validatedItem, options) => {
60
60
  const result = { ...validatedItem };
61
61
  for (const [key, attr] of Object.entries(model.attributes)) {
62
62
  if (result[key] === undefined) {
63
- if (attr.generate === 'ulid') {
63
+ const generate = 'generate' in attr ? attr.generate : undefined;
64
+ if (generate === 'ulid') {
64
65
  result[key] = (0, ulid_1.ulid)();
65
66
  }
66
- else if (attr.generate === 'uuid') {
67
+ else if (generate === 'uuid') {
67
68
  result[key] = crypto.randomUUID();
68
69
  }
69
70
  else if (attr.default !== undefined) {
@@ -86,3 +87,31 @@ const applyPostDefaults = (model, validatedItem, options) => {
86
87
  return result;
87
88
  };
88
89
  exports.applyPostDefaults = applyPostDefaults;
90
+ /**
91
+ * Internal DynamoDB keys that should be stripped when cleanInternalKeys is enabled
92
+ */
93
+ const INTERNAL_KEYS = ['PK', 'SK', '_type'];
94
+ /**
95
+ * Removes internal DynamoDB keys from an item or array of items
96
+ * @param data - Single item or array of items from DynamoDB
97
+ * @returns Data with internal keys removed
98
+ */
99
+ const stripInternalKeys = (data) => {
100
+ if (data === undefined || data === null) {
101
+ return data;
102
+ }
103
+ if (Array.isArray(data)) {
104
+ return data.map((item) => (0, exports.stripInternalKeys)(item));
105
+ }
106
+ if (typeof data === 'object') {
107
+ const cleaned = {};
108
+ for (const [key, value] of Object.entries(data)) {
109
+ if (!INTERNAL_KEYS.includes(key)) {
110
+ cleaned[key] = value;
111
+ }
112
+ }
113
+ return cleaned;
114
+ }
115
+ return data;
116
+ };
117
+ exports.stripInternalKeys = stripInternalKeys;
@@ -1,7 +1,8 @@
1
- import { AttributeDefinition, ModelDefinition } from '@/core/types';
1
+ import { AttributeDefinition, ModelDefinition } from '../core/types';
2
2
  import { ZodObject, ZodType } from 'zod';
3
3
  /**
4
- * Converts an attribute definition to a Zod schema type
4
+ * Converts an attribute definition to a Zod schema type.
5
+ * Supports scalars, nested objects (with schema), and arrays (with items).
5
6
  */
6
7
  export declare const typeToZod: (attr: AttributeDefinition) => ZodType;
7
8
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"zod-utils.d.ts","sourceRoot":"","sources":["../../src/utils/zod-utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAK,SAAS,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAE5C;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,mBAAmB,KAAG,OAarD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,eAAe,KAAG,SAAS,CAAC,GAAG,CAMhE,CAAC"}
1
+ {"version":3,"file":"zod-utils.d.ts","sourceRoot":"","sources":["../../src/utils/zod-utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAK,SAAS,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAE5C;;;GAGG;AACH,eAAO,MAAM,SAAS,GAAI,MAAM,mBAAmB,KAAG,OA+BrD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,UAAU,GAAI,OAAO,eAAe,KAAG,SAAS,CAAC,GAAG,CAMhE,CAAC"}
@@ -5,18 +5,40 @@ Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.modelToZod = exports.typeToZod = void 0;
6
6
  const zod_1 = require("zod");
7
7
  /**
8
- * Converts an attribute definition to a Zod schema type
8
+ * Converts an attribute definition to a Zod schema type.
9
+ * Supports scalars, nested objects (with schema), and arrays (with items).
9
10
  */
10
11
  const typeToZod = (attr) => {
11
- const zodType = attr.type === String
12
- ? zod_1.z.string()
13
- : attr.type === Number
14
- ? zod_1.z.number()
15
- : attr.type === Boolean
16
- ? zod_1.z.boolean()
17
- : attr.type === Date
18
- ? zod_1.z.date()
19
- : zod_1.z.unknown();
12
+ let zodType;
13
+ if (attr.type === Object) {
14
+ const schema = attr.schema;
15
+ if (schema) {
16
+ const shape = {};
17
+ for (const [key, nestedAttr] of Object.entries(schema)) {
18
+ shape[key] = (0, exports.typeToZod)(nestedAttr);
19
+ }
20
+ zodType = zod_1.z.looseObject(shape);
21
+ }
22
+ else {
23
+ zodType = zod_1.z.record(zod_1.z.string(), zod_1.z.unknown());
24
+ }
25
+ }
26
+ else if (attr.type === Array) {
27
+ const items = attr.items;
28
+ zodType = items ? zod_1.z.array((0, exports.typeToZod)(items)) : zod_1.z.array(zod_1.z.unknown());
29
+ }
30
+ else {
31
+ zodType =
32
+ attr.type === String
33
+ ? zod_1.z.string()
34
+ : attr.type === Number
35
+ ? zod_1.z.number()
36
+ : attr.type === Boolean
37
+ ? zod_1.z.boolean()
38
+ : attr.type === Date
39
+ ? zod_1.z.date()
40
+ : zod_1.z.unknown();
41
+ }
20
42
  return attr.required ? zodType : zodType.optional();
21
43
  };
22
44
  exports.typeToZod = typeToZod;
@@ -30,6 +52,6 @@ const modelToZod = (model) => {
30
52
  for (const [key, attr] of Object.entries(model.attributes)) {
31
53
  shape[key] = (0, exports.typeToZod)(attr);
32
54
  }
33
- return zod_1.z.object(shape).passthrough();
55
+ return zod_1.z.looseObject(shape);
34
56
  };
35
57
  exports.modelToZod = modelToZod;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ftschopp/dynatable-core",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Core library for DynamoDB single table design",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "access": "public"
9
9
  },
10
10
  "scripts": {
11
- "build": "tsc",
11
+ "build": "tsc && tsc-alias",
12
12
  "clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
13
13
  "test": "jest"
14
14
  },
@@ -22,6 +22,7 @@
22
22
  "jest": "^30.2.0",
23
23
  "ts-jest": "^29.4.6",
24
24
  "ts-node": "^10.9.2",
25
+ "tsc-alias": "^1.8.16",
25
26
  "typescript": "5.9.3"
26
27
  },
27
28
  "dependencies": {
@@ -209,7 +209,33 @@ The `op` parameter in `.where()` provides these operators:
209
209
  - `op.gt(attr, value)` - Greater than (>)
210
210
  - `op.gte(attr, value)` - Greater than or equal (>=)
211
211
  - `op.between(attr, low, high)` - Between two values
212
- - `op.beginsWith(attr, prefix)` - Begins with a string (for string fields)
212
+
213
+ **String Operators:**
214
+
215
+ - `op.beginsWith(attr, prefix)` - Begins with a string prefix (for string/binary fields)
216
+ - `op.contains(attr, value)` - Contains a substring or value (works with strings, sets, and lists)
217
+
218
+ **Existence Operators:**
219
+
220
+ - `op.exists(attr)` - Attribute exists
221
+ - `op.notExists(attr)` - Attribute does not exist
222
+
223
+ **Type Checking:**
224
+
225
+ - `op.attributeType(attr, type)` - Check the attribute's type
226
+ - Valid types: `'S'` (String), `'N'` (Number), `'B'` (Binary), `'SS'` (String Set), `'NS'` (Number Set), `'BS'` (Binary Set), `'M'` (Map), `'L'` (List), `'NULL'`, `'BOOL'`
227
+
228
+ **Advanced Operators:**
229
+
230
+ - `op.in(attr, values[])` - Attribute value is in the provided array
231
+ - `op.size(attr)` - Get the size of an attribute (string length, number of elements in set/list, etc.)
232
+ - Returns a `SizeRef` object with comparison methods:
233
+ - `.eq(n)` - Size equals n
234
+ - `.ne(n)` - Size not equals n
235
+ - `.lt(n)` - Size less than n
236
+ - `.lte(n)` - Size less than or equal to n
237
+ - `.gt(n)` - Size greater than n
238
+ - `.gte(n)` - Size greater than or equal to n
213
239
 
214
240
  **Logical Operators:**
215
241
 
@@ -257,6 +283,47 @@ The `op` parameter in `.where()` provides these operators:
257
283
  // NOT operator
258
284
  .where((attr, op) => op.not(op.eq(attr.status, 'deleted')))
259
285
 
286
+ // Existence operators
287
+ .where((attr, op) => op.and(
288
+ op.eq(attr.username, 'alice'),
289
+ op.exists(attr.email) // Has email field
290
+ ))
291
+
292
+ .where((attr, op) => op.notExists(attr.deletedAt)) // Not deleted
293
+
294
+ // Contains operator (for strings, sets, lists)
295
+ .where((attr, op) => op.and(
296
+ op.eq(attr.username, 'alice'),
297
+ op.contains(attr.bio, 'developer') // Bio contains "developer"
298
+ ))
299
+
300
+ .where((attr, op) => op.contains(attr.tags, 'premium')) // Has "premium" in tags set/list
301
+
302
+ // IN operator
303
+ .where((attr, op) => op.in(attr.status, ['active', 'pending', 'verified']))
304
+
305
+ // Size operator
306
+ .where((attr, op) => op.size(attr.tags).gte(3)) // At least 3 tags
307
+ .where((attr, op) => op.size(attr.username).lt(20)) // Username shorter than 20 chars
308
+ .where((attr, op) => op.size(attr.comments).eq(0)) // No comments
309
+
310
+ // Attribute type checking
311
+ .where((attr, op) => op.attributeType(attr.metadata, 'M')) // Is a Map
312
+ .where((attr, op) => op.attributeType(attr.items, 'L')) // Is a List
313
+
314
+ // Complex example with new operators
315
+ await table.entities.User.query()
316
+ .where((attr, op) => op.and(
317
+ op.eq(attr.status, 'active'),
318
+ op.exists(attr.email),
319
+ op.size(attr.followers).gte(10),
320
+ op.or(
321
+ op.contains(attr.tags, 'premium'),
322
+ op.contains(attr.tags, 'verified')
323
+ )
324
+ ))
325
+ .execute();
326
+
260
327
  // Complete example with all features
261
328
  await table.entities.Photo.query()
262
329
  .where((attr, op) => op.and(
@@ -1,11 +1,17 @@
1
+ import type { DeleteCommandInput } from '@aws-sdk/lib-dynamodb';
1
2
  import { OperationBuilder } from '../shared';
2
3
 
3
4
  /**
4
5
  * Builder interface for DynamoDB DeleteItem operations
5
6
  */
6
- export interface DeleteBuilder<Model> extends OperationBuilder<Model> {
7
+ export interface DeleteBuilder<Model> extends Omit<OperationBuilder<Model>, 'dbParams'> {
7
8
  /**
8
9
  * Configures what values should be returned after the delete operation
9
10
  */
10
11
  returning(mode: 'NONE' | 'ALL_OLD'): DeleteBuilder<Model>;
12
+
13
+ /**
14
+ * Converts the builder state to DynamoDB DeleteItem parameters
15
+ */
16
+ dbParams(): DeleteCommandInput;
11
17
  }
@@ -1,10 +1,11 @@
1
+ import type { GetCommandInput } from '@aws-sdk/lib-dynamodb';
1
2
  import { ExecutableBuilder } from '../shared';
2
3
 
3
4
  /**
4
5
  * Builder for DynamoDB GetItem operations.
5
6
  * Supports projection, consistent read, and inspection of the final parameters.
6
7
  */
7
- export interface GetBuilder<KeyInput, Model> extends ExecutableBuilder<Model | undefined> {
8
+ export interface GetBuilder<KeyInput, Model> extends Omit<ExecutableBuilder<Model | undefined>, 'dbParams'> {
8
9
  /**
9
10
  * Adds a projection expression to only return specific attributes.
10
11
  * Returns a new immutable builder.
@@ -27,4 +28,9 @@ export interface GetBuilder<KeyInput, Model> extends ExecutableBuilder<Model | u
27
28
  * - NONE: No consumed capacity data returned (default)
28
29
  */
29
30
  returnConsumedCapacity(mode: 'INDEXES' | 'TOTAL' | 'NONE'): GetBuilder<KeyInput, Model>;
31
+
32
+ /**
33
+ * Converts the builder state to DynamoDB GetItem parameters
34
+ */
35
+ dbParams(): GetCommandInput;
30
36
  }
@@ -1,9 +1,10 @@
1
+ import type { PutCommandInput } from '@aws-sdk/lib-dynamodb';
1
2
  import { OperationBuilder } from '../shared';
2
3
 
3
4
  /**
4
5
  * Builder interface for DynamoDB PutItem operations
5
6
  */
6
- export interface PutBuilder<Model> extends OperationBuilder<Model> {
7
+ export interface PutBuilder<Model> extends Omit<OperationBuilder<Model>, 'dbParams'> {
7
8
  /**
8
9
  * Adds a condition that the item must not exist (checks pk and sk)
9
10
  */
@@ -15,4 +16,9 @@ export interface PutBuilder<Model> extends OperationBuilder<Model> {
15
16
  * - ALL_OLD: Returns the item as it was before being replaced (if it existed)
16
17
  */
17
18
  returning(mode: 'NONE' | 'ALL_OLD'): PutBuilder<Model>;
19
+
20
+ /**
21
+ * Converts the builder state to DynamoDB PutItem parameters
22
+ */
23
+ dbParams(): PutCommandInput;
18
24
  }
@@ -181,9 +181,7 @@ function buildSimpleAndExpression(conditions: Condition[]): {
181
181
  /**
182
182
  * Creates the query executor with all query methods
183
183
  */
184
- function createQueryExecutor<Model, M extends ModelDefinition = any>(
185
- state: QueryState<Model>
186
- ): QueryExecutor<Model, M> {
184
+ function createQueryExecutor<Model>(state: QueryState<Model>): QueryExecutor<Model> {
187
185
  return {
188
186
  limit(count) {
189
187
  return createQueryExecutor({
@@ -317,12 +315,12 @@ function createQueryExecutor<Model, M extends ModelDefinition = any>(
317
315
  /**
318
316
  * Creates a QueryBuilder for a table
319
317
  */
320
- export function createQueryBuilder<Model, M extends ModelDefinition = any>(
318
+ export function createQueryBuilder<Model>(
321
319
  tableName: string,
322
320
  client: DynamoDBClient,
323
- model?: M,
321
+ model?: ModelDefinition,
324
322
  logger?: DynamoDBLogger
325
- ): QueryBuilder<Model, M> {
323
+ ): QueryBuilder<Model> {
326
324
  return {
327
325
  where(fn) {
328
326
  // Create attribute builder proxy