create-backon 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +626 -418
- package/package.json +5 -2
- package/src/cli.ts +323 -340
- package/src/prompts.ts +10 -1
- package/src/schema-edit.ts +25 -12
- package/src/templates/base.ts +14 -7
- package/src/templates/schema.ts +5 -2
- package/src/tui.ts +410 -73
package/src/prompts.ts
CHANGED
|
@@ -113,7 +113,16 @@ export async function runPrompts(): Promise<Answers> {
|
|
|
113
113
|
});
|
|
114
114
|
if (p.isCancel(name)) bail();
|
|
115
115
|
if (!name) break;
|
|
116
|
-
|
|
116
|
+
const kebab = toKebabCase(name);
|
|
117
|
+
if (!kebab) {
|
|
118
|
+
p.log.warn("Route name can't be empty.");
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (routes.includes(kebab)) {
|
|
122
|
+
p.log.warn(`"${kebab}" was already added — skipping duplicate.`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
routes.push(kebab);
|
|
117
126
|
}
|
|
118
127
|
|
|
119
128
|
p.outro(`Scaffolding ${projectName}...`);
|
package/src/schema-edit.ts
CHANGED
|
@@ -198,6 +198,18 @@ export interface AddTableResult {
|
|
|
198
198
|
filePath: string;
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
+
function resolveReferenceType(tables: ParsedTable[], targetIdentifier: string, colName: string): "integer" | "uuid" {
|
|
202
|
+
const target = tables.find((t) => t.identifier === targetIdentifier);
|
|
203
|
+
if (!target) {
|
|
204
|
+
throw new Error(`Column "${colName}" references unknown table "${targetIdentifier}".`);
|
|
205
|
+
}
|
|
206
|
+
const idCol = target.columns.find((c) => c.key === "id");
|
|
207
|
+
if (!idCol) {
|
|
208
|
+
throw new Error(`Table "${targetIdentifier}" has no "id" column for "${colName}" to reference.`);
|
|
209
|
+
}
|
|
210
|
+
return idCol.type === "uuid" ? "uuid" : "integer";
|
|
211
|
+
}
|
|
212
|
+
|
|
201
213
|
/** Appends one `pgTable(...)` export to `schema.ts`. */
|
|
202
214
|
export async function addTable(spec: TableSpec, cwd: string = process.cwd()): Promise<AddTableResult> {
|
|
203
215
|
await requireNeonOrPostgres(cwd);
|
|
@@ -209,17 +221,18 @@ export async function addTable(spec: TableSpec, cwd: string = process.cwd()): Pr
|
|
|
209
221
|
if (existingTables.some((t) => t.identifier === identifier)) {
|
|
210
222
|
throw new Error(`"${identifier}" is already a table in ${path}.`);
|
|
211
223
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
224
|
+
const normalizedColumns = spec.columns.map((col) =>
|
|
225
|
+
col.references
|
|
226
|
+
? { ...col, referenceType: col.referenceType ?? resolveReferenceType(existingTables, col.references, col.name) }
|
|
227
|
+
: col
|
|
228
|
+
);
|
|
229
|
+
const normalizedSpec: TableSpec = { ...spec, columns: normalizedColumns };
|
|
217
230
|
|
|
218
231
|
const withoutPlaceholder = contents.includes(renderBlankSchema()) ? contents.replace(renderBlankSchema(), "") : contents;
|
|
219
232
|
|
|
220
233
|
const updated = editSource(path, withoutPlaceholder, (_project, sourceFile) => {
|
|
221
|
-
ensurePgCoreImport(sourceFile, requiredBuilders(
|
|
222
|
-
appendStatements(sourceFile, renderTableBlock(
|
|
234
|
+
ensurePgCoreImport(sourceFile, requiredBuilders(normalizedSpec));
|
|
235
|
+
appendStatements(sourceFile, renderTableBlock(normalizedSpec));
|
|
223
236
|
});
|
|
224
237
|
|
|
225
238
|
await writeFile(path, updated);
|
|
@@ -257,12 +270,12 @@ export async function addColumn(
|
|
|
257
270
|
if (target.columns.some((c) => c.key === columnKey)) {
|
|
258
271
|
throw new Error(`Column "${columnKey}" already exists on table "${target.identifier}".`);
|
|
259
272
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
273
|
+
const normalizedCol: ColumnSpec = col.references
|
|
274
|
+
? { ...col, referenceType: col.referenceType ?? resolveReferenceType(existingTables, col.references, col.name) }
|
|
275
|
+
: col;
|
|
263
276
|
|
|
264
|
-
const builder =
|
|
265
|
-
const initializer = renderColumnExpression(
|
|
277
|
+
const builder = normalizedCol.references ? (normalizedCol.referenceType ?? "integer") : normalizedCol.type;
|
|
278
|
+
const initializer = renderColumnExpression(normalizedCol);
|
|
266
279
|
|
|
267
280
|
const updated = editSource(path, contents, (_project, sourceFile) => {
|
|
268
281
|
ensurePgCoreImport(sourceFile, new Set([builder]));
|
package/src/templates/base.ts
CHANGED
|
@@ -78,7 +78,7 @@ export const app = createApp();
|
|
|
78
78
|
app.use(exampleMiddleware);
|
|
79
79
|
app.use("/", endpoints);
|
|
80
80
|
|
|
81
|
-
serveApiDocs(app, "/docs", { title:
|
|
81
|
+
serveApiDocs(app, "/docs", { title: ${JSON.stringify(projectName)} });
|
|
82
82
|
${defaultExport}`;
|
|
83
83
|
}
|
|
84
84
|
return `${detectionImport}import { createApp, serveApiDocs } from "@mednours/backon";
|
|
@@ -90,7 +90,7 @@ export const app = createApp();
|
|
|
90
90
|
app.use("*", exampleMiddleware);
|
|
91
91
|
app.route("/", endpoints);
|
|
92
92
|
|
|
93
|
-
serveApiDocs(app, "/docs", { title:
|
|
93
|
+
serveApiDocs(app, "/docs", { title: ${JSON.stringify(projectName)} });
|
|
94
94
|
${defaultExport}`;
|
|
95
95
|
}
|
|
96
96
|
|
|
@@ -254,9 +254,9 @@ export function renderGenerateDocsScript(projectName: string, framework: Framewo
|
|
|
254
254
|
import { generateOpenApiSpec, apiReferenceHtmlInline } from "${backonImport}";
|
|
255
255
|
import { app } from "../src/app";
|
|
256
256
|
|
|
257
|
-
const spec = generateOpenApiSpec(app, { title:
|
|
257
|
+
const spec = generateOpenApiSpec(app, { title: ${JSON.stringify(projectName)}, version: "0.1.0" });
|
|
258
258
|
writeFileSync("openapi.json", JSON.stringify(spec, null, 2));
|
|
259
|
-
writeFileSync("docs.html", apiReferenceHtmlInline(spec,
|
|
259
|
+
writeFileSync("docs.html", apiReferenceHtmlInline(spec, ${JSON.stringify(projectName)}));
|
|
260
260
|
|
|
261
261
|
console.log("Wrote openapi.json and docs.html");
|
|
262
262
|
`;
|
|
@@ -371,8 +371,8 @@ for (const route of routes) {
|
|
|
371
371
|
verb: m[1],
|
|
372
372
|
path: m[2],
|
|
373
373
|
name: m[3],
|
|
374
|
-
needsBody:
|
|
375
|
-
needsQuery:
|
|
374
|
+
needsBody: new RegExp("export\\\\s+const\\\\s+" + m[3] + "BodySchema\\\\b").test(controllerContents),
|
|
375
|
+
needsQuery: new RegExp("export\\\\s+const\\\\s+" + m[3] + "QuerySchema\\\\b").test(controllerContents),
|
|
376
376
|
}));
|
|
377
377
|
if (methods.length > 0) groups.push({ route, methods });
|
|
378
378
|
}
|
|
@@ -403,14 +403,21 @@ const helperLines = [
|
|
|
403
403
|
'const BASE_URL = (typeof process !== "undefined" && process.env.API_BASE_URL) || "";',
|
|
404
404
|
"",
|
|
405
405
|
TYPED ? "async function request<T>(path: string, init?: RequestInit): Promise<T> {" : "async function request(path, init) {",
|
|
406
|
+
" const headers = {",
|
|
407
|
+
' ...(init && init.body !== undefined ? { "Content-Type": "application/json" } : {}),',
|
|
408
|
+
" ...(init && init.headers ? init.headers : {}),",
|
|
409
|
+
" };",
|
|
406
410
|
" const res = await fetch(BASE_URL + path, {",
|
|
407
411
|
" ...init,",
|
|
408
|
-
|
|
412
|
+
" headers,",
|
|
409
413
|
" });",
|
|
410
414
|
" if (!res.ok) {",
|
|
411
415
|
TYPED ? " const errBody: any = await res.json().catch(() => undefined);" : " const errBody = await res.json().catch(() => undefined);",
|
|
412
416
|
' throw new Error((errBody && errBody.error && errBody.error.message) || ("Request to " + path + " failed with " + res.status));',
|
|
413
417
|
" }",
|
|
418
|
+
' if (res.status === 204 || res.headers.get("content-length") === "0") {',
|
|
419
|
+
TYPED ? " return undefined as T;" : " return undefined;",
|
|
420
|
+
" }",
|
|
414
421
|
TYPED ? " return res.json() as Promise<T>;" : " return res.json();",
|
|
415
422
|
"}",
|
|
416
423
|
];
|
package/src/templates/schema.ts
CHANGED
|
@@ -42,6 +42,8 @@ export interface ColumnSpec {
|
|
|
42
42
|
* column is rendered as `integer(...).references(() => X.id)`.
|
|
43
43
|
*/
|
|
44
44
|
references?: string;
|
|
45
|
+
/** Column type matching the referenced table's primary key (`"integer"` for serial/int, `"uuid"` for uuid). Defaults to `"integer"`. */
|
|
46
|
+
referenceType?: "integer" | "uuid";
|
|
45
47
|
/** Optional foreign-key referential action (`onDelete`). */
|
|
46
48
|
onDelete?: OnDeleteAction;
|
|
47
49
|
}
|
|
@@ -124,7 +126,7 @@ export function requiredBuilders(table: TableSpec): Set<string> {
|
|
|
124
126
|
if (table.includeTimestamps) builders.add("timestamp");
|
|
125
127
|
if (table.compositePrimaryKey?.length) builders.add("primaryKey");
|
|
126
128
|
for (const col of table.columns) {
|
|
127
|
-
builders.add(col.references ? "integer" : col.type);
|
|
129
|
+
builders.add(col.references ? (col.referenceType ?? "integer") : col.type);
|
|
128
130
|
}
|
|
129
131
|
return builders;
|
|
130
132
|
}
|
|
@@ -135,7 +137,8 @@ export function renderColumnExpression(col: ColumnSpec): string {
|
|
|
135
137
|
let expr: string;
|
|
136
138
|
if (col.references) {
|
|
137
139
|
const refOpts = col.onDelete ? `, { onDelete: "${col.onDelete}" }` : "";
|
|
138
|
-
|
|
140
|
+
const fkBuilder = col.referenceType ?? "integer";
|
|
141
|
+
expr = `${fkBuilder}("${sqlName}").references(() => ${col.references}.id${refOpts})`;
|
|
139
142
|
} else if (col.type === "varchar") {
|
|
140
143
|
const length = col.varcharLength ?? 255;
|
|
141
144
|
expr = `varchar("${sqlName}", { length: ${length} })`;
|