@hot-updater/plugin-core 1.0.0-rc.2 → 1.0.0-rc.22
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/README.md +33 -0
- package/dist/baseCandidateKey.cjs +50 -0
- package/dist/baseCandidateKey.d.cts +27 -0
- package/dist/baseCandidateKey.d.mts +27 -0
- package/dist/baseCandidateKey.mjs +49 -0
- package/dist/bundleStorageLayout.cjs +7 -0
- package/dist/bundleStorageLayout.d.cts +5 -1
- package/dist/bundleStorageLayout.d.mts +5 -1
- package/dist/bundleStorageLayout.mjs +7 -1
- package/dist/contentType.cjs +9 -0
- package/dist/contentType.d.cts +4 -0
- package/dist/contentType.d.mts +4 -0
- package/dist/contentType.mjs +7 -0
- package/dist/coreApi.d.cts +131 -0
- package/dist/coreApi.d.mts +131 -0
- package/dist/createStorageAdapter.cjs +8 -0
- package/dist/createStorageAdapter.d.cts +9 -0
- package/dist/createStorageAdapter.d.mts +9 -0
- package/dist/createStorageAdapter.mjs +7 -0
- package/dist/database/adapter.cjs +61 -0
- package/dist/database/adapter.d.cts +200 -0
- package/dist/database/adapter.d.mts +200 -0
- package/dist/database/adapter.mjs +55 -0
- package/dist/database/distinct.cjs +88 -0
- package/dist/database/distinct.d.cts +18 -0
- package/dist/database/distinct.d.mts +18 -0
- package/dist/database/distinct.mjs +85 -0
- package/dist/database/memoryAdapter.cjs +107 -0
- package/dist/database/memoryAdapter.d.cts +14 -0
- package/dist/database/memoryAdapter.d.mts +14 -0
- package/dist/database/memoryAdapter.mjs +107 -0
- package/dist/database/values.cjs +114 -0
- package/dist/database/values.d.cts +17 -0
- package/dist/database/values.d.mts +17 -0
- package/dist/database/values.mjs +104 -0
- package/dist/database/verifyAdapter.cjs +137 -0
- package/dist/database/verifyAdapter.d.cts +37 -0
- package/dist/database/verifyAdapter.d.mts +37 -0
- package/dist/database/verifyAdapter.mjs +136 -0
- package/dist/databaseErrors.cjs +30 -0
- package/dist/databaseErrors.d.cts +21 -0
- package/dist/databaseErrors.d.mts +21 -0
- package/dist/databaseErrors.mjs +28 -0
- package/dist/databaseJsonValue.cjs +6 -0
- package/dist/databaseJsonValue.d.cts +9 -2
- package/dist/databaseJsonValue.d.mts +9 -2
- package/dist/databaseJsonValue.mjs +6 -0
- package/dist/databaseMetadata.cjs +4 -4
- package/dist/databaseMetadata.mjs +3 -3
- package/dist/databaseRows.cjs +6 -12
- package/dist/databaseRows.d.cts +1 -1
- package/dist/databaseRows.d.mts +1 -1
- package/dist/databaseRows.mjs +1 -7
- package/dist/engine/core/schema.cjs +279 -0
- package/dist/engine/core/schema.d.cts +312 -0
- package/dist/engine/core/schema.d.mts +312 -0
- package/dist/engine/core/schema.mjs +277 -0
- package/dist/engine/createEngine.cjs +72 -0
- package/dist/engine/createEngine.d.cts +45 -0
- package/dist/engine/createEngine.d.mts +45 -0
- package/dist/engine/createEngine.mjs +72 -0
- package/dist/engine/database/aggregateBatching.cjs +627 -0
- package/dist/engine/database/aggregateBatching.mjs +626 -0
- package/dist/engine/database/cursor.cjs +42 -0
- package/dist/engine/database/cursor.mjs +38 -0
- package/dist/engine/database/database.cjs +68 -0
- package/dist/engine/database/database.mjs +68 -0
- package/dist/engine/database/engine.cjs +37 -0
- package/dist/engine/database/engine.mjs +37 -0
- package/dist/engine/database/engineAggregates.cjs +120 -0
- package/dist/engine/database/engineAggregates.mjs +117 -0
- package/dist/engine/database/engineReads.cjs +182 -0
- package/dist/engine/database/engineReads.d.cts +10 -0
- package/dist/engine/database/engineReads.d.mts +10 -0
- package/dist/engine/database/engineReads.mjs +181 -0
- package/dist/engine/database/engineTransaction.cjs +399 -0
- package/dist/engine/database/engineTransaction.d.cts +18 -0
- package/dist/engine/database/engineTransaction.d.mts +18 -0
- package/dist/engine/database/engineTransaction.mjs +398 -0
- package/dist/engine/database/fence.cjs +156 -0
- package/dist/engine/database/fence.d.cts +41 -0
- package/dist/engine/database/fence.d.mts +41 -0
- package/dist/engine/database/fence.mjs +149 -0
- package/dist/engine/database/kv/kvAdapter.cjs +293 -0
- package/dist/engine/database/kv/kvAdapter.d.cts +116 -0
- package/dist/engine/database/kv/kvAdapter.d.mts +116 -0
- package/dist/engine/database/kv/kvAdapter.mjs +292 -0
- package/dist/engine/database/readMeter.cjs +80 -0
- package/dist/engine/database/readMeter.d.cts +34 -0
- package/dist/engine/database/readMeter.d.mts +34 -0
- package/dist/engine/database/readMeter.mjs +79 -0
- package/dist/engine/database/resolveSchema.cjs +197 -0
- package/dist/engine/database/resolveSchema.d.cts +35 -0
- package/dist/engine/database/resolveSchema.d.mts +35 -0
- package/dist/engine/database/resolveSchema.mjs +195 -0
- package/dist/engine/database/retention.cjs +100 -0
- package/dist/engine/database/retention.mjs +98 -0
- package/dist/engine/database/sql/sqlAdapter.cjs +290 -0
- package/dist/engine/database/sql/sqlAdapter.d.cts +45 -0
- package/dist/engine/database/sql/sqlAdapter.d.mts +45 -0
- package/dist/engine/database/sql/sqlAdapter.mjs +288 -0
- package/dist/engine/database/sql/sqlSchema.cjs +175 -0
- package/dist/engine/database/sql/sqlSchema.d.cts +45 -0
- package/dist/engine/database/sql/sqlSchema.d.mts +45 -0
- package/dist/engine/database/sql/sqlSchema.mjs +169 -0
- package/dist/engine/db/coreDatabase.cjs +94 -0
- package/dist/engine/db/coreDatabase.d.cts +78 -0
- package/dist/engine/db/coreDatabase.d.mts +78 -0
- package/dist/engine/db/coreDatabase.mjs +86 -0
- package/dist/engine/db/engineMigrator.cjs +53 -0
- package/dist/engine/db/engineMigrator.mjs +53 -0
- package/dist/engine/db/engineSql.cjs +26 -0
- package/dist/engine/db/engineSql.d.cts +16 -0
- package/dist/engine/db/engineSql.d.mts +16 -0
- package/dist/engine/db/engineSql.mjs +25 -0
- package/dist/engine/db/engineSqlMigrator.cjs +67 -0
- package/dist/engine/db/engineSqlMigrator.d.cts +21 -0
- package/dist/engine/db/engineSqlMigrator.d.mts +21 -0
- package/dist/engine/db/engineSqlMigrator.mjs +67 -0
- package/dist/engine/db/fixedMigratorShared.cjs +12 -0
- package/dist/engine/db/fixedMigratorShared.mjs +11 -0
- package/dist/engine/db/schemaSettings.cjs +63 -0
- package/dist/engine/db/schemaSettings.d.cts +7 -0
- package/dist/engine/db/schemaSettings.d.mts +7 -0
- package/dist/engine/db/schemaSettings.mjs +62 -0
- package/dist/engine/db/settingsMigrator.cjs +94 -0
- package/dist/engine/db/settingsMigrator.d.cts +25 -0
- package/dist/engine/db/settingsMigrator.d.mts +25 -0
- package/dist/engine/db/settingsMigrator.mjs +91 -0
- package/dist/engine/db/types.d.cts +78 -0
- package/dist/engine/db/types.d.mts +78 -0
- package/dist/index.cjs +97 -49
- package/dist/index.d.cts +46 -22
- package/dist/index.d.mts +46 -22
- package/dist/index.mjs +32 -17
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/comparison-DenM3wCn.cjs +0 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/comparison-DenM3wCn.mjs +1 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.cjs +8 -0
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/range-DvX-Y6iv.mjs +9 -2
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/set-CC5YeoYX.cjs +4 -0
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/set-CC5YeoYX.mjs +4 -1
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/version-CQ98ZBpL.cjs +0 -5
- package/dist/node_modules/.pnpm/verkit@0.4.0/node_modules/verkit/dist/version-CQ98ZBpL.mjs +2 -6
- package/dist/releaseCatalogCompiler.cjs +27 -26
- package/dist/releaseCatalogCompiler.d.cts +1 -2
- package/dist/releaseCatalogCompiler.d.mts +2 -3
- package/dist/releaseCatalogCompiler.mjs +5 -9
- package/dist/releaseCatalogMutation.cjs +0 -292
- package/dist/releaseCatalogMutation.d.cts +4 -45
- package/dist/releaseCatalogMutation.d.mts +4 -45
- package/dist/releaseCatalogMutation.mjs +1 -288
- package/dist/releaseManagement.cjs +0 -221
- package/dist/releaseManagement.d.cts +4 -28
- package/dist/releaseManagement.d.mts +4 -28
- package/dist/releaseManagement.mjs +1 -216
- package/dist/remoteBundleSigning.d.cts +2 -2
- package/dist/remoteBundleSigning.d.mts +2 -2
- package/dist/serverPlugin/busy.cjs +36 -0
- package/dist/serverPlugin/busy.d.cts +12 -0
- package/dist/serverPlugin/busy.d.mts +12 -0
- package/dist/serverPlugin/busy.mjs +36 -0
- package/dist/serverPlugin/configError.cjs +7 -0
- package/dist/serverPlugin/configError.d.cts +7 -0
- package/dist/serverPlugin/configError.d.mts +7 -0
- package/dist/serverPlugin/configError.mjs +7 -0
- package/dist/serverPlugin/coreReads.d.cts +48 -0
- package/dist/serverPlugin/coreReads.d.mts +48 -0
- package/dist/serverPlugin/databaseHandle.d.cts +105 -0
- package/dist/serverPlugin/databaseHandle.d.mts +105 -0
- package/dist/serverPlugin/definePlugin.cjs +5 -0
- package/dist/serverPlugin/definePlugin.d.cts +130 -0
- package/dist/serverPlugin/definePlugin.d.mts +130 -0
- package/dist/serverPlugin/definePlugin.mjs +5 -0
- package/dist/serverPlugin/definitions.d.cts +76 -0
- package/dist/serverPlugin/definitions.d.mts +76 -0
- package/dist/serverPlugin/errors.cjs +37 -0
- package/dist/serverPlugin/errors.d.cts +29 -0
- package/dist/serverPlugin/errors.d.mts +29 -0
- package/dist/serverPlugin/errors.mjs +32 -0
- package/dist/serverPlugin/schema.cjs +24 -0
- package/dist/serverPlugin/schema.d.cts +90 -0
- package/dist/serverPlugin/schema.d.mts +90 -0
- package/dist/serverPlugin/schema.mjs +23 -0
- package/dist/types/databaseConfig.cjs +5 -0
- package/dist/types/databaseConfig.d.cts +68 -0
- package/dist/types/databaseConfig.d.mts +68 -0
- package/dist/types/databaseConfig.mjs +5 -0
- package/dist/types/databaseOperations.d.cts +5 -180
- package/dist/types/databaseOperations.d.mts +5 -180
- package/dist/types/databaseRows.d.cts +5 -56
- package/dist/types/databaseRows.d.mts +5 -56
- package/dist/types/index.cjs +2 -0
- package/dist/types/index.d.cts +31 -97
- package/dist/types/index.d.mts +31 -97
- package/dist/types/index.mjs +2 -1
- package/dist/types/models.d.cts +14 -0
- package/dist/types/models.d.mts +14 -0
- package/dist/types/public.d.cts +4 -3
- package/dist/types/public.d.mts +4 -3
- package/dist/uuidv7.cjs +3 -3
- package/dist/uuidv7.d.cts +1 -1
- package/dist/uuidv7.d.mts +1 -1
- package/dist/uuidv7.mjs +1 -1
- package/package.json +10 -7
- package/dist/calculatePagination.cjs +0 -25
- package/dist/calculatePagination.d.cts +0 -12
- package/dist/calculatePagination.d.mts +0 -12
- package/dist/calculatePagination.mjs +0 -25
- package/dist/compressionFormat.cjs +0 -62
- package/dist/compressionFormat.d.cts +0 -33
- package/dist/compressionFormat.d.mts +0 -33
- package/dist/compressionFormat.mjs +0 -58
- package/dist/createDatabasePlugin.cjs +0 -920
- package/dist/createDatabasePlugin.d.cts +0 -28
- package/dist/createDatabasePlugin.d.mts +0 -28
- package/dist/createDatabasePlugin.mjs +0 -916
- package/dist/createStoragePlugin.cjs +0 -8
- package/dist/createStoragePlugin.d.cts +0 -9
- package/dist/createStoragePlugin.d.mts +0 -9
- package/dist/createStoragePlugin.mjs +0 -7
- package/dist/databaseClient.cjs +0 -117
- package/dist/databaseClient.d.cts +0 -32
- package/dist/databaseClient.d.mts +0 -32
- package/dist/databaseClient.mjs +0 -114
- package/dist/databaseClientReads.cjs +0 -115
- package/dist/databaseClientReads.mjs +0 -113
- package/dist/databaseClientUpdates.cjs +0 -40
- package/dist/databaseClientUpdates.d.cts +0 -11
- package/dist/databaseClientUpdates.d.mts +0 -11
- package/dist/databaseClientUpdates.mjs +0 -38
- package/dist/databasePluginCrud.cjs +0 -88
- package/dist/databasePluginCrud.d.cts +0 -4
- package/dist/databasePluginCrud.d.mts +0 -4
- package/dist/databasePluginCrud.mjs +0 -87
- package/dist/databasePluginCrudValidation.d.cts +0 -6
- package/dist/databasePluginCrudValidation.d.mts +0 -6
- package/dist/databasePluginCrudValidationErrors.cjs +0 -11
- package/dist/databasePluginCrudValidationErrors.d.cts +0 -9
- package/dist/databasePluginCrudValidationErrors.d.mts +0 -9
- package/dist/databasePluginCrudValidationErrors.mjs +0 -11
- package/dist/databasePluginCrudValidationFields.cjs +0 -321
- package/dist/databasePluginCrudValidationFields.d.cts +0 -1
- package/dist/databasePluginCrudValidationFields.d.mts +0 -1
- package/dist/databasePluginCrudValidationFields.mjs +0 -309
- package/dist/databasePluginCrudValidationMutations.cjs +0 -107
- package/dist/databasePluginCrudValidationMutations.d.cts +0 -1
- package/dist/databasePluginCrudValidationMutations.d.mts +0 -1
- package/dist/databasePluginCrudValidationMutations.mjs +0 -100
- package/dist/databasePluginCrudValidationQueries.cjs +0 -103
- package/dist/databasePluginCrudValidationQueries.d.cts +0 -2
- package/dist/databasePluginCrudValidationQueries.d.mts +0 -2
- package/dist/databasePluginCrudValidationQueries.mjs +0 -97
- package/dist/databasePluginCrudValidationRows.cjs +0 -55
- package/dist/databasePluginCrudValidationRows.d.cts +0 -1
- package/dist/databasePluginCrudValidationRows.d.mts +0 -1
- package/dist/databasePluginCrudValidationRows.mjs +0 -53
- package/dist/databasePluginTransaction.cjs +0 -7
- package/dist/databasePluginTransaction.mjs +0 -7
- package/dist/insightsContract.cjs +0 -157
- package/dist/insightsContract.d.cts +0 -11
- package/dist/insightsContract.d.mts +0 -11
- package/dist/insightsContract.mjs +0 -153
- package/dist/internal.cjs +0 -6
- package/dist/internal.d.cts +0 -10
- package/dist/internal.d.mts +0 -10
- package/dist/internal.mjs +0 -3
- package/dist/paginateBundles.cjs +0 -53
- package/dist/paginateBundles.d.cts +0 -13
- package/dist/paginateBundles.d.mts +0 -13
- package/dist/paginateBundles.mjs +0 -53
- package/dist/queryBundles.cjs +0 -34
- package/dist/queryBundles.d.cts +0 -9
- package/dist/queryBundles.d.mts +0 -9
- package/dist/queryBundles.mjs +0 -32
- package/dist/requestBundleCache.cjs +0 -22
- package/dist/requestBundleCache.d.cts +0 -8
- package/dist/requestBundleCache.d.mts +0 -8
- package/dist/requestBundleCache.mjs +0 -22
- package/dist/types/database.d.cts +0 -5
- package/dist/types/database.d.mts +0 -5
- package/dist/types/databaseFields.cjs +0 -105
- package/dist/types/databaseFields.d.cts +0 -13
- package/dist/types/databaseFields.d.mts +0 -13
- package/dist/types/databaseFields.mjs +0 -105
- package/dist/types/databasePlugin.d.cts +0 -298
- package/dist/types/databasePlugin.d.mts +0 -298
- package/dist/types/databaseQuery.d.cts +0 -46
- package/dist/types/databaseQuery.d.mts +0 -46
- package/dist/types/internal.d.cts +0 -8
- package/dist/types/internal.d.mts +0 -8
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
const require_adapter = require("../../../database/adapter.cjs");
|
|
2
|
+
//#region src/engine/database/sql/sqlSchema.ts
|
|
3
|
+
/**
|
|
4
|
+
* Quotes an identifier: backticks on MySQL, double quotes elsewhere. An ORM
|
|
5
|
+
* schema generator, or an executor that issues statements of its own, quotes
|
|
6
|
+
* names as the SQL core does with it, as the Prisma adapter's do.
|
|
7
|
+
*/
|
|
8
|
+
const quoteSql = (dialect, name) => dialect === "mysql" ? `\`${name.replaceAll("`", "``")}\`` : `"${name.replaceAll("\"", "\"\"")}"`;
|
|
9
|
+
const isMultiIndex = (table, index) => index.eq.some((column) => require_adapter.findPhysicalColumn(table, column).multi);
|
|
10
|
+
/** Keeps an identifier within PostgreSQL's 63 and MySQL's 64 characters. */
|
|
11
|
+
const shortSqlName = (name) => {
|
|
12
|
+
if (name.length <= 60) return name;
|
|
13
|
+
let hash = 2166136261;
|
|
14
|
+
for (const char of name) hash = Math.imul(hash ^ char.charCodeAt(0), 16777619) >>> 0;
|
|
15
|
+
return `${name.slice(0, 51)}_${hash.toString(36)}`;
|
|
16
|
+
};
|
|
17
|
+
/** The column type; `entry` types one value of a multi-valued column in its index table. */
|
|
18
|
+
const columnType = (dialect, column, indexed, entry = false) => {
|
|
19
|
+
if (column.multi && !entry || column.type === "json") return {
|
|
20
|
+
postgresql: "jsonb",
|
|
21
|
+
mysql: "json",
|
|
22
|
+
sqlite: "TEXT"
|
|
23
|
+
}[dialect];
|
|
24
|
+
if (column.type === "string") {
|
|
25
|
+
const length = column.maxLength ?? (indexed ? 255 : void 0);
|
|
26
|
+
if (dialect === "sqlite") return "TEXT";
|
|
27
|
+
if (dialect === "postgresql") return `${column.maxLength ? `varchar(${column.maxLength})` : "text"} COLLATE "C"`;
|
|
28
|
+
const charset = column.ascii ? "ascii COLLATE ascii_bin" : "utf8mb4 COLLATE utf8mb4_0900_bin";
|
|
29
|
+
return `${length ? `varchar(${length})` : "longtext"} CHARACTER SET ${charset}`;
|
|
30
|
+
}
|
|
31
|
+
if (column.type === "integer") return dialect === "sqlite" ? "INTEGER" : "bigint";
|
|
32
|
+
if (column.type === "number") return {
|
|
33
|
+
postgresql: "double precision",
|
|
34
|
+
mysql: "double",
|
|
35
|
+
sqlite: "REAL"
|
|
36
|
+
}[dialect];
|
|
37
|
+
return dialect === "sqlite" ? "INTEGER" : "boolean";
|
|
38
|
+
};
|
|
39
|
+
/** InnoDB's limit on the bytes of one key or index. */
|
|
40
|
+
const MYSQL_KEY_BYTES = 3072;
|
|
41
|
+
/** Refuses a MySQL key or index over InnoDB's limit before any DDL runs. */
|
|
42
|
+
const checkMysqlKey = (table, name, columns) => {
|
|
43
|
+
const bytes = columns.reduce((sum, column) => {
|
|
44
|
+
const { type, maxLength, ascii } = require_adapter.findPhysicalColumn(table, column);
|
|
45
|
+
if (type === "string") return sum + (maxLength ?? 255) * (ascii ? 1 : 4);
|
|
46
|
+
return sum + (type === "boolean" ? 1 : 8);
|
|
47
|
+
}, 0);
|
|
48
|
+
if (bytes > MYSQL_KEY_BYTES) throw new require_adapter.DatabaseSchemaError(`${table.name} ${name} needs ${bytes} bytes on MySQL, over its ${MYSQL_KEY_BYTES}; shorten its strings or mark ASCII ones ascii.`);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Each table as the DDL creates it, for the DDL below and for ORM schema
|
|
52
|
+
* generators, such as the Drizzle and Prisma adapters': binary collation, one index per declared index, and an index
|
|
53
|
+
* table `<table>__<index>` for each index over a multi-valued field, keyed by
|
|
54
|
+
* every column it holds.
|
|
55
|
+
*/
|
|
56
|
+
const sqlTableShapes = (dialect, tables, tablePrefix = "") => tables.map((table) => {
|
|
57
|
+
const name = tablePrefix + table.name;
|
|
58
|
+
const indexed = /* @__PURE__ */ new Set([...table.key, ...table.indexes.flatMap(({ eq, sort }) => [...eq, ...sort])]);
|
|
59
|
+
if (dialect === "mysql") checkMysqlKey(table, "key", table.key);
|
|
60
|
+
const indexes = table.indexes.flatMap((index) => {
|
|
61
|
+
const columns = [...index.eq, ...require_adapter.indexOrderColumns(table, index)];
|
|
62
|
+
if (dialect === "mysql") checkMysqlKey(table, `index ${index.name}`, columns);
|
|
63
|
+
if (isMultiIndex(table, index)) {
|
|
64
|
+
const entries = columns.map((column) => ({
|
|
65
|
+
name: column,
|
|
66
|
+
type: columnType(dialect, require_adapter.findPhysicalColumn(table, column), true, true),
|
|
67
|
+
notNull: true
|
|
68
|
+
}));
|
|
69
|
+
return [{
|
|
70
|
+
kind: "table",
|
|
71
|
+
table: {
|
|
72
|
+
name: `${name}__${index.name}`,
|
|
73
|
+
columns: entries,
|
|
74
|
+
key: columns,
|
|
75
|
+
indexes: []
|
|
76
|
+
}
|
|
77
|
+
}];
|
|
78
|
+
}
|
|
79
|
+
if (!index.unique && columns.join() === table.key.join()) return [];
|
|
80
|
+
return [{
|
|
81
|
+
kind: "index",
|
|
82
|
+
name: shortSqlName(`${name}_${index.name}`),
|
|
83
|
+
unique: index.unique === true,
|
|
84
|
+
columns: index.unique ? index.eq : columns
|
|
85
|
+
}];
|
|
86
|
+
});
|
|
87
|
+
const retention = table.retention && retentionOrder(table);
|
|
88
|
+
if (retention && !retention.indexed) indexes.push({
|
|
89
|
+
kind: "index",
|
|
90
|
+
name: shortSqlName(`${name}__retention`),
|
|
91
|
+
unique: false,
|
|
92
|
+
columns: retention.columns
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
name,
|
|
96
|
+
columns: table.columns.map((column) => ({
|
|
97
|
+
name: column.name,
|
|
98
|
+
type: columnType(dialect, column, indexed.has(column.name)),
|
|
99
|
+
notNull: !column.nullable,
|
|
100
|
+
...column.default === void 0 ? {} : { default: column.default }
|
|
101
|
+
})),
|
|
102
|
+
key: table.key,
|
|
103
|
+
indexes
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
/** DDL for the tables' shapes: each table, then its indexes and index tables. */
|
|
107
|
+
const createTableStatements = (dialect, tables, tablePrefix = "") => {
|
|
108
|
+
const quote = (name) => quoteSql(dialect, name);
|
|
109
|
+
const list = (columns) => columns.map(quote).join(", ");
|
|
110
|
+
const render = (shape) => {
|
|
111
|
+
const definitions = shape.columns.map((column) => `${quote(column.name)} ${column.type}${column.notNull ? " NOT NULL" : ""}${column.default === void 0 ? "" : ` DEFAULT ${column.default}`}`);
|
|
112
|
+
definitions.push(`PRIMARY KEY (${list(shape.key)})`);
|
|
113
|
+
const after = [];
|
|
114
|
+
for (const index of shape.indexes) if (index.kind === "table") after.push(...render(index.table));
|
|
115
|
+
else if (dialect === "mysql") definitions.push(`${index.unique ? "UNIQUE " : ""}INDEX ${quote(index.name)} (${list(index.columns)})`);
|
|
116
|
+
else after.push(`CREATE ${index.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS ${quote(index.name)} ON ${quote(shape.name)} (${list(index.columns)})`);
|
|
117
|
+
return [`CREATE TABLE IF NOT EXISTS ${quote(shape.name)} (${definitions.join(", ")})`, ...after];
|
|
118
|
+
};
|
|
119
|
+
return sqlTableShapes(dialect, tables, tablePrefix).flatMap(render);
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* The order a prune walks a table's expired rows in, which ends with the
|
|
123
|
+
* key so it is total: the columns of the first index that leads with the
|
|
124
|
+
* retention column, or the key when it does, or else the column and the
|
|
125
|
+
* key, which then need an index of their own (`indexed` false).
|
|
126
|
+
*/
|
|
127
|
+
const retentionOrder = (table) => {
|
|
128
|
+
const { column } = table.retention;
|
|
129
|
+
for (const index of table.indexes) {
|
|
130
|
+
const columns = [...index.eq, ...require_adapter.indexOrderColumns(table, index)];
|
|
131
|
+
if (!index.unique && !isMultiIndex(table, index) && columns[0] === column) return {
|
|
132
|
+
columns,
|
|
133
|
+
indexed: true
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (table.key[0] === column) return {
|
|
137
|
+
columns: table.key,
|
|
138
|
+
indexed: true
|
|
139
|
+
};
|
|
140
|
+
return {
|
|
141
|
+
columns: [column, ...table.key.filter((field) => field !== column)],
|
|
142
|
+
indexed: false
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* The statements that delete up to `limit` expired rows of a table with
|
|
147
|
+
* retention, oldest first in {@link retentionOrder}: its multi-valued index
|
|
148
|
+
* entries, then the rows, which a concurrent write that moved the retention
|
|
149
|
+
* column past `before` keeps. `before` renders the bound each time it
|
|
150
|
+
* appears. MySQL deletes with ORDER BY and LIMIT. Elsewhere the cutoff is
|
|
151
|
+
* the last of the `limit` oldest expired rows in that order, a row value
|
|
152
|
+
* the index ranges over, and the shapes pass Supabase's apply RPC: no IN, no
|
|
153
|
+
* AS, aliases `i` and `b`.
|
|
154
|
+
*/
|
|
155
|
+
const pruneStatements = (dialect, table, tablePrefix, limit) => {
|
|
156
|
+
const quote = (name) => quoteSql(dialect, name);
|
|
157
|
+
const name = quote(`${tablePrefix}${table.name}`);
|
|
158
|
+
const at = quote(table.retention.column);
|
|
159
|
+
const order = retentionOrder(table).columns.map(quote);
|
|
160
|
+
const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
|
|
161
|
+
const oldest = (before) => `SELECT ${list("i")} FROM ${name} i WHERE i.${at} <= ${before()} ORDER BY ${list("i", " ASC")} LIMIT ${limit}`;
|
|
162
|
+
const doomed = (alias, before) => `${alias}.${at} <= ${before()} AND (${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1)`;
|
|
163
|
+
const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
|
|
164
|
+
const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
|
|
165
|
+
if (dialect === "mysql") return [...entries.map((index) => (before) => `DELETE e FROM ${index} e JOIN (${oldest(before)}) d ON ${keyOf("e", "d")}`), (before) => `DELETE FROM ${name} WHERE ${at} <= ${before()} ORDER BY ${order.join(", ")} LIMIT ${limit}`];
|
|
166
|
+
return [...entries.map((index) => (before) => `DELETE FROM ${index} WHERE EXISTS (SELECT * FROM ${name} b WHERE ${keyOf("b", index)} AND ${doomed("b", before)})`), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
|
|
167
|
+
};
|
|
168
|
+
//#endregion
|
|
169
|
+
exports.createTableStatements = createTableStatements;
|
|
170
|
+
exports.isMultiIndex = isMultiIndex;
|
|
171
|
+
exports.pruneStatements = pruneStatements;
|
|
172
|
+
exports.quoteSql = quoteSql;
|
|
173
|
+
exports.retentionOrder = retentionOrder;
|
|
174
|
+
exports.shortSqlName = shortSqlName;
|
|
175
|
+
exports.sqlTableShapes = sqlTableShapes;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { PhysicalIndex, PhysicalTable } from "../../../database/adapter.cjs";
|
|
2
|
+
//#region src/engine/database/sql/sqlSchema.d.ts
|
|
3
|
+
type SqlDialect = "postgresql" | "mysql" | "sqlite";
|
|
4
|
+
/**
|
|
5
|
+
* Quotes an identifier: backticks on MySQL, double quotes elsewhere. An ORM
|
|
6
|
+
* schema generator, or an executor that issues statements of its own, quotes
|
|
7
|
+
* names as the SQL core does with it, as the Prisma adapter's do.
|
|
8
|
+
*/
|
|
9
|
+
declare const quoteSql: (dialect: SqlDialect, name: string) => string;
|
|
10
|
+
declare const isMultiIndex: (table: PhysicalTable, index: PhysicalIndex) => boolean;
|
|
11
|
+
/** A column of a `SqlTableShape`. */
|
|
12
|
+
interface SqlColumnShape {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** The column's DDL type, collation included. */
|
|
15
|
+
readonly type: string;
|
|
16
|
+
readonly notNull: boolean;
|
|
17
|
+
readonly default?: number;
|
|
18
|
+
}
|
|
19
|
+
/** A table as the DDL creates it, which an ORM schema generator renders. */
|
|
20
|
+
interface SqlTableShape {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly columns: readonly SqlColumnShape[];
|
|
23
|
+
readonly key: readonly string[];
|
|
24
|
+
/** Indexes, and index tables for multi-valued fields, in declaration order. */
|
|
25
|
+
readonly indexes: readonly ({
|
|
26
|
+
readonly kind: "index";
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly unique: boolean;
|
|
29
|
+
readonly columns: readonly string[];
|
|
30
|
+
} | {
|
|
31
|
+
readonly kind: "table";
|
|
32
|
+
readonly table: SqlTableShape;
|
|
33
|
+
})[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Each table as the DDL creates it, for the DDL below and for ORM schema
|
|
37
|
+
* generators, such as the Drizzle and Prisma adapters': binary collation, one index per declared index, and an index
|
|
38
|
+
* table `<table>__<index>` for each index over a multi-valued field, keyed by
|
|
39
|
+
* every column it holds.
|
|
40
|
+
*/
|
|
41
|
+
declare const sqlTableShapes: (dialect: SqlDialect, tables: readonly PhysicalTable[], tablePrefix?: string) => SqlTableShape[];
|
|
42
|
+
/** DDL for the tables' shapes: each table, then its indexes and index tables. */
|
|
43
|
+
declare const createTableStatements: (dialect: SqlDialect, tables: readonly PhysicalTable[], tablePrefix?: string) => string[];
|
|
44
|
+
//#endregion
|
|
45
|
+
export { SqlColumnShape, SqlDialect, SqlTableShape, createTableStatements, isMultiIndex, quoteSql, sqlTableShapes };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { PhysicalIndex, PhysicalTable } from "../../../database/adapter.mjs";
|
|
2
|
+
//#region src/engine/database/sql/sqlSchema.d.ts
|
|
3
|
+
type SqlDialect = "postgresql" | "mysql" | "sqlite";
|
|
4
|
+
/**
|
|
5
|
+
* Quotes an identifier: backticks on MySQL, double quotes elsewhere. An ORM
|
|
6
|
+
* schema generator, or an executor that issues statements of its own, quotes
|
|
7
|
+
* names as the SQL core does with it, as the Prisma adapter's do.
|
|
8
|
+
*/
|
|
9
|
+
declare const quoteSql: (dialect: SqlDialect, name: string) => string;
|
|
10
|
+
declare const isMultiIndex: (table: PhysicalTable, index: PhysicalIndex) => boolean;
|
|
11
|
+
/** A column of a `SqlTableShape`. */
|
|
12
|
+
interface SqlColumnShape {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** The column's DDL type, collation included. */
|
|
15
|
+
readonly type: string;
|
|
16
|
+
readonly notNull: boolean;
|
|
17
|
+
readonly default?: number;
|
|
18
|
+
}
|
|
19
|
+
/** A table as the DDL creates it, which an ORM schema generator renders. */
|
|
20
|
+
interface SqlTableShape {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly columns: readonly SqlColumnShape[];
|
|
23
|
+
readonly key: readonly string[];
|
|
24
|
+
/** Indexes, and index tables for multi-valued fields, in declaration order. */
|
|
25
|
+
readonly indexes: readonly ({
|
|
26
|
+
readonly kind: "index";
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly unique: boolean;
|
|
29
|
+
readonly columns: readonly string[];
|
|
30
|
+
} | {
|
|
31
|
+
readonly kind: "table";
|
|
32
|
+
readonly table: SqlTableShape;
|
|
33
|
+
})[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Each table as the DDL creates it, for the DDL below and for ORM schema
|
|
37
|
+
* generators, such as the Drizzle and Prisma adapters': binary collation, one index per declared index, and an index
|
|
38
|
+
* table `<table>__<index>` for each index over a multi-valued field, keyed by
|
|
39
|
+
* every column it holds.
|
|
40
|
+
*/
|
|
41
|
+
declare const sqlTableShapes: (dialect: SqlDialect, tables: readonly PhysicalTable[], tablePrefix?: string) => SqlTableShape[];
|
|
42
|
+
/** DDL for the tables' shapes: each table, then its indexes and index tables. */
|
|
43
|
+
declare const createTableStatements: (dialect: SqlDialect, tables: readonly PhysicalTable[], tablePrefix?: string) => string[];
|
|
44
|
+
//#endregion
|
|
45
|
+
export { SqlColumnShape, SqlDialect, SqlTableShape, createTableStatements, isMultiIndex, quoteSql, sqlTableShapes };
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { DatabaseSchemaError, findPhysicalColumn, indexOrderColumns } from "../../../database/adapter.mjs";
|
|
2
|
+
//#region src/engine/database/sql/sqlSchema.ts
|
|
3
|
+
/**
|
|
4
|
+
* Quotes an identifier: backticks on MySQL, double quotes elsewhere. An ORM
|
|
5
|
+
* schema generator, or an executor that issues statements of its own, quotes
|
|
6
|
+
* names as the SQL core does with it, as the Prisma adapter's do.
|
|
7
|
+
*/
|
|
8
|
+
const quoteSql = (dialect, name) => dialect === "mysql" ? `\`${name.replaceAll("`", "``")}\`` : `"${name.replaceAll("\"", "\"\"")}"`;
|
|
9
|
+
const isMultiIndex = (table, index) => index.eq.some((column) => findPhysicalColumn(table, column).multi);
|
|
10
|
+
/** Keeps an identifier within PostgreSQL's 63 and MySQL's 64 characters. */
|
|
11
|
+
const shortSqlName = (name) => {
|
|
12
|
+
if (name.length <= 60) return name;
|
|
13
|
+
let hash = 2166136261;
|
|
14
|
+
for (const char of name) hash = Math.imul(hash ^ char.charCodeAt(0), 16777619) >>> 0;
|
|
15
|
+
return `${name.slice(0, 51)}_${hash.toString(36)}`;
|
|
16
|
+
};
|
|
17
|
+
/** The column type; `entry` types one value of a multi-valued column in its index table. */
|
|
18
|
+
const columnType = (dialect, column, indexed, entry = false) => {
|
|
19
|
+
if (column.multi && !entry || column.type === "json") return {
|
|
20
|
+
postgresql: "jsonb",
|
|
21
|
+
mysql: "json",
|
|
22
|
+
sqlite: "TEXT"
|
|
23
|
+
}[dialect];
|
|
24
|
+
if (column.type === "string") {
|
|
25
|
+
const length = column.maxLength ?? (indexed ? 255 : void 0);
|
|
26
|
+
if (dialect === "sqlite") return "TEXT";
|
|
27
|
+
if (dialect === "postgresql") return `${column.maxLength ? `varchar(${column.maxLength})` : "text"} COLLATE "C"`;
|
|
28
|
+
const charset = column.ascii ? "ascii COLLATE ascii_bin" : "utf8mb4 COLLATE utf8mb4_0900_bin";
|
|
29
|
+
return `${length ? `varchar(${length})` : "longtext"} CHARACTER SET ${charset}`;
|
|
30
|
+
}
|
|
31
|
+
if (column.type === "integer") return dialect === "sqlite" ? "INTEGER" : "bigint";
|
|
32
|
+
if (column.type === "number") return {
|
|
33
|
+
postgresql: "double precision",
|
|
34
|
+
mysql: "double",
|
|
35
|
+
sqlite: "REAL"
|
|
36
|
+
}[dialect];
|
|
37
|
+
return dialect === "sqlite" ? "INTEGER" : "boolean";
|
|
38
|
+
};
|
|
39
|
+
/** InnoDB's limit on the bytes of one key or index. */
|
|
40
|
+
const MYSQL_KEY_BYTES = 3072;
|
|
41
|
+
/** Refuses a MySQL key or index over InnoDB's limit before any DDL runs. */
|
|
42
|
+
const checkMysqlKey = (table, name, columns) => {
|
|
43
|
+
const bytes = columns.reduce((sum, column) => {
|
|
44
|
+
const { type, maxLength, ascii } = findPhysicalColumn(table, column);
|
|
45
|
+
if (type === "string") return sum + (maxLength ?? 255) * (ascii ? 1 : 4);
|
|
46
|
+
return sum + (type === "boolean" ? 1 : 8);
|
|
47
|
+
}, 0);
|
|
48
|
+
if (bytes > MYSQL_KEY_BYTES) throw new DatabaseSchemaError(`${table.name} ${name} needs ${bytes} bytes on MySQL, over its ${MYSQL_KEY_BYTES}; shorten its strings or mark ASCII ones ascii.`);
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Each table as the DDL creates it, for the DDL below and for ORM schema
|
|
52
|
+
* generators, such as the Drizzle and Prisma adapters': binary collation, one index per declared index, and an index
|
|
53
|
+
* table `<table>__<index>` for each index over a multi-valued field, keyed by
|
|
54
|
+
* every column it holds.
|
|
55
|
+
*/
|
|
56
|
+
const sqlTableShapes = (dialect, tables, tablePrefix = "") => tables.map((table) => {
|
|
57
|
+
const name = tablePrefix + table.name;
|
|
58
|
+
const indexed = /* @__PURE__ */ new Set([...table.key, ...table.indexes.flatMap(({ eq, sort }) => [...eq, ...sort])]);
|
|
59
|
+
if (dialect === "mysql") checkMysqlKey(table, "key", table.key);
|
|
60
|
+
const indexes = table.indexes.flatMap((index) => {
|
|
61
|
+
const columns = [...index.eq, ...indexOrderColumns(table, index)];
|
|
62
|
+
if (dialect === "mysql") checkMysqlKey(table, `index ${index.name}`, columns);
|
|
63
|
+
if (isMultiIndex(table, index)) {
|
|
64
|
+
const entries = columns.map((column) => ({
|
|
65
|
+
name: column,
|
|
66
|
+
type: columnType(dialect, findPhysicalColumn(table, column), true, true),
|
|
67
|
+
notNull: true
|
|
68
|
+
}));
|
|
69
|
+
return [{
|
|
70
|
+
kind: "table",
|
|
71
|
+
table: {
|
|
72
|
+
name: `${name}__${index.name}`,
|
|
73
|
+
columns: entries,
|
|
74
|
+
key: columns,
|
|
75
|
+
indexes: []
|
|
76
|
+
}
|
|
77
|
+
}];
|
|
78
|
+
}
|
|
79
|
+
if (!index.unique && columns.join() === table.key.join()) return [];
|
|
80
|
+
return [{
|
|
81
|
+
kind: "index",
|
|
82
|
+
name: shortSqlName(`${name}_${index.name}`),
|
|
83
|
+
unique: index.unique === true,
|
|
84
|
+
columns: index.unique ? index.eq : columns
|
|
85
|
+
}];
|
|
86
|
+
});
|
|
87
|
+
const retention = table.retention && retentionOrder(table);
|
|
88
|
+
if (retention && !retention.indexed) indexes.push({
|
|
89
|
+
kind: "index",
|
|
90
|
+
name: shortSqlName(`${name}__retention`),
|
|
91
|
+
unique: false,
|
|
92
|
+
columns: retention.columns
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
name,
|
|
96
|
+
columns: table.columns.map((column) => ({
|
|
97
|
+
name: column.name,
|
|
98
|
+
type: columnType(dialect, column, indexed.has(column.name)),
|
|
99
|
+
notNull: !column.nullable,
|
|
100
|
+
...column.default === void 0 ? {} : { default: column.default }
|
|
101
|
+
})),
|
|
102
|
+
key: table.key,
|
|
103
|
+
indexes
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
/** DDL for the tables' shapes: each table, then its indexes and index tables. */
|
|
107
|
+
const createTableStatements = (dialect, tables, tablePrefix = "") => {
|
|
108
|
+
const quote = (name) => quoteSql(dialect, name);
|
|
109
|
+
const list = (columns) => columns.map(quote).join(", ");
|
|
110
|
+
const render = (shape) => {
|
|
111
|
+
const definitions = shape.columns.map((column) => `${quote(column.name)} ${column.type}${column.notNull ? " NOT NULL" : ""}${column.default === void 0 ? "" : ` DEFAULT ${column.default}`}`);
|
|
112
|
+
definitions.push(`PRIMARY KEY (${list(shape.key)})`);
|
|
113
|
+
const after = [];
|
|
114
|
+
for (const index of shape.indexes) if (index.kind === "table") after.push(...render(index.table));
|
|
115
|
+
else if (dialect === "mysql") definitions.push(`${index.unique ? "UNIQUE " : ""}INDEX ${quote(index.name)} (${list(index.columns)})`);
|
|
116
|
+
else after.push(`CREATE ${index.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS ${quote(index.name)} ON ${quote(shape.name)} (${list(index.columns)})`);
|
|
117
|
+
return [`CREATE TABLE IF NOT EXISTS ${quote(shape.name)} (${definitions.join(", ")})`, ...after];
|
|
118
|
+
};
|
|
119
|
+
return sqlTableShapes(dialect, tables, tablePrefix).flatMap(render);
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* The order a prune walks a table's expired rows in, which ends with the
|
|
123
|
+
* key so it is total: the columns of the first index that leads with the
|
|
124
|
+
* retention column, or the key when it does, or else the column and the
|
|
125
|
+
* key, which then need an index of their own (`indexed` false).
|
|
126
|
+
*/
|
|
127
|
+
const retentionOrder = (table) => {
|
|
128
|
+
const { column } = table.retention;
|
|
129
|
+
for (const index of table.indexes) {
|
|
130
|
+
const columns = [...index.eq, ...indexOrderColumns(table, index)];
|
|
131
|
+
if (!index.unique && !isMultiIndex(table, index) && columns[0] === column) return {
|
|
132
|
+
columns,
|
|
133
|
+
indexed: true
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (table.key[0] === column) return {
|
|
137
|
+
columns: table.key,
|
|
138
|
+
indexed: true
|
|
139
|
+
};
|
|
140
|
+
return {
|
|
141
|
+
columns: [column, ...table.key.filter((field) => field !== column)],
|
|
142
|
+
indexed: false
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* The statements that delete up to `limit` expired rows of a table with
|
|
147
|
+
* retention, oldest first in {@link retentionOrder}: its multi-valued index
|
|
148
|
+
* entries, then the rows, which a concurrent write that moved the retention
|
|
149
|
+
* column past `before` keeps. `before` renders the bound each time it
|
|
150
|
+
* appears. MySQL deletes with ORDER BY and LIMIT. Elsewhere the cutoff is
|
|
151
|
+
* the last of the `limit` oldest expired rows in that order, a row value
|
|
152
|
+
* the index ranges over, and the shapes pass Supabase's apply RPC: no IN, no
|
|
153
|
+
* AS, aliases `i` and `b`.
|
|
154
|
+
*/
|
|
155
|
+
const pruneStatements = (dialect, table, tablePrefix, limit) => {
|
|
156
|
+
const quote = (name) => quoteSql(dialect, name);
|
|
157
|
+
const name = quote(`${tablePrefix}${table.name}`);
|
|
158
|
+
const at = quote(table.retention.column);
|
|
159
|
+
const order = retentionOrder(table).columns.map(quote);
|
|
160
|
+
const list = (alias, direction = "") => order.map((column) => `${alias}.${column}${direction}`).join(", ");
|
|
161
|
+
const oldest = (before) => `SELECT ${list("i")} FROM ${name} i WHERE i.${at} <= ${before()} ORDER BY ${list("i", " ASC")} LIMIT ${limit}`;
|
|
162
|
+
const doomed = (alias, before) => `${alias}.${at} <= ${before()} AND (${list(alias)}) <= (SELECT ${list("b")} FROM (${oldest(before)}) b ORDER BY ${list("b", " DESC")} LIMIT 1)`;
|
|
163
|
+
const entries = table.indexes.filter((index) => isMultiIndex(table, index)).map((index) => quote(`${tablePrefix}${table.name}__${index.name}`));
|
|
164
|
+
const keyOf = (left, right) => table.key.map((column) => `${left}.${quote(column)} = ${right}.${quote(column)}`).join(" AND ");
|
|
165
|
+
if (dialect === "mysql") return [...entries.map((index) => (before) => `DELETE e FROM ${index} e JOIN (${oldest(before)}) d ON ${keyOf("e", "d")}`), (before) => `DELETE FROM ${name} WHERE ${at} <= ${before()} ORDER BY ${order.join(", ")} LIMIT ${limit}`];
|
|
166
|
+
return [...entries.map((index) => (before) => `DELETE FROM ${index} WHERE EXISTS (SELECT * FROM ${name} b WHERE ${keyOf("b", index)} AND ${doomed("b", before)})`), (before) => `DELETE FROM ${name} WHERE ${doomed(name, before)}`];
|
|
167
|
+
};
|
|
168
|
+
//#endregion
|
|
169
|
+
export { createTableStatements, isMultiIndex, pruneStatements, quoteSql, retentionOrder, shortSqlName, sqlTableShapes };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
const require_schema = require("../core/schema.cjs");
|
|
2
|
+
const require_aggregateBatching = require("../database/aggregateBatching.cjs");
|
|
3
|
+
const require_resolveSchema = require("../database/resolveSchema.cjs");
|
|
4
|
+
const require_fence = require("../database/fence.cjs");
|
|
5
|
+
const require_schemaSettings = require("./schemaSettings.cjs");
|
|
6
|
+
const require_engineMigrator = require("./engineMigrator.cjs");
|
|
7
|
+
//#region src/engine/db/coreDatabase.ts
|
|
8
|
+
/** Core's tables: the tables every provider's tooling creates. */
|
|
9
|
+
const coreSchema = require_resolveSchema.resolveSchema([require_schema.coreModule]);
|
|
10
|
+
/** The settings rows a provider's database is fenced by. */
|
|
11
|
+
const coreSettings = {
|
|
12
|
+
[require_fence.ENGINE_SCHEMA_KEY]: "1",
|
|
13
|
+
"schema.core": require_schema.HOT_UPDATER_SCHEMA_VERSION
|
|
14
|
+
};
|
|
15
|
+
/** What `hot-updater db` creates for a server without plugins. */
|
|
16
|
+
const coreTarget = {
|
|
17
|
+
schema: coreSchema,
|
|
18
|
+
settings: coreSettings
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The log and lease tables a database with `aggregateBatching` writes
|
|
22
|
+
* beside core's and the plugins', which a provider's access policy names,
|
|
23
|
+
* such as the managed AWS server's IAM role.
|
|
24
|
+
*/
|
|
25
|
+
const aggregateBatchingTables = require_resolveSchema.resolveSchema([require_aggregateBatching.aggregateBatchingModule]).tables;
|
|
26
|
+
/** Each plugin's `schema.<id>` settings row. */
|
|
27
|
+
const pluginSettings = (plugins) => Object.fromEntries(plugins.map(({ id, schemaVersion }) => [`schema.${id}`, schemaVersion]));
|
|
28
|
+
/** A plugin's schema as the engine resolves it: prefixed by its id unless it keeps its names. */
|
|
29
|
+
const pluginModule = ({ id, schema, namespace }) => ({
|
|
30
|
+
id,
|
|
31
|
+
schema,
|
|
32
|
+
...namespace === false ? {} : { namespace: id }
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* What `hot-updater db` creates for a server's plugins: core's tables, then
|
|
36
|
+
* each plugin's tables with its `schema.<id>` settings row.
|
|
37
|
+
*/
|
|
38
|
+
const toolingTargetOf = (plugins) => plugins.length === 0 ? coreTarget : {
|
|
39
|
+
schema: require_resolveSchema.resolveSchema([require_schema.coreModule, ...plugins.map(pluginModule)]),
|
|
40
|
+
settings: {
|
|
41
|
+
...coreSettings,
|
|
42
|
+
...pluginSettings(plugins)
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Creates core's tables and those of `plugins`, then writes their settings
|
|
47
|
+
* rows: what a provider's setup runs for the plugins its server runs.
|
|
48
|
+
*/
|
|
49
|
+
const migrateCoreSchema = (adapter, name, plugins = []) => {
|
|
50
|
+
const { schema, settings } = toolingTargetOf(plugins);
|
|
51
|
+
return require_schemaSettings.migrateSchema(adapter, name, schema.tables, settings);
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* A provider's database on the storage engine: its adapter behind the schema
|
|
55
|
+
* fence, which checks core's settings rows before the first read or write
|
|
56
|
+
* and names `hot-updater db migrate` when they are missing or stale. When the
|
|
57
|
+
* adapter creates its own tables, `hot-updater db migrate` creates core's
|
|
58
|
+
* tables and the server's plugin tables, then writes their settings rows.
|
|
59
|
+
*/
|
|
60
|
+
const createEngineDatabase = ({ name, adapter, onCachedRoutesChange, aggregateBatching, retry, readSettings: readStored }) => {
|
|
61
|
+
const fenced = require_fence.withSchemaFence(adapter, name, coreSettings);
|
|
62
|
+
const createMigrator = ({ schema, settings } = coreTarget) => require_engineMigrator.createEngineMigrator({
|
|
63
|
+
adapterName: name,
|
|
64
|
+
adapter,
|
|
65
|
+
schema,
|
|
66
|
+
settings,
|
|
67
|
+
readSettings: readStored ?? (async () => {
|
|
68
|
+
const rows = await require_fence.readSettings(adapter, Object.keys(settings)).catch((error) => {
|
|
69
|
+
if (require_fence.isMissingSchemaError(error)) return [];
|
|
70
|
+
throw error;
|
|
71
|
+
});
|
|
72
|
+
return new Map(rows.flatMap((row) => row === null ? [] : [[String(row.key), row.value]]));
|
|
73
|
+
})
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
name,
|
|
77
|
+
adapter: fenced,
|
|
78
|
+
...onCachedRoutesChange === void 0 ? {} : { onCachedRoutesChange },
|
|
79
|
+
...aggregateBatching ? { aggregateBatching } : {},
|
|
80
|
+
...retry === void 0 ? {} : { retry },
|
|
81
|
+
...adapter.dispose === void 0 ? {} : { dispose: () => adapter.dispose() },
|
|
82
|
+
...adapter.migrations === void 0 ? {} : { createMigrator }
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.aggregateBatchingTables = aggregateBatchingTables;
|
|
87
|
+
exports.coreSchema = coreSchema;
|
|
88
|
+
exports.coreSettings = coreSettings;
|
|
89
|
+
exports.coreTarget = coreTarget;
|
|
90
|
+
exports.createEngineDatabase = createEngineDatabase;
|
|
91
|
+
exports.migrateCoreSchema = migrateCoreSchema;
|
|
92
|
+
exports.pluginModule = pluginModule;
|
|
93
|
+
exports.pluginSettings = pluginSettings;
|
|
94
|
+
exports.toolingTargetOf = toolingTargetOf;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { DatabaseAdapter, PhysicalTable } from "../../database/adapter.cjs";
|
|
2
|
+
import { ModelShape } from "../../serverPlugin/definitions.cjs";
|
|
3
|
+
import { ResolvedSchema } from "../database/resolveSchema.cjs";
|
|
4
|
+
import { RetryOptions } from "../database/engineTransaction.cjs";
|
|
5
|
+
import { AggregateBatching } from "../../types/databaseConfig.cjs";
|
|
6
|
+
import { SchemaSettings } from "../database/fence.cjs";
|
|
7
|
+
import { ToolingDatabase, ToolingTarget } from "./types.cjs";
|
|
8
|
+
import "../../index.cjs";
|
|
9
|
+
//#region src/engine/db/coreDatabase.d.ts
|
|
10
|
+
/** Core's tables: the tables every provider's tooling creates. */
|
|
11
|
+
declare const coreSchema: ResolvedSchema;
|
|
12
|
+
/** The settings rows a provider's database is fenced by. */
|
|
13
|
+
declare const coreSettings: SchemaSettings;
|
|
14
|
+
/** What `hot-updater db` creates for a server without plugins. */
|
|
15
|
+
declare const coreTarget: ToolingTarget;
|
|
16
|
+
/**
|
|
17
|
+
* The log and lease tables a database with `aggregateBatching` writes
|
|
18
|
+
* beside core's and the plugins', which a provider's access policy names,
|
|
19
|
+
* such as the managed AWS server's IAM role.
|
|
20
|
+
*/
|
|
21
|
+
declare const aggregateBatchingTables: readonly PhysicalTable[];
|
|
22
|
+
/** A plugin as the tooling reads it: its id, its tables, and their version. */
|
|
23
|
+
interface PluginTables {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly schemaVersion: string;
|
|
26
|
+
readonly schema: Readonly<Record<string, ModelShape>>;
|
|
27
|
+
/** `false` keeps the declared table names instead of prefixing them with the id. */
|
|
28
|
+
readonly namespace?: false;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* What `hot-updater db` creates for a server's plugins: core's tables, then
|
|
32
|
+
* each plugin's tables with its `schema.<id>` settings row.
|
|
33
|
+
*/
|
|
34
|
+
declare const toolingTargetOf: (plugins: readonly PluginTables[]) => ToolingTarget;
|
|
35
|
+
/**
|
|
36
|
+
* Creates core's tables and those of `plugins`, then writes their settings
|
|
37
|
+
* rows: what a provider's setup runs for the plugins its server runs.
|
|
38
|
+
*/
|
|
39
|
+
declare const migrateCoreSchema: (adapter: DatabaseAdapter, name: string, plugins?: readonly PluginTables[]) => Promise<void>;
|
|
40
|
+
/** `createEngineDatabase`'s options. */
|
|
41
|
+
interface EngineDatabaseOptions {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly adapter: DatabaseAdapter;
|
|
44
|
+
/**
|
|
45
|
+
* Purges a CDN's copies of the cacheable client routes. Core calls it after
|
|
46
|
+
* a committed write that changes what those routes answer, since only core
|
|
47
|
+
* knows which of its tables they read.
|
|
48
|
+
*/
|
|
49
|
+
readonly onCachedRoutesChange?: () => Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Batches aggregates declared `batched`, for stores that bill each write;
|
|
52
|
+
* `false` or absent commits them with each transaction.
|
|
53
|
+
*/
|
|
54
|
+
readonly aggregateBatching?: AggregateBatching | false;
|
|
55
|
+
/**
|
|
56
|
+
* How a transaction that conflicts with another write runs again: more
|
|
57
|
+
* attempts for a store many writers contend on, and `onRetry` to observe
|
|
58
|
+
* each rerun. 8 attempts by default.
|
|
59
|
+
*/
|
|
60
|
+
readonly retry?: RetryOptions;
|
|
61
|
+
/**
|
|
62
|
+
* How `hot-updater db migrate` reads the stored settings rows, by key; by
|
|
63
|
+
* default through the adapter. An adapter whose earlier versions stored
|
|
64
|
+
* them elsewhere reads them itself, so the migrator refuses a database
|
|
65
|
+
* from before the storage engine.
|
|
66
|
+
*/
|
|
67
|
+
readonly readSettings?: () => Promise<ReadonlyMap<string, unknown>>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A provider's database on the storage engine: its adapter behind the schema
|
|
71
|
+
* fence, which checks core's settings rows before the first read or write
|
|
72
|
+
* and names `hot-updater db migrate` when they are missing or stale. When the
|
|
73
|
+
* adapter creates its own tables, `hot-updater db migrate` creates core's
|
|
74
|
+
* tables and the server's plugin tables, then writes their settings rows.
|
|
75
|
+
*/
|
|
76
|
+
declare const createEngineDatabase: ({ name, adapter, onCachedRoutesChange, aggregateBatching, retry, readSettings: readStored }: EngineDatabaseOptions) => ToolingDatabase;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { EngineDatabaseOptions, PluginTables, aggregateBatchingTables, coreSchema, coreSettings, coreTarget, createEngineDatabase, migrateCoreSchema, toolingTargetOf };
|