@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.
- package/.turbo/turbo-build.log +37 -0
- package/LICENSE +21 -0
- package/README.md +166 -57
- package/dist/chunk-AQVECMXP.js +1 -0
- package/dist/chunk-DU7UPWBW.js +1 -0
- package/dist/chunk-GPYZK4WY.js +1 -0
- package/dist/chunk-NPPZW6VT.js +1 -0
- package/dist/chunk-TOYV2M4M.js +1 -0
- package/dist/chunk-UM3YF5EC.js +1 -0
- package/dist/columns.d.ts +1 -0
- package/dist/columns.js +1 -0
- package/dist/db-zHIHBm1E.d.ts +815 -0
- package/dist/db.d.ts +3 -0
- package/dist/db.js +1 -0
- package/dist/diff.d.ts +18 -0
- package/dist/diff.js +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +1 -0
- package/dist/introspection.d.ts +7 -0
- package/dist/introspection.js +1 -0
- package/dist/operators-BVreW0ky.d.ts +719 -0
- package/dist/snapshot.d.ts +24 -0
- package/dist/snapshot.js +1 -0
- package/dist/table.d.ts +1 -0
- package/dist/table.js +1 -0
- package/dist/transaction-RE7LXTGV.js +1 -0
- package/package.json +82 -66
- package/src/builders/base.ts +53 -56
- package/src/builders/batch-get.ts +63 -58
- package/src/builders/batch-write.ts +81 -78
- package/src/builders/delete.ts +46 -53
- package/src/builders/insert.ts +158 -150
- package/src/builders/query-promise.ts +26 -35
- package/src/builders/relational-builder.ts +214 -191
- package/src/builders/select.ts +250 -237
- package/src/builders/transaction.ts +170 -152
- package/src/builders/update.ts +197 -192
- package/src/columns/binary-set.ts +29 -38
- package/src/columns/binary.ts +25 -35
- package/src/columns/boolean.ts +25 -30
- package/src/columns/date.ts +57 -64
- package/src/columns/index.ts +15 -15
- package/src/columns/json.ts +39 -48
- package/src/columns/list.ts +26 -36
- package/src/columns/map.ts +26 -34
- package/src/columns/number-set.ts +29 -38
- package/src/columns/number.ts +33 -40
- package/src/columns/string-set.ts +38 -47
- package/src/columns/string.ts +37 -49
- package/src/columns/uuid.ts +26 -33
- package/src/core/client.ts +9 -9
- package/src/core/column-builder.ts +194 -220
- package/src/core/column.ts +127 -135
- package/src/core/diff.ts +40 -34
- package/src/core/errors.ts +20 -17
- package/src/core/introspection.ts +62 -58
- package/src/core/operations.ts +17 -23
- package/src/core/parser.ts +82 -89
- package/src/core/relations.ts +164 -154
- package/src/core/retry.ts +52 -52
- package/src/core/snapshot.ts +131 -130
- package/src/core/strategies.ts +222 -218
- package/src/core/table.ts +189 -202
- package/src/core/validation.ts +52 -52
- package/src/db.ts +211 -209
- package/src/expressions/actions.ts +26 -26
- package/src/expressions/builder.ts +62 -54
- package/src/expressions/operators.ts +48 -48
- package/src/expressions/update-builder.ts +78 -76
- package/src/index.ts +1 -1
- package/src/indexes.ts +8 -8
- package/test/batch-resilience.test.ts +138 -0
- package/test/builders/delete.test.ts +100 -0
- package/test/builders/insert.test.ts +216 -0
- package/test/builders/relational-types.test.ts +55 -0
- package/test/builders/relational.integration.test.ts +291 -0
- package/test/builders/relational.test.ts +66 -0
- package/test/builders/select.test.ts +411 -0
- package/test/builders/transaction-errors.test.ts +46 -0
- package/test/builders/transaction-execution.test.ts +99 -0
- package/test/builders/transaction-proxy.test.ts +41 -0
- package/test/builders/update-expression.test.ts +106 -0
- package/test/builders/update.test.ts +179 -0
- package/test/core/diff.test.ts +152 -0
- package/test/core/expressions.test.ts +64 -0
- package/test/core/introspection.test.ts +47 -0
- package/test/core/parser.test.ts +69 -0
- package/test/core/snapshot-gen.test.ts +155 -0
- package/test/core/snapshot.test.ts +52 -0
- package/test/date-column.test.ts +159 -0
- package/test/fluent-writes.integration.test.ts +148 -0
- package/test/integration-retry.test.ts +77 -0
- package/test/integration.test.ts +105 -0
- package/test/item-size-error.test.ts +16 -0
- package/test/item-size-validation.test.ts +82 -0
- package/test/item-size.test.ts +47 -0
- package/test/iterator-pagination.integration.test.ts +132 -0
- package/test/jsdoc-builders.test.ts +55 -0
- package/test/jsdoc-schema.test.ts +107 -0
- package/test/json-column.test.ts +51 -0
- package/test/metadata.test.ts +54 -0
- package/test/mizzle-package.test.ts +20 -0
- package/test/relational-centralized.test.ts +83 -0
- package/test/relational-definition.test.ts +75 -0
- package/test/relational-init.test.ts +30 -0
- package/test/relational-proxy.test.ts +52 -0
- package/test/relations.test.ts +45 -0
- package/test/resilience-config.test.ts +34 -0
- package/test/retry-handler.test.ts +63 -0
- package/test/transaction.integration.test.ts +153 -0
- package/test/unified-select.integration.test.ts +153 -0
- package/test/unified-update.integration.test.ts +139 -0
- package/test/update.integration.test.ts +132 -0
- package/tsconfig.json +12 -8
- package/tsup.config.ts +11 -11
- 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 "@
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
)
|
|
47
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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
|
+
}
|