@forestfuture/active-record 0.6.0 → 0.8.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/README.md +5 -4
- package/config-schema.json +60 -0
- package/dist/adapter.d.mts +3 -2
- package/dist/adapter.mjs +5 -3
- package/dist/batch-ReqVoCfa.mjs +9559 -0
- package/dist/cli.d.mts +216 -46
- package/dist/cli.mjs +4 -3
- package/dist/command.mjs +1102 -529
- package/dist/d1.d.mts +60 -0
- package/dist/d1.mjs +334 -0
- package/dist/epoch-storage-CDlQZcBB.mjs +988 -0
- package/dist/for-adapters-B0xMP6Dq.d.mts +23 -0
- package/dist/for-adapters-CfXMGeOu.mjs +31 -0
- package/dist/for-adapters-DhNV1mWf.mjs +276 -0
- package/dist/{generated-model-CfDggoVN.mjs → generated-model-g5oWsDKE.mjs} +732 -540
- package/dist/index-BATasvJa.d.mts +241 -0
- package/dist/index.d.mts +4 -143
- package/dist/index.mjs +5 -3
- package/dist/metadata-CzntpYzC.d.mts +182 -0
- package/dist/plugin-wOSILvSB.mjs +699 -0
- package/dist/plugin-x5-d3TSN.mjs +1035 -0
- package/dist/postgresql.d.mts +64 -0
- package/dist/postgresql.mjs +604 -0
- package/dist/src-D56_3odT.mjs +46 -0
- package/dist/storage-override-n4w1cS_M.mjs +220 -0
- package/dist/validations-DHDVV8hB.d.mts +1996 -0
- package/llms-full.txt +3283 -656
- package/package.json +25 -2
- package/skills/active-record/SKILL.md +24 -16
- package/dist/adapter-BSvFhTDf.d.mts +0 -1293
- package/dist/batch-DBeYk5wx.mjs +0 -4171
- package/dist/epoch-storage-o2aoOH7H.mjs +0 -112
- package/dist/storage-override-BYBYP8Mj.mjs +0 -91
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { O as AnyDatabase } from "./validations-DHDVV8hB.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Bytes as hex text, two digits each, for SQL that reads bytes from JSON (`unhex`, `\\x…`). The
|
|
4
|
+
* digits' character codes are written into one array and decoded once: mapping each byte to a
|
|
5
|
+
* string of its own takes many times the memory and time, which a large file can't afford.
|
|
6
|
+
*/
|
|
7
|
+
declare function hexOf(bytes: Uint8Array): string;
|
|
8
|
+
/**
|
|
9
|
+
* The request's database for a query on `binding` outside a Worker, or a `NoDatabase` saying what
|
|
10
|
+
* to do. `connect` is how the adapter's docs open one, such as `connect(env.DB)`.
|
|
11
|
+
*/
|
|
12
|
+
declare function onlyRequested(requested: readonly AnyDatabase[], binding: string | undefined, connect: string): AnyDatabase;
|
|
13
|
+
/** A Worker's `env`: its bindings and variables, by name. */
|
|
14
|
+
type WorkersEnv = Record<string, unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* The Worker's `env`, from `load`, or undefined outside a Worker (Node, scripts). An adapter
|
|
17
|
+
* passes `() => import("cloudflare:workers")` from its own module, with that literal specifier:
|
|
18
|
+
* Wrangler only bundles those, and a test that mocks the module with the adapter inlined reaches it.
|
|
19
|
+
*/
|
|
20
|
+
declare function workersEnv(load: () => Promise<{
|
|
21
|
+
env: unknown;
|
|
22
|
+
}>): Promise<WorkersEnv | undefined>;
|
|
23
|
+
export { hexOf as i, workersEnv as n, onlyRequested as r, WorkersEnv as t };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z as NoDatabase } from "./epoch-storage-CDlQZcBB.mjs";
|
|
2
|
+
// src/request-database.ts
|
|
3
|
+
/**
|
|
4
|
+
* The request's database for a query on `binding` outside a Worker, or a `NoDatabase` saying what
|
|
5
|
+
* to do. `connect` is how the adapter's docs open one, such as `connect(env.DB)`.
|
|
6
|
+
*/
|
|
7
|
+
function onlyRequested(requested, binding, connect) {
|
|
8
|
+
if (requested.length === 1) return requested[0];
|
|
9
|
+
const reason = requested.length > 1 ? "withDatabase was given several, and outside a Worker a binding can't tell which" : binding === void 0 ? "its Model's base class holds no generated schema, which names its binding" : `the binding ${binding} is only there inside a Worker`;
|
|
10
|
+
throw new NoDatabase(`No database for this query: ${reason}. Wrap the code in withDatabase(${connect}, …), or use db.model(Model)`);
|
|
11
|
+
}
|
|
12
|
+
// src/workers-env.ts
|
|
13
|
+
/** Whether importing `cloudflare:workers` failed because this isn't a Worker (Node, scripts). */
|
|
14
|
+
function isOutsideWorker(error) {
|
|
15
|
+
const { code, message } = error;
|
|
16
|
+
return code === "ERR_UNSUPPORTED_ESM_URL_SCHEME" || code === "ERR_MODULE_NOT_FOUND" || /cloudflare:workers/.test(String(message));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The Worker's `env`, from `load`, or undefined outside a Worker (Node, scripts). An adapter
|
|
20
|
+
* passes `() => import("cloudflare:workers")` from its own module, with that literal specifier:
|
|
21
|
+
* Wrangler only bundles those, and a test that mocks the module with the adapter inlined reaches it.
|
|
22
|
+
*/
|
|
23
|
+
async function workersEnv(load) {
|
|
24
|
+
try {
|
|
25
|
+
return (await load()).env;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (isOutsideWorker(error)) return void 0;
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export { onlyRequested as n, workersEnv as t };
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { C as pluralize, E as underscore, n as digitsProblem } from "./epoch-storage-CDlQZcBB.mjs";
|
|
2
|
+
import { f as byName, o as exportName } from "./storage-override-n4w1cS_M.mjs";
|
|
3
|
+
// cli/generate/migration-plan.ts
|
|
4
|
+
/** What to do, read from a migration's name: `create_products`, `add_sku_to_products`, …; else empty. */
|
|
5
|
+
function migrationPlan(name) {
|
|
6
|
+
const words = underscore(name);
|
|
7
|
+
const create = /^create_(\w+)$/.exec(words);
|
|
8
|
+
if (create) return {
|
|
9
|
+
kind: "create",
|
|
10
|
+
table: pluralize(create[1])
|
|
11
|
+
};
|
|
12
|
+
const add = /^add_\w+?_to_(\w+)$/.exec(words);
|
|
13
|
+
if (add) return {
|
|
14
|
+
kind: "add",
|
|
15
|
+
table: pluralize(add[1])
|
|
16
|
+
};
|
|
17
|
+
const remove = /^remove_\w+?_from_(\w+)$/.exec(words);
|
|
18
|
+
return remove ? {
|
|
19
|
+
kind: "remove",
|
|
20
|
+
table: pluralize(remove[1])
|
|
21
|
+
} : { kind: "empty" };
|
|
22
|
+
}
|
|
23
|
+
/** The index the generators name a column's index by: `index_products_on_sku`. */
|
|
24
|
+
function indexName(table, column) {
|
|
25
|
+
return `index_${table}_on_${column}`;
|
|
26
|
+
}
|
|
27
|
+
// cli/generate/migration-sql.ts
|
|
28
|
+
/** A name as SQL: quoted only when the database reserves it, so migrations stay readable. */
|
|
29
|
+
function quotedName(dialect, name) {
|
|
30
|
+
return dialect.reserved.has(name) ? `"${name}"` : name;
|
|
31
|
+
}
|
|
32
|
+
/** A default as a SQL literal: `TRUE`, `1.5`, `'it''s'`. */
|
|
33
|
+
function literal(value) {
|
|
34
|
+
if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
|
|
35
|
+
return typeof value === "number" ? String(value) : `'${value.replaceAll("'", "''")}'`;
|
|
36
|
+
}
|
|
37
|
+
/** A column's type, with a decimal's precision and scale: `NUMERIC(10, 2)` (#398). */
|
|
38
|
+
function sizedType(column, dialect) {
|
|
39
|
+
const type = dialect.types[column.type];
|
|
40
|
+
if (column.precision === void 0) return type;
|
|
41
|
+
return `${type}(${column.scale === void 0 ? `${column.precision}` : `${column.precision}, ${column.scale}`})`;
|
|
42
|
+
}
|
|
43
|
+
/** A column's definition, as `CREATE TABLE` and `ADD COLUMN` write it. */
|
|
44
|
+
function definition(column, { dialect, keys }, notNull) {
|
|
45
|
+
const name = quotedName(dialect, column.name);
|
|
46
|
+
if (column.type === "references") {
|
|
47
|
+
const type = (keys.get(column.references) ?? dialect.guessedKeyType).toUpperCase();
|
|
48
|
+
const target = quotedName(dialect, column.references);
|
|
49
|
+
return `${name} ${type}${notNull ? " NOT NULL" : ""} REFERENCES ${target} (id)`;
|
|
50
|
+
}
|
|
51
|
+
const parts = [name, sizedType(column, dialect)];
|
|
52
|
+
if (notNull) parts.push("NOT NULL");
|
|
53
|
+
if (column.default !== void 0) parts.push(`DEFAULT ${literal(column.default)}`);
|
|
54
|
+
const check = dialect.check?.(column, name);
|
|
55
|
+
if (check !== void 0) parts.push(check);
|
|
56
|
+
return parts.join(" ");
|
|
57
|
+
}
|
|
58
|
+
/** `CREATE [UNIQUE] INDEX` for each indexed column. */
|
|
59
|
+
function indexes(dialect, table, columns) {
|
|
60
|
+
return columns.filter((column) => column.index !== "none").map((column) => {
|
|
61
|
+
const unique = column.index === "unique" ? "UNIQUE " : "";
|
|
62
|
+
const on = `${quotedName(dialect, table)} (${quotedName(dialect, column.name)})`;
|
|
63
|
+
return `CREATE ${unique}INDEX ${indexName(table, column.name)} ON ${on};\n`;
|
|
64
|
+
}).join("");
|
|
65
|
+
}
|
|
66
|
+
/** `CREATE TABLE`, with its key, columns, timestamps and indexes. */
|
|
67
|
+
function createTable(table, change, writing) {
|
|
68
|
+
const { dialect } = writing;
|
|
69
|
+
const lines = [dialect.key(change.options.integerId === true)];
|
|
70
|
+
for (const column of change.columns) lines.push(definition(column, writing, column.notNull));
|
|
71
|
+
if (change.options.timestamps !== false) {
|
|
72
|
+
const type = dialect.timestampType;
|
|
73
|
+
lines.push(`created_at ${type} NOT NULL`, `updated_at ${type} NOT NULL`);
|
|
74
|
+
}
|
|
75
|
+
const body = lines.join(",\n ");
|
|
76
|
+
return `CREATE TABLE ${quotedName(dialect, table)} (\n ${body}\n);\n${indexes(dialect, table, change.columns)}`;
|
|
77
|
+
}
|
|
78
|
+
/** `ADD COLUMN` for each column; a reference added later can be null, as Rails' `add_reference`. */
|
|
79
|
+
function addColumns(table, columns, writing) {
|
|
80
|
+
const { dialect } = writing;
|
|
81
|
+
for (const column of columns) {
|
|
82
|
+
const error = dialect.addRefusal?.(table, column);
|
|
83
|
+
if (error !== void 0) return {
|
|
84
|
+
ok: false,
|
|
85
|
+
error
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
ok: true,
|
|
90
|
+
sql: columns.map((column) => {
|
|
91
|
+
const notNull = column.type !== "references" && column.notNull;
|
|
92
|
+
return `ALTER TABLE ${quotedName(dialect, table)} ADD COLUMN ${definition(column, writing, notNull)};\n`;
|
|
93
|
+
}).join("") + indexes(dialect, table, columns)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** The SQL for the migration the change names, or why it can't be written. */
|
|
97
|
+
function migrationFor(change, writing) {
|
|
98
|
+
const { name, columns } = change;
|
|
99
|
+
const plan = migrationPlan(name);
|
|
100
|
+
if (plan.kind === "empty") return columns.length > 0 ? {
|
|
101
|
+
ok: false,
|
|
102
|
+
error: `${name}: columns only go with Create…, Add…To… or Remove…From… migrations`
|
|
103
|
+
} : {
|
|
104
|
+
ok: true,
|
|
105
|
+
sql: "-- Write the migration's SQL here.\n"
|
|
106
|
+
};
|
|
107
|
+
if (columns.length === 0 && plan.kind !== "create") return {
|
|
108
|
+
ok: false,
|
|
109
|
+
error: `${name}: name the columns to ${plan.kind} (sku:string)`
|
|
110
|
+
};
|
|
111
|
+
if (plan.kind === "create") return {
|
|
112
|
+
ok: true,
|
|
113
|
+
sql: createTable(plan.table, change, writing)
|
|
114
|
+
};
|
|
115
|
+
if (plan.kind === "add") return addColumns(plan.table, columns, writing);
|
|
116
|
+
return {
|
|
117
|
+
ok: true,
|
|
118
|
+
sql: columns.map((column) => writing.dialect.dropColumn(plan.table, column.name)).join("")
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Notes for references to tables the migrations don't make yet: their key type is a guess. */
|
|
122
|
+
function referenceNotes(change, dialect) {
|
|
123
|
+
if (migrationPlan(change.name).kind === "remove") return [];
|
|
124
|
+
return change.columns.filter((column) => column.type === "references" && !change.keys.has(column.references)).map((column) => `${column.references} isn't in the migrations yet, so ${column.name} is ${dialect.guessedKeyType}`);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* A migration's SQL in the dialect given: a table created, columns added or removed, or an empty
|
|
128
|
+
* migration, as its name says (`CreateProducts`, `AddSkuToProducts`, …), with a note for each key
|
|
129
|
+
* type it guessed.
|
|
130
|
+
*/
|
|
131
|
+
function migrationSql(change, dialect) {
|
|
132
|
+
const result = migrationFor(change, {
|
|
133
|
+
dialect,
|
|
134
|
+
keys: change.keys
|
|
135
|
+
});
|
|
136
|
+
if (!result.ok) return result;
|
|
137
|
+
const notes = referenceNotes(change, dialect);
|
|
138
|
+
return notes.length === 0 ? result : {
|
|
139
|
+
...result,
|
|
140
|
+
notes
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// cli/schema/decimal-digits.ts
|
|
144
|
+
/**
|
|
145
|
+
* `spec` with a decimal's precision and scale, as a column's type gives them: neither when the
|
|
146
|
+
* column has no precision, and the scale only when it's given. Digits no `NUMERIC(p, s)` has (a
|
|
147
|
+
* precision past 1000, a scale past the precision) leave both off, so the Model stays valid.
|
|
148
|
+
*/
|
|
149
|
+
function withDigits(spec, precision, scale) {
|
|
150
|
+
if (spec.type !== "decimal" || precision === void 0 || precision === null) return spec;
|
|
151
|
+
const digits = {
|
|
152
|
+
precision,
|
|
153
|
+
...scale !== void 0 && scale !== null && { scale }
|
|
154
|
+
};
|
|
155
|
+
return digitsProblem(digits) === void 0 ? {
|
|
156
|
+
...spec,
|
|
157
|
+
...digits
|
|
158
|
+
} : spec;
|
|
159
|
+
}
|
|
160
|
+
// cli/schema/generated-enum.ts
|
|
161
|
+
/** A string spec with the names its column is restricted to, as a generated enum; any other spec as it is. */
|
|
162
|
+
function withGeneratedEnum(spec, names) {
|
|
163
|
+
if (names === void 0 || spec.type !== "string") return spec;
|
|
164
|
+
return {
|
|
165
|
+
...spec,
|
|
166
|
+
enum: names,
|
|
167
|
+
scopes: false,
|
|
168
|
+
instanceMethods: false
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/** A CHECK's list of SQL string literals as their text, each once: `'a', 'it''s', 'a'` → `["a", "it's"]`. */
|
|
172
|
+
function namesInList(list) {
|
|
173
|
+
const texts = [...list.matchAll(/'((?:[^']|'')*)'/g)].map((match) => match[1]);
|
|
174
|
+
return [...new Set(texts.map((text) => text.replaceAll("''", "'")))];
|
|
175
|
+
}
|
|
176
|
+
// cli/schema/tables.ts
|
|
177
|
+
const noFindings = () => ({
|
|
178
|
+
errors: [],
|
|
179
|
+
notes: [],
|
|
180
|
+
textDates: []
|
|
181
|
+
});
|
|
182
|
+
function merge(into, from) {
|
|
183
|
+
into.errors.push(...from.errors);
|
|
184
|
+
into.notes.push(...from.notes);
|
|
185
|
+
into.textDates.push(...from.textDates);
|
|
186
|
+
}
|
|
187
|
+
/** A table's attributes, in column order, with what their columns turn up. */
|
|
188
|
+
function attributesOf(columns, convert) {
|
|
189
|
+
const attributes = {};
|
|
190
|
+
const found = noFindings();
|
|
191
|
+
const columnOf = /* @__PURE__ */ new Map();
|
|
192
|
+
for (const column of columns) {
|
|
193
|
+
const converted = convert(column);
|
|
194
|
+
if (!converted.ok) {
|
|
195
|
+
found.errors.push(converted.error);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const { attribute, spec, note, textDate } = converted;
|
|
199
|
+
const taken = columnOf.get(attribute);
|
|
200
|
+
if (taken !== void 0) found.errors.push(`${column.table}: columns ${taken} and ${column.name} would both be the attribute ${attribute}; rename one`);
|
|
201
|
+
columnOf.set(attribute, column.name);
|
|
202
|
+
attributes[attribute] = spec;
|
|
203
|
+
if (note !== void 0) found.notes.push(note);
|
|
204
|
+
if (textDate) found.textDates.push(`${column.table}.${column.name}`);
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
attributes,
|
|
208
|
+
...found
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/** One table's schema, or the errors that stop it from being generated. */
|
|
212
|
+
function tableSchema(table, reading) {
|
|
213
|
+
const { name, columns, lastChangedBy, keyType } = table;
|
|
214
|
+
const keys = columns.filter((column) => column.primaryKey).map((column) => column.name);
|
|
215
|
+
if (keys.length !== 1) {
|
|
216
|
+
const error = keys.length === 0 ? reading.missingKey(name) : `${name}: a composite primary key (${keys.join(", ")}) isn't supported; give the table a single key`;
|
|
217
|
+
return {
|
|
218
|
+
...noFindings(),
|
|
219
|
+
errors: [error]
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
const { attributes, ...found } = attributesOf(columns, reading.convert);
|
|
223
|
+
const generated = {
|
|
224
|
+
name,
|
|
225
|
+
exportName: exportName(name),
|
|
226
|
+
primaryKey: keys[0],
|
|
227
|
+
lastChangedBy
|
|
228
|
+
};
|
|
229
|
+
const withKeyType = keyType === void 0 ? {} : { keyType };
|
|
230
|
+
return {
|
|
231
|
+
table: {
|
|
232
|
+
...generated,
|
|
233
|
+
attributes,
|
|
234
|
+
...withKeyType
|
|
235
|
+
},
|
|
236
|
+
...found
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
/** Tables whose names become the same export (`line_items`, `line-items` → `lineItems`). */
|
|
240
|
+
function exportCollisions(names) {
|
|
241
|
+
const seen = /* @__PURE__ */ new Map();
|
|
242
|
+
const errors = [];
|
|
243
|
+
for (const name of names) {
|
|
244
|
+
const as = exportName(name);
|
|
245
|
+
const other = seen.get(as);
|
|
246
|
+
if (other !== void 0) errors.push(`${other} and ${name} would both be exported as ${as}; rename one of the tables`);
|
|
247
|
+
seen.set(as, name);
|
|
248
|
+
}
|
|
249
|
+
return errors;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* The generated schema's tables, from the tables an adapter read, in alphabetical order (so the
|
|
253
|
+
* result only changes with the schema), or every error that stops them.
|
|
254
|
+
*/
|
|
255
|
+
function generatedSchema(read, reading) {
|
|
256
|
+
const sorted = [...read].sort((a, b) => byName(a.name, b.name));
|
|
257
|
+
const found = noFindings();
|
|
258
|
+
found.errors.push(...exportCollisions(sorted.map((table) => table.name)));
|
|
259
|
+
const tables = [];
|
|
260
|
+
for (const table of sorted) {
|
|
261
|
+
const result = tableSchema(table, reading);
|
|
262
|
+
merge(found, result);
|
|
263
|
+
if (result.table !== void 0) tables.push(result.table);
|
|
264
|
+
}
|
|
265
|
+
const { errors, notes, textDates } = found;
|
|
266
|
+
return errors.length > 0 ? {
|
|
267
|
+
ok: false,
|
|
268
|
+
errors
|
|
269
|
+
} : {
|
|
270
|
+
ok: true,
|
|
271
|
+
tables,
|
|
272
|
+
notes,
|
|
273
|
+
textDates
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
export { migrationSql as a, migrationPlan as c, withDigits as i, namesInList as n, quotedName as o, withGeneratedEnum as r, indexName as s, generatedSchema as t };
|