@aurios/mizzle 1.1.2 → 1.1.4

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 (116) hide show
  1. package/.turbo/turbo-build.log +37 -0
  2. package/LICENSE +21 -0
  3. package/README.md +166 -57
  4. package/dist/chunk-AQVECMXP.js +1 -0
  5. package/dist/chunk-DU7UPWBW.js +1 -0
  6. package/dist/chunk-GPYZK4WY.js +1 -0
  7. package/dist/chunk-NPPZW6VT.js +1 -0
  8. package/dist/chunk-TOYV2M4M.js +1 -0
  9. package/dist/chunk-UM3YF5EC.js +1 -0
  10. package/dist/columns.d.ts +1 -0
  11. package/dist/columns.js +1 -0
  12. package/dist/db-zHIHBm1E.d.ts +815 -0
  13. package/dist/db.d.ts +3 -0
  14. package/dist/db.js +1 -0
  15. package/dist/diff.d.ts +18 -0
  16. package/dist/diff.js +1 -0
  17. package/dist/index.d.ts +42 -0
  18. package/dist/index.js +1 -0
  19. package/dist/introspection.d.ts +7 -0
  20. package/dist/introspection.js +1 -0
  21. package/dist/operators-BVreW0ky.d.ts +719 -0
  22. package/dist/snapshot.d.ts +24 -0
  23. package/dist/snapshot.js +1 -0
  24. package/dist/table.d.ts +1 -0
  25. package/dist/table.js +1 -0
  26. package/dist/transaction-RE7LXTGV.js +1 -0
  27. package/package.json +82 -66
  28. package/src/builders/base.ts +53 -56
  29. package/src/builders/batch-get.ts +63 -58
  30. package/src/builders/batch-write.ts +81 -78
  31. package/src/builders/delete.ts +46 -53
  32. package/src/builders/insert.ts +158 -150
  33. package/src/builders/query-promise.ts +26 -35
  34. package/src/builders/relational-builder.ts +214 -191
  35. package/src/builders/select.ts +250 -237
  36. package/src/builders/transaction.ts +170 -152
  37. package/src/builders/update.ts +197 -192
  38. package/src/columns/binary-set.ts +29 -38
  39. package/src/columns/binary.ts +25 -35
  40. package/src/columns/boolean.ts +25 -30
  41. package/src/columns/date.ts +57 -64
  42. package/src/columns/index.ts +15 -15
  43. package/src/columns/json.ts +39 -48
  44. package/src/columns/list.ts +26 -36
  45. package/src/columns/map.ts +26 -34
  46. package/src/columns/number-set.ts +29 -38
  47. package/src/columns/number.ts +33 -40
  48. package/src/columns/string-set.ts +38 -47
  49. package/src/columns/string.ts +37 -49
  50. package/src/columns/uuid.ts +26 -33
  51. package/src/core/client.ts +9 -9
  52. package/src/core/column-builder.ts +194 -220
  53. package/src/core/column.ts +127 -135
  54. package/src/core/diff.ts +40 -34
  55. package/src/core/errors.ts +20 -17
  56. package/src/core/introspection.ts +62 -58
  57. package/src/core/operations.ts +17 -23
  58. package/src/core/parser.ts +82 -89
  59. package/src/core/relations.ts +164 -154
  60. package/src/core/retry.ts +52 -52
  61. package/src/core/snapshot.ts +131 -130
  62. package/src/core/strategies.ts +222 -218
  63. package/src/core/table.ts +189 -202
  64. package/src/core/validation.ts +52 -52
  65. package/src/db.ts +211 -209
  66. package/src/expressions/actions.ts +26 -26
  67. package/src/expressions/builder.ts +62 -54
  68. package/src/expressions/operators.ts +48 -48
  69. package/src/expressions/update-builder.ts +78 -76
  70. package/src/index.ts +1 -1
  71. package/src/indexes.ts +8 -8
  72. package/test/batch-resilience.test.ts +138 -0
  73. package/test/builders/delete.test.ts +100 -0
  74. package/test/builders/insert.test.ts +216 -0
  75. package/test/builders/relational-types.test.ts +55 -0
  76. package/test/builders/relational.integration.test.ts +291 -0
  77. package/test/builders/relational.test.ts +66 -0
  78. package/test/builders/select.test.ts +411 -0
  79. package/test/builders/transaction-errors.test.ts +46 -0
  80. package/test/builders/transaction-execution.test.ts +99 -0
  81. package/test/builders/transaction-proxy.test.ts +41 -0
  82. package/test/builders/update-expression.test.ts +106 -0
  83. package/test/builders/update.test.ts +179 -0
  84. package/test/core/diff.test.ts +152 -0
  85. package/test/core/expressions.test.ts +64 -0
  86. package/test/core/introspection.test.ts +47 -0
  87. package/test/core/parser.test.ts +69 -0
  88. package/test/core/snapshot-gen.test.ts +155 -0
  89. package/test/core/snapshot.test.ts +52 -0
  90. package/test/date-column.test.ts +159 -0
  91. package/test/fluent-writes.integration.test.ts +148 -0
  92. package/test/integration-retry.test.ts +77 -0
  93. package/test/integration.test.ts +105 -0
  94. package/test/item-size-error.test.ts +16 -0
  95. package/test/item-size-validation.test.ts +82 -0
  96. package/test/item-size.test.ts +47 -0
  97. package/test/iterator-pagination.integration.test.ts +132 -0
  98. package/test/jsdoc-builders.test.ts +55 -0
  99. package/test/jsdoc-schema.test.ts +107 -0
  100. package/test/json-column.test.ts +51 -0
  101. package/test/metadata.test.ts +54 -0
  102. package/test/mizzle-package.test.ts +20 -0
  103. package/test/relational-centralized.test.ts +83 -0
  104. package/test/relational-definition.test.ts +75 -0
  105. package/test/relational-init.test.ts +30 -0
  106. package/test/relational-proxy.test.ts +52 -0
  107. package/test/relations.test.ts +45 -0
  108. package/test/resilience-config.test.ts +34 -0
  109. package/test/retry-handler.test.ts +63 -0
  110. package/test/transaction.integration.test.ts +153 -0
  111. package/test/unified-select.integration.test.ts +153 -0
  112. package/test/unified-update.integration.test.ts +139 -0
  113. package/test/update.integration.test.ts +132 -0
  114. package/tsconfig.json +12 -8
  115. package/tsup.config.ts +11 -11
  116. package/vitest.config.ts +8 -0
@@ -6,166 +6,184 @@ import { DeleteBuilder } from "./delete";
6
6
  import { Expression } from "../expressions/operators";
7
7
  import { BaseBuilder } from "./base";
8
8
  import type { IMizzleClient } from "../core/client";
9
- import { ENTITY_SYMBOLS } from "@mizzle/shared";
9
+ import { ENTITY_SYMBOLS } from "@repo/shared";
10
10
  import { buildExpression } from "../expressions/builder";
11
11
  import { buildUpdateExpressionString } from "../expressions/update-builder";
12
12
  import { TransactionFailedError } from "../core/errors";
13
13
 
14
14
  export class ConditionCheckBuilder<TEntity extends Entity> extends BaseBuilder<TEntity, void> {
15
- static readonly [ENTITY_SYMBOLS.ENTITY_KIND]: string = "ConditionCheckBuilder";
16
-
17
- private _whereClause?: Expression;
18
-
19
- constructor(entity: TEntity, client: IMizzleClient) {
20
- super(entity, client);
21
- }
22
-
23
- where(expression: Expression): this {
24
- this._whereClause = expression;
25
- return this;
26
- }
27
-
28
- public override async execute(): Promise<void> {
29
- throw new Error("ConditionCheckBuilder cannot be executed directly. Use it within a transaction.");
30
- }
31
-
32
- /** @internal */
33
- get whereClause() {
34
- return this._whereClause;
35
- }
36
-
37
- /** @internal */
38
- public override createExpressionContext(prefix = "") {
39
- return super.createExpressionContext(prefix);
40
- }
41
-
42
- /** @internal */
43
- public override resolveKeys(
44
- whereClause?: Expression,
45
- providedValues?: Record<string, unknown>,
46
- ) {
47
- return super.resolveKeys(whereClause, providedValues);
48
- }
15
+ static readonly [ENTITY_SYMBOLS.ENTITY_KIND]: string = "ConditionCheckBuilder";
16
+
17
+ private _whereClause?: Expression;
18
+
19
+ constructor(entity: TEntity, client: IMizzleClient) {
20
+ super(entity, client);
21
+ }
22
+
23
+ where(expression: Expression): this {
24
+ this._whereClause = expression;
25
+ return this;
26
+ }
27
+
28
+ public override async execute(): Promise<void> {
29
+ throw new Error(
30
+ "ConditionCheckBuilder cannot be executed directly. Use it within a transaction.",
31
+ );
32
+ }
33
+
34
+ /** @internal */
35
+ get whereClause() {
36
+ return this._whereClause;
37
+ }
38
+
39
+ /** @internal */
40
+ public override createExpressionContext(prefix = "") {
41
+ return super.createExpressionContext(prefix);
42
+ }
43
+
44
+ /** @internal */
45
+ public override resolveKeys(whereClause?: Expression, providedValues?: Record<string, unknown>) {
46
+ return super.resolveKeys(whereClause, providedValues);
47
+ }
49
48
  }
50
49
 
51
50
  export class TransactionProxy {
52
- constructor(private client: IMizzleClient) {}
53
-
54
- insert<TEntity extends Entity>(entity: TEntity): InsertBuilder<TEntity> {
55
- return new InsertBuilder(entity, this.client);
56
- }
57
-
58
- update<TEntity extends Entity>(entity: TEntity): UpdateBuilder<TEntity> {
59
- return new UpdateBuilder(entity, this.client);
60
- }
61
-
62
- delete<TEntity extends Entity>(entity: TEntity, keys: Record<string, unknown>): DeleteBuilder<TEntity> {
63
- return new DeleteBuilder(entity, this.client, keys);
64
- }
65
-
66
- conditionCheck<TEntity extends Entity>(entity: TEntity): ConditionCheckBuilder<TEntity> {
67
- return new ConditionCheckBuilder(entity, this.client);
68
- }
51
+ constructor(private client: IMizzleClient) {}
52
+
53
+ insert<TEntity extends Entity>(entity: TEntity): InsertBuilder<TEntity> {
54
+ return new InsertBuilder(entity, this.client);
55
+ }
56
+
57
+ update<TEntity extends Entity>(entity: TEntity): UpdateBuilder<TEntity> {
58
+ return new UpdateBuilder(entity, this.client);
59
+ }
60
+
61
+ delete<TEntity extends Entity>(
62
+ entity: TEntity,
63
+ keys: Record<string, unknown>,
64
+ ): DeleteBuilder<TEntity> {
65
+ return new DeleteBuilder(entity, this.client, keys);
66
+ }
67
+
68
+ conditionCheck<TEntity extends Entity>(entity: TEntity): ConditionCheckBuilder<TEntity> {
69
+ return new ConditionCheckBuilder(entity, this.client);
70
+ }
69
71
  }
70
72
 
71
73
  export class TransactionExecutor {
72
- constructor(private client: IMizzleClient) {}
73
-
74
- async execute(token: string, operations: BaseBuilder<Entity, unknown>[]): Promise<void> {
75
- const transactItems = operations.map(op => this.mapToTransactItem(op));
76
-
77
- const command = new TransactWriteCommand({
78
- TransactItems: transactItems,
79
- ClientRequestToken: token,
80
- });
81
-
82
- try {
83
- await this.client.send(command);
84
- } catch (error: unknown) {
85
- const err = error as Error & { __type?: string; CancellationReasons?: Record<string, unknown>[] };
86
- if (err.name === "TransactionCanceledException" || err.__type?.includes("TransactionCanceledException")) {
87
- const reasons = (err.CancellationReasons || []).map((reason: Record<string, unknown>, index: number) => ({
88
- index,
89
- code: reason.Code as string,
90
- message: reason.Message as string,
91
- item: reason.Item as Record<string, unknown> | undefined
92
- })).filter((reason) => reason.code !== "None");
93
-
94
- throw new TransactionFailedError("Transaction canceled by server.", reasons);
95
- }
96
- throw error;
97
- }
98
- }
99
-
100
- private mapToTransactItem(builder: BaseBuilder<Entity, unknown>): Record<string, unknown> {
101
- const kind = (builder.constructor as unknown as { [ENTITY_SYMBOLS.ENTITY_KIND]: string })[ENTITY_SYMBOLS.ENTITY_KIND];
102
-
103
- switch (kind) {
104
- case "InsertBase":
105
- return { Put: this.mapPut(builder as InsertBase<Entity, unknown>) };
106
- case "UpdateBuilder":
107
- return { Update: this.mapUpdate(builder as UpdateBuilder<Entity, unknown>) };
108
- case "DeleteBuilder":
109
- return { Delete: this.mapDelete(builder as DeleteBuilder<Entity, unknown>) };
110
- case "ConditionCheckBuilder":
111
- return { ConditionCheck: this.mapConditionCheck(builder as ConditionCheckBuilder<Entity>) };
112
- default:
113
- throw new Error(`Unsupported transaction operation: ${kind}`);
114
- }
115
- }
116
-
117
- private mapPut(builder: InsertBase<Entity, unknown>): Record<string, unknown> {
118
- const finalItem = builder.buildItem();
119
-
120
- return {
121
- TableName: builder.tableName,
122
- Item: finalItem,
123
- };
124
- }
125
-
126
- private mapUpdate(builder: UpdateBuilder<Entity, unknown>): Record<string, unknown> {
127
- const { expressionAttributeNames, expressionAttributeValues, addName, addValue } = builder.createExpressionContext("up_");
128
- const updateExpression = buildUpdateExpressionString(builder.state, addName, addValue);
129
-
130
- let conditionExpression: string | undefined;
131
- if (builder.whereClause) {
132
- conditionExpression = buildExpression(builder.whereClause, addName, addValue);
133
- }
134
-
135
- return {
136
- TableName: builder.tableName,
137
- Key: builder.resolveUpdateKeys(),
138
- UpdateExpression: updateExpression,
139
- ConditionExpression: conditionExpression,
140
- ExpressionAttributeNames: Object.keys(expressionAttributeNames).length > 0 ? expressionAttributeNames : undefined,
141
- ExpressionAttributeValues: Object.keys(expressionAttributeValues).length > 0 ? expressionAttributeValues : undefined,
142
- };
143
- }
144
-
145
- private mapDelete(builder: DeleteBuilder<Entity, unknown>): Record<string, unknown> {
146
- const resolution = builder.resolveKeys(undefined, builder.keys);
147
- return {
148
- TableName: builder.tableName,
149
- Key: resolution.keys,
150
- };
151
- }
152
-
153
- private mapConditionCheck(builder: ConditionCheckBuilder<Entity>): Record<string, unknown> {
154
- const { expressionAttributeNames, expressionAttributeValues, addName, addValue } = builder.createExpressionContext("cc_");
155
-
156
- const resolution = builder.resolveKeys(builder.whereClause);
157
-
158
- let conditionExpression: string | undefined;
159
- if (builder.whereClause) {
160
- conditionExpression = buildExpression(builder.whereClause, addName, addValue);
161
- }
162
-
163
- return {
164
- TableName: builder.tableName,
165
- Key: resolution.keys,
166
- ConditionExpression: conditionExpression,
167
- ExpressionAttributeNames: Object.keys(expressionAttributeNames).length > 0 ? expressionAttributeNames : undefined,
168
- ExpressionAttributeValues: Object.keys(expressionAttributeValues).length > 0 ? expressionAttributeValues : undefined,
169
- };
170
- }
171
- }
74
+ constructor(private client: IMizzleClient) {}
75
+
76
+ async execute(token: string, operations: BaseBuilder<Entity, unknown>[]): Promise<void> {
77
+ const transactItems = operations.map((op) => this.mapToTransactItem(op));
78
+
79
+ const command = new TransactWriteCommand({
80
+ TransactItems: transactItems,
81
+ ClientRequestToken: token,
82
+ });
83
+
84
+ try {
85
+ await this.client.send(command);
86
+ } catch (error: unknown) {
87
+ const err = error as Error & {
88
+ __type?: string;
89
+ CancellationReasons?: Record<string, unknown>[];
90
+ };
91
+ if (
92
+ err.name === "TransactionCanceledException" ||
93
+ err.__type?.includes("TransactionCanceledException")
94
+ ) {
95
+ const reasons = (err.CancellationReasons || [])
96
+ .map((reason: Record<string, unknown>, index: number) => ({
97
+ index,
98
+ code: reason.Code as string,
99
+ message: reason.Message as string,
100
+ item: reason.Item as Record<string, unknown> | undefined,
101
+ }))
102
+ .filter((reason) => reason.code !== "None");
103
+
104
+ throw new TransactionFailedError("Transaction canceled by server.", reasons);
105
+ }
106
+ throw error;
107
+ }
108
+ }
109
+
110
+ private mapToTransactItem(builder: BaseBuilder<Entity, unknown>): Record<string, unknown> {
111
+ const kind = (builder.constructor as unknown as { [ENTITY_SYMBOLS.ENTITY_KIND]: string })[
112
+ ENTITY_SYMBOLS.ENTITY_KIND
113
+ ];
114
+
115
+ switch (kind) {
116
+ case "InsertBase":
117
+ return { Put: this.mapPut(builder as InsertBase<Entity, unknown>) };
118
+ case "UpdateBuilder":
119
+ return { Update: this.mapUpdate(builder as UpdateBuilder<Entity, unknown>) };
120
+ case "DeleteBuilder":
121
+ return { Delete: this.mapDelete(builder as DeleteBuilder<Entity, unknown>) };
122
+ case "ConditionCheckBuilder":
123
+ return { ConditionCheck: this.mapConditionCheck(builder as ConditionCheckBuilder<Entity>) };
124
+ default:
125
+ throw new Error(`Unsupported transaction operation: ${kind}`);
126
+ }
127
+ }
128
+
129
+ private mapPut(builder: InsertBase<Entity, unknown>): Record<string, unknown> {
130
+ const finalItem = builder.buildItem();
131
+
132
+ return {
133
+ TableName: builder.tableName,
134
+ Item: finalItem,
135
+ };
136
+ }
137
+
138
+ private mapUpdate(builder: UpdateBuilder<Entity, unknown>): Record<string, unknown> {
139
+ const { expressionAttributeNames, expressionAttributeValues, addName, addValue } =
140
+ builder.createExpressionContext("up_");
141
+ const updateExpression = buildUpdateExpressionString(builder.state, addName, addValue);
142
+
143
+ let conditionExpression: string | undefined;
144
+ if (builder.whereClause) {
145
+ conditionExpression = buildExpression(builder.whereClause, addName, addValue);
146
+ }
147
+
148
+ return {
149
+ TableName: builder.tableName,
150
+ Key: builder.resolveUpdateKeys(),
151
+ UpdateExpression: updateExpression,
152
+ ConditionExpression: conditionExpression,
153
+ ExpressionAttributeNames:
154
+ Object.keys(expressionAttributeNames).length > 0 ? expressionAttributeNames : undefined,
155
+ ExpressionAttributeValues:
156
+ Object.keys(expressionAttributeValues).length > 0 ? expressionAttributeValues : undefined,
157
+ };
158
+ }
159
+
160
+ private mapDelete(builder: DeleteBuilder<Entity, unknown>): Record<string, unknown> {
161
+ const resolution = builder.resolveKeys(undefined, builder.keys);
162
+ return {
163
+ TableName: builder.tableName,
164
+ Key: resolution.keys,
165
+ };
166
+ }
167
+
168
+ private mapConditionCheck(builder: ConditionCheckBuilder<Entity>): Record<string, unknown> {
169
+ const { expressionAttributeNames, expressionAttributeValues, addName, addValue } =
170
+ builder.createExpressionContext("cc_");
171
+
172
+ const resolution = builder.resolveKeys(builder.whereClause);
173
+
174
+ let conditionExpression: string | undefined;
175
+ if (builder.whereClause) {
176
+ conditionExpression = buildExpression(builder.whereClause, addName, addValue);
177
+ }
178
+
179
+ return {
180
+ TableName: builder.tableName,
181
+ Key: resolution.keys,
182
+ ConditionExpression: conditionExpression,
183
+ ExpressionAttributeNames:
184
+ Object.keys(expressionAttributeNames).length > 0 ? expressionAttributeNames : undefined,
185
+ ExpressionAttributeValues:
186
+ Object.keys(expressionAttributeValues).length > 0 ? expressionAttributeValues : undefined,
187
+ };
188
+ }
189
+ }