create-backon 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +49 -0
- package/package.json +34 -0
- package/src/cli.ts +17 -0
- package/src/generate.ts +64 -0
- package/src/prompts.ts +69 -0
- package/src/templates/base.ts +146 -0
- package/src/templates/db.ts +105 -0
- package/src/templates/route.ts +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nour Sabir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# create-backon
|
|
2
|
+
|
|
3
|
+
Scaffolds a new backend project on [@mednours/backon](https://www.npmjs.com/package/@mednours/backon) —
|
|
4
|
+
like `bun create vite@latest`, but for a consistent controller/routes/services
|
|
5
|
+
backend layout. Currently generates a Hono-backed project; `@mednours/backon`
|
|
6
|
+
also has an Express adapter, but this CLI doesn't have a framework prompt for
|
|
7
|
+
it yet.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
bun create backon
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or, without the `bun create` shorthand:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
bunx create-backon
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Prompts for a project name, a database (Neon, Postgres, SQLite, or none — via
|
|
22
|
+
[Drizzle](https://orm.drizzle.team)), and any number of routes, then generates:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
my-api/
|
|
26
|
+
index.ts
|
|
27
|
+
.env.example
|
|
28
|
+
src/
|
|
29
|
+
app.ts
|
|
30
|
+
config/db.ts
|
|
31
|
+
middleware/example.middleware.ts
|
|
32
|
+
endpoints/
|
|
33
|
+
index.ts
|
|
34
|
+
<route>/
|
|
35
|
+
<route>.controller.ts
|
|
36
|
+
<route>.routes.ts
|
|
37
|
+
<route>.services.ts
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Each route's `*.routes.ts` wires HTTP methods to controller functions,
|
|
41
|
+
`*.controller.ts` validates the request via `@mednours/backon`'s
|
|
42
|
+
`defineRoute` and calls into `*.services.ts`, which is where real data
|
|
43
|
+
access belongs.
|
|
44
|
+
|
|
45
|
+
## Requirements
|
|
46
|
+
|
|
47
|
+
Bun >= 1.3.
|
|
48
|
+
|
|
49
|
+
Part of the [backon monorepo](https://github.com/mednours/backon).
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "create-backon",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Scaffolds a new backend project on backon — like `bun create vite@latest`, but for a consistent controller/routes/services backend layout.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/mednours/backon/tree/main/packages/create-backon#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/mednours/backon.git",
|
|
11
|
+
"directory": "packages/create-backon"
|
|
12
|
+
},
|
|
13
|
+
"bugs": "https://github.com/mednours/backon/issues",
|
|
14
|
+
"engines": {
|
|
15
|
+
"bun": ">=1.3.0"
|
|
16
|
+
},
|
|
17
|
+
"bin": {
|
|
18
|
+
"create-backon": "./src/cli.ts"
|
|
19
|
+
},
|
|
20
|
+
"files": ["src"],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"start": "bun run src/cli.ts",
|
|
23
|
+
"test": "bun test",
|
|
24
|
+
"typecheck": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@clack/prompts": "^0.9.1",
|
|
28
|
+
"@mednours/backon": "workspace:*"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/bun": "latest",
|
|
32
|
+
"typescript": "^5.7.2"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import * as p from "@clack/prompts";
|
|
3
|
+
import { runPrompts } from "./prompts";
|
|
4
|
+
import { generateProject } from "./generate";
|
|
5
|
+
|
|
6
|
+
async function main() {
|
|
7
|
+
const answers = await runPrompts();
|
|
8
|
+
const { root, filesWritten } = await generateProject(answers);
|
|
9
|
+
|
|
10
|
+
p.note(`cd ${answers.projectName}\nbun install\nbun run dev`, "Next steps");
|
|
11
|
+
p.log.success(`Wrote ${filesWritten.length} files to ${root}`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
main().catch((err) => {
|
|
15
|
+
console.error(err);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
});
|
package/src/generate.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import type { Answers } from "./prompts";
|
|
4
|
+
import {
|
|
5
|
+
renderAppTs,
|
|
6
|
+
renderEndpointsIndex,
|
|
7
|
+
renderEnvExample,
|
|
8
|
+
renderExampleMiddleware,
|
|
9
|
+
renderIndexTs,
|
|
10
|
+
renderPackageJson,
|
|
11
|
+
renderReadme,
|
|
12
|
+
renderTsconfig,
|
|
13
|
+
} from "./templates/base";
|
|
14
|
+
import { renderController, renderRoutes, renderServices } from "./templates/route";
|
|
15
|
+
import { renderDbFile } from "./templates/db";
|
|
16
|
+
|
|
17
|
+
async function write(path: string, contents: string): Promise<void> {
|
|
18
|
+
await mkdir(dirname(path), { recursive: true });
|
|
19
|
+
await writeFile(path, contents);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface GenerateResult {
|
|
23
|
+
root: string;
|
|
24
|
+
filesWritten: string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Writes the whole scaffold to `<cwd>/<projectName>`. Pure file I/O, no
|
|
29
|
+
* prompting — kept separate from prompts.ts so it can be tested directly
|
|
30
|
+
* with a fixed Answers object instead of driving fake stdin.
|
|
31
|
+
*/
|
|
32
|
+
export async function generateProject(answers: Answers, cwd: string = process.cwd()): Promise<GenerateResult> {
|
|
33
|
+
const root = join(cwd, answers.projectName);
|
|
34
|
+
const filesWritten: string[] = [];
|
|
35
|
+
|
|
36
|
+
const files: Array<[string, string]> = [
|
|
37
|
+
[join(root, "index.ts"), renderIndexTs()],
|
|
38
|
+
[join(root, ".env.example"), renderEnvExample(answers.db)],
|
|
39
|
+
[join(root, "package.json"), renderPackageJson(answers.projectName, answers.db)],
|
|
40
|
+
[join(root, "tsconfig.json"), renderTsconfig()],
|
|
41
|
+
[join(root, "README.md"), renderReadme(answers)],
|
|
42
|
+
[join(root, ".gitignore"), "node_modules/\ndist/\n*.log\n.env\n.env.local\n"],
|
|
43
|
+
[join(root, "src/app.ts"), renderAppTs()],
|
|
44
|
+
[join(root, "src/config/db.ts"), renderDbFile(answers.db)],
|
|
45
|
+
[join(root, "src/middleware/example.middleware.ts"), renderExampleMiddleware()],
|
|
46
|
+
[join(root, "src/endpoints/index.ts"), renderEndpointsIndex(answers.routes)],
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
for (const route of answers.routes) {
|
|
50
|
+
const dir = join(root, "src/endpoints", route);
|
|
51
|
+
files.push(
|
|
52
|
+
[join(dir, `${route}.controller.ts`), renderController(route)],
|
|
53
|
+
[join(dir, `${route}.routes.ts`), renderRoutes(route)],
|
|
54
|
+
[join(dir, `${route}.services.ts`), renderServices(route)]
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const [path, contents] of files) {
|
|
59
|
+
await write(path, contents);
|
|
60
|
+
filesWritten.push(path);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { root, filesWritten };
|
|
64
|
+
}
|
package/src/prompts.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as p from "@clack/prompts";
|
|
2
|
+
|
|
3
|
+
export type DbChoice = "neon" | "postgres" | "sqlite" | "none";
|
|
4
|
+
|
|
5
|
+
export interface Answers {
|
|
6
|
+
projectName: string;
|
|
7
|
+
db: DbChoice;
|
|
8
|
+
routes: string[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function bail(): never {
|
|
12
|
+
p.cancel("Cancelled.");
|
|
13
|
+
process.exit(0);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function toKebabCase(input: string): string {
|
|
17
|
+
return input
|
|
18
|
+
.trim()
|
|
19
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
20
|
+
.replace(/[\s_]+/g, "-")
|
|
21
|
+
.toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function runPrompts(): Promise<Answers> {
|
|
25
|
+
p.intro("create-backon");
|
|
26
|
+
|
|
27
|
+
const projectName = await p.text({
|
|
28
|
+
message: "Project name",
|
|
29
|
+
placeholder: "my-api",
|
|
30
|
+
validate: (value) => {
|
|
31
|
+
if (!value) return "Required.";
|
|
32
|
+
if (!/^[a-z0-9-]+$/.test(value)) return "Lowercase letters, numbers, and hyphens only.";
|
|
33
|
+
return undefined;
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
if (p.isCancel(projectName)) bail();
|
|
37
|
+
|
|
38
|
+
const db = await p.select({
|
|
39
|
+
message: "Database",
|
|
40
|
+
options: [
|
|
41
|
+
{ value: "neon", label: "Neon", hint: "serverless Postgres — matches a Vercel deploy" },
|
|
42
|
+
{ value: "postgres", label: "Postgres", hint: "local/self-hosted — dev or a long-running server only" },
|
|
43
|
+
{ value: "sqlite", label: "SQLite", hint: "local dev only" },
|
|
44
|
+
{ value: "none", label: "None", hint: "empty db.ts stub, wire it up later" },
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
if (p.isCancel(db)) bail();
|
|
48
|
+
|
|
49
|
+
const routes: string[] = [];
|
|
50
|
+
p.log.step("Add routes one at a time. Leave the name blank when you're done.");
|
|
51
|
+
// eslint-disable-next-line no-constant-condition
|
|
52
|
+
while (true) {
|
|
53
|
+
const name = await p.text({
|
|
54
|
+
message: routes.length === 0 ? "Route name (e.g. telemetry)" : "Another route? (blank to finish)",
|
|
55
|
+
placeholder: routes.length === 0 ? "telemetry" : "",
|
|
56
|
+
});
|
|
57
|
+
if (p.isCancel(name)) bail();
|
|
58
|
+
if (!name) break;
|
|
59
|
+
routes.push(toKebabCase(name));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
p.outro(`Scaffolding ${projectName}...`);
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
projectName: projectName as string,
|
|
66
|
+
db: db as DbChoice,
|
|
67
|
+
routes,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { Answers } from "../prompts";
|
|
2
|
+
import { camelCase } from "./route";
|
|
3
|
+
import { dbDependencies, dbEnvVars, dbReadmeNote } from "./db";
|
|
4
|
+
|
|
5
|
+
export function renderIndexTs(): string {
|
|
6
|
+
return `import { serve } from "@mednours/backon";
|
|
7
|
+
import { app } from "./src/app";
|
|
8
|
+
|
|
9
|
+
serve(app);
|
|
10
|
+
`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function renderAppTs(): string {
|
|
14
|
+
return `import { createApp } from "@mednours/backon";
|
|
15
|
+
import { endpoints } from "./endpoints";
|
|
16
|
+
import { exampleMiddleware } from "./middleware/example.middleware";
|
|
17
|
+
|
|
18
|
+
export const app = createApp();
|
|
19
|
+
|
|
20
|
+
app.use("*", exampleMiddleware);
|
|
21
|
+
app.route("/", endpoints);
|
|
22
|
+
`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function renderExampleMiddleware(): string {
|
|
26
|
+
return `import type { Next } from "@mednours/backon";
|
|
27
|
+
import type { BackonContext } from "@mednours/backon";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Example middleware, just to show the pattern — adds a response-time
|
|
31
|
+
* header. Error handling, request logging, secure headers, CORS, and the
|
|
32
|
+
* request body size limit are already wired into createApp() by backon
|
|
33
|
+
* itself; you don't need your own error.middleware.ts unless you want to
|
|
34
|
+
* override that default behavior.
|
|
35
|
+
*/
|
|
36
|
+
export async function exampleMiddleware(c: BackonContext, next: Next) {
|
|
37
|
+
const start = performance.now();
|
|
38
|
+
await next();
|
|
39
|
+
c.header("x-response-time-ms", String(Math.round(performance.now() - start)));
|
|
40
|
+
}
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function renderEndpointsIndex(routes: string[]): string {
|
|
45
|
+
const imports = routes
|
|
46
|
+
.map((route) => `import { ${camelCase(route)}Routes } from "./${route}/${route}.routes";`)
|
|
47
|
+
.join("\n");
|
|
48
|
+
const mounts = routes.map((route) => `endpoints.route("/${route}", ${camelCase(route)}Routes);`).join("\n");
|
|
49
|
+
|
|
50
|
+
return `import { Hono } from "@mednours/backon";
|
|
51
|
+
${imports}
|
|
52
|
+
|
|
53
|
+
export const endpoints = new Hono();
|
|
54
|
+
|
|
55
|
+
${mounts}
|
|
56
|
+
`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function renderEnvExample(db: Answers["db"]): string {
|
|
60
|
+
return `PORT=3000
|
|
61
|
+
ALLOWED_ORIGINS=
|
|
62
|
+
${dbEnvVars(db)}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function renderTsconfig(): string {
|
|
66
|
+
return `{
|
|
67
|
+
"compilerOptions": {
|
|
68
|
+
"target": "ESNext",
|
|
69
|
+
"module": "ESNext",
|
|
70
|
+
"moduleResolution": "bundler",
|
|
71
|
+
"lib": ["ESNext"],
|
|
72
|
+
"types": ["bun-types"],
|
|
73
|
+
"strict": true,
|
|
74
|
+
"esModuleInterop": true,
|
|
75
|
+
"skipLibCheck": true,
|
|
76
|
+
"forceConsistentCasingInFileNames": true,
|
|
77
|
+
"resolveJsonModule": true,
|
|
78
|
+
"isolatedModules": true,
|
|
79
|
+
"noEmit": true,
|
|
80
|
+
"allowJs": false
|
|
81
|
+
},
|
|
82
|
+
"include": ["index.ts", "src"]
|
|
83
|
+
}
|
|
84
|
+
`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function renderPackageJson(projectName: string, db: Answers["db"]): string {
|
|
88
|
+
const dependencies: Record<string, string> = {
|
|
89
|
+
"@mednours/backon": "latest",
|
|
90
|
+
hono: "^4.6.14",
|
|
91
|
+
zod: "^3.24.1",
|
|
92
|
+
...dbDependencies(db),
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const pkg = {
|
|
96
|
+
name: projectName,
|
|
97
|
+
version: "0.1.0",
|
|
98
|
+
private: true,
|
|
99
|
+
type: "module",
|
|
100
|
+
scripts: {
|
|
101
|
+
dev: "bun run --watch index.ts",
|
|
102
|
+
start: "bun run index.ts",
|
|
103
|
+
test: "bun test",
|
|
104
|
+
typecheck: "tsc --noEmit",
|
|
105
|
+
},
|
|
106
|
+
dependencies,
|
|
107
|
+
devDependencies: {
|
|
108
|
+
"@types/bun": "latest",
|
|
109
|
+
typescript: "^5.7.2",
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return `${JSON.stringify(pkg, null, 2)}\n`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function renderReadme(answers: Answers): string {
|
|
117
|
+
const routeList = answers.routes.length
|
|
118
|
+
? answers.routes.map((route) => `- \`src/endpoints/${route}/\``).join("\n")
|
|
119
|
+
: "- (none yet — add one under src/endpoints/)";
|
|
120
|
+
|
|
121
|
+
return `# ${answers.projectName}
|
|
122
|
+
|
|
123
|
+
Generated by \`create-backon\`.
|
|
124
|
+
|
|
125
|
+
## Getting started
|
|
126
|
+
|
|
127
|
+
\`\`\`
|
|
128
|
+
bun install
|
|
129
|
+
cp .env.example .env # fill in real values
|
|
130
|
+
bun run dev
|
|
131
|
+
\`\`\`
|
|
132
|
+
|
|
133
|
+
## Database: ${answers.db}
|
|
134
|
+
|
|
135
|
+
${dbReadmeNote(answers.db)}
|
|
136
|
+
|
|
137
|
+
## Routes
|
|
138
|
+
|
|
139
|
+
${routeList}
|
|
140
|
+
|
|
141
|
+
Each route folder follows the same shape: \`*.routes.ts\` wires HTTP methods
|
|
142
|
+
to controller functions, \`*.controller.ts\` validates the request (via
|
|
143
|
+
\`defineRoute\` from \`@mednours/backon\`) and calls into \`*.services.ts\`,
|
|
144
|
+
which is where real data access belongs.
|
|
145
|
+
`;
|
|
146
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { DbChoice } from "../prompts";
|
|
2
|
+
|
|
3
|
+
export interface DbTemplate {
|
|
4
|
+
/** Contents of src/config/db.ts */
|
|
5
|
+
file: string;
|
|
6
|
+
/** Extra runtime dependencies the generated package.json needs. */
|
|
7
|
+
dependencies: Record<string, string>;
|
|
8
|
+
/** Line(s) appended to .env.example. */
|
|
9
|
+
envVars: string;
|
|
10
|
+
/** One-line note surfaced in the generated README so the caveats aren't silently lost. */
|
|
11
|
+
readmeNote: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const TEMPLATES: Record<DbChoice, DbTemplate> = {
|
|
15
|
+
neon: {
|
|
16
|
+
file: `import { drizzle } from "drizzle-orm/neon-http";
|
|
17
|
+
import { neon } from "@neondatabase/serverless";
|
|
18
|
+
|
|
19
|
+
if (!process.env.DATABASE_URL) {
|
|
20
|
+
throw new Error("DATABASE_URL is required (Neon connection string)");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const sql = neon(process.env.DATABASE_URL);
|
|
24
|
+
|
|
25
|
+
export const db = drizzle(sql);
|
|
26
|
+
`,
|
|
27
|
+
dependencies: {
|
|
28
|
+
"drizzle-orm": "^0.38.2",
|
|
29
|
+
"@neondatabase/serverless": "^0.10.4",
|
|
30
|
+
},
|
|
31
|
+
envVars: "DATABASE_URL=postgres://user:password@ep-example-123456.us-east-2.aws.neon.tech/dbname?sslmode=require\n",
|
|
32
|
+
readmeNote:
|
|
33
|
+
"Uses Neon's HTTP driver — no connection pooling to manage, which is what makes it safe to use from serverless functions (e.g. Vercel).",
|
|
34
|
+
},
|
|
35
|
+
postgres: {
|
|
36
|
+
file: `import { drizzle } from "drizzle-orm/node-postgres";
|
|
37
|
+
import { Pool } from "pg";
|
|
38
|
+
|
|
39
|
+
// NOTE: this pooled connection is for local dev or a long-running server
|
|
40
|
+
// (Docker, a VPS, Render, etc). It is NOT safe to use as-is on Vercel
|
|
41
|
+
// serverless functions — each cold start can open a new connection until
|
|
42
|
+
// you exhaust the database's connection limit. Use the "neon" db option
|
|
43
|
+
// instead for serverless deploys.
|
|
44
|
+
|
|
45
|
+
if (!process.env.DATABASE_URL) {
|
|
46
|
+
throw new Error("DATABASE_URL is required");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
50
|
+
|
|
51
|
+
export const db = drizzle(pool);
|
|
52
|
+
`,
|
|
53
|
+
dependencies: {
|
|
54
|
+
"drizzle-orm": "^0.38.2",
|
|
55
|
+
pg: "^8.13.1",
|
|
56
|
+
},
|
|
57
|
+
envVars: "DATABASE_URL=postgres://user:password@localhost:5432/dbname\n",
|
|
58
|
+
readmeNote:
|
|
59
|
+
"Pooled Postgres connection — fine for local dev or a long-running server, NOT safe as-is on Vercel serverless functions (connection pool exhaustion on cold starts). Use Neon for serverless deploys.",
|
|
60
|
+
},
|
|
61
|
+
sqlite: {
|
|
62
|
+
file: `import { drizzle } from "drizzle-orm/bun-sqlite";
|
|
63
|
+
import { Database } from "bun:sqlite";
|
|
64
|
+
|
|
65
|
+
// NOTE: local dev only. A serverless function's filesystem is ephemeral —
|
|
66
|
+
// this file won't persist between invocations if deployed as-is.
|
|
67
|
+
|
|
68
|
+
const sqlite = new Database(process.env.DATABASE_URL ?? "./dev.db");
|
|
69
|
+
|
|
70
|
+
export const db = drizzle(sqlite);
|
|
71
|
+
`,
|
|
72
|
+
dependencies: {
|
|
73
|
+
"drizzle-orm": "^0.38.2",
|
|
74
|
+
},
|
|
75
|
+
envVars: "DATABASE_URL=./dev.db\n",
|
|
76
|
+
readmeNote: "Local file-based SQLite — for local dev only, not for a serverless deploy target.",
|
|
77
|
+
},
|
|
78
|
+
none: {
|
|
79
|
+
file: `// No database configured yet. Wire up a real client here — e.g. one of
|
|
80
|
+
// the neon/postgres/sqlite variants this CLI can also generate — and
|
|
81
|
+
// export it as \`db\` so src/endpoints/*/*.services.ts can import it.
|
|
82
|
+
|
|
83
|
+
export const db = undefined;
|
|
84
|
+
`,
|
|
85
|
+
dependencies: {},
|
|
86
|
+
envVars: "",
|
|
87
|
+
readmeNote: "No database configured. src/config/db.ts is an empty stub.",
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export function renderDbFile(db: DbChoice): string {
|
|
92
|
+
return TEMPLATES[db].file;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function dbDependencies(db: DbChoice): Record<string, string> {
|
|
96
|
+
return TEMPLATES[db].dependencies;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function dbEnvVars(db: DbChoice): string {
|
|
100
|
+
return TEMPLATES[db].envVars;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function dbReadmeNote(db: DbChoice): string {
|
|
104
|
+
return TEMPLATES[db].readmeNote;
|
|
105
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
function pascalCase(name: string): string {
|
|
2
|
+
return name
|
|
3
|
+
.split("-")
|
|
4
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
5
|
+
.join("");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function renderController(route: string): string {
|
|
9
|
+
const Route = pascalCase(route);
|
|
10
|
+
return `import { z } from "zod";
|
|
11
|
+
import { defineRoute } from "@mednours/backon";
|
|
12
|
+
import * as service from "./${route}.services";
|
|
13
|
+
|
|
14
|
+
export const list = defineRoute({}, async (c) => {
|
|
15
|
+
const items = await service.list();
|
|
16
|
+
return c.json(items);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// TODO: replace with the real shape of a ${Route}
|
|
20
|
+
const create${Route}Schema = z.object({});
|
|
21
|
+
|
|
22
|
+
export const create = defineRoute({ body: create${Route}Schema }, async (c, { body }) => {
|
|
23
|
+
const item = await service.create(body);
|
|
24
|
+
return c.json(item, 201);
|
|
25
|
+
});
|
|
26
|
+
`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function renderRoutes(route: string): string {
|
|
30
|
+
const Route = pascalCase(route);
|
|
31
|
+
return `import { Hono, type Env } from "@mednours/backon";
|
|
32
|
+
import * as controller from "./${route}.controller";
|
|
33
|
+
|
|
34
|
+
export const ${camelCase(route)}Routes = new Hono<Env>();
|
|
35
|
+
|
|
36
|
+
${camelCase(route)}Routes.get("/", controller.list);
|
|
37
|
+
${camelCase(route)}Routes.post("/", controller.create);
|
|
38
|
+
`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function renderServices(route: string): string {
|
|
42
|
+
return `// TODO: replace with real data access (see src/config/db.ts)
|
|
43
|
+
|
|
44
|
+
export async function list() {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function create(data: unknown) {
|
|
49
|
+
return { id: crypto.randomUUID(), ...(data as object) };
|
|
50
|
+
}
|
|
51
|
+
`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function camelCase(name: string): string {
|
|
55
|
+
const [first, ...rest] = name.split("-");
|
|
56
|
+
return [first, ...rest.map((part) => part.charAt(0).toUpperCase() + part.slice(1))].join("");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { camelCase };
|