@spine-event-engine/storage-mysql 2.0.0-snapshot.13
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/LICENSE +201 -0
- package/README.md +243 -0
- package/REFERENCE.md +140 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/mysql/column-mapping.d.ts +27 -0
- package/dist/mysql/column-mapping.d.ts.map +1 -0
- package/dist/mysql/column-mapping.js +94 -0
- package/dist/mysql/column-mapping.js.map +1 -0
- package/dist/mysql/delivery-cleanup.d.ts +37 -0
- package/dist/mysql/delivery-cleanup.d.ts.map +1 -0
- package/dist/mysql/delivery-cleanup.js +108 -0
- package/dist/mysql/delivery-cleanup.js.map +1 -0
- package/dist/mysql/entity-commit.d.ts +79 -0
- package/dist/mysql/entity-commit.d.ts.map +1 -0
- package/dist/mysql/entity-commit.js +123 -0
- package/dist/mysql/entity-commit.js.map +1 -0
- package/dist/mysql/entity-history.d.ts +123 -0
- package/dist/mysql/entity-history.d.ts.map +1 -0
- package/dist/mysql/entity-history.js +347 -0
- package/dist/mysql/entity-history.js.map +1 -0
- package/dist/mysql/errors.d.ts +25 -0
- package/dist/mysql/errors.d.ts.map +1 -0
- package/dist/mysql/errors.js +44 -0
- package/dist/mysql/errors.js.map +1 -0
- package/dist/mysql/id-column.d.ts +36 -0
- package/dist/mysql/id-column.d.ts.map +1 -0
- package/dist/mysql/id-column.js +113 -0
- package/dist/mysql/id-column.js.map +1 -0
- package/dist/mysql/record-storage.d.ts +379 -0
- package/dist/mysql/record-storage.d.ts.map +1 -0
- package/dist/mysql/record-storage.js +941 -0
- package/dist/mysql/record-storage.js.map +1 -0
- package/dist/mysql/storage-factory.d.ts +218 -0
- package/dist/mysql/storage-factory.d.ts.map +1 -0
- package/dist/mysql/storage-factory.js +477 -0
- package/dist/mysql/storage-factory.js.map +1 -0
- package/dist/mysql/table-resolver.d.ts +34 -0
- package/dist/mysql/table-resolver.d.ts.map +1 -0
- package/dist/mysql/table-resolver.js +87 -0
- package/dist/mysql/table-resolver.js.map +1 -0
- package/dist/mysql/table-spec.d.ts +88 -0
- package/dist/mysql/table-spec.d.ts.map +1 -0
- package/dist/mysql/table-spec.js +105 -0
- package/dist/mysql/table-spec.js.map +1 -0
- package/dist/mysql/testing.d.ts +47 -0
- package/dist/mysql/testing.d.ts.map +1 -0
- package/dist/mysql/testing.js +111 -0
- package/dist/mysql/testing.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026, CodeMatters. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
5
|
+
* in compliance with the License. You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
10
|
+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
11
|
+
* or implied. See the License for the specific language governing permissions and limitations under
|
|
12
|
+
* the License.
|
|
13
|
+
*/
|
|
14
|
+
import { createPool } from "mysql2/promise";
|
|
15
|
+
import { toBinary } from "@bufbuild/protobuf";
|
|
16
|
+
import { StringifierRegistry } from "@spine-event-engine/core";
|
|
17
|
+
import { StorageFactory, } from "@spine-event-engine/storage";
|
|
18
|
+
import { TenantBoundary } from "@spine-event-engine/storage/provider";
|
|
19
|
+
import { EntityCommitStorageFactories, } from "@spine-event-engine/storage/provider";
|
|
20
|
+
import { DeliveryCleanupStorageFactories } from "@spine-event-engine/storage/provider";
|
|
21
|
+
import { eventStoreRecordSpec } from "@spine-event-engine/storage/provider";
|
|
22
|
+
import { EntityRecordSchema, } from "@spine-event-engine/proto/generated/spine/server/entity/entity_pb.js";
|
|
23
|
+
import { MysqlRecordStorage } from "./record-storage.js";
|
|
24
|
+
import { MysqlTableResolver } from "./table-resolver.js";
|
|
25
|
+
import { MysqlEntityStorage } from "./entity-history.js";
|
|
26
|
+
import { mysqlEntityLockKey, MysqlEntityCommitCoordinator } from "./entity-commit.js";
|
|
27
|
+
import { MysqlDeliveryCleanupStorage } from "./delivery-cleanup.js";
|
|
28
|
+
import { resolvedMysqlTableSpec } from "./table-spec.js";
|
|
29
|
+
import { MysqlStorageOperationError, mysqlError } from "./errors.js";
|
|
30
|
+
/**
|
|
31
|
+
* Reports invalid MySQL storage configuration.
|
|
32
|
+
*/
|
|
33
|
+
export class MysqlStorageConfigurationError extends Error {
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Reports MySQL connection failures.
|
|
37
|
+
*/
|
|
38
|
+
export class MysqlStorageConnectionError extends Error {
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reports incompatible MySQL record-family schemas.
|
|
42
|
+
*/
|
|
43
|
+
export { MysqlStorageSchemaError } from "./errors.js";
|
|
44
|
+
/**
|
|
45
|
+
* Reports invalid MySQL storage data.
|
|
46
|
+
*/
|
|
47
|
+
export { MysqlStorageDataError } from "./errors.js";
|
|
48
|
+
/**
|
|
49
|
+
* Reports sanitized MySQL storage operation failures.
|
|
50
|
+
*/
|
|
51
|
+
export { MysqlStorageOperationError } from "./errors.js";
|
|
52
|
+
/**
|
|
53
|
+
* Provides MySQL record-family storage.
|
|
54
|
+
*/
|
|
55
|
+
export class MysqlStorageFactory extends StorageFactory {
|
|
56
|
+
createOperation;
|
|
57
|
+
stringifiers;
|
|
58
|
+
#handles = new Set();
|
|
59
|
+
#resolver;
|
|
60
|
+
#databases;
|
|
61
|
+
#catalog;
|
|
62
|
+
#closed;
|
|
63
|
+
constructor(databases, resolver = new MysqlTableResolver(), createOperation, stringifiers = new StringifierRegistry()) {
|
|
64
|
+
super();
|
|
65
|
+
this.createOperation = createOperation;
|
|
66
|
+
this.stringifiers = stringifiers;
|
|
67
|
+
this.#resolver = resolver;
|
|
68
|
+
this.#databases = new Map(databases.map((database) => [database.boundary.key, database]));
|
|
69
|
+
this.#catalog = Object.freeze({
|
|
70
|
+
all: () => Promise.resolve(databases.map(({ boundary }) => boundary)),
|
|
71
|
+
close: () => Promise.resolve(),
|
|
72
|
+
keep: (boundary) => {
|
|
73
|
+
if (!this.#databases.has(boundary.key)) {
|
|
74
|
+
return Promise.reject(new MysqlStorageConfigurationError("MySQL storage has no configured tenant."));
|
|
75
|
+
}
|
|
76
|
+
return Promise.resolve();
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
EntityCommitStorageFactories.register(this, {
|
|
80
|
+
createEntityCommitStorage: (input) => this.createEntityCommitStorage(input),
|
|
81
|
+
});
|
|
82
|
+
DeliveryCleanupStorageFactories.register(this, {
|
|
83
|
+
createDeliveryCleanupStorage: () => new MysqlDeliveryCleanupStorage((context, spec) => this.createMysqlRecordStorage(context, spec), (context, tables, key, work) => {
|
|
84
|
+
const database = this.database(context);
|
|
85
|
+
return new MysqlEntityCommitCoordinator(this.connections(database)).commit(tables, key, (connection) => work(connection), { requireTransaction: true });
|
|
86
|
+
}, () => "spine-delivery-cleanup"),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Creates a MySQL storage factory builder.
|
|
91
|
+
*
|
|
92
|
+
* @returns Returns a new builder.
|
|
93
|
+
*/
|
|
94
|
+
static newBuilder() {
|
|
95
|
+
return new Builder((entries, resolver, operation, stringifiers) => MysqlStorageFactory.connect(entries, resolver, operation, stringifiers));
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Returns the provider-owned configured tenant catalog.
|
|
99
|
+
* @returns The factory-owned tenant catalog.
|
|
100
|
+
*/
|
|
101
|
+
tenantCatalog() {
|
|
102
|
+
return this.#catalog;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Closes all storage handles and the connection pool.
|
|
106
|
+
*/
|
|
107
|
+
close() {
|
|
108
|
+
this.#closed ??= (async () => {
|
|
109
|
+
super.close();
|
|
110
|
+
for (const handle of this.#handles)
|
|
111
|
+
handle.close();
|
|
112
|
+
await Promise.all([...this.#databases.values()].map(({ pool }) => pool.end()));
|
|
113
|
+
})();
|
|
114
|
+
void this.#closed;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Creates storage for one resolved record family.
|
|
118
|
+
*
|
|
119
|
+
* @param context Provides the storage context.
|
|
120
|
+
* @param spec Describes the record family.
|
|
121
|
+
* @param group Identifies an optional storage group.
|
|
122
|
+
* @returns Returns the record-family storage handle.
|
|
123
|
+
*/
|
|
124
|
+
onCreateRecordStorage(context, spec, group) {
|
|
125
|
+
return this.createMysqlRecordStorage(context, spec, group);
|
|
126
|
+
}
|
|
127
|
+
createMysqlRecordStorage(context, spec, group, database = this.database(context)) {
|
|
128
|
+
const table = this.#resolver.resolve(spec.sourceType.typeName, group?.name, undefined, spec.recordType.typeName);
|
|
129
|
+
const tableSpec = resolvedMysqlTableSpec({
|
|
130
|
+
tableName: table.tableName,
|
|
131
|
+
sourceType: spec.sourceType,
|
|
132
|
+
recordType: spec.recordType,
|
|
133
|
+
idType: spec.idType,
|
|
134
|
+
...(group === undefined ? {} : { groupName: group.name }),
|
|
135
|
+
declaredColumns: spec.columns,
|
|
136
|
+
});
|
|
137
|
+
const operation = this.createOperation;
|
|
138
|
+
const create = operation === undefined ? undefined : () => operation(tableSpec).sql;
|
|
139
|
+
const handle = new MysqlRecordStorage(context, spec, table, this.connections(database), () => this.#handles.delete(handle), create, tableSpec, this.stringifiers);
|
|
140
|
+
this.#handles.add(handle);
|
|
141
|
+
return handle;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Creates MySQL storage for one Entity family.
|
|
145
|
+
*
|
|
146
|
+
* @param input Configures the Entity storage families.
|
|
147
|
+
* @returns Returns the MySQL Entity storage handle.
|
|
148
|
+
*/
|
|
149
|
+
createEntityStorage(input) {
|
|
150
|
+
const database = this.database(input.context);
|
|
151
|
+
const registration = {};
|
|
152
|
+
const handle = new MysqlEntityStorage(input, (spec, group) => this.createMysqlRecordStorage(input.context, spec, group, database), () => this.#handles.delete(registration.handle));
|
|
153
|
+
registration.handle = handle;
|
|
154
|
+
this.#handles.add(handle);
|
|
155
|
+
return handle;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Creates MySQL storage for atomic Entity commits.
|
|
159
|
+
*
|
|
160
|
+
* @param input Configures the Entity storage families.
|
|
161
|
+
* @returns Returns the Entity commit storage handle.
|
|
162
|
+
*/
|
|
163
|
+
createEntityCommitStorage(input) {
|
|
164
|
+
const database = this.database(input.context);
|
|
165
|
+
const registration = {};
|
|
166
|
+
const handle = new MysqlEntityCommitStorage(input, () => this.createEntityStorage(input), () => this.createMysqlRecordStorage(input.context, eventStoreRecordSpec, undefined, database), new MysqlEntityCommitCoordinator(this.connections(database)), database.databaseName, () => this.#handles.delete(registration.handle));
|
|
167
|
+
registration.handle = handle;
|
|
168
|
+
this.#handles.add(handle);
|
|
169
|
+
return handle;
|
|
170
|
+
}
|
|
171
|
+
connections(database) {
|
|
172
|
+
return {
|
|
173
|
+
databaseName: database.databaseName,
|
|
174
|
+
acquire: () => database.pool.getConnection(),
|
|
175
|
+
release: (connection) => {
|
|
176
|
+
connection.release();
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
database(context) {
|
|
181
|
+
const boundary = TenantBoundary.of(context);
|
|
182
|
+
const database = this.#databases.get(boundary.key);
|
|
183
|
+
if (database !== undefined)
|
|
184
|
+
return database;
|
|
185
|
+
throw new MysqlStorageConfigurationError(boundary.single
|
|
186
|
+
? "MySQL storage is configured for multiple tenants."
|
|
187
|
+
: "MySQL storage has no configured database for the requested tenant.");
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Connects to MySQL and creates an initialized storage factory.
|
|
191
|
+
*
|
|
192
|
+
* @param options Specifies the MySQL connection options.
|
|
193
|
+
* @param resolver Resolves configured record-family table names.
|
|
194
|
+
* @param createOperation Creates optional table-creation SQL.
|
|
195
|
+
* @param stringifiers Converts message-valued IDs and columns.
|
|
196
|
+
* @internal
|
|
197
|
+
* @returns Resolves to the initialized factory.
|
|
198
|
+
*/
|
|
199
|
+
static async connect(entries, resolver = new MysqlTableResolver(), createOperation, stringifiers = new StringifierRegistry()) {
|
|
200
|
+
const connected = [];
|
|
201
|
+
try {
|
|
202
|
+
for (const entry of entries) {
|
|
203
|
+
const pool = createPool(entry.poolOptions);
|
|
204
|
+
try {
|
|
205
|
+
const connection = await pool.getConnection();
|
|
206
|
+
try {
|
|
207
|
+
await assertNoRetiredLayout(connection);
|
|
208
|
+
}
|
|
209
|
+
finally {
|
|
210
|
+
connection.release();
|
|
211
|
+
}
|
|
212
|
+
connected.push({
|
|
213
|
+
boundary: entry.boundary,
|
|
214
|
+
databaseName: entry.databaseName,
|
|
215
|
+
pool,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
await pool.end().catch(() => undefined);
|
|
220
|
+
if (error instanceof MysqlStorageConfigurationError)
|
|
221
|
+
throw error;
|
|
222
|
+
throw new MysqlStorageConnectionError("Unable to connect to MySQL.");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return new MysqlStorageFactory(connected, resolver, createOperation, stringifiers);
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
await Promise.all(connected.map(({ pool }) => pool.end().catch(() => undefined)));
|
|
229
|
+
if (error instanceof MysqlStorageConfigurationError)
|
|
230
|
+
throw error;
|
|
231
|
+
throw new MysqlStorageConnectionError("Unable to connect to MySQL.");
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
class Builder {
|
|
236
|
+
connect;
|
|
237
|
+
#options;
|
|
238
|
+
#tenantOptions;
|
|
239
|
+
#resolver = new MysqlTableResolver();
|
|
240
|
+
#operation;
|
|
241
|
+
#stringifiers = new StringifierRegistry();
|
|
242
|
+
constructor(connect) {
|
|
243
|
+
this.connect = connect;
|
|
244
|
+
}
|
|
245
|
+
// prettier-ignore
|
|
246
|
+
/**
|
|
247
|
+
* Sets the MySQL connection options.
|
|
248
|
+
*
|
|
249
|
+
* @param options Specifies the MySQL connection options.
|
|
250
|
+
* @returns Returns this builder.
|
|
251
|
+
*/
|
|
252
|
+
setOptions(options) {
|
|
253
|
+
this.#options = options;
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Sets the complete multitenant database registry.
|
|
258
|
+
* @param entries Assign generated tenants to distinct physical databases.
|
|
259
|
+
* @returns Returns this builder.
|
|
260
|
+
*/
|
|
261
|
+
setTenantOptions(entries) {
|
|
262
|
+
this.#tenantOptions = [...entries];
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Sets custom reversible message stringifiers used by IDs and columns.
|
|
267
|
+
*
|
|
268
|
+
* @param registry The schema-bound stringifier registry.
|
|
269
|
+
* @returns Returns this builder.
|
|
270
|
+
*/
|
|
271
|
+
setStringifierRegistry(registry) {
|
|
272
|
+
this.#stringifiers = new StringifierRegistry(registry);
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Sets a configured table name.
|
|
277
|
+
*
|
|
278
|
+
* @param args Provides the record-only or grouped registration arguments.
|
|
279
|
+
* @returns Returns this builder.
|
|
280
|
+
*/
|
|
281
|
+
setTableName(...args) {
|
|
282
|
+
if (args.length === 2) {
|
|
283
|
+
const [source, name] = args;
|
|
284
|
+
this.#resolver.setRecordName(source.typeName, name);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const [source, record, name] = args;
|
|
288
|
+
this.#resolver.setGroupName(source.typeName, record.typeName, name);
|
|
289
|
+
}
|
|
290
|
+
return this;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Sets the create-table operation factory.
|
|
294
|
+
*
|
|
295
|
+
* @param factory Creates SQL for resolved record-family tables.
|
|
296
|
+
* @returns Returns this builder.
|
|
297
|
+
*/
|
|
298
|
+
useOperationFactory(factory) {
|
|
299
|
+
this.#operation = factory;
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Builds an initialized MySQL storage factory.
|
|
304
|
+
*
|
|
305
|
+
* @returns Resolves to the initialized factory.
|
|
306
|
+
*/
|
|
307
|
+
async build() {
|
|
308
|
+
if (this.#options !== undefined && this.#tenantOptions !== undefined) {
|
|
309
|
+
throw new MysqlStorageConfigurationError("Configure either single-tenant or multitenant MySQL storage, not both.");
|
|
310
|
+
}
|
|
311
|
+
if (this.#options === undefined && this.#tenantOptions === undefined)
|
|
312
|
+
throw new MysqlStorageConfigurationError("MySQL storage options are required.");
|
|
313
|
+
const entries = this.#options === undefined
|
|
314
|
+
? MysqlConfigurations.multitenant(this.#tenantOptions ?? [])
|
|
315
|
+
: [MysqlConfigurations.single(this.#options)];
|
|
316
|
+
return this.connect(entries, this.#resolver, this.#operation, this.#stringifiers);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
async function assertNoRetiredLayout(connection) {
|
|
320
|
+
const [columns] = await connection.query("SELECT table_name AS table_name, column_name AS column_name " +
|
|
321
|
+
"FROM information_schema.columns WHERE table_schema=DATABASE() " +
|
|
322
|
+
"AND LOWER(column_name) IN ('_scope', '_revision')");
|
|
323
|
+
const [primary] = await connection.query("SELECT table_name AS table_name, column_name AS column_name, seq_in_index AS seq_in_index " +
|
|
324
|
+
"FROM information_schema.statistics WHERE table_schema=DATABASE() " +
|
|
325
|
+
"AND index_name='PRIMARY' ORDER BY table_name, seq_in_index");
|
|
326
|
+
if (columns.length > 0 ||
|
|
327
|
+
primary.some((column) => column.column_name.toLowerCase() === "_scope")) {
|
|
328
|
+
throw new MysqlStorageConfigurationError("The configured database contains the retired MySQL storage layout.");
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const MysqlConfigurations = Object.freeze({
|
|
332
|
+
single(options) {
|
|
333
|
+
return MysqlConfigurations.parse(TenantBoundary.single, options);
|
|
334
|
+
},
|
|
335
|
+
multitenant(entries) {
|
|
336
|
+
if (entries.length === 0) {
|
|
337
|
+
throw new MysqlStorageConfigurationError("Multitenant MySQL storage requires tenants.");
|
|
338
|
+
}
|
|
339
|
+
const configured = entries.map(({ tenantId, options }) => MysqlConfigurations.parse(TenantBoundary.from(tenantId), options));
|
|
340
|
+
const tenants = new Set();
|
|
341
|
+
const targets = new Set();
|
|
342
|
+
for (const entry of configured) {
|
|
343
|
+
if (tenants.has(entry.boundary.key)) {
|
|
344
|
+
throw new MysqlStorageConfigurationError("MySQL storage has a duplicate tenant.");
|
|
345
|
+
}
|
|
346
|
+
if (targets.has(entry.target)) {
|
|
347
|
+
throw new MysqlStorageConfigurationError("MySQL tenants must use distinct physical databases.");
|
|
348
|
+
}
|
|
349
|
+
tenants.add(entry.boundary.key);
|
|
350
|
+
targets.add(entry.target);
|
|
351
|
+
}
|
|
352
|
+
return configured;
|
|
353
|
+
},
|
|
354
|
+
parse(boundary, options) {
|
|
355
|
+
let url;
|
|
356
|
+
try {
|
|
357
|
+
url = new URL(options.url);
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
throw new MysqlStorageConfigurationError("MySQL storage requires a valid URL.");
|
|
361
|
+
}
|
|
362
|
+
if (url.protocol !== "mysql:" || url.pathname.length <= 1) {
|
|
363
|
+
throw new MysqlStorageConfigurationError("MySQL storage URL requires a database.");
|
|
364
|
+
}
|
|
365
|
+
const databaseName = decodeURIComponent(url.pathname.slice(1));
|
|
366
|
+
const port = url.port === "" ? 3306 : Number(url.port);
|
|
367
|
+
return {
|
|
368
|
+
boundary,
|
|
369
|
+
databaseName,
|
|
370
|
+
target: `${url.hostname.toLowerCase()}:${String(port)}/${databaseName.toLowerCase()}`,
|
|
371
|
+
poolOptions: {
|
|
372
|
+
host: url.hostname,
|
|
373
|
+
...(url.port === "" ? {} : { port }),
|
|
374
|
+
database: databaseName,
|
|
375
|
+
supportBigNumbers: true,
|
|
376
|
+
bigNumberStrings: true,
|
|
377
|
+
...(url.username === "" ? {} : { user: decodeURIComponent(url.username) }),
|
|
378
|
+
...(url.password === "" ? {} : { password: decodeURIComponent(url.password) }),
|
|
379
|
+
...(options.connectionLimit === undefined
|
|
380
|
+
? {}
|
|
381
|
+
: { connectionLimit: options.connectionLimit }),
|
|
382
|
+
...(options.connectTimeoutMs === undefined
|
|
383
|
+
? {}
|
|
384
|
+
: { connectTimeout: options.connectTimeoutMs }),
|
|
385
|
+
...(options.tls === undefined ? {} : { ssl: options.tls }),
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
class MysqlEntityCommitStorage {
|
|
391
|
+
entity;
|
|
392
|
+
openStorage;
|
|
393
|
+
openEvents;
|
|
394
|
+
coordinator;
|
|
395
|
+
databaseName;
|
|
396
|
+
onClose;
|
|
397
|
+
#open = true;
|
|
398
|
+
constructor(entity, openStorage, openEvents, coordinator, databaseName, onClose) {
|
|
399
|
+
this.entity = entity;
|
|
400
|
+
this.openStorage = openStorage;
|
|
401
|
+
this.openEvents = openEvents;
|
|
402
|
+
this.coordinator = coordinator;
|
|
403
|
+
this.databaseName = databaseName;
|
|
404
|
+
this.onClose = onClose;
|
|
405
|
+
}
|
|
406
|
+
async commit(input) {
|
|
407
|
+
if (!this.#open)
|
|
408
|
+
throw new MysqlStorageOperationError("Entity commit storage is closed.");
|
|
409
|
+
if (input.entity.sourceType.typeName !== this.entity.sourceType.typeName)
|
|
410
|
+
throw new MysqlStorageOperationError("Entity commit source type is incompatible.");
|
|
411
|
+
if (!this.accepts(input))
|
|
412
|
+
throw new MysqlStorageOperationError("Entity commit context is incompatible.");
|
|
413
|
+
if (!this.entity.stateHistory && (input.states?.length ?? 0) > 0)
|
|
414
|
+
throw new MysqlStorageOperationError("Entity state history is disabled.");
|
|
415
|
+
if (!this.entity.eventHistory && (input.diagnostics?.length ?? 0) > 0)
|
|
416
|
+
throw new MysqlStorageOperationError("Entity event history is disabled.");
|
|
417
|
+
if ((input.events ?? []).some((event) => event.id === undefined))
|
|
418
|
+
throw new MysqlStorageOperationError("Entity commit requires delivery-event IDs.");
|
|
419
|
+
const storage = this.openStorage();
|
|
420
|
+
const events = this.openEvents();
|
|
421
|
+
try {
|
|
422
|
+
const families = storage.commitCapability();
|
|
423
|
+
await families.prepare();
|
|
424
|
+
await events.prepare();
|
|
425
|
+
const lockKey = mysqlEntityLockKey({
|
|
426
|
+
databaseName: this.databaseName,
|
|
427
|
+
entityKey: input.entity.id.key(input.entityId),
|
|
428
|
+
sourceTypeName: this.entity.sourceType.typeName,
|
|
429
|
+
});
|
|
430
|
+
return await this.coordinator.commit([...families.tableNames(), events.tableName], lockKey, (connection, transactional) => families.withConnection(connection, () => events.withConnection(connection, async () => {
|
|
431
|
+
const current = await families.readCurrentLocked(input.entityId);
|
|
432
|
+
if (!sameEntity(current, input.expected) && !sameEntity(current, input.next))
|
|
433
|
+
return "conflict";
|
|
434
|
+
if (!transactional) {
|
|
435
|
+
await families.preflightImmutable(input.states ?? [], input.diagnostics ?? []);
|
|
436
|
+
for (const event of input.events ?? [])
|
|
437
|
+
await events.assertImmutable(event);
|
|
438
|
+
}
|
|
439
|
+
for (const state of input.states ?? [])
|
|
440
|
+
await families.appendStateImmutable(state);
|
|
441
|
+
for (const diagnostic of input.diagnostics ?? [])
|
|
442
|
+
await families.appendDiagnosticImmutable(diagnostic);
|
|
443
|
+
for (const event of input.events ?? []) {
|
|
444
|
+
if (event.id === undefined)
|
|
445
|
+
throw new MysqlStorageOperationError("Entity commit requires delivery-event IDs.");
|
|
446
|
+
await events.writeImmutable(event);
|
|
447
|
+
}
|
|
448
|
+
if (!sameEntity(current, input.next))
|
|
449
|
+
await storage.current.write(input.next);
|
|
450
|
+
return "committed";
|
|
451
|
+
})));
|
|
452
|
+
}
|
|
453
|
+
catch (error) {
|
|
454
|
+
throw mysqlError(MysqlStorageOperationError, "MySQL Entity commit failed.", error);
|
|
455
|
+
}
|
|
456
|
+
finally {
|
|
457
|
+
events.close();
|
|
458
|
+
storage.close();
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
close() {
|
|
462
|
+
if (!this.#open)
|
|
463
|
+
return;
|
|
464
|
+
this.#open = false;
|
|
465
|
+
this.onClose();
|
|
466
|
+
}
|
|
467
|
+
accepts(input) {
|
|
468
|
+
return (input.context.multitenant === this.entity.context.multitenant &&
|
|
469
|
+
TenantBoundary.of(input.context).key === TenantBoundary.of(this.entity.context).key);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function sameEntity(left, right) {
|
|
473
|
+
return left === undefined || right === undefined
|
|
474
|
+
? left === right
|
|
475
|
+
: Buffer.compare(toBinary(EntityRecordSchema, left), toBinary(EntityRecordSchema, right)) === 0;
|
|
476
|
+
}
|
|
477
|
+
//# sourceMappingURL=storage-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-factory.js","sourceRoot":"","sources":["../../src/mysql/storage-factory.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,UAAU,EAAsD,MAAM,gBAAgB,CAAC;AAChG,OAAO,EAAE,QAAQ,EAAgB,MAAM,oBAAoB,CAAC;AAE5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EACL,cAAc,GAKf,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAAE,cAAc,EAAsB,MAAM,sCAAsC,CAAC;AAC1F,OAAO,EACL,4BAA4B,GAE7B,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EAAE,+BAA+B,EAAE,MAAM,sCAAsC,CAAC;AAEvF,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAE5E,OAAO,EACL,kBAAk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ACH,CAAC;IACD,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,OAAO;QACxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IACO,OAAO,CACb,KAAkF;QAElF,OAAO,CACL,KAAK,CAAC,OAAO,CAAC,WAAW,KAAK,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,WAAW;YAC7D,cAAc,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,GAAG,KAAK,cAAc,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CACpF,CAAC;IACJ,CAAC;CACF;AACD,SAAS,UAAU,CAAC,IAA8B,EAAE,KAA+B;IACjF,OAAO,IAAI,KAAK,SAAS,IAAI,KAAK,KAAK,SAAS;QAC9C,CAAC,CAAC,IAAI,KAAK,KAAK;QAChB,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,kBAAkB,EAAE,IAAI,CAAC,EAAE,QAAQ,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;AACpG,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { MysqlResolvedTable } from "./table-spec.js";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves stable MySQL-safe table names for record families.
|
|
4
|
+
*/
|
|
5
|
+
export declare class MysqlTableResolver {
|
|
6
|
+
#private;
|
|
7
|
+
/**
|
|
8
|
+
* Sets the ungrouped table name for a record type.
|
|
9
|
+
*
|
|
10
|
+
* @param recordType Names the record type.
|
|
11
|
+
* @param name Specifies the physical table name.
|
|
12
|
+
*/
|
|
13
|
+
setRecordName(recordType: string, name: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Sets the grouped table name for a source and record type.
|
|
16
|
+
*
|
|
17
|
+
* @param sourceType Names the source type.
|
|
18
|
+
* @param recordType Names the record type.
|
|
19
|
+
* @param name Specifies the physical table name.
|
|
20
|
+
*/
|
|
21
|
+
setGroupName(sourceType: string, recordType: string, name: string): void;
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the table layout for a record family.
|
|
24
|
+
*
|
|
25
|
+
* @param sourceType Names the source type.
|
|
26
|
+
* @param group Names the optional storage group.
|
|
27
|
+
* @param name Supplies an explicit table name.
|
|
28
|
+
* @param recordType Names the grouped record type.
|
|
29
|
+
* @returns Returns the resolved table layout.
|
|
30
|
+
*/
|
|
31
|
+
resolve(sourceType: string, group: string | undefined, name?: string, recordType?: string): MysqlResolvedTable;
|
|
32
|
+
private set;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=table-resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table-resolver.d.ts","sourceRoot":"","sources":["../../src/mysql/table-resolver.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE1D;;GAEG;AACH,qBAAa,kBAAkB;;IAI7B;;;;;OAKG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAIrD;;;;;;OAMG;IACH,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAIxE;;;;;;;;OAQG;IACH,OAAO,CACL,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,IAAI,CAAC,EAAE,MAAM,EACb,UAAU,CAAC,EAAE,MAAM,GAClB,kBAAkB;IAmCrB,OAAO,CAAC,GAAG;CAQZ"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2026, CodeMatters. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
5
|
+
* in compliance with the License. You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
10
|
+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
11
|
+
* or implied. See the License for the specific language governing permissions and limitations under
|
|
12
|
+
* the License.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Resolves stable MySQL-safe table names for record families.
|
|
16
|
+
*/
|
|
17
|
+
export class MysqlTableResolver {
|
|
18
|
+
#names = new Map();
|
|
19
|
+
#resolved = new Map();
|
|
20
|
+
/**
|
|
21
|
+
* Sets the ungrouped table name for a record type.
|
|
22
|
+
*
|
|
23
|
+
* @param recordType Names the record type.
|
|
24
|
+
* @param name Specifies the physical table name.
|
|
25
|
+
*/
|
|
26
|
+
setRecordName(recordType, name) {
|
|
27
|
+
this.set(`record:${recordType}`, name);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Sets the grouped table name for a source and record type.
|
|
31
|
+
*
|
|
32
|
+
* @param sourceType Names the source type.
|
|
33
|
+
* @param recordType Names the record type.
|
|
34
|
+
* @param name Specifies the physical table name.
|
|
35
|
+
*/
|
|
36
|
+
setGroupName(sourceType, recordType, name) {
|
|
37
|
+
this.set(`group:${sourceType}:${recordType}`, name);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolves the table layout for a record family.
|
|
41
|
+
*
|
|
42
|
+
* @param sourceType Names the source type.
|
|
43
|
+
* @param group Names the optional storage group.
|
|
44
|
+
* @param name Supplies an explicit table name.
|
|
45
|
+
* @param recordType Names the grouped record type.
|
|
46
|
+
* @returns Returns the resolved table layout.
|
|
47
|
+
*/
|
|
48
|
+
resolve(sourceType, group, name, recordType) {
|
|
49
|
+
const registered = group === undefined
|
|
50
|
+
? this.#names.get(`record:${sourceType}`)
|
|
51
|
+
: (this.#names.get(`group:${sourceType}:${recordType ?? ""}`) ??
|
|
52
|
+
this.#names.get(`record:${recordType ?? ""}`));
|
|
53
|
+
const readable = name ??
|
|
54
|
+
registered ??
|
|
55
|
+
(group === undefined
|
|
56
|
+
? sourceType.replaceAll(".", "_")
|
|
57
|
+
: `${group.replaceAll(".", "_")}_${(recordType ?? sourceType).slice((recordType ?? sourceType).lastIndexOf(".") + 1)}`);
|
|
58
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(readable) || Buffer.byteLength(readable) > 64) {
|
|
59
|
+
throw new Error(`MySQL table name is invalid: ${readable}`);
|
|
60
|
+
}
|
|
61
|
+
const identity = group === undefined && this.#names.get(`record:${sourceType}`) !== undefined
|
|
62
|
+
? `record:${sourceType}`
|
|
63
|
+
: group !== undefined &&
|
|
64
|
+
this.#names.get(`group:${sourceType}:${recordType ?? ""}`) === undefined &&
|
|
65
|
+
this.#names.get(`record:${recordType ?? ""}`) !== undefined
|
|
66
|
+
? `record:${recordType ?? sourceType}`
|
|
67
|
+
: group === undefined
|
|
68
|
+
? sourceType
|
|
69
|
+
: `${sourceType}\u0000${recordType ?? sourceType}\u0000${group}`;
|
|
70
|
+
const previous = this.#resolved.get(readable);
|
|
71
|
+
if (previous !== undefined && previous !== identity)
|
|
72
|
+
throw new Error(`MySQL table name collides: ${readable}`);
|
|
73
|
+
this.#resolved.set(readable, identity);
|
|
74
|
+
return Object.freeze({
|
|
75
|
+
tableName: readable,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
set(identity, name) {
|
|
79
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name) || Buffer.byteLength(name) > 64)
|
|
80
|
+
throw new Error(`MySQL table name is invalid: ${name}`);
|
|
81
|
+
for (const [registered, value] of this.#names)
|
|
82
|
+
if (registered !== identity && value === name)
|
|
83
|
+
throw new Error(`MySQL table name collides: ${name}`);
|
|
84
|
+
this.#names.set(identity, name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=table-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table-resolver.js","sourceRoot":"","sources":["../../src/mysql/table-resolver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH;;GAEG;AACH,MAAM,OAAO,kBAAkB;IACpB,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IACnC,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE/C;;;;;OAKG;IACH,aAAa,CAAC,UAAkB,EAAE,IAAY;QAC5C,IAAI,CAAC,GAAG,CAAC,UAAU,UAAU,EAAE,EAAE,IAAI,CAAC,CAAC;IACzC,CAAC;IAED;;;;;;OAMG;IACH,YAAY,CAAC,UAAkB,EAAE,UAAkB,EAAE,IAAY;QAC/D,IAAI,CAAC,GAAG,CAAC,SAAS,UAAU,IAAI,UAAU,EAAE,EAAE,IAAI,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;;OAQG;IACH,OAAO,CACL,UAAkB,EAClB,KAAyB,EACzB,IAAa,EACb,UAAmB;QAEnB,MAAM,UAAU,GACd,KAAK,KAAK,SAAS;YACjB,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,UAAU,EAAE,CAAC;YACzC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,UAAU,IAAI,UAAU,IAAI,EAAE,EAAE,CAAC;gBAC3D,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,UAAU,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;QACrD,MAAM,QAAQ,GACZ,IAAI;YACJ,UAAU;YACV,CAAC,KAAK,KAAK,SAAS;gBAClB,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC;gBACjC,CAAC,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,CAAC,UAAU,IAAI,U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