@rebasepro/codegen 0.23.0 → 0.24.1-canary.g56af4a4
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/index.d.ts +1 -1
- package/dist/index.es.js +92 -55
- package/dist/index.es.js.map +1 -1
- package/dist/utils.d.ts +3 -18
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { CollectionConfig } from "@rebasepro/types";
|
|
7
7
|
export { generateTypedefs, CodegenError } from "./generate-types.js";
|
|
8
|
-
export { toPascalCase, toCamelCase, toSafeIdentifier, indent } from "./utils.js";
|
|
8
|
+
export { toPascalCase, toCamelCase, toSafeIdentifier, toCollectionAccessor, indent } from "./utils.js";
|
|
9
9
|
export interface GeneratedFile {
|
|
10
10
|
/** Relative file path within the output directory */
|
|
11
11
|
path: string;
|
package/dist/index.es.js
CHANGED
|
@@ -1,41 +1,7 @@
|
|
|
1
1
|
import { getDeclaredSubcollections } from "@rebasepro/types";
|
|
2
|
-
import { effectiveAccess, fieldKeyForColumn, findRelation, isRelationRequired, resolveCollectionRelations, sortCollectionsBySlug } from "@rebasepro/common";
|
|
2
|
+
import { effectiveAccess, fieldKeyForColumn, findRelation, isRelationRequired, resolveCollectionRelations, sortCollectionsBySlug, toCamelCase, toCollectionAccessor, toPascalCase, toSafeIdentifier } from "@rebasepro/common";
|
|
3
3
|
//#region src/utils.ts
|
|
4
4
|
/**
|
|
5
|
-
* Utility functions for the SDK generator
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Convert a slug/snake_case string to PascalCase
|
|
9
|
-
* e.g. "private_notes" → "PrivateNotes"
|
|
10
|
-
*
|
|
11
|
-
* Capitals already inside a word are meaningful and are kept: lowercasing the
|
|
12
|
-
* tail of every chunk turned "TestEntities" into "Testentities", which is what
|
|
13
|
-
* ended up in the generated type names. SHOUTING_CASE is the one shape where
|
|
14
|
-
* the tail is not meaningful, so it is folded down.
|
|
15
|
-
*/
|
|
16
|
-
function toPascalCase(str) {
|
|
17
|
-
return str.split(/[_\-\s]+/).filter(Boolean).map((word) => {
|
|
18
|
-
const rest = /^[A-Z0-9]+$/.test(word) ? word.slice(1).toLowerCase() : word.slice(1);
|
|
19
|
-
return word.charAt(0).toUpperCase() + rest;
|
|
20
|
-
}).join("");
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Convert a slug/snake_case string to camelCase
|
|
24
|
-
* e.g. "private_notes" → "privateNotes"
|
|
25
|
-
*/
|
|
26
|
-
function toCamelCase(str) {
|
|
27
|
-
if (!/[_\-\s]/.test(str)) return str.charAt(0).toLowerCase() + str.slice(1);
|
|
28
|
-
const pascal = toPascalCase(str);
|
|
29
|
-
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Convert a slug to a safe JS identifier
|
|
33
|
-
* e.g. "private-notes" → "privateNotes"
|
|
34
|
-
*/
|
|
35
|
-
function toSafeIdentifier(str) {
|
|
36
|
-
return toCamelCase(str.replace(/[^a-zA-Z0-9_]/g, "_"));
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
5
|
* Indent a block of text by a given number of spaces
|
|
40
6
|
*/
|
|
41
7
|
function indent(text, spaces) {
|
|
@@ -197,7 +163,9 @@ function includedRelationType(relation, accessors) {
|
|
|
197
163
|
const slug = resolveTargetCollection(relation)?.slug ?? relation.targetSlug;
|
|
198
164
|
const accessor = slug ? accessors.get(slug) : void 0;
|
|
199
165
|
const rowType = accessor ? `Database[${emitString(accessor)}]["Row"]` : "Record<string, unknown>";
|
|
200
|
-
|
|
166
|
+
const pivot = relation.kind === "manyToMany" ? pivotType(relation.through.properties, "read") : void 0;
|
|
167
|
+
const elementType = pivot ? `${rowType} & { _pivot?: ${pivot} }` : rowType;
|
|
168
|
+
return relation.cardinality === "many" ? `Array<${elementType}>` : elementType;
|
|
201
169
|
}
|
|
202
170
|
/**
|
|
203
171
|
* Map every slug to the property name it is reachable under on `client.data`.
|
|
@@ -217,13 +185,29 @@ function buildAccessors(collections) {
|
|
|
217
185
|
if (typeof slug !== "string" || slug.length === 0) throw new CodegenError("A collection has no slug, so it has no name to generate a type for. Every collection needs a unique `slug`.");
|
|
218
186
|
const safe = toSafeIdentifier(slug);
|
|
219
187
|
if (safe.length === 0) throw new CodegenError(`The slug ${emitString(slug)} has no characters that can form a property name, so it cannot be reached as \`client.data.<name>\`. Use a slug containing letters, digits, underscores or dashes.`);
|
|
220
|
-
const
|
|
221
|
-
if (
|
|
222
|
-
bySafeName.
|
|
223
|
-
|
|
188
|
+
const accessor = toCollectionAccessor(slug);
|
|
189
|
+
if (accessor !== safe) console.warn(`[codegen] The collection ${emitString(slug)} would be \`client.data.${safe}\`, a name the client reserves, so its typed accessor is \`client.data.${accessor}\`. \`client.data.collection(${emitString(slug)})\` reaches it too.`);
|
|
190
|
+
const existing = bySafeName.get(accessor);
|
|
191
|
+
if (existing !== void 0) throw new CodegenError(`The collections ${emitString(existing)} and ${emitString(slug)} both generate the accessor "${accessor}", so only one of them could be reached from the generated client and the other's reads would silently go to the wrong table. Rename one of the slugs.`);
|
|
192
|
+
bySafeName.set(accessor, slug);
|
|
193
|
+
accessors.set(slug, accessor);
|
|
224
194
|
}
|
|
225
195
|
return accessors;
|
|
226
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* A column's type on a write: its own type (a date also as a `Date`), or
|
|
199
|
+
* `null` too when it is not required.
|
|
200
|
+
*
|
|
201
|
+
* `Row` has typed a non-required column `T | null` for a while; its write twins
|
|
202
|
+
* did not, so the one way to clear a value — `update(id, { publishedAt: null })`,
|
|
203
|
+
* `{ authorId: null }` to unassign, the edit-form round trip of a row that came
|
|
204
|
+
* back with nulls in it — was a compile error the server would have accepted.
|
|
205
|
+
* A primary key is never null, required or not.
|
|
206
|
+
*/
|
|
207
|
+
function writableType(prop, tsType) {
|
|
208
|
+
const written = prop.type === "date" ? `${tsType} | Date` : tsType;
|
|
209
|
+
return prop.validation?.required || isPrimaryKey(prop) ? written : `${written} | null`;
|
|
210
|
+
}
|
|
227
211
|
/** One emitted `key: type;` line, already indented. */
|
|
228
212
|
function line(key, type, optional) {
|
|
229
213
|
return ` ${emitKey(key)}${optional ? "?" : ""}: ${type};`;
|
|
@@ -235,9 +219,9 @@ function line(key, type, optional) {
|
|
|
235
219
|
* property. `Row` already honoured that; `Insert` and `Update` deliberately did
|
|
236
220
|
* not, on the reading that the column is stripped from responses rather than
|
|
237
221
|
* from writes. That left the generated types as the one place a password hash
|
|
238
|
-
* was still named, and it invited a client to send one. The server
|
|
239
|
-
*
|
|
240
|
-
*
|
|
222
|
+
* was still named, and it invited a client to send one. The server refuses
|
|
223
|
+
* such a field on a write (`VALIDATION_EXCLUDED_FIELDS`), so nothing generated
|
|
224
|
+
* advertises it.
|
|
241
225
|
*
|
|
242
226
|
* Read through `effectiveAccess`, so the flag and its longhand
|
|
243
227
|
* `access: { read: [], write: [] }` produce the same file. A *role* rule is
|
|
@@ -261,6 +245,28 @@ function excludedApiKeys(properties) {
|
|
|
261
245
|
}
|
|
262
246
|
return excluded;
|
|
263
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* The keys nobody may write: `access: { write: [] }`, readable or not.
|
|
250
|
+
*
|
|
251
|
+
* Off `Insert` and `Update` for the same reason `excludedApiKeys` takes a
|
|
252
|
+
* hidden property off all three — except that a field everyone can read but
|
|
253
|
+
* nobody can write stays on `Row`. The server refuses one on any write with
|
|
254
|
+
* `VALIDATION_EXCLUDED_FIELDS`, for every caller, `admin` included, so a
|
|
255
|
+
* generated `create({ computed })` compiled and could only ever be a 400.
|
|
256
|
+
*
|
|
257
|
+
* A write rule naming *roles* is not this: some caller can write it, and one
|
|
258
|
+
* generated shape cannot say which.
|
|
259
|
+
*/
|
|
260
|
+
function unwritableKeys(properties) {
|
|
261
|
+
const unwritable = /* @__PURE__ */ new Set();
|
|
262
|
+
for (const [key, rawProp] of Object.entries(properties)) {
|
|
263
|
+
const prop = rawProp;
|
|
264
|
+
if (effectiveAccess(prop)?.write?.length !== 0) continue;
|
|
265
|
+
unwritable.add(key);
|
|
266
|
+
if (prop.columnName) unwritable.add(prop.columnName);
|
|
267
|
+
}
|
|
268
|
+
return unwritable;
|
|
269
|
+
}
|
|
264
270
|
function generateTypedefs(input) {
|
|
265
271
|
const collections = sortCollectionsBySlug(input);
|
|
266
272
|
const accessors = buildAccessors(collections);
|
|
@@ -325,39 +331,42 @@ function generateTypedefs(input) {
|
|
|
325
331
|
}
|
|
326
332
|
lines.push(" };");
|
|
327
333
|
lines.push(" Insert: {");
|
|
334
|
+
const notWritable = /* @__PURE__ */ new Set([...excluded, ...unwritableKeys(properties)]);
|
|
328
335
|
emittedKeys.clear();
|
|
329
|
-
for (const key of
|
|
336
|
+
for (const key of notWritable) emittedKeys.add(key);
|
|
330
337
|
for (const [key, rawProp] of Object.entries(properties)) {
|
|
331
338
|
const prop = rawProp;
|
|
332
339
|
if (prop.type === "relation") continue;
|
|
333
|
-
if (
|
|
340
|
+
if (notWritable.has(key)) continue;
|
|
334
341
|
const tsType = propertyToTypeScriptType(prop);
|
|
335
|
-
const isOptional = !prop.validation?.required || isAutoAssignedId(prop);
|
|
336
|
-
lines.push(line(key, tsType, isOptional));
|
|
342
|
+
const isOptional = !prop.validation?.required || isAutoAssignedId(prop) || Boolean(prop.databaseDefault);
|
|
343
|
+
lines.push(line(key, writableType(prop, tsType), isOptional));
|
|
337
344
|
emittedKeys.add(key);
|
|
338
345
|
}
|
|
339
346
|
emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, false);
|
|
340
347
|
lines.push(" };");
|
|
341
348
|
lines.push(" Update: {");
|
|
342
349
|
emittedKeys.clear();
|
|
343
|
-
for (const key of
|
|
350
|
+
for (const key of notWritable) emittedKeys.add(key);
|
|
344
351
|
for (const [key, rawProp] of Object.entries(properties)) {
|
|
345
352
|
const prop = rawProp;
|
|
346
353
|
if (prop.type === "relation") continue;
|
|
347
354
|
if (isPrimaryKey(prop)) continue;
|
|
348
|
-
if (
|
|
349
|
-
lines.push(line(key, propertyToTypeScriptType(prop), true));
|
|
355
|
+
if (notWritable.has(key)) continue;
|
|
356
|
+
lines.push(line(key, writableType(prop, propertyToTypeScriptType(prop)), true));
|
|
350
357
|
emittedKeys.add(key);
|
|
351
358
|
}
|
|
352
359
|
emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, true);
|
|
353
360
|
lines.push(" };");
|
|
354
|
-
lines.push(" Relations:
|
|
361
|
+
if (Object.keys(resolvedRelations).length === 0) lines.push(" Relations: Record<never, never>;");
|
|
362
|
+
else lines.push(" Relations: {");
|
|
355
363
|
for (const [key, relation] of Object.entries(resolvedRelations)) {
|
|
356
364
|
const slug = resolveTargetCollection(relation)?.slug ?? relation.targetSlug;
|
|
357
365
|
const accessor = slug ? accessors.get(slug) : void 0;
|
|
358
366
|
lines.push(` ${emitKey(key)}: ${accessor ? emitString(accessor) : "never"};`);
|
|
359
367
|
}
|
|
360
|
-
lines.push(" };");
|
|
368
|
+
if (Object.keys(resolvedRelations).length > 0) lines.push(" };");
|
|
369
|
+
lines.push(` Slug: ${emitString(collection.slug)};`);
|
|
361
370
|
lines.push(" };");
|
|
362
371
|
}
|
|
363
372
|
lines.push("}");
|
|
@@ -480,8 +489,9 @@ function includeHelperLines() {
|
|
|
480
489
|
function emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, allOptional) {
|
|
481
490
|
const emit = (key, relation) => {
|
|
482
491
|
if (emittedKeys.has(key)) return;
|
|
483
|
-
const
|
|
484
|
-
|
|
492
|
+
const required = isRelationRequired(collection, relation);
|
|
493
|
+
const type = required ? foreignKeyType(relation) : `${foreignKeyType(relation)} | null`;
|
|
494
|
+
lines.push(line(key, type, allOptional || !required));
|
|
485
495
|
emittedKeys.add(key);
|
|
486
496
|
};
|
|
487
497
|
for (const relation of Object.values(resolvedRelations)) if (relation.kind === "belongsTo" && relation.localKey) emit(fieldKeyForColumn(collection, relation.localKey), relation);
|
|
@@ -490,6 +500,31 @@ function emitWritableRelations(lines, collection, properties, resolvedRelations,
|
|
|
490
500
|
const relation = findRelation(resolvedRelations, key);
|
|
491
501
|
if (relation?.kind === "belongsTo" && relation.localKey) emit(key, relation);
|
|
492
502
|
}
|
|
503
|
+
for (const [key, relation] of Object.entries(resolvedRelations)) {
|
|
504
|
+
if (relation.cardinality !== "many" || !relation.writable) continue;
|
|
505
|
+
if (emittedKeys.has(key)) continue;
|
|
506
|
+
const fk = foreignKeyType(relation);
|
|
507
|
+
const pivot = relation.kind === "manyToMany" ? pivotType(relation.through.properties, "write") : void 0;
|
|
508
|
+
const element = `${fk} | { id: ${fk}${pivot ? `; _pivot?: ${pivot}` : ""} }`;
|
|
509
|
+
lines.push(line(key, `Array<${element}>`, true));
|
|
510
|
+
emittedKeys.add(key);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* The `_pivot` a many-to-many link carries: the junction's payload columns,
|
|
515
|
+
* typed as a row's columns are on a read and as a write's on a write. `undefined`
|
|
516
|
+
* when the junction declares none, so there is no `_pivot` to name.
|
|
517
|
+
*/
|
|
518
|
+
function pivotType(pivotProperties, direction) {
|
|
519
|
+
const entries = Object.entries(pivotProperties);
|
|
520
|
+
if (entries.length === 0) return void 0;
|
|
521
|
+
return `{ ${entries.map(([key, rawProp]) => {
|
|
522
|
+
const prop = rawProp;
|
|
523
|
+
const tsType = propertyToTypeScriptType(prop);
|
|
524
|
+
const required = Boolean(prop.validation?.required);
|
|
525
|
+
const type = direction === "write" ? writableType(prop, tsType) : required ? tsType : `${tsType} | null`;
|
|
526
|
+
return `${emitKey(key)}${required && direction === "read" ? "" : "?"}: ${type};`;
|
|
527
|
+
}).join(" ")} }`;
|
|
493
528
|
}
|
|
494
529
|
//#endregion
|
|
495
530
|
//#region src/index.ts
|
|
@@ -543,6 +578,8 @@ JSON — and nothing here renames anything.
|
|
|
543
578
|
the column spelling.
|
|
544
579
|
- **A collection accessor is camelCase too** (\`my-notes\` → \`rebase.data.myNotes\`),
|
|
545
580
|
which is what \`collectionsDictionary\` maps back to the slug.
|
|
581
|
+
A slug whose accessor the client reserves (\`collection\`, \`then\`, \`toJSON\`)
|
|
582
|
+
is generated as \`<name>Collection\`: \`rebase.data.collectionCollection\`.
|
|
546
583
|
|
|
547
584
|
\`where\` and \`orderBy\` are keyed off the same type, so what compiles is what the
|
|
548
585
|
backend answers to.
|
|
@@ -568,6 +605,6 @@ but it is generic over \`Record<string, unknown>\` and gives up everything above
|
|
|
568
605
|
return files;
|
|
569
606
|
}
|
|
570
607
|
//#endregion
|
|
571
|
-
export { CodegenError, generateSDK, generateTypedefs, indent, toCamelCase, toPascalCase, toSafeIdentifier };
|
|
608
|
+
export { CodegenError, generateSDK, generateTypedefs, indent, toCamelCase, toCollectionAccessor, toPascalCase, toSafeIdentifier };
|
|
572
609
|
|
|
573
610
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","names":[],"sources":["../src/utils.ts","../src/generate-types.ts","../src/index.ts"],"sourcesContent":["/**\n * Utility functions for the SDK generator\n */\n\n/**\n * Convert a slug/snake_case string to PascalCase\n * e.g. \"private_notes\" → \"PrivateNotes\"\n *\n * Capitals already inside a word are meaningful and are kept: lowercasing the\n * tail of every chunk turned \"TestEntities\" into \"Testentities\", which is what\n * ended up in the generated type names. SHOUTING_CASE is the one shape where\n * the tail is not meaningful, so it is folded down.\n */\nexport function toPascalCase(str: string): string {\n return str\n .split(/[_\\-\\s]+/)\n .filter(Boolean)\n .map(word => {\n const rest = /^[A-Z0-9]+$/.test(word) ? word.slice(1).toLowerCase() : word.slice(1);\n return word.charAt(0).toUpperCase() + rest;\n })\n .join(\"\");\n}\n\n/**\n * Convert a slug/snake_case string to camelCase\n * e.g. \"private_notes\" → \"privateNotes\"\n */\nexport function toCamelCase(str: string): string {\n if (!/[_\\-\\s]/.test(str)) {\n return str.charAt(0).toLowerCase() + str.slice(1);\n }\n const pascal = toPascalCase(str);\n return pascal.charAt(0).toLowerCase() + pascal.slice(1);\n}\n\n/**\n * Convert a slug to a safe JS identifier\n * e.g. \"private-notes\" → \"privateNotes\"\n */\nexport function toSafeIdentifier(str: string): string {\n return toCamelCase(str.replace(/[^a-zA-Z0-9_]/g, \"_\"));\n}\n\n/**\n * Indent a block of text by a given number of spaces\n */\nexport function indent(text: string, spaces: number): string {\n const pad = \" \".repeat(spaces);\n return text\n .split(\"\\n\")\n .map(line => (line.trim() ? pad + line : line))\n .join(\"\\n\");\n}\n","import { getDeclaredSubcollections, CollectionConfig, Property, Properties, MapProperty, ArrayProperty, StringProperty, NumberProperty, ResolvedRelation } from \"@rebasepro/types\";\nimport { effectiveAccess, fieldKeyForColumn, findRelation, isRelationRequired, resolveCollectionRelations, sortCollectionsBySlug } from \"@rebasepro/common\";\nimport { toSafeIdentifier } from \"./utils\";\n\n/**\n * A schema that cannot be expressed as a valid TypeScript file.\n *\n * Thrown rather than emitted. The generator used to concatenate whatever it was\n * given, so a slug that collided with another one, or that was not an\n * identifier, produced a file that either failed to compile or — worse —\n * compiled while quietly routing one collection to another's slug.\n */\nexport class CodegenError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"CodegenError\";\n }\n}\n\nconst IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * A property name for the emitted TypeScript: verbatim when it is a valid\n * identifier, quoted otherwise.\n *\n * Every key that reaches the output goes through here. Column names are not\n * required to be identifiers — `\"order\"`, `\"user id\"`, a quoted Postgres\n * identifier — and the previous behaviour of camel-casing them into shape\n * renamed the column in the type while the wire kept the original, so the\n * generated `Row` described fields that did not exist.\n */\nfunction emitKey(key: string): string {\n return IDENTIFIER.test(key) ? key : JSON.stringify(key);\n}\n\n/**\n * A string literal, escaped.\n *\n * `\"${value}\"` was the previous form. A value containing a quote closed the\n * literal early, which at best broke the file and at worst let a slug from a\n * remote contract inject top-level statements into a file the developer\n * compiles and bundles.\n */\nfunction emitString(value: string): string {\n return JSON.stringify(value);\n}\n\n/** The `id`s of an enum declared as an array, an array of `{ id }`, or an object map. */\nfunction enumIds(raw: unknown): (string | number)[] {\n if (Array.isArray(raw)) {\n return raw.map((entry: string | number | { id: string | number }) =>\n entry && typeof entry === \"object\" ? entry.id : entry);\n }\n if (raw && typeof raw === \"object\") return Object.keys(raw);\n return [];\n}\n\nfunction propertyToTypeScriptType(prop: Property): string {\n switch (prop.type) {\n case \"string\": {\n const sp = prop as StringProperty;\n if (sp.enum) {\n const ids = enumIds(sp.enum);\n if (ids.length === 0) return \"string\";\n return ids.map(v => emitString(String(v))).join(\" | \");\n }\n return \"string\";\n }\n case \"number\": {\n const np = prop as NumberProperty;\n if (np.enum) {\n const ids = enumIds(np.enum);\n const numbers = ids.map(Number);\n // A numeric enum carrying something that is not a number cannot\n // be written as a union of numeric literals. Widening to\n // `number` is imprecise; emitting `NaN | undefined` is invalid.\n if (ids.length === 0 || numbers.some(n => !Number.isFinite(n))) return \"number\";\n return numbers.map(n => String(n)).join(\" | \");\n }\n return \"number\";\n }\n case \"boolean\":\n return \"boolean\";\n case \"date\":\n return \"string\"; // ISO 8601 string over the wire\n case \"geopoint\":\n return \"{ latitude: number; longitude: number; }\";\n case \"reference\":\n return \"string | number\";\n case \"relation\":\n return \"string | number\";\n case \"map\": {\n const mapProp = prop as MapProperty;\n if (mapProp.properties) {\n const inner = Object.entries(mapProp.properties)\n .map(([k, v]) => {\n const child = v as Property;\n // Nested fields carry validation like any other. Emitting\n // them all required claimed a shape the payload does not\n // have to satisfy.\n const optional = !child.validation?.required;\n const type = propertyToTypeScriptType(child);\n return `${emitKey(k)}${optional ? \"?\" : \"\"}: ${optional ? `${type} | null` : type};`;\n })\n .join(\" \");\n return `{ ${inner} }`;\n }\n return \"Record<string, unknown>\";\n }\n case \"array\": {\n const arrProp = prop as ArrayProperty;\n if (arrProp.of) {\n return `Array<${propertyToTypeScriptType(arrProp.of as Property)}>`;\n }\n return \"Array<unknown>\";\n }\n case \"vector\":\n return \"number[]\";\n case \"binary\":\n return \"string\";\n default:\n return \"unknown\";\n }\n}\n\n/**\n * Unwrap a relation target that arrived as a module namespace rather than the\n * collection itself — `target: () => import(\"./authors\")` is a common slip.\n */\nfunction resolveTargetCollection(relation: ResolvedRelation): CollectionConfig | undefined {\n try {\n let target = relation.target() as CollectionConfig & { default?: CollectionConfig; __esModule?: boolean };\n if (target && (target.default || target.__esModule)) {\n target = (target.default ?? target) as typeof target;\n }\n return target;\n } catch {\n return undefined;\n }\n}\n\n/** The TypeScript type of a foreign key: whatever the target's primary key is. */\nfunction foreignKeyType(relation: ResolvedRelation): string {\n const target = resolveTargetCollection(relation);\n if (!target?.properties) return \"string | number\";\n const idProp = Object.entries(target.properties).find(([_, p]) => (p as Record<string, unknown>).isId);\n if (!idProp) return \"string | number\";\n return (idProp[1] as Property).type === \"number\" ? \"number\" : \"string\";\n}\n\n/**\n * The property's `isId`, on the two property kinds that declare one.\n *\n * `Property` is a union and only `StringProperty` and `NumberProperty` carry\n * `isId`, so it cannot be read off the union directly — which is what\n * `(prop as unknown as Record<string, unknown>).isId` was for, twice. The union\n * discriminates on `type`, so narrowing gets there with no claim at all, and\n * with the declared type (`boolean | \"manual\" | \"uuid\" | string`) intact\n * instead of flattened to `unknown`.\n */\nfunction declaredIsId(prop: Property): StringProperty[\"isId\"] | NumberProperty[\"isId\"] {\n return prop.type === \"string\" || prop.type === \"number\" ? prop.isId : undefined;\n}\n\n/** Whether a property is the collection's primary key. */\nfunction isPrimaryKey(prop: Property): boolean {\n return Boolean(declaredIsId(prop));\n}\n\n/**\n * Whether the server assigns this primary key, so a write does not have to.\n * `true` and `\"manual\"` both mean the caller supplies it.\n */\nfunction isAutoAssignedId(prop: Property): boolean {\n const isId = declaredIsId(prop);\n return Boolean(isId) && isId !== \"manual\" && isId !== true;\n}\n\n/**\n * The type an *included* relation arrives as: the target's own row, inlined.\n *\n * This is what the read pipeline actually serves — `toRestRow` puts the\n * target's flat columns where the relation was, and the SDK and the HTTP API\n * both go through it. It is deliberately *not* a `{ __type: \"relation\" }`\n * envelope: that shape is the admin's view-model and never reaches a\n * developer's `find()`.\n *\n * Falls back to an open record when the target is not part of this generation\n * run, since there is no `Row` to point at.\n */\nfunction includedRelationType(\n relation: ResolvedRelation,\n accessors: Map<string, string>\n): string {\n const target = resolveTargetCollection(relation);\n const slug = target?.slug ?? relation.targetSlug;\n const accessor = slug ? accessors.get(slug) : undefined;\n const rowType = accessor\n ? `Database[${emitString(accessor)}][\"Row\"]`\n : \"Record<string, unknown>\";\n return relation.cardinality === \"many\" ? `Array<${rowType}>` : rowType;\n}\n\n/**\n * Map every slug to the property name it is reachable under on `client.data`.\n *\n * The accessor is a safe identifier because `client.data.myNotes` is the point\n * of generating this at all, and `collectionsDictionary` maps it back to the\n * slug the wire uses. Two slugs that safe down to the same identifier cannot\n * both have it: the interface would not compile, and the dictionary — an object\n * literal — would silently keep only the last, routing one collection's reads\n * to the other's table. There is no defensible way to pick, so this refuses.\n */\nfunction buildAccessors(collections: CollectionConfig[]): Map<string, string> {\n const accessors = new Map<string, string>();\n const bySafeName = new Map<string, string>();\n\n for (const collection of collections) {\n const slug = collection.slug;\n if (typeof slug !== \"string\" || slug.length === 0) {\n throw new CodegenError(\n \"A collection has no slug, so it has no name to generate a type for. \" +\n \"Every collection needs a unique `slug`.\"\n );\n }\n\n const safe = toSafeIdentifier(slug);\n if (safe.length === 0) {\n throw new CodegenError(\n `The slug ${emitString(slug)} has no characters that can form a property name, ` +\n \"so it cannot be reached as `client.data.<name>`. Use a slug containing \" +\n \"letters, digits, underscores or dashes.\"\n );\n }\n\n const existing = bySafeName.get(safe);\n if (existing !== undefined) {\n throw new CodegenError(\n `The collections ${emitString(existing)} and ${emitString(slug)} both generate the ` +\n `accessor \"${safe}\", so only one of them could be reached from the generated client ` +\n \"and the other's reads would silently go to the wrong table. Rename one of the slugs.\"\n );\n }\n\n bySafeName.set(safe, slug);\n accessors.set(slug, safe);\n }\n\n return accessors;\n}\n\n/** One emitted `key: type;` line, already indented. */\nfunction line(key: string, type: string, optional: boolean): string {\n return ` ${emitKey(key)}${optional ? \"?\" : \"\"}: ${type};`;\n}\n\n/**\n * The keys nobody can reach take off the API surface — in *both* directions.\n *\n * `excludeFromApi` means one thing: the API surface does not mention this\n * property. `Row` already honoured that; `Insert` and `Update` deliberately did\n * not, on the reading that the column is stripped from responses rather than\n * from writes. That left the generated types as the one place a password hash\n * was still named, and it invited a client to send one. The server still\n * *accepts* such a field on a write — this describes the surface, it does not\n * add an enforcement point — but nothing generated advertises it.\n *\n * Read through `effectiveAccess`, so the flag and its longhand\n * `access: { read: [], write: [] }` produce the same file. A *role* rule is\n * deliberately not honoured here and `Row` is unchanged by one: a generated type\n * is one shape for every caller, and there is no `Row` that is right for both a\n * reader who holds `hr` and one who does not. The server is the enforcement\n * point; the types describe the surface a caller may name.\n *\n * Keyed by the property name *and* by its column name, the same pair the\n * server's `stripUnreadable` deletes, so a foreign key or a relation addressed\n * under the column name cannot put the property back.\n */\nfunction excludedApiKeys(properties: Properties): Set<string> {\n const excluded = new Set<string>();\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n const access = effectiveAccess(prop);\n if (access?.read?.length !== 0 || access?.write?.length !== 0) continue;\n excluded.add(key);\n if (prop.columnName) excluded.add(prop.columnName);\n }\n return excluded;\n}\n\nexport function generateTypedefs(input: CollectionConfig[]): string {\n // Sorted here rather than only in `generate-sdk`: the output is\n // order-dependent and `rebase doctor` regenerates it in memory to diff\n // against the file on disk. While only the writer sorted, a project whose\n // file order differed from its slug order was reported permanently stale.\n const collections = sortCollectionsBySlug(input);\n const accessors = buildAccessors(collections);\n const lines: string[] = [\n \"/**\",\n \" * This file was auto-generated by Rebase.\",\n \" * Do not make direct changes to the file.\",\n \" */\",\n \"\",\n \"export interface Database {\"\n ];\n\n for (const collection of collections) {\n const properties = (collection.properties ?? {}) as Properties;\n\n // Resolve relations\n let resolvedRelations: Record<string, ResolvedRelation> = {};\n try {\n resolvedRelations = resolveCollectionRelations(collection);\n } catch (e) {\n // Swallowed before, which made this the quietest way to ship a\n // wrong type. The foreign-key columns are emitted from the resolved\n // relations rather than from the properties, so losing them drops\n // both the relation fields *and* columns that exist in the\n // database — and the resulting error surfaces in the user's code,\n // typechecking against a `Database` that is missing `author_id`,\n // with nothing pointing back at generation.\n //\n // A target thunk usually throws because of a circular import; the\n // boot-time relation validator names the same cause.\n console.warn(\n `[rebase] Could not resolve the relations of \"${collection.slug}\", so its generated ` +\n \"type has no relation fields and none of their foreign-key columns. This is usually a \" +\n \"circular import in the collection files — make sure the target is `() => otherCollection` \" +\n `and not evaluated at module load.\\n ${e instanceof Error ? e.message : String(e)}`\n );\n }\n\n // Subcollections are collections in their own right and are addressed\n // over a nested path, not as `client.data.<name>`. Generating them here\n // would invent an accessor the client does not serve, so they are\n // skipped — loudly, because doing it silently is how a developer\n // concludes the generator is broken.\n //\n // `getDeclaredSubcollections` rather than a local shape. What stood here\n // declared `subcollections?: unknown[]` and tested it with\n // `Array.isArray` — but the field is `() => CollectionConfig[]`, a thunk,\n // so that test was always false and this warning has never once fired.\n // The two declarations of the same field disagreed, and the\n // `as unknown as` between them is what let the wrong one compile.\n //\n // The thunk is called inside the same `try` shape the relation resolver\n // above uses: it exists to defer a circular import, and evaluating it\n // during codegen can throw for exactly that reason. A count that cannot\n // be taken is not worth failing a generate over.\n const declared = getDeclaredSubcollections(collection);\n let subcollectionCount = 0;\n if (declared) {\n try {\n subcollectionCount = declared().length;\n } catch {\n // Declared, but not resolvable from here. Still worth saying.\n subcollectionCount = -1;\n }\n }\n if (subcollectionCount !== 0) {\n console.warn(\n `[rebase] \"${collection.slug}\" declares ${subcollectionCount < 0 ? \"\" : `${subcollectionCount} `}subcollection(s), which are ` +\n \"not part of the generated Database: they are reached over a nested path \" +\n `(\\`data/${collection.slug}/<id>/<relation>\\`), not as a top-level accessor. Register a ` +\n \"subcollection as a collection of its own if you want a typed accessor for it.\"\n );\n }\n\n lines.push(` ${emitKey(accessors.get(collection.slug)!)}: {`);\n\n // ── Row Type ──\n //\n // What a read serves.\n //\n // ## A `belongsTo` has three shapes, and all three are typed\n //\n // One relation, three places it appears, and the wire is not symmetric\n // about it — so the types are not either:\n //\n // 1. **Write** — `Insert`/`Update` accept *either* the foreign key\n // under its own wire name (`{ authorId: 5 }`) or the relation\n // property (`{ author: 5 }`), because the write transformer maps\n // the second onto the first. Emitted by `emitWritableRelations`.\n // 2. **Read** — a row carries `authorId`. Always: it is a column.\n // 3. **Read with `include`** — the target's own row arrives under\n // `author`. Emitted below as optional, because it is absent from\n // every read that did not name it; the generated `RowWith` makes it\n // required for a read that did.\n //\n // The three collapse in exactly one case — a relation named identically\n // to its own foreign key, where 3 is served *over* 2 — handled below.\n //\n // `fields` narrows which columns a read returns, but a column is\n // optional here only because the value may be absent or null, never\n // because the caller might not have asked for it: a projection is a\n // choice made at one call site, and typing every column optional to\n // describe it would make every row unusable everywhere else.\n lines.push(\" Row: {\");\n const emittedKeys = new Set<string>();\n\n // Off the surface entirely — see `excludedApiKeys`. Seeding the emitted\n // set means every later pass (foreign keys, relations, unresolved\n // relations) skips them too, since each of those already refuses to\n // emit a key twice.\n const excluded = excludedApiKeys(properties);\n for (const key of excluded) emittedKeys.add(key);\n\n // 1. Direct properties\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (prop.type === \"relation\") continue;\n if (excluded.has(key)) continue;\n\n const tsType = propertyToTypeScriptType(prop);\n // A primary key is on every row a read can return, whether or not\n // anyone wrote `validation: { required: true }` next to it —\n // introspection never does, so `row.id` was `string | undefined`\n // for every baas project.\n const isRequired = Boolean(prop.validation?.required) || isPrimaryKey(prop);\n lines.push(line(key, isRequired ? tsType : `${tsType} | null`, !isRequired));\n emittedKeys.add(key);\n }\n\n // 2. FK columns from relations.\n //\n // Emitted under the relation's *wire* name, which is what\n // `fieldKeyForColumn` answers: `localKey` is the database column\n // (`author_id`) and the row arrives keyed `authorId`. Emitting the\n // column, which this used to do, described a key the JSON does not\n // carry and hid the one it does — including from `where` and `orderBy`,\n // which are keyed off this type. The column name is not a second name\n // for the field: it is the name of a different thing.\n for (const [relKey, relation] of Object.entries(resolvedRelations)) {\n if (relation.kind === \"belongsTo\" && relation.localKey) {\n const fkKey = fieldKeyForColumn(collection, relation.localKey);\n if (emittedKeys.has(fkKey)) continue;\n\n const fkType = foreignKeyType(relation);\n\n // A relation addressed by the same name as its own foreign key\n // is served *over* that column when the read includes it: the\n // query nests the target under the relation name, and the\n // scalar it shadows is gone. Both outcomes are real, so the\n // column is typed as both — which is what stops a plain\n // `const id: string = row.author_id` from compiling.\n const shadowedByInclude = relKey === fkKey;\n const tsType = shadowedByInclude\n ? `${fkType} | ${includedRelationType(relation, accessors)}`\n : fkType;\n\n const isRequired = isRelationRequired(collection, relation) && !shadowedByInclude;\n lines.push(line(fkKey, isRequired ? tsType : `${tsType} | null`, !isRequired));\n emittedKeys.add(fkKey);\n }\n }\n\n // 3. Relation fields — the target's own row, inlined.\n //\n // Optional throughout: a relation is only loaded when the read names it\n // in `include`, so it is absent from every other read.\n for (const [key, relation] of Object.entries(resolvedRelations)) {\n if (emittedKeys.has(key)) continue;\n lines.push(line(key, includedRelationType(relation, accessors), true));\n emittedKeys.add(key);\n }\n\n // A `relation` property whose relation could not be resolved — an\n // engine without relation support, or a target that did not load. It is\n // still a column on the row, so it is still typed, just not precisely.\n for (const [key, rawProp] of Object.entries(properties)) {\n if ((rawProp as Property).type !== \"relation\") continue;\n if (emittedKeys.has(key)) continue;\n lines.push(line(key, \"Record<string, unknown>\", true));\n emittedKeys.add(key);\n }\n lines.push(\" };\");\n\n // ── Insert Type ──\n //\n // What `create()` accepts, minus the `excludeFromApi` columns: the\n // property is off the API surface in both directions, so a generated\n // client never names it.\n lines.push(\" Insert: {\");\n emittedKeys.clear();\n for (const key of excluded) emittedKeys.add(key);\n\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (prop.type === \"relation\") continue;\n if (excluded.has(key)) continue;\n const tsType = propertyToTypeScriptType(prop);\n const isOptional = !prop.validation?.required || isAutoAssignedId(prop);\n lines.push(line(key, tsType, isOptional));\n emittedKeys.add(key);\n }\n\n emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, false);\n lines.push(\" };\");\n\n // ── Update Type ──\n //\n // Everything optional, and the primary key left out: an update\n // addresses a row by id, it does not reassign one. Accepting `id` here\n // typechecked `update(id, { id: someoneElses })`.\n lines.push(\" Update: {\");\n emittedKeys.clear();\n for (const key of excluded) emittedKeys.add(key);\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (prop.type === \"relation\") continue;\n if (isPrimaryKey(prop)) continue;\n if (excluded.has(key)) continue;\n lines.push(line(key, propertyToTypeScriptType(prop), true));\n emittedKeys.add(key);\n }\n emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, true);\n lines.push(\" };\");\n\n // ── Relations ──\n //\n // Which relations this collection has, and which collection each one\n // reaches. Not a shape a read ever returns — it is the *graph*, and\n // `include` is the one parameter that needs it: `IncludeFor` below\n // walks it to constrain an include's keys to relations that exist, at\n // every level, and `RowWith` reads it to make an included relation\n // non-optional on the row that comes back.\n //\n // Emitted as the accessor NAME rather than the target's `Row`, so a\n // self-referencing or mutually-referencing relation is a finite string\n // instead of a type that expands forever.\n lines.push(\" Relations: {\");\n for (const [key, relation] of Object.entries(resolvedRelations)) {\n const target = resolveTargetCollection(relation);\n const slug = target?.slug ?? relation.targetSlug;\n const accessor = slug ? accessors.get(slug) : undefined;\n // A target outside this generation run has no accessor to name, so\n // the relation is typed as reaching nothing rather than as reaching\n // something that does not exist.\n lines.push(` ${emitKey(key)}: ${accessor ? emitString(accessor) : \"never\"};`);\n }\n lines.push(\" };\");\n\n lines.push(\" };\");\n }\n\n lines.push(\"}\");\n lines.push(\"\");\n lines.push(\"export type CollectionName = keyof Database;\");\n lines.push(\"\");\n lines.push(\"export const collectionsDictionary = {\");\n for (const collection of collections) {\n lines.push(` ${emitKey(accessors.get(collection.slug)!)}: ${emitString(collection.slug)},`);\n }\n lines.push(\"} as const;\");\n lines.push(\"\");\n // Describes the const above rather than restating its keys. The previous\n // `{ [K in CollectionName]: K }` said every value equalled its key, which is\n // false for any slug that is not already an identifier — `myNotes` maps to\n // `\"my-notes\"` — so the export the CLI tells people to pass did not satisfy\n // its own published type.\n lines.push(\"export type CollectionsDictionary = typeof collectionsDictionary;\");\n lines.push(\"\");\n lines.push(...includeHelperLines());\n\n return lines.join(\"\\n\");\n}\n\n/**\n * The `include` type machinery, emitted into the generated file.\n *\n * It lives here rather than in `@rebasepro/types` because it is the *graph*\n * that makes it work, and the graph only exists once a project's collections\n * have been generated. `IncludeSpec` in `@rebasepro/types` is deliberately\n * unconstrained — a hand-written row type has no relations in it to check\n * against — and these narrow it for a project that does have them.\n *\n * Two things a caller gets:\n *\n * - `IncludeFor<\"posts\">` — an include whose keys are relations that exist,\n * recursively, so `include: { comments: { include: { authr: true } } }` is a\n * compile error rather than a 400 at runtime.\n * - `RowWith<\"posts\", I>` — the row a read with that include returns, where\n * every included relation is **present** rather than optional. `Row` types a\n * relation as optional because it is absent from every read that did not ask\n * for it; once a read has asked, `row.author.name` should not need a `?.`.\n *\n * The depth bound matches the server's (`MAX_INCLUDE_DEPTH`), and is spelled as\n * a decrementing tuple because TypeScript has no arithmetic — `Prev[3]` is `2`.\n * Without a bound, a self-referencing relation makes the type infinite and the\n * compiler gives up with \"type instantiation is excessively deep\".\n */\nfunction includeHelperLines(): string[] {\n return [\n \"/** Which collection each of `A`'s relations reaches. */\",\n \"export type RelationsOf<A extends CollectionName> =\",\n \" Database[A] extends { Relations: infer R } ? R : Record<string, never>;\",\n \"\",\n \"/** The names of `A`'s relations. */\",\n \"export type RelationKeys<A extends CollectionName> = keyof RelationsOf<A> & string;\",\n \"\",\n \"/** The collection a relation reaches, or `never` when it left this project. */\",\n \"export type RelationTarget<A extends CollectionName, K extends RelationKeys<A>> =\",\n \" RelationsOf<A>[K] extends CollectionName ? RelationsOf<A>[K] : never;\",\n \"\",\n \"/** Counts `IncludeFor` down; TypeScript has no arithmetic. */\",\n \"type Prev = [never, 0, 1, 2, 3];\",\n \"\",\n \"/**\",\n \" * Per-relation options, minus `include` — which `IncludeFor` supplies at\",\n \" * the next depth so the nested keys are checked against the *target's*\",\n \" * relations rather than this collection's.\",\n \" */\",\n \"export interface IncludeOptionsFor<A extends CollectionName, K extends RelationKeys<A>, D extends number> {\",\n \" limit?: number;\",\n \" where?: Record<string, unknown>;\",\n \" logical?: unknown;\",\n \" orderBy?: unknown;\",\n \" fields?: string[];\",\n \" include?: RelationTarget<A, K> extends CollectionName\",\n \" ? IncludeFor<RelationTarget<A, K>, Prev[D]>\",\n \" : never;\",\n \"}\",\n \"\",\n \"/**\",\n \" * An `include` for collection `A`: its relation names, dotted paths, or the\",\n \" * parametrised tree — with every key checked against the relations that\",\n \" * actually exist, at every level.\",\n \" */\",\n \"export type IncludeFor<A extends CollectionName, D extends number = 3> =\",\n \" D extends 0\",\n \" ? never\",\n \" : | readonly (RelationKeys<A> | \\\"*\\\")[]\",\n \" | { [K in RelationKeys<A>]?: true | IncludeOptionsFor<A, K, D> };\",\n \"\",\n \"/** The relation names an include asks for at the top level. */\",\n \"type IncludedKeys<A extends CollectionName, I> =\",\n \" I extends readonly (infer K)[]\",\n \" ? Extract<K, RelationKeys<A>>\",\n \" : Extract<keyof I, RelationKeys<A>>;\",\n \"\",\n \"/**\",\n \" * The row a read with include `I` returns: `Row`, with every included\",\n \" * relation made **required**.\",\n \" *\",\n \" * `Row` types a relation as optional because it is absent from every read\",\n \" * that did not name it. Once a read has named it, it is there — and having\",\n \" * to write `row.author?.name` after asking for the author is the type\",\n \" * describing a possibility the query already ruled out.\",\n \" */\",\n \"export type RowWith<A extends CollectionName, I> =\",\n \" Database[A][\\\"Row\\\"] & Required<Pick<Database[A][\\\"Row\\\"], IncludedKeys<A, I> & keyof Database[A][\\\"Row\\\"]>>;\",\n \"\"\n ];\n}\n\n/**\n * The two ways a write can name a `belongsTo` target, both of which the server\n * accepts: the foreign key under its own wire name (`{ authorId: 5 }`, which\n * passes through to the `author_id` column untouched) and the relation\n * *property* (`{ author: 5 }`, which the write transformer maps onto that\n * column).\n *\n * Only the first was generated, so the documented and idiomatic write shape was\n * a type error.\n *\n * The second form is emitted under the **property key**, not the resolved\n * relation name, because that is what the transformer keys off: it looks the\n * payload key up in `properties` and only treats it as a relation if what it\n * finds there is one. A relation whose `relationName` differs from its property\n * key is reachable as the property and not as the name, so emitting the name\n * would have offered a key that writes to a column that does not exist.\n */\nfunction emitWritableRelations(\n lines: string[],\n collection: CollectionConfig,\n properties: Properties,\n resolvedRelations: Record<string, ResolvedRelation>,\n emittedKeys: Set<string>,\n allOptional: boolean\n): void {\n // The target's primary key type is the same one `Row` uses. A hardcoded\n // `string | number` here accepted a string for a numeric-keyed target.\n const emit = (key: string, relation: ResolvedRelation): void => {\n if (emittedKeys.has(key)) return;\n const optional = allOptional || !isRelationRequired(collection, relation);\n lines.push(line(key, foreignKeyType(relation), optional));\n emittedKeys.add(key);\n };\n\n for (const relation of Object.values(resolvedRelations)) {\n if (relation.kind === \"belongsTo\" && relation.localKey) {\n emit(fieldKeyForColumn(collection, relation.localKey), relation);\n }\n }\n\n for (const [key, rawProp] of Object.entries(properties)) {\n if ((rawProp as Property).type !== \"relation\") continue;\n const relation = findRelation(resolvedRelations, key);\n if (relation?.kind === \"belongsTo\" && relation.localKey) emit(key, relation);\n }\n}\n","/**\n * @rebasepro/codegen\n *\n * Generates a purely typed Typescript database definition.\n */\n\nimport { CollectionConfig } from \"@rebasepro/types\";\nimport { generateTypedefs } from \"./generate-types\";\n\nexport { generateTypedefs, CodegenError } from \"./generate-types\";\nexport { toPascalCase, toCamelCase, toSafeIdentifier, indent } from \"./utils\";\n\n// ─── Public API ────────────────────────────────────────────────────\n\nexport interface GeneratedFile {\n /** Relative file path within the output directory */\n path: string;\n /** File content */\n content: string;\n}\n\nexport interface GenerateSDKOptions {\n /** Whether to include a README file (default: true) */\n includeReadme?: boolean;\n}\n\nexport function generateSDK(\n collections: CollectionConfig[],\n options: GenerateSDKOptions = {}\n): GeneratedFile[] {\n const files: GeneratedFile[] = [];\n\n files.push({\n path: \"database.types.ts\",\n content: generateTypedefs(collections)\n });\n\n if (options.includeReadme !== false) {\n files.push({\n path: \"README.md\",\n content: `# Rebase SDK\n\n> Auto-generated by \\`rebase generate-sdk\\`. Do not edit manually.\n\n## Usage\n\n1. Install the client package:\n \\`\\`\\`bash\n npm install @rebasepro/client\n \\`\\`\\`\n\n2. Initialize with your generated types:\n \\`\\`\\`typescript\n import { createRebaseClient } from '@rebasepro/client';\n import { collectionsDictionary, type Database } from './database.types';\n\n const rebase = createRebaseClient<Database>({\n baseUrl: 'http://localhost:3001',\n // Maps each accessor back to the slug the wire uses. Without it a\n // hyphenated slug is not resolvable from the property name alone.\n collections: collectionsDictionary,\n });\n\n // Property access is the typed surface: rows, filters and sorts are all\n // checked against the generated Database.\n const { data: users } = await rebase.data.users.find();\n console.log(users[0].email); // flat access — no .values wrapper\n \\`\\`\\`\n\n## Field names are the ones the API serves\n\nThe generated \\`Row\\` uses each field's **wire** name — the key it arrives under in\nJSON — and nothing here renames anything.\n\n- **A declared property is its key in the collection.** A property keyed\n \\`createdAt\\` is \\`row.createdAt\\`, whatever \\`columnName\\` says. A column name is\n the name of a different thing: where the value lives, not what the API calls it.\n- **A foreign key derived from a relation is camelCase**, because that is what\n the wire carries. A \\`belongsTo\\` named \\`author\\` gives you \\`row.authorId\\`, not\n the column spelling.\n- **A collection accessor is camelCase too** (\\`my-notes\\` → \\`rebase.data.myNotes\\`),\n which is what \\`collectionsDictionary\\` maps back to the slug.\n\n\\`where\\` and \\`orderBy\\` are keyed off the same type, so what compiles is what the\nbackend answers to.\n\n## \\`Row\\` vs \\`Insert\\` vs \\`Update\\`\n\n| Type | What it describes |\n|---|---|\n| \\`Row\\` | What a read serves. Nullable columns are \\`T \\\\| null\\`; relations appear only when \\`include\\` names them. |\n| \\`Insert\\` | What \\`create()\\` accepts. Server-assigned ids are optional; a \\`belongsTo\\` target may be named either way (\\`{ author: 5 }\\` or \\`{ authorId: 5 }\\`). |\n| \\`Update\\` | What \\`update()\\` accepts. Everything optional, and the primary key is not settable. |\n\nA property marked \\`excludeFromApi\\` is absent from all three: the API surface\ndoes not mention it, in either direction. The server holds the same line — a\nread never serves the column and a write naming it is refused — so this is a\nguarantee rather than a description, and nothing generated names a password\nhash.\n\nIf you need an untyped escape hatch, \\`rebase.data.collection(slug)\\` still works —\nbut it is generic over \\`Record<string, unknown>\\` and gives up everything above.\n`\n });\n }\n\n return files;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,SAAgB,aAAa,KAAqB;CAC9C,OAAO,IACF,MAAM,UAAU,CAAC,CACjB,OAAO,OAAO,CAAC,CACf,KAAI,SAAQ;EACT,MAAM,OAAO,cAAc,KAAK,IAAI,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,MAAM,CAAC;EAClF,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI;CAC1C,CAAC,CAAC,CACD,KAAK,EAAE;AAChB;;;;;AAMA,SAAgB,YAAY,KAAqB;CAC7C,IAAI,CAAC,UAAU,KAAK,GAAG,GACnB,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,IAAI,MAAM,CAAC;CAEpD,MAAM,SAAS,aAAa,GAAG;CAC/B,OAAO,OAAO,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,OAAO,MAAM,CAAC;AAC1D;;;;;AAMA,SAAgB,iBAAiB,KAAqB;CAClD,OAAO,YAAY,IAAI,QAAQ,kBAAkB,GAAG,CAAC;AACzD;;;;AAKA,SAAgB,OAAO,MAAc,QAAwB;CACzD,MAAM,MAAM,IAAI,OAAO,MA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AkB,YAAY,SAAS,QAAQ,GAAG,QAAQ;CAIvE,KAAK,MAAM,CAAC,KAAK,YAAY,OAAO,QAAQ,UAAU,GAAG;EACrD,IAAK,QAAqB,SAAS,YAAY;EAC/C,MAAM,WAAW,aAAa,mBAAmB,GAAG;EACpD,IAAI,UAAU,SAAS,eAAe,SAAS,UAAU,KAAK,KAAK,QAAQ;CAC/E;AACJ;;;AClqBA,SAAgB,YACZ,aACA,UAA8B,CAAC,GAChB;CACf,MAAM,QAAyB,CAAC;CAEhC,MAAM,KAAK;EACP,MAAM;EACN,SAAS,iBAAiB,WAAW;CACzC,CAAC;CAED,IAAI,QAAQ,kBAAkB,OAC1B,MAAM,KAAK;EACP,MAAM;EACN,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+Db,CAAC;CAGL,OAAO;AACX"}
|
|
1
|
+
{"version":3,"file":"index.es.js","names":[],"sources":["../src/utils.ts","../src/generate-types.ts","../src/index.ts"],"sourcesContent":["/**\n * Utility functions for the SDK generator\n */\n\n/**\n * The naming rules shared with the server's data proxies, which resolve a\n * generated accessor back to its slug: one home, so the two cannot drift.\n */\nexport { toPascalCase, toCamelCase, toSafeIdentifier, toCollectionAccessor } from \"@rebasepro/common\";\n\n/**\n * Indent a block of text by a given number of spaces\n */\nexport function indent(text: string, spaces: number): string {\n const pad = \" \".repeat(spaces);\n return text\n .split(\"\\n\")\n .map(line => (line.trim() ? pad + line : line))\n .join(\"\\n\");\n}\n","import { getDeclaredSubcollections, CollectionConfig, Property, Properties, MapProperty, ArrayProperty, StringProperty, NumberProperty, ResolvedRelation } from \"@rebasepro/types\";\nimport { effectiveAccess, fieldKeyForColumn, findRelation, isRelationRequired, resolveCollectionRelations, sortCollectionsBySlug } from \"@rebasepro/common\";\nimport { toCollectionAccessor, toSafeIdentifier } from \"./utils\";\n\n/**\n * A schema that cannot be expressed as a valid TypeScript file.\n *\n * Thrown rather than emitted. The generator used to concatenate whatever it was\n * given, so a slug that collided with another one, or that was not an\n * identifier, produced a file that either failed to compile or — worse —\n * compiled while quietly routing one collection to another's slug.\n */\nexport class CodegenError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"CodegenError\";\n }\n}\n\nconst IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\n\n/**\n * A property name for the emitted TypeScript: verbatim when it is a valid\n * identifier, quoted otherwise.\n *\n * Every key that reaches the output goes through here. Column names are not\n * required to be identifiers — `\"order\"`, `\"user id\"`, a quoted Postgres\n * identifier — and the previous behaviour of camel-casing them into shape\n * renamed the column in the type while the wire kept the original, so the\n * generated `Row` described fields that did not exist.\n */\nfunction emitKey(key: string): string {\n return IDENTIFIER.test(key) ? key : JSON.stringify(key);\n}\n\n/**\n * A string literal, escaped.\n *\n * `\"${value}\"` was the previous form. A value containing a quote closed the\n * literal early, which at best broke the file and at worst let a slug from a\n * remote contract inject top-level statements into a file the developer\n * compiles and bundles.\n */\nfunction emitString(value: string): string {\n return JSON.stringify(value);\n}\n\n/** The `id`s of an enum declared as an array, an array of `{ id }`, or an object map. */\nfunction enumIds(raw: unknown): (string | number)[] {\n if (Array.isArray(raw)) {\n return raw.map((entry: string | number | { id: string | number }) =>\n entry && typeof entry === \"object\" ? entry.id : entry);\n }\n if (raw && typeof raw === \"object\") return Object.keys(raw);\n return [];\n}\n\nfunction propertyToTypeScriptType(prop: Property): string {\n switch (prop.type) {\n case \"string\": {\n const sp = prop as StringProperty;\n if (sp.enum) {\n const ids = enumIds(sp.enum);\n if (ids.length === 0) return \"string\";\n return ids.map(v => emitString(String(v))).join(\" | \");\n }\n return \"string\";\n }\n case \"number\": {\n const np = prop as NumberProperty;\n if (np.enum) {\n const ids = enumIds(np.enum);\n const numbers = ids.map(Number);\n // A numeric enum carrying something that is not a number cannot\n // be written as a union of numeric literals. Widening to\n // `number` is imprecise; emitting `NaN | undefined` is invalid.\n if (ids.length === 0 || numbers.some(n => !Number.isFinite(n))) return \"number\";\n return numbers.map(n => String(n)).join(\" | \");\n }\n return \"number\";\n }\n case \"boolean\":\n return \"boolean\";\n case \"date\":\n return \"string\"; // ISO 8601 string over the wire\n case \"geopoint\":\n return \"{ latitude: number; longitude: number; }\";\n case \"reference\":\n return \"string | number\";\n case \"relation\":\n return \"string | number\";\n case \"map\": {\n const mapProp = prop as MapProperty;\n if (mapProp.properties) {\n const inner = Object.entries(mapProp.properties)\n .map(([k, v]) => {\n const child = v as Property;\n // Nested fields carry validation like any other. Emitting\n // them all required claimed a shape the payload does not\n // have to satisfy.\n const optional = !child.validation?.required;\n const type = propertyToTypeScriptType(child);\n return `${emitKey(k)}${optional ? \"?\" : \"\"}: ${optional ? `${type} | null` : type};`;\n })\n .join(\" \");\n return `{ ${inner} }`;\n }\n return \"Record<string, unknown>\";\n }\n case \"array\": {\n const arrProp = prop as ArrayProperty;\n if (arrProp.of) {\n return `Array<${propertyToTypeScriptType(arrProp.of as Property)}>`;\n }\n return \"Array<unknown>\";\n }\n case \"vector\":\n return \"number[]\";\n case \"binary\":\n return \"string\";\n default:\n return \"unknown\";\n }\n}\n\n/**\n * Unwrap a relation target that arrived as a module namespace rather than the\n * collection itself — `target: () => import(\"./authors\")` is a common slip.\n */\nfunction resolveTargetCollection(relation: ResolvedRelation): CollectionConfig | undefined {\n try {\n let target = relation.target() as CollectionConfig & { default?: CollectionConfig; __esModule?: boolean };\n if (target && (target.default || target.__esModule)) {\n target = (target.default ?? target) as typeof target;\n }\n return target;\n } catch {\n return undefined;\n }\n}\n\n/** The TypeScript type of a foreign key: whatever the target's primary key is. */\nfunction foreignKeyType(relation: ResolvedRelation): string {\n const target = resolveTargetCollection(relation);\n if (!target?.properties) return \"string | number\";\n const idProp = Object.entries(target.properties).find(([_, p]) => (p as Record<string, unknown>).isId);\n if (!idProp) return \"string | number\";\n return (idProp[1] as Property).type === \"number\" ? \"number\" : \"string\";\n}\n\n/**\n * The property's `isId`, on the two property kinds that declare one.\n *\n * `Property` is a union and only `StringProperty` and `NumberProperty` carry\n * `isId`, so it cannot be read off the union directly — which is what\n * `(prop as unknown as Record<string, unknown>).isId` was for, twice. The union\n * discriminates on `type`, so narrowing gets there with no claim at all, and\n * with the declared type (`boolean | \"manual\" | \"uuid\" | string`) intact\n * instead of flattened to `unknown`.\n */\nfunction declaredIsId(prop: Property): StringProperty[\"isId\"] | NumberProperty[\"isId\"] {\n return prop.type === \"string\" || prop.type === \"number\" ? prop.isId : undefined;\n}\n\n/** Whether a property is the collection's primary key. */\nfunction isPrimaryKey(prop: Property): boolean {\n return Boolean(declaredIsId(prop));\n}\n\n/**\n * Whether the server assigns this primary key, so a write does not have to.\n * `true` and `\"manual\"` both mean the caller supplies it.\n */\nfunction isAutoAssignedId(prop: Property): boolean {\n const isId = declaredIsId(prop);\n return Boolean(isId) && isId !== \"manual\" && isId !== true;\n}\n\n/**\n * The type an *included* relation arrives as: the target's own row, inlined.\n *\n * This is what the read pipeline actually serves — `toRestRow` puts the\n * target's flat columns where the relation was, and the SDK and the HTTP API\n * both go through it. It is deliberately *not* a `{ __type: \"relation\" }`\n * envelope: that shape is the admin's view-model and never reaches a\n * developer's `find()`.\n *\n * Falls back to an open record when the target is not part of this generation\n * run, since there is no `Row` to point at.\n */\nfunction includedRelationType(\n relation: ResolvedRelation,\n accessors: Map<string, string>\n): string {\n const target = resolveTargetCollection(relation);\n const slug = target?.slug ?? relation.targetSlug;\n const accessor = slug ? accessors.get(slug) : undefined;\n const rowType = accessor\n ? `Database[${emitString(accessor)}][\"Row\"]`\n : \"Record<string, unknown>\";\n // A read of a many-to-many whose junction carries columns serves them on\n // each target as `_pivot` — see `ManyToManyRelation.through.properties`.\n const pivot = relation.kind === \"manyToMany\" ? pivotType(relation.through.properties, \"read\") : undefined;\n const elementType = pivot ? `${rowType} & { _pivot?: ${pivot} }` : rowType;\n return relation.cardinality === \"many\" ? `Array<${elementType}>` : elementType;\n}\n\n/**\n * Map every slug to the property name it is reachable under on `client.data`.\n *\n * The accessor is a safe identifier because `client.data.myNotes` is the point\n * of generating this at all, and `collectionsDictionary` maps it back to the\n * slug the wire uses. Two slugs that safe down to the same identifier cannot\n * both have it: the interface would not compile, and the dictionary — an object\n * literal — would silently keep only the last, routing one collection's reads\n * to the other's table. There is no defensible way to pick, so this refuses.\n */\nfunction buildAccessors(collections: CollectionConfig[]): Map<string, string> {\n const accessors = new Map<string, string>();\n const bySafeName = new Map<string, string>();\n\n for (const collection of collections) {\n const slug = collection.slug;\n if (typeof slug !== \"string\" || slug.length === 0) {\n throw new CodegenError(\n \"A collection has no slug, so it has no name to generate a type for. \" +\n \"Every collection needs a unique `slug`.\"\n );\n }\n\n const safe = toSafeIdentifier(slug);\n if (safe.length === 0) {\n throw new CodegenError(\n `The slug ${emitString(slug)} has no characters that can form a property name, ` +\n \"so it cannot be reached as `client.data.<name>`. Use a slug containing \" +\n \"letters, digits, underscores or dashes.\"\n );\n }\n\n const accessor = toCollectionAccessor(slug);\n if (accessor !== safe) {\n console.warn(\n `[codegen] The collection ${emitString(slug)} would be \\`client.data.${safe}\\`, a name ` +\n `the client reserves, so its typed accessor is \\`client.data.${accessor}\\`. ` +\n `\\`client.data.collection(${emitString(slug)})\\` reaches it too.`\n );\n }\n\n const existing = bySafeName.get(accessor);\n if (existing !== undefined) {\n throw new CodegenError(\n `The collections ${emitString(existing)} and ${emitString(slug)} both generate the ` +\n `accessor \"${accessor}\", so only one of them could be reached from the generated client ` +\n \"and the other's reads would silently go to the wrong table. Rename one of the slugs.\"\n );\n }\n\n bySafeName.set(accessor, slug);\n accessors.set(slug, accessor);\n }\n\n return accessors;\n}\n\n/**\n * A column's type on a write: its own type (a date also as a `Date`), or\n * `null` too when it is not required.\n *\n * `Row` has typed a non-required column `T | null` for a while; its write twins\n * did not, so the one way to clear a value — `update(id, { publishedAt: null })`,\n * `{ authorId: null }` to unassign, the edit-form round trip of a row that came\n * back with nulls in it — was a compile error the server would have accepted.\n * A primary key is never null, required or not.\n */\nfunction writableType(prop: Property, tsType: string): string {\n // A date is written as a `Date` as readily as an ISO string: the client\n // sends it through `JSON.stringify`, which spells it as the ISO string the\n // server stores. `Row` keeps `string`, because that is what a read returns.\n const written = prop.type === \"date\" ? `${tsType} | Date` : tsType;\n return prop.validation?.required || isPrimaryKey(prop) ? written : `${written} | null`;\n}\n\n/** One emitted `key: type;` line, already indented. */\nfunction line(key: string, type: string, optional: boolean): string {\n return ` ${emitKey(key)}${optional ? \"?\" : \"\"}: ${type};`;\n}\n\n/**\n * The keys nobody can reach take off the API surface — in *both* directions.\n *\n * `excludeFromApi` means one thing: the API surface does not mention this\n * property. `Row` already honoured that; `Insert` and `Update` deliberately did\n * not, on the reading that the column is stripped from responses rather than\n * from writes. That left the generated types as the one place a password hash\n * was still named, and it invited a client to send one. The server refuses\n * such a field on a write (`VALIDATION_EXCLUDED_FIELDS`), so nothing generated\n * advertises it.\n *\n * Read through `effectiveAccess`, so the flag and its longhand\n * `access: { read: [], write: [] }` produce the same file. A *role* rule is\n * deliberately not honoured here and `Row` is unchanged by one: a generated type\n * is one shape for every caller, and there is no `Row` that is right for both a\n * reader who holds `hr` and one who does not. The server is the enforcement\n * point; the types describe the surface a caller may name.\n *\n * Keyed by the property name *and* by its column name, the same pair the\n * server's `stripUnreadable` deletes, so a foreign key or a relation addressed\n * under the column name cannot put the property back.\n */\nfunction excludedApiKeys(properties: Properties): Set<string> {\n const excluded = new Set<string>();\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n const access = effectiveAccess(prop);\n if (access?.read?.length !== 0 || access?.write?.length !== 0) continue;\n excluded.add(key);\n if (prop.columnName) excluded.add(prop.columnName);\n }\n return excluded;\n}\n\n/**\n * The keys nobody may write: `access: { write: [] }`, readable or not.\n *\n * Off `Insert` and `Update` for the same reason `excludedApiKeys` takes a\n * hidden property off all three — except that a field everyone can read but\n * nobody can write stays on `Row`. The server refuses one on any write with\n * `VALIDATION_EXCLUDED_FIELDS`, for every caller, `admin` included, so a\n * generated `create({ computed })` compiled and could only ever be a 400.\n *\n * A write rule naming *roles* is not this: some caller can write it, and one\n * generated shape cannot say which.\n */\nfunction unwritableKeys(properties: Properties): Set<string> {\n const unwritable = new Set<string>();\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (effectiveAccess(prop)?.write?.length !== 0) continue;\n unwritable.add(key);\n if (prop.columnName) unwritable.add(prop.columnName);\n }\n return unwritable;\n}\n\nexport function generateTypedefs(input: CollectionConfig[]): string {\n // Sorted here rather than only in `generate-sdk`: the output is\n // order-dependent and `rebase doctor` regenerates it in memory to diff\n // against the file on disk. While only the writer sorted, a project whose\n // file order differed from its slug order was reported permanently stale.\n const collections = sortCollectionsBySlug(input);\n const accessors = buildAccessors(collections);\n const lines: string[] = [\n \"/**\",\n \" * This file was auto-generated by Rebase.\",\n \" * Do not make direct changes to the file.\",\n \" */\",\n \"\",\n \"export interface Database {\"\n ];\n\n for (const collection of collections) {\n const properties = (collection.properties ?? {}) as Properties;\n\n // Resolve relations\n let resolvedRelations: Record<string, ResolvedRelation> = {};\n try {\n resolvedRelations = resolveCollectionRelations(collection);\n } catch (e) {\n // Swallowed before, which made this the quietest way to ship a\n // wrong type. The foreign-key columns are emitted from the resolved\n // relations rather than from the properties, so losing them drops\n // both the relation fields *and* columns that exist in the\n // database — and the resulting error surfaces in the user's code,\n // typechecking against a `Database` that is missing `author_id`,\n // with nothing pointing back at generation.\n //\n // A target thunk usually throws because of a circular import; the\n // boot-time relation validator names the same cause.\n console.warn(\n `[rebase] Could not resolve the relations of \"${collection.slug}\", so its generated ` +\n \"type has no relation fields and none of their foreign-key columns. This is usually a \" +\n \"circular import in the collection files — make sure the target is `() => otherCollection` \" +\n `and not evaluated at module load.\\n ${e instanceof Error ? e.message : String(e)}`\n );\n }\n\n // Subcollections are collections in their own right and are addressed\n // over a nested path, not as `client.data.<name>`. Generating them here\n // would invent an accessor the client does not serve, so they are\n // skipped — loudly, because doing it silently is how a developer\n // concludes the generator is broken.\n //\n // `getDeclaredSubcollections` rather than a local shape. What stood here\n // declared `subcollections?: unknown[]` and tested it with\n // `Array.isArray` — but the field is `() => CollectionConfig[]`, a thunk,\n // so that test was always false and this warning has never once fired.\n // The two declarations of the same field disagreed, and the\n // `as unknown as` between them is what let the wrong one compile.\n //\n // The thunk is called inside the same `try` shape the relation resolver\n // above uses: it exists to defer a circular import, and evaluating it\n // during codegen can throw for exactly that reason. A count that cannot\n // be taken is not worth failing a generate over.\n const declared = getDeclaredSubcollections(collection);\n let subcollectionCount = 0;\n if (declared) {\n try {\n subcollectionCount = declared().length;\n } catch {\n // Declared, but not resolvable from here. Still worth saying.\n subcollectionCount = -1;\n }\n }\n if (subcollectionCount !== 0) {\n console.warn(\n `[rebase] \"${collection.slug}\" declares ${subcollectionCount < 0 ? \"\" : `${subcollectionCount} `}subcollection(s), which are ` +\n \"not part of the generated Database: they are reached over a nested path \" +\n `(\\`data/${collection.slug}/<id>/<relation>\\`), not as a top-level accessor. Register a ` +\n \"subcollection as a collection of its own if you want a typed accessor for it.\"\n );\n }\n\n lines.push(` ${emitKey(accessors.get(collection.slug)!)}: {`);\n\n // ── Row Type ──\n //\n // What a read serves.\n //\n // ## A `belongsTo` has three shapes, and all three are typed\n //\n // One relation, three places it appears, and the wire is not symmetric\n // about it — so the types are not either:\n //\n // 1. **Write** — `Insert`/`Update` accept *either* the foreign key\n // under its own wire name (`{ authorId: 5 }`) or the relation\n // property (`{ author: 5 }`), because the write transformer maps\n // the second onto the first. Emitted by `emitWritableRelations`.\n // 2. **Read** — a row carries `authorId`. Always: it is a column.\n // 3. **Read with `include`** — the target's own row arrives under\n // `author`. Emitted below as optional, because it is absent from\n // every read that did not name it; the generated `RowWith` makes it\n // required for a read that did.\n //\n // The three collapse in exactly one case — a relation named identically\n // to its own foreign key, where 3 is served *over* 2 — handled below.\n //\n // `fields` narrows which columns a read returns, but a column is\n // optional here only because the value may be absent or null, never\n // because the caller might not have asked for it: a projection is a\n // choice made at one call site, and typing every column optional to\n // describe it would make every row unusable everywhere else.\n lines.push(\" Row: {\");\n const emittedKeys = new Set<string>();\n\n // Off the surface entirely — see `excludedApiKeys`. Seeding the emitted\n // set means every later pass (foreign keys, relations, unresolved\n // relations) skips them too, since each of those already refuses to\n // emit a key twice.\n const excluded = excludedApiKeys(properties);\n for (const key of excluded) emittedKeys.add(key);\n\n // 1. Direct properties\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (prop.type === \"relation\") continue;\n if (excluded.has(key)) continue;\n\n const tsType = propertyToTypeScriptType(prop);\n // A primary key is on every row a read can return, whether or not\n // anyone wrote `validation: { required: true }` next to it —\n // introspection never does, so `row.id` was `string | undefined`\n // for every baas project.\n const isRequired = Boolean(prop.validation?.required) || isPrimaryKey(prop);\n lines.push(line(key, isRequired ? tsType : `${tsType} | null`, !isRequired));\n emittedKeys.add(key);\n }\n\n // 2. FK columns from relations.\n //\n // Emitted under the relation's *wire* name, which is what\n // `fieldKeyForColumn` answers: `localKey` is the database column\n // (`author_id`) and the row arrives keyed `authorId`. Emitting the\n // column, which this used to do, described a key the JSON does not\n // carry and hid the one it does — including from `where` and `orderBy`,\n // which are keyed off this type. The column name is not a second name\n // for the field: it is the name of a different thing.\n for (const [relKey, relation] of Object.entries(resolvedRelations)) {\n if (relation.kind === \"belongsTo\" && relation.localKey) {\n const fkKey = fieldKeyForColumn(collection, relation.localKey);\n if (emittedKeys.has(fkKey)) continue;\n\n const fkType = foreignKeyType(relation);\n\n // A relation addressed by the same name as its own foreign key\n // is served *over* that column when the read includes it: the\n // query nests the target under the relation name, and the\n // scalar it shadows is gone. Both outcomes are real, so the\n // column is typed as both — which is what stops a plain\n // `const id: string = row.author_id` from compiling.\n const shadowedByInclude = relKey === fkKey;\n const tsType = shadowedByInclude\n ? `${fkType} | ${includedRelationType(relation, accessors)}`\n : fkType;\n\n const isRequired = isRelationRequired(collection, relation) && !shadowedByInclude;\n lines.push(line(fkKey, isRequired ? tsType : `${tsType} | null`, !isRequired));\n emittedKeys.add(fkKey);\n }\n }\n\n // 3. Relation fields — the target's own row, inlined.\n //\n // Optional throughout: a relation is only loaded when the read names it\n // in `include`, so it is absent from every other read.\n for (const [key, relation] of Object.entries(resolvedRelations)) {\n if (emittedKeys.has(key)) continue;\n lines.push(line(key, includedRelationType(relation, accessors), true));\n emittedKeys.add(key);\n }\n\n // A `relation` property whose relation could not be resolved — an\n // engine without relation support, or a target that did not load. It is\n // still a column on the row, so it is still typed, just not precisely.\n for (const [key, rawProp] of Object.entries(properties)) {\n if ((rawProp as Property).type !== \"relation\") continue;\n if (emittedKeys.has(key)) continue;\n lines.push(line(key, \"Record<string, unknown>\", true));\n emittedKeys.add(key);\n }\n lines.push(\" };\");\n\n // ── Insert Type ──\n //\n // What `create()` accepts, minus the `excludeFromApi` columns — the\n // property is off the API surface in both directions, so a generated\n // client never names it — and minus every field nobody may write.\n lines.push(\" Insert: {\");\n const notWritable = new Set([...excluded, ...unwritableKeys(properties)]);\n emittedKeys.clear();\n for (const key of notWritable) emittedKeys.add(key);\n\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (prop.type === \"relation\") continue;\n if (notWritable.has(key)) continue;\n const tsType = propertyToTypeScriptType(prop);\n // A column the database fills (`databaseDefault`) may be left out\n // of a create, as an id the database generates may.\n const isOptional = !prop.validation?.required || isAutoAssignedId(prop) || Boolean(prop.databaseDefault);\n lines.push(line(key, writableType(prop, tsType), isOptional));\n emittedKeys.add(key);\n }\n\n emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, false);\n lines.push(\" };\");\n\n // ── Update Type ──\n //\n // Everything optional, and the primary key left out: an update\n // addresses a row by id, it does not reassign one. Accepting `id` here\n // typechecked `update(id, { id: someoneElses })`.\n lines.push(\" Update: {\");\n emittedKeys.clear();\n for (const key of notWritable) emittedKeys.add(key);\n for (const [key, rawProp] of Object.entries(properties)) {\n const prop = rawProp as Property;\n if (prop.type === \"relation\") continue;\n if (isPrimaryKey(prop)) continue;\n if (notWritable.has(key)) continue;\n lines.push(line(key, writableType(prop, propertyToTypeScriptType(prop)), true));\n emittedKeys.add(key);\n }\n emitWritableRelations(lines, collection, properties, resolvedRelations, emittedKeys, true);\n lines.push(\" };\");\n\n // ── Relations ──\n //\n // Which relations this collection has, and which collection each one\n // reaches. Not a shape a read ever returns — it is the *graph*, and\n // `include` is the one parameter that needs it: `IncludeFor` below\n // walks it to constrain an include's keys to relations that exist, at\n // every level, and `RowWith` reads it to make an included relation\n // non-optional on the row that comes back.\n //\n // Emitted as the accessor NAME rather than the target's `Row`, so a\n // self-referencing or mutually-referencing relation is a finite string\n // instead of a type that expands forever.\n // A collection with no relations gets `Record<never, never>`, not `{}`:\n // the same type for `keyof` (never), but `{}` is what typescript-eslint's\n // recommended `no-empty-object-type` refuses, in every project that\n // lints its generated SDK.\n if (Object.keys(resolvedRelations).length === 0) {\n lines.push(\" Relations: Record<never, never>;\");\n } else {\n lines.push(\" Relations: {\");\n }\n for (const [key, relation] of Object.entries(resolvedRelations)) {\n const target = resolveTargetCollection(relation);\n const slug = target?.slug ?? relation.targetSlug;\n const accessor = slug ? accessors.get(slug) : undefined;\n // A target outside this generation run has no accessor to name, so\n // the relation is typed as reaching nothing rather than as reaching\n // something that does not exist.\n lines.push(` ${emitKey(key)}: ${accessor ? emitString(accessor) : \"never\"};`);\n }\n if (Object.keys(resolvedRelations).length > 0) lines.push(\" };\");\n\n // ── Slug ──\n //\n // The name the wire knows this collection by. The entry is keyed by the\n // accessor (`orderItems`), and the client's typed `collection(slug)`\n // has to get from one to the other: it used to re-derive the accessor\n // from the slug at the type level, handling `-` and nothing else, so\n // `collection(\"order_items\")` — every snake_case table — came back\n // untyped. Written by the one function that chose the accessor, so the\n // two cannot disagree.\n lines.push(` Slug: ${emitString(collection.slug)};`);\n\n lines.push(\" };\");\n }\n\n lines.push(\"}\");\n lines.push(\"\");\n lines.push(\"export type CollectionName = keyof Database;\");\n lines.push(\"\");\n lines.push(\"export const collectionsDictionary = {\");\n for (const collection of collections) {\n lines.push(` ${emitKey(accessors.get(collection.slug)!)}: ${emitString(collection.slug)},`);\n }\n lines.push(\"} as const;\");\n lines.push(\"\");\n // Describes the const above rather than restating its keys. The previous\n // `{ [K in CollectionName]: K }` said every value equalled its key, which is\n // false for any slug that is not already an identifier — `myNotes` maps to\n // `\"my-notes\"` — so the export the CLI tells people to pass did not satisfy\n // its own published type.\n lines.push(\"export type CollectionsDictionary = typeof collectionsDictionary;\");\n lines.push(\"\");\n lines.push(...includeHelperLines());\n\n return lines.join(\"\\n\");\n}\n\n/**\n * The `include` type machinery, emitted into the generated file.\n *\n * It lives here rather than in `@rebasepro/types` because it is the *graph*\n * that makes it work, and the graph only exists once a project's collections\n * have been generated. `IncludeSpec` in `@rebasepro/types` is deliberately\n * unconstrained — a hand-written row type has no relations in it to check\n * against — and these narrow it for a project that does have them.\n *\n * Two things a caller gets:\n *\n * - `IncludeFor<\"posts\">` — an include whose keys are relations that exist,\n * recursively, so `include: { comments: { include: { authr: true } } }` is a\n * compile error rather than a 400 at runtime.\n * - `RowWith<\"posts\", I>` — the row a read with that include returns, where\n * every included relation is **present** rather than optional. `Row` types a\n * relation as optional because it is absent from every read that did not ask\n * for it; once a read has asked, `row.author.name` should not need a `?.`.\n *\n * The depth bound matches the server's (`MAX_INCLUDE_DEPTH`), and is spelled as\n * a decrementing tuple because TypeScript has no arithmetic — `Prev[3]` is `2`.\n * Without a bound, a self-referencing relation makes the type infinite and the\n * compiler gives up with \"type instantiation is excessively deep\".\n */\nfunction includeHelperLines(): string[] {\n return [\n \"/** Which collection each of `A`'s relations reaches. */\",\n \"export type RelationsOf<A extends CollectionName> =\",\n \" Database[A] extends { Relations: infer R } ? R : Record<string, never>;\",\n \"\",\n \"/** The names of `A`'s relations. */\",\n \"export type RelationKeys<A extends CollectionName> = keyof RelationsOf<A> & string;\",\n \"\",\n \"/** The collection a relation reaches, or `never` when it left this project. */\",\n \"export type RelationTarget<A extends CollectionName, K extends RelationKeys<A>> =\",\n \" RelationsOf<A>[K] extends CollectionName ? RelationsOf<A>[K] : never;\",\n \"\",\n \"/** Counts `IncludeFor` down; TypeScript has no arithmetic. */\",\n \"type Prev = [never, 0, 1, 2, 3];\",\n \"\",\n \"/**\",\n \" * Per-relation options, minus `include` — which `IncludeFor` supplies at\",\n \" * the next depth so the nested keys are checked against the *target's*\",\n \" * relations rather than this collection's.\",\n \" */\",\n \"export interface IncludeOptionsFor<A extends CollectionName, K extends RelationKeys<A>, D extends number> {\",\n \" limit?: number;\",\n \" where?: Record<string, unknown>;\",\n \" logical?: unknown;\",\n \" orderBy?: unknown;\",\n \" fields?: string[];\",\n \" include?: RelationTarget<A, K> extends CollectionName\",\n \" ? IncludeFor<RelationTarget<A, K>, Prev[D]>\",\n \" : never;\",\n \"}\",\n \"\",\n \"/**\",\n \" * An `include` for collection `A`: its relation names, dotted paths, or the\",\n \" * parametrised tree — with every key checked against the relations that\",\n \" * actually exist, at every level.\",\n \" */\",\n \"export type IncludeFor<A extends CollectionName, D extends number = 3> =\",\n \" D extends 0\",\n \" ? never\",\n \" : | readonly (RelationKeys<A> | \\\"*\\\")[]\",\n \" | { [K in RelationKeys<A>]?: true | IncludeOptionsFor<A, K, D> };\",\n \"\",\n \"/** The relation names an include asks for at the top level. */\",\n \"type IncludedKeys<A extends CollectionName, I> =\",\n \" I extends readonly (infer K)[]\",\n \" ? Extract<K, RelationKeys<A>>\",\n \" : Extract<keyof I, RelationKeys<A>>;\",\n \"\",\n \"/**\",\n \" * The row a read with include `I` returns: `Row`, with every included\",\n \" * relation made **required**.\",\n \" *\",\n \" * `Row` types a relation as optional because it is absent from every read\",\n \" * that did not name it. Once a read has named it, it is there — and having\",\n \" * to write `row.author?.name` after asking for the author is the type\",\n \" * describing a possibility the query already ruled out.\",\n \" */\",\n \"export type RowWith<A extends CollectionName, I> =\",\n \" Database[A][\\\"Row\\\"] & Required<Pick<Database[A][\\\"Row\\\"], IncludedKeys<A, I> & keyof Database[A][\\\"Row\\\"]>>;\",\n \"\"\n ];\n}\n\n/**\n * The two ways a write can name a `belongsTo` target, both of which the server\n * accepts: the foreign key under its own wire name (`{ authorId: 5 }`, which\n * passes through to the `author_id` column untouched) and the relation\n * *property* (`{ author: 5 }`, which the write transformer maps onto that\n * column).\n *\n * Only the first was generated, so the documented and idiomatic write shape was\n * a type error.\n *\n * The second form is emitted under the **property key**, not the resolved\n * relation name, because that is what the transformer keys off: it looks the\n * payload key up in `properties` and only treats it as a relation if what it\n * finds there is one. A relation whose `relationName` differs from its property\n * key is reachable as the property and not as the name, so emitting the name\n * would have offered a key that writes to a column that does not exist.\n */\nfunction emitWritableRelations(\n lines: string[],\n collection: CollectionConfig,\n properties: Properties,\n resolvedRelations: Record<string, ResolvedRelation>,\n emittedKeys: Set<string>,\n allOptional: boolean\n): void {\n // The target's primary key type is the same one `Row` uses. A hardcoded\n // `string | number` here accepted a string for a numeric-keyed target.\n const emit = (key: string, relation: ResolvedRelation): void => {\n if (emittedKeys.has(key)) return;\n const required = isRelationRequired(collection, relation);\n // A foreign key that is not required can be unassigned, which is a\n // write of `null` — see `writableType`.\n const type = required ? foreignKeyType(relation) : `${foreignKeyType(relation)} | null`;\n lines.push(line(key, type, allOptional || !required));\n emittedKeys.add(key);\n };\n\n for (const relation of Object.values(resolvedRelations)) {\n if (relation.kind === \"belongsTo\" && relation.localKey) {\n emit(fieldKeyForColumn(collection, relation.localKey), relation);\n }\n }\n\n for (const [key, rawProp] of Object.entries(properties)) {\n if ((rawProp as Property).type !== \"relation\") continue;\n const relation = findRelation(resolvedRelations, key);\n if (relation?.kind === \"belongsTo\" && relation.localKey) emit(key, relation);\n }\n\n // A to-many membership: `hasMany` (the targets' foreign key is pointed at\n // this row) and `manyToMany` (junction rows). The server writes both on a\n // create and an update — bare keys, or `{ id }`, or `{ id, _pivot }` where\n // the junction declares payload columns — but only `belongsTo` was\n // emitted, so the field a read returns could not be written back. A `via`\n // is read-only (`writable: false`), and stays off.\n for (const [key, relation] of Object.entries(resolvedRelations)) {\n if (relation.cardinality !== \"many\" || !relation.writable) continue;\n if (emittedKeys.has(key)) continue;\n const fk = foreignKeyType(relation);\n const pivot = relation.kind === \"manyToMany\" ? pivotType(relation.through.properties, \"write\") : undefined;\n const element = `${fk} | { id: ${fk}${pivot ? `; _pivot?: ${pivot}` : \"\"} }`;\n lines.push(line(key, `Array<${element}>`, true));\n emittedKeys.add(key);\n }\n}\n\n/**\n * The `_pivot` a many-to-many link carries: the junction's payload columns,\n * typed as a row's columns are on a read and as a write's on a write. `undefined`\n * when the junction declares none, so there is no `_pivot` to name.\n */\nfunction pivotType(pivotProperties: Properties, direction: \"read\" | \"write\"): string | undefined {\n const entries = Object.entries(pivotProperties);\n if (entries.length === 0) return undefined;\n const fields = entries.map(([key, rawProp]) => {\n const prop = rawProp as Property;\n const tsType = propertyToTypeScriptType(prop);\n const required = Boolean(prop.validation?.required);\n const type = direction === \"write\" ? writableType(prop, tsType) : (required ? tsType : `${tsType} | null`);\n return `${emitKey(key)}${required && direction === \"read\" ? \"\" : \"?\"}: ${type};`;\n });\n return `{ ${fields.join(\" \")} }`;\n}\n","/**\n * @rebasepro/codegen\n *\n * Generates a purely typed Typescript database definition.\n */\n\nimport { CollectionConfig } from \"@rebasepro/types\";\nimport { generateTypedefs } from \"./generate-types\";\n\nexport { generateTypedefs, CodegenError } from \"./generate-types\";\nexport { toPascalCase, toCamelCase, toSafeIdentifier, toCollectionAccessor, indent } from \"./utils\";\n\n// ─── Public API ────────────────────────────────────────────────────\n\nexport interface GeneratedFile {\n /** Relative file path within the output directory */\n path: string;\n /** File content */\n content: string;\n}\n\nexport interface GenerateSDKOptions {\n /** Whether to include a README file (default: true) */\n includeReadme?: boolean;\n}\n\nexport function generateSDK(\n collections: CollectionConfig[],\n options: GenerateSDKOptions = {}\n): GeneratedFile[] {\n const files: GeneratedFile[] = [];\n\n files.push({\n path: \"database.types.ts\",\n content: generateTypedefs(collections)\n });\n\n if (options.includeReadme !== false) {\n files.push({\n path: \"README.md\",\n content: `# Rebase SDK\n\n> Auto-generated by \\`rebase generate-sdk\\`. Do not edit manually.\n\n## Usage\n\n1. Install the client package:\n \\`\\`\\`bash\n npm install @rebasepro/client\n \\`\\`\\`\n\n2. Initialize with your generated types:\n \\`\\`\\`typescript\n import { createRebaseClient } from '@rebasepro/client';\n import { collectionsDictionary, type Database } from './database.types';\n\n const rebase = createRebaseClient<Database>({\n baseUrl: 'http://localhost:3001',\n // Maps each accessor back to the slug the wire uses. Without it a\n // hyphenated slug is not resolvable from the property name alone.\n collections: collectionsDictionary,\n });\n\n // Property access is the typed surface: rows, filters and sorts are all\n // checked against the generated Database.\n const { data: users } = await rebase.data.users.find();\n console.log(users[0].email); // flat access — no .values wrapper\n \\`\\`\\`\n\n## Field names are the ones the API serves\n\nThe generated \\`Row\\` uses each field's **wire** name — the key it arrives under in\nJSON — and nothing here renames anything.\n\n- **A declared property is its key in the collection.** A property keyed\n \\`createdAt\\` is \\`row.createdAt\\`, whatever \\`columnName\\` says. A column name is\n the name of a different thing: where the value lives, not what the API calls it.\n- **A foreign key derived from a relation is camelCase**, because that is what\n the wire carries. A \\`belongsTo\\` named \\`author\\` gives you \\`row.authorId\\`, not\n the column spelling.\n- **A collection accessor is camelCase too** (\\`my-notes\\` → \\`rebase.data.myNotes\\`),\n which is what \\`collectionsDictionary\\` maps back to the slug.\n A slug whose accessor the client reserves (\\`collection\\`, \\`then\\`, \\`toJSON\\`)\n is generated as \\`<name>Collection\\`: \\`rebase.data.collectionCollection\\`.\n\n\\`where\\` and \\`orderBy\\` are keyed off the same type, so what compiles is what the\nbackend answers to.\n\n## \\`Row\\` vs \\`Insert\\` vs \\`Update\\`\n\n| Type | What it describes |\n|---|---|\n| \\`Row\\` | What a read serves. Nullable columns are \\`T \\\\| null\\`; relations appear only when \\`include\\` names them. |\n| \\`Insert\\` | What \\`create()\\` accepts. Server-assigned ids are optional; a \\`belongsTo\\` target may be named either way (\\`{ author: 5 }\\` or \\`{ authorId: 5 }\\`). |\n| \\`Update\\` | What \\`update()\\` accepts. Everything optional, and the primary key is not settable. |\n\nA property marked \\`excludeFromApi\\` is absent from all three: the API surface\ndoes not mention it, in either direction. The server holds the same line — a\nread never serves the column and a write naming it is refused — so this is a\nguarantee rather than a description, and nothing generated names a password\nhash.\n\nIf you need an untyped escape hatch, \\`rebase.data.collection(slug)\\` still works —\nbut it is generic over \\`Record<string, unknown>\\` and gives up everything above.\n`\n });\n }\n\n return files;\n}\n"],"mappings":";;;;;;AAaA,SAAgB,OAAO,MAAc,QAAwB;CACzD,MAAM,MAAM,IAAI,OAAO,MAAM;CAC7B,OAAO,KACF,MAAM,IAAI,CAAC,CACX,KAAI,SAAS,KAAK,KAAK,IAAI,MAAM,OAAO,IAAK,CAAC,CAC9C,KAAK,IAAI;AAClB;;;;;;;;;;;ACPA,IAAa,eAAb,cAAkC,MAAM;CACpC,YAAY,SAAiB;EACzB,MAAM,OAAO;EACb,KAAK,OAAO;CAChB;AACJ;AAEA,IAAM,aAAa;;;;;;;;;;;AAYnB,SAAS,QAAQ,KAAqB;CAClC,OAAO,WAAW,KAAK,GAAG,IAAI,MAAM,KAAK,UAAU,GAAG;AAC1D;;;;;;;;;AAUA,SAAS,WAAW,OAAuB;CACvC,OAAO,KAAK,UAAU,KAAK;AAC/B;;AAGA,SAAS,QAAQ,KAAmC;CAChD,IAAI,MAAM,QAAQ,GAAG,GACjB,OAAO,IAAI,KAAK,UACZ,SAAS,OAAO,UAAU,WAAW,MAAM,KAAK,KAAK;CAE7D,IAAI,OAAO,OAAO,QAAQ,UAAU,OAAO,OAAO,KAAK,GAAG;CAC1D,OAAO,CAAC;AACZ;AAEA,SAAS,yBAAyB,MAAwB;CACtD,QAAQ,KAAK,MAAb;EACI,KAAK,UAAU;GACX,MAAM,KAAK;GACX,IAAI,GAAG,MAAM;IACT,MAAM,MAAM,QAAQ,GAAG,IAAI;IAC3B,IAAI,IAAI,WAAW,GAAG,OAAO;IAC7B,OAAO,IAAI,KAAI,MAAK,WAA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EAClD,MAAM,OAAO,cAAc,UAAU,aAAa,MAAM,MAAM,IAAK,WAAW,SAAS,GAAG,OAAO;EACjG,OAAO,GAAG,QAAQ,GAAG,IAAI,YAAY,cAAc,SAAS,KAAK,IAAI,IAAI,KAAK;CAClF,CACY,CAAA,CAAO,KAAK,GAAG,EAAE;AACjC;;;ACnxBA,SAAgB,YACZ,aACA,UAA8B,CAAC,GAChB;CACf,MAAM,QAAyB,CAAC;CAEhC,MAAM,KAAK;EACP,MAAM;EACN,SAAS,iBAAiB,WAAW;CACzC,CAAC;CAED,IAAI,QAAQ,kBAAkB,OAC1B,MAAM,KAAK;EACP,MAAM;EACN,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAiEb,CAAC;CAGL,OAAO;AACX"}
|
package/dist/utils.d.ts
CHANGED
|
@@ -2,25 +2,10 @@
|
|
|
2
2
|
* Utility functions for the SDK generator
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Capitals already inside a word are meaningful and are kept: lowercasing the
|
|
9
|
-
* tail of every chunk turned "TestEntities" into "Testentities", which is what
|
|
10
|
-
* ended up in the generated type names. SHOUTING_CASE is the one shape where
|
|
11
|
-
* the tail is not meaningful, so it is folded down.
|
|
5
|
+
* The naming rules shared with the server's data proxies, which resolve a
|
|
6
|
+
* generated accessor back to its slug: one home, so the two cannot drift.
|
|
12
7
|
*/
|
|
13
|
-
export
|
|
14
|
-
/**
|
|
15
|
-
* Convert a slug/snake_case string to camelCase
|
|
16
|
-
* e.g. "private_notes" → "privateNotes"
|
|
17
|
-
*/
|
|
18
|
-
export declare function toCamelCase(str: string): string;
|
|
19
|
-
/**
|
|
20
|
-
* Convert a slug to a safe JS identifier
|
|
21
|
-
* e.g. "private-notes" → "privateNotes"
|
|
22
|
-
*/
|
|
23
|
-
export declare function toSafeIdentifier(str: string): string;
|
|
8
|
+
export { toPascalCase, toCamelCase, toSafeIdentifier, toCollectionAccessor } from "@rebasepro/common";
|
|
24
9
|
/**
|
|
25
10
|
* Indent a block of text by a given number of spaces
|
|
26
11
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rebasepro/codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.24.1-canary.g56af4a4",
|
|
4
4
|
"description": "Generate a typed JS SDK from Rebase collection definitions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sdk",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@rebasepro/types": "0.
|
|
36
|
+
"@rebasepro/types": "0.24.1-canary.g56af4a4"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@jest/globals": "^30.4.1",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"ts-jest": "^29.4.12",
|
|
44
44
|
"typescript": "^6.0.3",
|
|
45
45
|
"vite": "^8.1.5",
|
|
46
|
-
"@rebasepro/types": "0.
|
|
46
|
+
"@rebasepro/types": "0.24.1-canary.g56af4a4"
|
|
47
47
|
},
|
|
48
48
|
"exports": {
|
|
49
49
|
".": {
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
},
|
|
55
55
|
"gitHead": "d935eefa5aa8d1009a2398cfac2c1e4ee9aeb6b6",
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@rebasepro/common": "0.
|
|
57
|
+
"@rebasepro/common": "0.24.1-canary.g56af4a4"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"test": "jest --config jest.config.cjs",
|