@cogenta/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache/entry.d.ts +32 -0
- package/dist/cache/entry.d.ts.map +1 -0
- package/dist/cache/entry.js +72 -0
- package/dist/cache/entry.js.map +1 -0
- package/dist/cache/file.d.ts +16 -0
- package/dist/cache/file.d.ts.map +1 -0
- package/dist/cache/file.js +191 -0
- package/dist/cache/file.js.map +1 -0
- package/dist/cache/index.d.ts +20 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/cache/index.js +25 -0
- package/dist/cache/index.js.map +1 -0
- package/dist/cache/memory.d.ts +11 -0
- package/dist/cache/memory.d.ts.map +1 -0
- package/dist/cache/memory.js +89 -0
- package/dist/cache/memory.js.map +1 -0
- package/dist/cache/redis.d.ts +54 -0
- package/dist/cache/redis.d.ts.map +1 -0
- package/dist/cache/redis.js +167 -0
- package/dist/cache/redis.js.map +1 -0
- package/dist/cache/types.d.ts +37 -0
- package/dist/cache/types.d.ts.map +1 -0
- package/dist/cache/types.js +2 -0
- package/dist/cache/types.js.map +1 -0
- package/dist/config/define-config.d.ts +19 -0
- package/dist/config/define-config.d.ts.map +1 -0
- package/dist/config/define-config.js +20 -0
- package/dist/config/define-config.js.map +1 -0
- package/dist/config/env.d.ts +30 -0
- package/dist/config/env.d.ts.map +1 -0
- package/dist/config/env.js +139 -0
- package/dist/config/env.js.map +1 -0
- package/dist/config/index.d.ts +7 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +5 -0
- package/dist/config/index.js.map +1 -0
- package/dist/config/load-config.d.ts +30 -0
- package/dist/config/load-config.d.ts.map +1 -0
- package/dist/config/load-config.js +83 -0
- package/dist/config/load-config.js.map +1 -0
- package/dist/config/resolve-config.d.ts +14 -0
- package/dist/config/resolve-config.d.ts.map +1 -0
- package/dist/config/resolve-config.js +146 -0
- package/dist/config/resolve-config.js.map +1 -0
- package/dist/config/schema.d.ts +68 -0
- package/dist/config/schema.d.ts.map +1 -0
- package/dist/config/schema.js +75 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/config/types.d.ts +126 -0
- package/dist/config/types.d.ts.map +1 -0
- package/dist/config/types.js +6 -0
- package/dist/config/types.js.map +1 -0
- package/dist/db/dialect.d.ts +64 -0
- package/dist/db/dialect.d.ts.map +1 -0
- package/dist/db/dialect.js +147 -0
- package/dist/db/dialect.js.map +1 -0
- package/dist/db/drizzle.d.ts +35 -0
- package/dist/db/drizzle.d.ts.map +1 -0
- package/dist/db/drizzle.js +128 -0
- package/dist/db/drizzle.js.map +1 -0
- package/dist/db/index.d.ts +28 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +30 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/mysql.d.ts +39 -0
- package/dist/db/mysql.d.ts.map +1 -0
- package/dist/db/mysql.js +224 -0
- package/dist/db/mysql.js.map +1 -0
- package/dist/db/postgres.d.ts +47 -0
- package/dist/db/postgres.d.ts.map +1 -0
- package/dist/db/postgres.js +174 -0
- package/dist/db/postgres.js.map +1 -0
- package/dist/db/sqlite.d.ts +41 -0
- package/dist/db/sqlite.d.ts.map +1 -0
- package/dist/db/sqlite.js +213 -0
- package/dist/db/sqlite.js.map +1 -0
- package/dist/db/types.d.ts +71 -0
- package/dist/db/types.d.ts.map +1 -0
- package/dist/db/types.js +2 -0
- package/dist/db/types.js.map +1 -0
- package/dist/drivers/index.d.ts +5 -0
- package/dist/drivers/index.d.ts.map +1 -0
- package/dist/drivers/index.js +3 -0
- package/dist/drivers/index.js.map +1 -0
- package/dist/drivers/registry.d.ts +27 -0
- package/dist/drivers/registry.d.ts.map +1 -0
- package/dist/drivers/registry.js +162 -0
- package/dist/drivers/registry.js.map +1 -0
- package/dist/drivers/types.d.ts +59 -0
- package/dist/drivers/types.d.ts.map +1 -0
- package/dist/drivers/types.js +2 -0
- package/dist/drivers/types.js.map +1 -0
- package/dist/errors/codes.d.ts +8 -0
- package/dist/errors/codes.d.ts.map +1 -0
- package/dist/errors/codes.js +201 -0
- package/dist/errors/codes.js.map +1 -0
- package/dist/errors/cogenta-error.d.ts +38 -0
- package/dist/errors/cogenta-error.d.ts.map +1 -0
- package/dist/errors/cogenta-error.js +31 -0
- package/dist/errors/cogenta-error.js.map +1 -0
- package/dist/errors/index.d.ts +5 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +3 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/id.d.ts +5 -0
- package/dist/id.d.ts.map +1 -0
- package/dist/id.js +82 -0
- package/dist/id.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/logger/index.d.ts +5 -0
- package/dist/logger/index.d.ts.map +1 -0
- package/dist/logger/index.js +4 -0
- package/dist/logger/index.js.map +1 -0
- package/dist/logger/logger.d.ts +10 -0
- package/dist/logger/logger.d.ts.map +1 -0
- package/dist/logger/logger.js +108 -0
- package/dist/logger/logger.js.map +1 -0
- package/dist/logger/redact.d.ts +12 -0
- package/dist/logger/redact.d.ts.map +1 -0
- package/dist/logger/redact.js +96 -0
- package/dist/logger/redact.js.map +1 -0
- package/dist/logger/types.d.ts +32 -0
- package/dist/logger/types.d.ts.map +1 -0
- package/dist/logger/types.js +3 -0
- package/dist/logger/types.js.map +1 -0
- package/dist/media/format-sniff.d.ts +22 -0
- package/dist/media/format-sniff.d.ts.map +1 -0
- package/dist/media/format-sniff.js +77 -0
- package/dist/media/format-sniff.js.map +1 -0
- package/dist/media/index.d.ts +5 -0
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/index.js +4 -0
- package/dist/media/index.js.map +1 -0
- package/dist/media/store.d.ts +13 -0
- package/dist/media/store.d.ts.map +1 -0
- package/dist/media/store.js +211 -0
- package/dist/media/store.js.map +1 -0
- package/dist/media/types.d.ts +70 -0
- package/dist/media/types.d.ts.map +1 -0
- package/dist/media/types.js +2 -0
- package/dist/media/types.js.map +1 -0
- package/dist/migrations/index.d.ts +4 -0
- package/dist/migrations/index.d.ts.map +1 -0
- package/dist/migrations/index.js +2 -0
- package/dist/migrations/index.js.map +1 -0
- package/dist/migrations/migrator.d.ts +18 -0
- package/dist/migrations/migrator.d.ts.map +1 -0
- package/dist/migrations/migrator.js +247 -0
- package/dist/migrations/migrator.js.map +1 -0
- package/dist/migrations/types.d.ts +63 -0
- package/dist/migrations/types.d.ts.map +1 -0
- package/dist/migrations/types.js +2 -0
- package/dist/migrations/types.js.map +1 -0
- package/dist/queue/bullmq.d.ts +119 -0
- package/dist/queue/bullmq.d.ts.map +1 -0
- package/dist/queue/bullmq.js +391 -0
- package/dist/queue/bullmq.js.map +1 -0
- package/dist/queue/database.d.ts +17 -0
- package/dist/queue/database.d.ts.map +1 -0
- package/dist/queue/database.js +242 -0
- package/dist/queue/database.js.map +1 -0
- package/dist/queue/index.d.ts +7 -0
- package/dist/queue/index.d.ts.map +1 -0
- package/dist/queue/index.js +4 -0
- package/dist/queue/index.js.map +1 -0
- package/dist/queue/types.d.ts +61 -0
- package/dist/queue/types.d.ts.map +1 -0
- package/dist/queue/types.js +2 -0
- package/dist/queue/types.js.map +1 -0
- package/dist/storage/index.d.ts +14 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +17 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/key.d.ts +12 -0
- package/dist/storage/key.d.ts.map +1 -0
- package/dist/storage/key.js +43 -0
- package/dist/storage/key.js.map +1 -0
- package/dist/storage/local.d.ts +23 -0
- package/dist/storage/local.d.ts.map +1 -0
- package/dist/storage/local.js +204 -0
- package/dist/storage/local.js.map +1 -0
- package/dist/storage/s3.d.ts +54 -0
- package/dist/storage/s3.d.ts.map +1 -0
- package/dist/storage/s3.js +240 -0
- package/dist/storage/s3.js.map +1 -0
- package/dist/storage/types.d.ts +45 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +2 -0
- package/dist/storage/types.js.map +1 -0
- package/package.json +82 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { CogentaError } from '../errors/index.js';
|
|
2
|
+
const RAW = Symbol('cogenta.sql.raw');
|
|
3
|
+
function isRaw(value) {
|
|
4
|
+
return typeof value === 'object' && value !== null && RAW in value;
|
|
5
|
+
}
|
|
6
|
+
function isFragment(value) {
|
|
7
|
+
return (typeof value === 'object' &&
|
|
8
|
+
value !== null &&
|
|
9
|
+
'parts' in value &&
|
|
10
|
+
'values' in value &&
|
|
11
|
+
Array.isArray(value.parts));
|
|
12
|
+
}
|
|
13
|
+
/** Tagged template for SQL. Interpolated values are always bound, never inlined. */
|
|
14
|
+
export function sql(strings, ...values) {
|
|
15
|
+
const parts = [];
|
|
16
|
+
const bound = [];
|
|
17
|
+
let current = strings[0] ?? '';
|
|
18
|
+
for (const [index, value] of values.entries()) {
|
|
19
|
+
const next = strings[index + 1] ?? '';
|
|
20
|
+
if (isRaw(value)) {
|
|
21
|
+
current += value[RAW] + next;
|
|
22
|
+
}
|
|
23
|
+
else if (isFragment(value)) {
|
|
24
|
+
// Nested fragment: splice its text and values into this one.
|
|
25
|
+
current += value.parts[0] ?? '';
|
|
26
|
+
for (const [nestedIndex, nestedValue] of value.values.entries()) {
|
|
27
|
+
parts.push(current);
|
|
28
|
+
bound.push(nestedValue);
|
|
29
|
+
current = value.parts[nestedIndex + 1] ?? '';
|
|
30
|
+
}
|
|
31
|
+
current += next;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
parts.push(current);
|
|
35
|
+
bound.push(value);
|
|
36
|
+
current = next;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
parts.push(current);
|
|
40
|
+
return { parts, values: bound };
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Text inserted verbatim, with no binding.
|
|
44
|
+
*
|
|
45
|
+
* Only ever for SQL the code itself wrote. A value that reached the process from
|
|
46
|
+
* outside must go through `sql` so it is bound — this is the one function in the
|
|
47
|
+
* layer that can produce an injection, and it is named to say so.
|
|
48
|
+
*/
|
|
49
|
+
export function unsafeRaw(text) {
|
|
50
|
+
return sql `${{ [RAW]: text }}`;
|
|
51
|
+
}
|
|
52
|
+
const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
53
|
+
/**
|
|
54
|
+
* Quotes a table or column name for the dialect. Postgres and SQLite use double
|
|
55
|
+
* quotes, MySQL uses backticks.
|
|
56
|
+
*/
|
|
57
|
+
export function identifier(name, dialect) {
|
|
58
|
+
if (!IDENTIFIER.test(name)) {
|
|
59
|
+
throw new CogentaError({
|
|
60
|
+
code: 'DB_DIALECT_UNSUPPORTED',
|
|
61
|
+
message: `"${name}" is not a usable SQL identifier.`,
|
|
62
|
+
hint: 'Table and column names must start with a letter or underscore and contain only letters, digits and underscores.',
|
|
63
|
+
details: { name },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return unsafeRaw(dialect === 'mysql' ? `\`${name}\`` : `"${name}"`);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Adapts a bound value to what the driver for this dialect accepts.
|
|
70
|
+
*
|
|
71
|
+
* SQLite has no boolean and no date type, so `true` and a `Date` have to become
|
|
72
|
+
* an integer and a string. Doing it here, once, is what stops every call site
|
|
73
|
+
* from having to know which database it is talking to.
|
|
74
|
+
*/
|
|
75
|
+
export function encodeValue(value, dialect) {
|
|
76
|
+
if (value === undefined)
|
|
77
|
+
return null;
|
|
78
|
+
if (typeof value === 'boolean') {
|
|
79
|
+
return dialect === 'postgres' ? value : Number(value);
|
|
80
|
+
}
|
|
81
|
+
if (value instanceof Date) {
|
|
82
|
+
// A timestamp is stored in UTC everywhere. MySQL has no time zone on
|
|
83
|
+
// `datetime`, so it gets a UTC string rather than an offset it would drop.
|
|
84
|
+
if (dialect === 'postgres')
|
|
85
|
+
return value;
|
|
86
|
+
if (dialect === 'mysql')
|
|
87
|
+
return value.toISOString().slice(0, 19).replace('T', ' ');
|
|
88
|
+
return value.toISOString();
|
|
89
|
+
}
|
|
90
|
+
if (typeof value === 'bigint')
|
|
91
|
+
return value.toString();
|
|
92
|
+
if (value !== null && typeof value === 'object' && !Buffer.isBuffer(value)) {
|
|
93
|
+
// Objects and arrays are stored as JSON text. Postgres would accept a native
|
|
94
|
+
// array here, but only for some column types, and the difference is not
|
|
95
|
+
// worth exposing.
|
|
96
|
+
return JSON.stringify(value);
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
/** Renders a fragment into the exact string and parameters this dialect expects. */
|
|
101
|
+
export function compile(fragment, dialect) {
|
|
102
|
+
let text = fragment.parts[0] ?? '';
|
|
103
|
+
const params = [];
|
|
104
|
+
for (const [index, value] of fragment.values.entries()) {
|
|
105
|
+
params.push(encodeValue(value, dialect));
|
|
106
|
+
text += dialect === 'postgres' ? `$${params.length}` : '?';
|
|
107
|
+
text += fragment.parts[index + 1] ?? '';
|
|
108
|
+
}
|
|
109
|
+
return { text, params };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Statements that hand back rows: a read, or a write with `RETURNING`.
|
|
113
|
+
*
|
|
114
|
+
* Shared by every driver so `rows` and `rowsAffected` mean the same thing on all
|
|
115
|
+
* three dialects. Left to each driver, Postgres would report a `SELECT` of two
|
|
116
|
+
* rows as "two rows affected" while SQLite reported zero — the kind of quiet
|
|
117
|
+
* divergence the contract suite exists to prevent.
|
|
118
|
+
*/
|
|
119
|
+
export function returnsRows(text) {
|
|
120
|
+
return READ_STATEMENT.test(text) || RETURNING_CLAUSE.test(text);
|
|
121
|
+
}
|
|
122
|
+
/** True when the statement creates, changes or removes rows. */
|
|
123
|
+
export function isWrite(text) {
|
|
124
|
+
return !READ_STATEMENT.test(text);
|
|
125
|
+
}
|
|
126
|
+
const READ_STATEMENT = /^[\s(]*(?:select|with|pragma|explain|show|values|describe)\b/i;
|
|
127
|
+
const RETURNING_CLAUSE = /\breturning\b/i;
|
|
128
|
+
/**
|
|
129
|
+
* Renders a `LIMIT` count as a literal rather than a bound parameter.
|
|
130
|
+
*
|
|
131
|
+
* MySQL prepared statements reject a placeholder there — `LIMIT ?` fails with
|
|
132
|
+
* "Incorrect arguments to mysqld_stmt_execute". Rather than encode that as one
|
|
133
|
+
* more dialect difference for callers to remember, the count is inlined
|
|
134
|
+
* everywhere. It is validated as a non-negative integer first, so inlining it
|
|
135
|
+
* cannot inject: this is the only place a number reaches the SQL text directly.
|
|
136
|
+
*/
|
|
137
|
+
export function limit(count) {
|
|
138
|
+
if (!Number.isSafeInteger(count) || count < 0) {
|
|
139
|
+
throw new CogentaError({
|
|
140
|
+
code: 'DB_DIALECT_UNSUPPORTED',
|
|
141
|
+
message: `A LIMIT must be a non-negative whole number, received ${String(count)}.`,
|
|
142
|
+
hint: 'Pass a plain integer, such as the batch size of a worker.',
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return unsafeRaw(String(count));
|
|
146
|
+
}
|
|
147
|
+
//# sourceMappingURL=dialect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dialect.js","sourceRoot":"","sources":["../../src/db/dialect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAgBjD,MAAM,GAAG,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAA;AAMrC,SAAS,KAAK,CAAC,KAAc;IAC3B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,CAAA;AACpE,CAAC;AAED,SAAS,UAAU,CAAC,KAAc;IAChC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,IAAI,KAAK;QAChB,QAAQ,IAAI,KAAK;QACjB,KAAK,CAAC,OAAO,CAAE,KAAqB,CAAC,KAAK,CAAC,CAC5C,CAAA;AACH,CAAC;AAED,oFAAoF;AACpF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,CAAC,IAAY;IAClC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACnC,CAAC;AAED,MAAM,cAAc,GAAG,+DAA+D,CAAA;AACtF,MAAM,gBAAgB,GAAG,gBAAgB,CAAA;AAEzC;;;;;;;;GAQG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,YAAY,CAAC;YACrB,IAAI,EAAE,wBAAwB;YAC9B,OAAO,EAAE,yDAAyD,MAAM,CAAC,KAAK,CAAC,GAAG;YAClF,IAAI,EAAE,2DAA2D;SAClE,CAAC,CAAA;IACJ,CAAC;IACD,OAAO,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAA;AACjC,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { DrizzleConfig } from 'drizzle-orm';
|
|
2
|
+
import { type MySqlRemoteDatabase } from 'drizzle-orm/mysql-proxy';
|
|
3
|
+
import { type PgRemoteDatabase } from 'drizzle-orm/pg-proxy';
|
|
4
|
+
import { type SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
|
|
5
|
+
import type { DatabaseHandle, SqlExecutor, TransactionOptions } from './types.js';
|
|
6
|
+
export type { MySqlRemoteDatabase, PgRemoteDatabase, SqliteRemoteDatabase };
|
|
7
|
+
/**
|
|
8
|
+
* Runs Drizzle work inside a handle transaction, on every dialect.
|
|
9
|
+
*
|
|
10
|
+
* Replaces `db.transaction()`, which a proxy instance cannot provide. The
|
|
11
|
+
* instance is rebuilt on the transaction's executor, so every statement it
|
|
12
|
+
* issues lands on the connection the transaction is pinned to — and rolls back
|
|
13
|
+
* with it. `{ immediate: true }` still means what it means on SQLite: take the
|
|
14
|
+
* write lock at `BEGIN`, which any read-modify-write needs.
|
|
15
|
+
*/
|
|
16
|
+
export declare function drizzleTransaction<TDrizzle, T>(handle: DatabaseHandle, create: (tx: SqlExecutor) => TDrizzle, run: (db: TDrizzle) => Promise<T>, options?: TransactionOptions): Promise<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Drizzle over a SQLite handle, or over the `tx` executor of one.
|
|
19
|
+
*
|
|
20
|
+
* Declare tables with `sqliteTable`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function createSqliteDrizzle<TSchema extends Record<string, unknown> = Record<string, never>>(executor: SqlExecutor, config?: DrizzleConfig<TSchema>): SqliteRemoteDatabase<TSchema>;
|
|
23
|
+
/**
|
|
24
|
+
* Drizzle over a Postgres handle, or over the `tx` executor of one.
|
|
25
|
+
*
|
|
26
|
+
* Declare tables with `pgTable`.
|
|
27
|
+
*/
|
|
28
|
+
export declare function createPostgresDrizzle<TSchema extends Record<string, unknown> = Record<string, never>>(executor: SqlExecutor, config?: DrizzleConfig<TSchema>): PgRemoteDatabase<TSchema>;
|
|
29
|
+
/**
|
|
30
|
+
* Drizzle over a MySQL or MariaDB handle, or over the `tx` executor of one.
|
|
31
|
+
*
|
|
32
|
+
* Declare tables with `mysqlTable`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createMysqlDrizzle<TSchema extends Record<string, unknown> = Record<string, never>>(executor: SqlExecutor, config?: DrizzleConfig<TSchema>): MySqlRemoteDatabase<TSchema>;
|
|
35
|
+
//# sourceMappingURL=drizzle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drizzle.d.ts","sourceRoot":"","sources":["../../src/db/drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAA2B,KAAK,mBAAmB,EAAE,MAAM,yBAAyB,CAAA;AAC3F,OAAO,EAA8B,KAAK,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACxF,OAAO,EAA4B,KAAK,oBAAoB,EAAE,MAAM,0BAA0B,CAAA;AAG9F,OAAO,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'drizzle-orm/mysql-proxy';
|
|
2
|
+
import { drizzle as drizzlePostgres } from 'drizzle-orm/pg-proxy';
|
|
3
|
+
import { drizzle as drizzleSqlite } from 'drizzle-orm/sqlite-proxy';
|
|
4
|
+
import { CogentaError } from '../errors/index.js';
|
|
5
|
+
import { returnsRows } from './dialect.js';
|
|
6
|
+
/**
|
|
7
|
+
* Drizzle on top of a `DatabaseHandle`, through Drizzle's proxy drivers.
|
|
8
|
+
*
|
|
9
|
+
* Every dialect goes through the proxy rather than through the driver Drizzle
|
|
10
|
+
* ships for it, for one reason: a proxy runs its SQL on **our** connection.
|
|
11
|
+
* That keeps a single pool, our transaction pinning, our typed errors and our
|
|
12
|
+
* "no parameters in the error" rule for ORM traffic as well as for raw SQL.
|
|
13
|
+
*
|
|
14
|
+
* The alternative was measured, not assumed. `drizzle-orm/postgres-js` takes the
|
|
15
|
+
* postgres.js client and rewrites `client.options.serializers` for every date
|
|
16
|
+
* type on it; the handle then shares a client that can no longer bind a `Date`
|
|
17
|
+
* — `insert … values ($1)` fails with "The string argument must be of type
|
|
18
|
+
* string". Handing Drizzle its own client instead would double the connection
|
|
19
|
+
* count on hosting that allows very few. On SQLite the question does not arise:
|
|
20
|
+
* `better-sqlite3` is forbidden by rule R10 and `node:sqlite` has no Drizzle
|
|
21
|
+
* driver, so the proxy is the only door.
|
|
22
|
+
*
|
|
23
|
+
* What it costs, in exchange:
|
|
24
|
+
*
|
|
25
|
+
* - `db.transaction()` on the returned instance is unusable. The Postgres and
|
|
26
|
+
* MySQL proxies throw by design, and on SQLite Drizzle would issue `begin` and
|
|
27
|
+
* `commit` as separate statements, each taking and releasing the handle's file
|
|
28
|
+
* lock, so a concurrent write could slip inside the transaction and be rolled
|
|
29
|
+
* back with it. Use `handle.transaction()` and build an instance on the `tx`
|
|
30
|
+
* executor — see `drizzleTransaction`.
|
|
31
|
+
* - No streaming and no `db.batch()`: both need a driver-level feature the proxy
|
|
32
|
+
* contract does not carry.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* Drizzle maps a result row by position, so the proxy must hand it the column
|
|
36
|
+
* values in order rather than an object — a join selects `users.id` and
|
|
37
|
+
* `posts.id`, and one of the two disappears from an object.
|
|
38
|
+
*/
|
|
39
|
+
const AS_ARRAYS = { asArrays: true };
|
|
40
|
+
/**
|
|
41
|
+
* Drizzle types the callback as always returning rows, but `get` on an empty
|
|
42
|
+
* result must hand back nothing at all: an empty array would be mapped into a
|
|
43
|
+
* row whose every column is `undefined`, and `db.get()` would answer with an
|
|
44
|
+
* object instead of nothing. The cast states that exception in one place.
|
|
45
|
+
*/
|
|
46
|
+
const NO_ROW = undefined;
|
|
47
|
+
function assertDialect(executor, expected) {
|
|
48
|
+
if (executor.dialect === expected)
|
|
49
|
+
return;
|
|
50
|
+
throw new CogentaError({
|
|
51
|
+
code: 'DB_DIALECT_UNSUPPORTED',
|
|
52
|
+
message: `This Drizzle instance writes ${expected} SQL, but the connection speaks ${executor.dialect}.`,
|
|
53
|
+
hint: `Build it with the factory for ${executor.dialect}, and declare the schema with that dialect's table builder.`,
|
|
54
|
+
details: { expected, actual: executor.dialect },
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Runs Drizzle work inside a handle transaction, on every dialect.
|
|
59
|
+
*
|
|
60
|
+
* Replaces `db.transaction()`, which a proxy instance cannot provide. The
|
|
61
|
+
* instance is rebuilt on the transaction's executor, so every statement it
|
|
62
|
+
* issues lands on the connection the transaction is pinned to — and rolls back
|
|
63
|
+
* with it. `{ immediate: true }` still means what it means on SQLite: take the
|
|
64
|
+
* write lock at `BEGIN`, which any read-modify-write needs.
|
|
65
|
+
*/
|
|
66
|
+
export async function drizzleTransaction(handle, create, run, options) {
|
|
67
|
+
return handle.transaction(async (tx) => run(create(tx)), options);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Drizzle over a SQLite handle, or over the `tx` executor of one.
|
|
71
|
+
*
|
|
72
|
+
* Declare tables with `sqliteTable`.
|
|
73
|
+
*/
|
|
74
|
+
export function createSqliteDrizzle(executor, config) {
|
|
75
|
+
assertDialect(executor, 'sqlite');
|
|
76
|
+
return drizzleSqlite(async (text, params, method) => {
|
|
77
|
+
const query = { text, params };
|
|
78
|
+
if (method === 'run') {
|
|
79
|
+
await executor.execute(query);
|
|
80
|
+
return { rows: [] };
|
|
81
|
+
}
|
|
82
|
+
const result = await executor.execute(query, AS_ARRAYS);
|
|
83
|
+
// `get` asks for one row and is handed that row, not a list holding it.
|
|
84
|
+
return method === 'get' ? { rows: result.rows[0] ?? NO_ROW } : { rows: result.rows };
|
|
85
|
+
}, config);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Drizzle over a Postgres handle, or over the `tx` executor of one.
|
|
89
|
+
*
|
|
90
|
+
* Declare tables with `pgTable`.
|
|
91
|
+
*/
|
|
92
|
+
export function createPostgresDrizzle(executor, config) {
|
|
93
|
+
assertDialect(executor, 'postgres');
|
|
94
|
+
return drizzlePostgres(async (text, params, method) => {
|
|
95
|
+
const query = { text, params };
|
|
96
|
+
// `execute` is the raw path — `db.execute(sql\`…\`)` — and its caller reads
|
|
97
|
+
// rows by name. Only `all` feeds Drizzle's positional mapper.
|
|
98
|
+
if (method === 'execute')
|
|
99
|
+
return { rows: (await executor.execute(query)).rows };
|
|
100
|
+
return { rows: (await executor.execute(query, AS_ARRAYS)).rows };
|
|
101
|
+
}, config);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Drizzle over a MySQL or MariaDB handle, or over the `tx` executor of one.
|
|
105
|
+
*
|
|
106
|
+
* Declare tables with `mysqlTable`.
|
|
107
|
+
*/
|
|
108
|
+
export function createMysqlDrizzle(executor, config) {
|
|
109
|
+
assertDialect(executor, 'mysql');
|
|
110
|
+
return drizzleMysql(async (text, params, method) => {
|
|
111
|
+
const query = { text, params };
|
|
112
|
+
if (method === 'all') {
|
|
113
|
+
return { rows: (await executor.execute(query, AS_ARRAYS)).rows };
|
|
114
|
+
}
|
|
115
|
+
// A write reaches Drizzle as a result header, not as rows: that is where it
|
|
116
|
+
// reads the auto-increment key back for `$returningId()`. MySQL has no
|
|
117
|
+
// RETURNING, so this is the only way to learn it.
|
|
118
|
+
const result = await executor.execute(query);
|
|
119
|
+
if (returnsRows(text))
|
|
120
|
+
return { rows: result.rows };
|
|
121
|
+
return {
|
|
122
|
+
rows: [{ insertId: result.insertId ?? 0, affectedRows: result.rowsAffected }],
|
|
123
|
+
insertId: result.insertId ?? 0,
|
|
124
|
+
affectedRows: result.rowsAffected,
|
|
125
|
+
};
|
|
126
|
+
}, config);
|
|
127
|
+
}
|
|
128
|
+
//# sourceMappingURL=drizzle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drizzle.js","sourceRoot":"","sources":["../../src/db/drizzle.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,YAAY,EAA4B,MAAM,yBAAyB,CAAA;AAC3F,OAAO,EAAE,OAAO,IAAI,eAAe,EAAyB,MAAM,sBAAsB,CAAA;AACxF,OAAO,EAAE,OAAO,IAAI,aAAa,EAA6B,MAAM,0BAA0B,CAAA;AAC9F,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAsB,WAAW,EAAE,MAAM,cAAc,CAAA;AAK9D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH;;;;GAIG;AACH,MAAM,SAAS,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAW,CAAA;AAE7C;;;;;GAKG;AACH,MAAM,MAAM,GAAG,SAAiC,CAAA;AAEhD,SAAS,aAAa,CAAC,QAAqB,EAAE,QAAyB;IACrE,IAAI,QAAQ,CAAC,OAAO,KAAK,QAAQ;QAAE,OAAM;IAEzC,MAAM,IAAI,YAAY,CAAC;QACrB,IAAI,EAAE,wBAAwB;QAC9B,OAAO,EAAE,gCAAgC,QAAQ,mCAAmC,QAAQ,CAAC,OAAO,GAA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,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,CAAC,EAAE,YAAY,EAAE,MAAM,CAAC,YAAY,EAAE,CAAC;YAC7E,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,CAAC;YAC9B,YAAY,EAAE,MAAM,CAAC,YAAY;SAClC,CAAA;IACH,CAAC,EAAE,MAAM,CAAC,CAAA;AACZ,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type DriverRegistry } from '../drivers/index.js';
|
|
2
|
+
import type { Logger } from '../logger/index.js';
|
|
3
|
+
import type { DatabaseConfig, DatabaseDriverOptions, DatabaseHandle } from './types.js';
|
|
4
|
+
export type { CompiledQuery, SqlFragment } from './dialect.js';
|
|
5
|
+
export { compile, encodeValue, identifier, isWrite, limit, returnsRows, sql, unsafeRaw, } from './dialect.js';
|
|
6
|
+
export type { MySqlRemoteDatabase, PgRemoteDatabase, SqliteRemoteDatabase, } from './drizzle.js';
|
|
7
|
+
export { createMysqlDrizzle, createPostgresDrizzle, createSqliteDrizzle, drizzleTransaction, } from './drizzle.js';
|
|
8
|
+
export type { MysqlHandleOptions } from './mysql.js';
|
|
9
|
+
export { createMysqlHandle, loadMysqlModule, mysqlDatabaseDriver } from './mysql.js';
|
|
10
|
+
export type { PostgresHandleOptions } from './postgres.js';
|
|
11
|
+
export { createPostgresHandle, loadPostgresModule, postgresDatabaseDriver } from './postgres.js';
|
|
12
|
+
export type { SqliteHandleOptions } from './sqlite.js';
|
|
13
|
+
export { createSqliteHandle, loadSqliteModule, sqliteDatabaseDriver } from './sqlite.js';
|
|
14
|
+
export type { DatabaseConfig, DatabaseDialect, DatabaseDriverOptions, DatabaseHandle, ExecuteOptions, QueryResult, SqlExecutor, TransactionOptions, } from './types.js';
|
|
15
|
+
export { DATABASE_DIALECTS } from './types.js';
|
|
16
|
+
export interface DatabaseRegistryOptions extends DatabaseDriverOptions {
|
|
17
|
+
readonly logger?: Logger;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The database drivers Cogenta ships.
|
|
21
|
+
*
|
|
22
|
+
* SQLite is registered as `degraded` on purpose: it works everywhere and needs
|
|
23
|
+
* nothing, but it is one machine, has no vector index, and does not survive a
|
|
24
|
+
* fleet. Postgres and MySQL register as `optimal` when their optional client is
|
|
25
|
+
* installed.
|
|
26
|
+
*/
|
|
27
|
+
export declare function createDatabaseRegistry(options?: DatabaseRegistryOptions): DriverRegistry<DatabaseHandle, DatabaseConfig>;
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/db/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAwB,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAC/E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAIhD,OAAO,KAAK,EAAE,cAAc,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAEvF,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC9D,OAAO,EACL,OAAO,EACP,WAAW,EACX,UAAU,EACV,OAAO,EACP,KAAK,EACL,WAAW,EACX,GAAG,EACH,SAAS,GACV,MAAM,cAAc,CAAA;AACrB,YAAY,EACV,mBAAmB,EACnB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,cAAc,CAAA;AACrB,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AACpF,YAAY,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAA;AAC1D,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAA;AAChG,YAAY,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AACxF,YAAY,EACV,cAAc,EACd,eAAe,EACf,qBAAqB,EACrB,cAAc,EACd,cAAc,EACd,WAAW,EACX,WAAW,EACX,kBAAkB,GACnB,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAE9C,MAAM,WAAW,uBAAwB,SAAQ,qBAAqB;IACpE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CACzB;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE,uBAA4B,GACpC,cAAc,CAAC,cAAc,EAAE,cAAc,CAAC,CAYhD"}
|
package/dist/db/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createDriverRegistry } from '../drivers/index.js';
|
|
2
|
+
import { mysqlDatabaseDriver } from './mysql.js';
|
|
3
|
+
import { postgresDatabaseDriver } from './postgres.js';
|
|
4
|
+
import { sqliteDatabaseDriver } from './sqlite.js';
|
|
5
|
+
export { compile, encodeValue, identifier, isWrite, limit, returnsRows, sql, unsafeRaw, } from './dialect.js';
|
|
6
|
+
export { createMysqlDrizzle, createPostgresDrizzle, createSqliteDrizzle, drizzleTransaction, } from './drizzle.js';
|
|
7
|
+
export { createMysqlHandle, loadMysqlModule, mysqlDatabaseDriver } from './mysql.js';
|
|
8
|
+
export { createPostgresHandle, loadPostgresModule, postgresDatabaseDriver } from './postgres.js';
|
|
9
|
+
export { createSqliteHandle, loadSqliteModule, sqliteDatabaseDriver } from './sqlite.js';
|
|
10
|
+
export { DATABASE_DIALECTS } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* The database drivers Cogenta ships.
|
|
13
|
+
*
|
|
14
|
+
* SQLite is registered as `degraded` on purpose: it works everywhere and needs
|
|
15
|
+
* nothing, but it is one machine, has no vector index, and does not survive a
|
|
16
|
+
* fleet. Postgres and MySQL register as `optimal` when their optional client is
|
|
17
|
+
* installed.
|
|
18
|
+
*/
|
|
19
|
+
export function createDatabaseRegistry(options = {}) {
|
|
20
|
+
const { logger } = options;
|
|
21
|
+
const registry = createDriverRegistry({
|
|
22
|
+
need: 'database',
|
|
23
|
+
...(logger === undefined ? {} : { logger }),
|
|
24
|
+
});
|
|
25
|
+
registry.register(postgresDatabaseDriver());
|
|
26
|
+
registry.register(mysqlDatabaseDriver());
|
|
27
|
+
registry.register(sqliteDatabaseDriver());
|
|
28
|
+
return registry;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/db/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAuB,MAAM,qBAAqB,CAAA;AAE/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAA;AACtD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAIlD,OAAO,EACL,OAAO,EACP,WAAW,EACX,UAAU,EACV,OAAO,EACP,KAAK,EACL,WAAW,EACX,GAAG,EACH,SAAS,GACV,MAAM,cAAc,CAAA;AAMrB,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,mBAAmB,EACnB,kBAAkB,GACnB,MAAM,cAAc,CAAA;AAErB,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAEpF,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAA;AAEhG,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAWxF,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAM9C;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,OAAO,GAA4B,EAAE;IAErC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAA;IAC1B,MAAM,QAAQ,GAAG,oBAAoB,CAAiC;QACpE,IAAI,EAAE,UAAU;QAChB,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;KAC5C,CAAC,CAAA;IAEF,QAAQ,CAAC,QAAQ,CAAC,sBAAsB,EAAE,CAAC,CAAA;IAC3C,QAAQ,CAAC,QAAQ,CAAC,mBAAmB,EAAE,CAAC,CAAA;IACxC,QAAQ,CAAC,QAAQ,CAAC,oBAAoB,EAAE,CAAC,CAAA;IAEzC,OAAO,QAAQ,CAAA;AACjB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Driver } from '../drivers/index.js';
|
|
2
|
+
import type { DatabaseConfig, DatabaseHandle } from './types.js';
|
|
3
|
+
/** Only the slice of `mysql2/promise` this driver uses. */
|
|
4
|
+
interface ResultSetHeader {
|
|
5
|
+
readonly affectedRows: number;
|
|
6
|
+
readonly insertId: number;
|
|
7
|
+
}
|
|
8
|
+
type MysqlRows = Record<string, unknown>[] | unknown[][];
|
|
9
|
+
type MysqlQueryResult = [MysqlRows | ResultSetHeader, unknown];
|
|
10
|
+
/** The options form of a statement, the only one that can ask for value lists. */
|
|
11
|
+
interface MysqlQueryOptions {
|
|
12
|
+
readonly sql: string;
|
|
13
|
+
readonly values?: readonly unknown[];
|
|
14
|
+
readonly rowsAsArray?: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface MysqlQueries {
|
|
17
|
+
query(sql: string | MysqlQueryOptions, values?: readonly unknown[]): Promise<MysqlQueryResult>;
|
|
18
|
+
execute(sql: string | MysqlQueryOptions, values?: readonly unknown[]): Promise<MysqlQueryResult>;
|
|
19
|
+
}
|
|
20
|
+
interface MysqlConnection extends MysqlQueries {
|
|
21
|
+
release(): void;
|
|
22
|
+
}
|
|
23
|
+
interface MysqlPool extends MysqlQueries {
|
|
24
|
+
getConnection(): Promise<MysqlConnection>;
|
|
25
|
+
end(): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
interface MysqlModuleLike {
|
|
28
|
+
createPool: (options: Record<string, unknown>) => MysqlPool;
|
|
29
|
+
}
|
|
30
|
+
/** `mysql2` is an optional peer: a site on SQLite or Postgres never installs it. */
|
|
31
|
+
export declare function loadMysqlModule(): Promise<MysqlModuleLike | null>;
|
|
32
|
+
export interface MysqlHandleOptions {
|
|
33
|
+
readonly url: string;
|
|
34
|
+
readonly poolSize?: number;
|
|
35
|
+
}
|
|
36
|
+
export declare function createMysqlHandle(options: MysqlHandleOptions): Promise<DatabaseHandle>;
|
|
37
|
+
export declare function mysqlDatabaseDriver(): Driver<DatabaseHandle, DatabaseConfig>;
|
|
38
|
+
export {};
|
|
39
|
+
//# sourceMappingURL=mysql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mysql.d.ts","sourceRoot":"","sources":["../../src/db/mysql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAgB,MAAM,qBAAqB,CAAA;AAG/D,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EAKf,MAAM,YAAY,CAAA;AAEnB,2DAA2D;AAC3D,UAAU,eAAe;IACvB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAA;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;CAC1B;AAED,KAAK,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,OAAO,EAAE,EAAE,CAAA;AAExD,KAAK,gBAAgB,GAAG,CAAC,SAAS,GAAG,eAAe,EAAE,OAAO,CAAC,CAAA;AAE9D,kFAAkF;AAClF,UAAU,iBAAiB;IACzB,QAAQ,CAAC,GA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