@rebasepro/cli 0.0.1-canary.2 → 0.0.1-canary.3263433

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.
Files changed (51) hide show
  1. package/dist/commands/auth.d.ts +1 -0
  2. package/dist/commands/cli.test.d.ts +1 -0
  3. package/dist/commands/db.d.ts +1 -0
  4. package/dist/commands/dev.d.ts +1 -0
  5. package/dist/commands/dev.test.d.ts +1 -0
  6. package/dist/commands/doctor.d.ts +1 -0
  7. package/dist/commands/generate_sdk.d.ts +18 -0
  8. package/dist/commands/init.d.ts +4 -0
  9. package/dist/commands/init.test.d.ts +1 -0
  10. package/dist/commands/schema.d.ts +1 -0
  11. package/dist/index.cjs +1064 -51
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.ts +5 -0
  14. package/dist/index.es.js +1065 -50
  15. package/dist/index.es.js.map +1 -1
  16. package/dist/utils/project.d.ts +45 -0
  17. package/dist/utils/project.test.d.ts +1 -0
  18. package/package.json +19 -11
  19. package/templates/template/.dockerignore +28 -0
  20. package/templates/template/.env.example +96 -0
  21. package/templates/template/README.md +84 -17
  22. package/templates/template/backend/Dockerfile +55 -10
  23. package/templates/template/backend/drizzle.config.ts +24 -4
  24. package/templates/template/backend/functions/hello.ts +52 -0
  25. package/templates/template/backend/package.json +30 -34
  26. package/templates/template/backend/src/env.ts +52 -0
  27. package/templates/template/backend/src/index.ts +113 -99
  28. package/templates/template/backend/tsconfig.json +1 -1
  29. package/templates/template/config/collections/authors.ts +45 -0
  30. package/templates/template/config/collections/index.ts +5 -0
  31. package/templates/template/config/collections/posts.ts +91 -0
  32. package/templates/template/config/collections/tags.ts +27 -0
  33. package/templates/template/config/package.json +28 -0
  34. package/templates/template/docker-compose.yml +49 -13
  35. package/templates/template/frontend/Dockerfile +36 -14
  36. package/templates/template/frontend/nginx.conf +40 -0
  37. package/templates/template/frontend/package.json +9 -10
  38. package/templates/template/frontend/src/App.tsx +24 -110
  39. package/templates/template/frontend/src/index.css +15 -1
  40. package/templates/template/frontend/src/main.tsx +2 -2
  41. package/templates/template/frontend/vite.config.ts +33 -3
  42. package/templates/template/package.json +12 -16
  43. package/templates/template/pnpm-workspace.yaml +4 -0
  44. package/templates/template/scripts/example.ts +91 -0
  45. package/templates/template/.env.template +0 -31
  46. package/templates/template/backend/scripts/db-generate.ts +0 -60
  47. package/templates/template/shared/collections/index.ts +0 -3
  48. package/templates/template/shared/collections/posts.ts +0 -53
  49. package/templates/template/shared/package.json +0 -28
  50. /package/templates/template/{shared → config}/index.ts +0 -0
  51. /package/templates/template/{shared → config}/tsconfig.json +0 -0
@@ -2,15 +2,31 @@
2
2
 
3
3
  A [Rebase](https://rebase.pro) project with a PostgreSQL backend.
4
4
 
5
- ## Getting Started
5
+ ## Quick Start
6
6
 
7
- ### Prerequisites
7
+ ### Option 1: Docker (recommended for production)
8
+
9
+ ```bash
10
+ cp .env.example .env
11
+ # Edit .env — set JWT_SECRET and DATABASE_URL (see comments for generators)
12
+
13
+ docker compose up -d
14
+ ```
15
+
16
+ That's it. Your app is running:
17
+ - **Frontend**: http://localhost (port 80)
18
+ - **Backend API**: http://localhost:3001
19
+ - **PostgreSQL**: localhost:5432
20
+
21
+ ### Option 2: Local Development
22
+
23
+ #### Prerequisites
8
24
 
9
25
  - [Node.js](https://nodejs.org) >= 18
10
26
  - [pnpm](https://pnpm.io)
11
27
  - A PostgreSQL database
12
28
 
13
- ### Setup
29
+ #### Setup
14
30
 
15
31
  1. Install dependencies:
16
32
 
@@ -18,17 +34,18 @@ A [Rebase](https://rebase.pro) project with a PostgreSQL backend.
18
34
  pnpm install
19
35
  ```
20
36
 
21
- 2. Configure your database — edit `.env` and set `DATABASE_URL`:
37
+ 2. Configure environment:
22
38
 
23
- ```
24
- DATABASE_URL=postgresql://postgres:password@localhost:5432/mydb
39
+ ```bash
40
+ cp .env.example .env
41
+ # Edit .env — set DATABASE_URL, JWT_SECRET
25
42
  ```
26
43
 
27
- 3. Generate and run database migrations:
44
+ 3. Generate schema and push to database:
28
45
 
29
46
  ```bash
30
- pnpm db:generate
31
- pnpm db:migrate
47
+ rebase schema generate
48
+ rebase db push
32
49
  ```
33
50
 
34
51
  4. Start the dev server:
@@ -37,23 +54,73 @@ pnpm db:migrate
37
54
  pnpm dev
38
55
  ```
39
56
 
40
- This starts both the backend (Express + PostgreSQL on port 3001) and the frontend (Vite + React on port 5173) concurrently.
57
+ Backend (Hono + PostgreSQL) on port 3001, frontend (Vite + React) on port 5173.
41
58
 
42
59
  ## Project Structure
43
60
 
44
61
  ```
45
- ├── frontend/ # React frontend (Vite)
46
- ├── backend/ # Express backend with PostgreSQL
47
- ├── shared/ # Shared collection definitions
48
- ├── .env # Environment variables
49
- └── package.json # Root workspace config
62
+ ├── frontend/ # React frontend (Vite)
63
+ ├── Dockerfile # Production build nginx
64
+ │ └── nginx.conf # SPA routing + compression
65
+ ├── backend/ # Hono backend with PostgreSQL
66
+ │ ├── Dockerfile # Multi-stage production build
67
+ │ ├── functions/ # Custom API endpoints (auto-discovered)
68
+ │ └── src/
69
+ ├── config/ # Shared collection definitions
70
+ │ └── collections/ # Schema-as-Code TypeScript files
71
+ ├── docker-compose.yml # Production stack (Postgres + Backend + Frontend)
72
+ ├── .env.example # Environment variable reference
73
+ └── package.json # Root workspace config
74
+ ```
75
+
76
+ ### Custom Functions
77
+
78
+ Drop a Hono app in `backend/functions/` and it's auto-mounted at `/api/functions/<name>`:
79
+
80
+ ```typescript
81
+ // backend/functions/hello.ts
82
+ import { Hono } from "hono";
83
+ const app = new Hono();
84
+ app.post("/", async (c) => {
85
+ const body = await c.req.json();
86
+ return c.json({ message: `Hello, ${body.name}!` });
87
+ });
88
+ export default app;
50
89
  ```
51
90
 
91
+ Call from the client SDK: `client.call("functions/hello", { name: "World" })`
92
+
52
93
  ### Shared Collections
53
94
 
54
- Collections are defined once in `shared/collections/` and used by both the frontend and backend. This ensures your schema stays in sync across the stack.
95
+ Collections are defined once in `config/collections/` and used by both the frontend and backend. This ensures your schema stays in sync across the stack.
96
+
97
+ ## Environment Configuration
98
+
99
+ All configuration is managed through a single `.env` file in the project root. Both the backend and frontend read from this file:
100
+
101
+ - **Backend**: loads via `dotenv` from `../../.env` (relative to `backend/src/`)
102
+ - **Frontend**: Vite reads `VITE_*` variables via `envDir` pointing to the project root
103
+ - **Scripts**: load via `dotenv` from the project root
104
+
105
+ Copy `.env.example` to `.env` to get started. See the comments in `.env.example` for details on each variable.
106
+
107
+ ## Production Deployment
108
+
109
+ ```bash
110
+ # Build and start
111
+ docker compose up -d --build
112
+
113
+ # View logs
114
+ docker compose logs -f backend
115
+
116
+ # Stop
117
+ docker compose down
118
+
119
+ # Stop and remove data
120
+ docker compose down -v
121
+ ```
55
122
 
56
123
  ## Documentation
57
124
 
58
125
  - [Rebase Docs](https://rebase.pro/docs)
59
- - [GitHub](https://github.com/rebaseco/rebase)
126
+ - [GitHub](https://github.com/rebasepro/rebase)
@@ -1,25 +1,70 @@
1
- FROM node:24-alpine AS base
1
+ # ─── Multi-stage production Dockerfile for the Rebase backend ─────────
2
+ # Produces a minimal image (~150MB) with only the runtime needed.
3
+ #
4
+ # Build context: the project root (where pnpm-workspace.yaml lives)
5
+ # Usage:
6
+ # docker build -t my-app-backend -f backend/Dockerfile .
7
+ # docker run -p 3001:3001 --env-file .env my-app-backend
8
+
9
+ # ── Stage 1: Install + Build ─────────────────────────────────────────
10
+ FROM node:22-alpine AS builder
11
+
2
12
  ENV PNPM_HOME="/pnpm"
3
13
  ENV PATH="$PNPM_HOME:$PATH"
4
14
  RUN corepack enable
15
+
16
+ # Native dependencies for bcrypt, pg, etc.
5
17
  RUN apk add --no-cache python3 make g++
6
18
 
7
19
  WORKDIR /app
8
20
 
9
- # Copy root configurations
10
- COPY package.json ./
21
+ # Copy workspace root files first (cache-friendly layer)
22
+ COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
11
23
 
12
- # Copy internal workspaces
24
+ # Copy workspace packages
13
25
  COPY backend ./backend
14
- COPY shared ./shared
26
+ COPY config ./config
27
+
28
+ # Install all deps (including devDependencies for build)
29
+ RUN pnpm install --frozen-lockfile
30
+
31
+ # Build config first, then backend
32
+ RUN pnpm --filter "*-config" run build
33
+ RUN pnpm --filter "*-backend" run build
34
+
35
+ # Prune dev dependencies for a smaller runtime
36
+ RUN pnpm install --frozen-lockfile --prod
37
+
38
+ # ── Stage 2: Production Runtime ──────────────────────────────────────
39
+ FROM node:22-alpine AS runtime
40
+
41
+ ENV PNPM_HOME="/pnpm"
42
+ ENV PATH="$PNPM_HOME:$PATH"
43
+ ENV NODE_ENV=production
44
+
45
+ RUN corepack enable
15
46
 
16
- # Install dependencies
17
- RUN pnpm install
47
+ # Security: run as non-root
48
+ RUN addgroup -g 1001 rebase && adduser -u 1001 -G rebase -s /bin/sh -D rebase
49
+
50
+ WORKDIR /app
18
51
 
19
- # Build shared and backend
20
- RUN pnpm run build:shared
21
- RUN pnpm run build:backend
52
+ # Copy only production artifacts
53
+ COPY --from=builder /app/package.json /app/pnpm-lock.yaml /app/pnpm-workspace.yaml ./
54
+ COPY --from=builder /app/node_modules ./node_modules
55
+ COPY --from=builder /app/backend ./backend
56
+ COPY --from=builder /app/config ./config
57
+
58
+ # Create uploads directory
59
+ RUN mkdir -p /app/backend/uploads && chown -R rebase:rebase /app
60
+
61
+ USER rebase
22
62
 
23
63
  WORKDIR /app/backend
24
64
  EXPOSE 3001
65
+
66
+ # Health check for orchestrators (Docker Compose, ECS, k8s)
67
+ HEALTHCHECK --interval=30s --timeout=5s --retries=3 --start-period=10s \
68
+ CMD wget --no-verbose --tries=1 --spider http://localhost:3001/health || exit 1
69
+
25
70
  CMD ["pnpm", "start"]
@@ -1,14 +1,23 @@
1
- import "dotenv/config";
1
+ import * as dotenv from "dotenv";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
2
4
  import { defineConfig } from "drizzle-kit";
3
5
  import { tables } from "./src/schema.generated";
4
6
  import { getTableName, Table } from "drizzle-orm";
5
7
 
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+
11
+ // Load .env from the project root (single file for the entire project)
12
+ dotenv.config({ path: path.resolve(__dirname, "../.env") });
13
+
6
14
  if (!process.env.DATABASE_URL) {
7
15
  throw new Error("DATABASE_URL is not set. Make sure .env file exists in the project root and contains DATABASE_URL");
8
16
  }
9
17
 
10
- // Extract table names from the generated schema
11
- // This ensures drizzle-kit ONLY manages tables defined in the schema
18
+ // Extract table names from the generated schema.
19
+ // This ensures drizzle-kit ONLY manages tables defined in the schema.
20
+ // Any tables in the database that are NOT part of the Rebase schema are left untouched.
12
21
  const tableNames = Object.values(tables).map(table => getTableName(table as Table));
13
22
 
14
23
  export default defineConfig({
@@ -18,5 +27,16 @@ export default defineConfig({
18
27
  dbCredentials: {
19
28
  url: process.env.DATABASE_URL
20
29
  },
21
- tablesFilter: tableNames
30
+ // Only manage tables defined in the generated schema.
31
+ // Unmapped tables in the database are completely ignored.
32
+ tablesFilter: tableNames,
33
+ // Restrict drizzle-kit to the public schema only — tables in other schemas
34
+ // (e.g. extensions, custom schemas) are never touched.
35
+ schemaFilter: ["public"],
36
+ // Prevent drizzle-kit from managing roles not defined in the schema
37
+ entities: {
38
+ roles: false
39
+ },
40
+ // If PostGIS or other extensions create helper tables, ignore them
41
+ extensionsFilters: ["postgis"]
22
42
  });
@@ -0,0 +1,52 @@
1
+ import { Hono } from "hono";
2
+ import type { HonoEnv } from "@rebasepro/server-core";
3
+ import { rebase } from "@rebasepro/server-core";
4
+
5
+ /**
6
+ * Example custom function route.
7
+ *
8
+ * This file is auto-discovered by Rebase and mounted at:
9
+ * POST /api/functions/hello
10
+ * GET /api/functions/hello
11
+ *
12
+ * Call from the client SDK:
13
+ * const result = await client.call("functions/hello", { name: "World" });
14
+ *
15
+ * This is a standard Hono app — use any Hono middleware,
16
+ * define any HTTP methods, access the request/response directly.
17
+ *
18
+ * The `rebase` singleton gives you admin-level access to all
19
+ * app-scoped services (data, auth, storage, email) from anywhere.
20
+ * For request-scoped / RLS-scoped access, use c.get("user") and c.get("driver").
21
+ */
22
+ const app = new Hono<HonoEnv>();
23
+
24
+ app.post("/", async (c) => {
25
+ const body = await c.req.json().catch(() => ({}));
26
+ const user = c.get("user");
27
+
28
+ const userId = (user && typeof user === "object" && "userId" in user)
29
+ ? user.userId
30
+ : "anonymous";
31
+
32
+ // Access any Rebase service — just import and use:
33
+ // await rebase.email?.send({
34
+ // to: "admin@example.com",
35
+ // subject: "Function called",
36
+ // html: `<p>Hello from ${userId}!</p>`,
37
+ // });
38
+ //
39
+ // const authors = await rebase.data.authors.find({ limit: 5 });
40
+
41
+ return c.json({
42
+ message: `Hello, ${body.name || "World"}!`,
43
+ user: userId
44
+ });
45
+ });
46
+
47
+ app.get("/", (c) => {
48
+ return c.json({ status: "ok",
49
+ endpoint: "hello" });
50
+ });
51
+
52
+ export default app;
@@ -1,36 +1,32 @@
1
1
  {
2
- "name": "{{PROJECT_NAME}}-backend",
3
- "version": "1.0.0",
4
- "description": "Rebase backend with PostgreSQL",
5
- "main": "src/index.ts",
6
- "type": "module",
7
- "scripts": {
8
- "dev": "tsx watch --watch=\"../shared/**/*\" src/index.ts",
9
- "build": "tsc --noEmit",
10
- "start": "tsx src/index.ts",
11
- "generate:schema": "tsx node_modules/@rebasepro/backend/src/generate-drizzle-schema.ts --collections=../shared/collections --output=src/schema.generated.ts",
12
- "db:generate": "pnpm generate:schema && tsx scripts/db-generate.ts",
13
- "db:migrate": "DOTENV_CONFIG_PATH=../.env drizzle-kit migrate",
14
- "db:push": "DOTENV_CONFIG_PATH=../.env drizzle-kit push",
15
- "db:studio": "DOTENV_CONFIG_PATH=../.env drizzle-kit studio"
16
- },
17
- "dependencies": {
18
- "{{PROJECT_NAME}}-shared": "workspace:*",
19
- "@rebasepro/backend": "^4.0.0",
20
- "cors": "^2.8.5",
21
- "drizzle-orm": "^0.44.4",
22
- "express": "^5.1.0",
23
- "pg": "^8.11.3",
24
- "ws": "^8.16.0",
25
- "zod": "^3.22.4",
26
- "dotenv": "^16.0.0"
27
- },
28
- "devDependencies": {
29
- "@types/pg": "^8.6.5",
30
- "@types/node": "^20.10.5",
31
- "@types/ws": "^8.5.10",
32
- "drizzle-kit": "^0.31.4",
33
- "tsx": "^4.20.6",
34
- "typescript": "^5.9.2"
35
- }
2
+ "name": "{{PROJECT_NAME}}-backend",
3
+ "version": "1.0.0",
4
+ "description": "Rebase backend with PostgreSQL",
5
+ "main": "src/index.ts",
6
+ "type": "module",
7
+ "scripts": {
8
+ "dev": "tsx watch --watch=\"../config/**/*\" src/index.ts",
9
+ "build": "tsc --noEmit",
10
+ "start": "tsx src/index.ts"
11
+ },
12
+ "dependencies": {
13
+ "{{PROJECT_NAME}}-config": "workspace:*",
14
+ "@rebasepro/server-core": "workspace:*",
15
+ "@rebasepro/server-postgresql": "workspace:*",
16
+ "drizzle-orm": "^0.44.4",
17
+ "hono": "^4.12.10",
18
+ "@hono/node-server": "^1.19.12",
19
+ "pg": "^8.11.3",
20
+ "ws": "^8.16.0",
21
+ "zod": "^3.22.4",
22
+ "dotenv": "^16.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/pg": "^8.6.5",
26
+ "@types/node": "^20.10.5",
27
+ "@types/ws": "^8.5.10",
28
+ "drizzle-kit": "^0.31.4",
29
+ "tsx": "^4.20.6",
30
+ "typescript": "^5.9.2"
31
+ }
36
32
  }
@@ -0,0 +1,52 @@
1
+ import { z } from "zod";
2
+ import * as dotenv from "dotenv";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = path.dirname(__filename);
8
+
9
+ dotenv.config({ path: path.resolve(__dirname, "../../.env") });
10
+
11
+ const envSchema = z.object({
12
+ NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
13
+ PORT: z.string().default("3001").transform(Number),
14
+ DATABASE_URL: z.string().url("DATABASE_URL must be a valid URL"),
15
+ ADMIN_CONNECTION_STRING: z.string().url().optional(),
16
+ JWT_SECRET: z.string().min(32, "JWT_SECRET must be at least 32 characters long"),
17
+ JWT_ACCESS_EXPIRES_IN: z.string().default("1h"),
18
+ JWT_REFRESH_EXPIRES_IN: z.string().default("30d"),
19
+ GOOGLE_CLIENT_ID: z.string().optional(),
20
+ ALLOW_REGISTRATION: z.enum(["true", "false", ""]).default("false").transform(v => v === "true"),
21
+ CORS_ORIGINS: z.string().optional(),
22
+ FRONTEND_URL: z.string().optional(),
23
+ DB_POOL_MAX: z.string().default("20").transform(Number),
24
+ DB_POOL_IDLE_TIMEOUT: z.string().default("30000").transform(Number),
25
+ DB_POOL_CONNECT_TIMEOUT: z.string().default("10000").transform(Number),
26
+ FORCE_LOCAL_STORAGE: z.enum(["true", "false", ""]).optional().transform(v => v === "true"),
27
+ STORAGE_TYPE: z.enum(["local", "s3"]).default("local"),
28
+ STORAGE_PATH: z.string().optional(),
29
+ S3_BUCKET: z.string().optional(),
30
+ S3_REGION: z.string().optional(),
31
+ S3_ACCESS_KEY_ID: z.string().optional(),
32
+ S3_SECRET_ACCESS_KEY: z.string().optional(),
33
+ S3_ENDPOINT: z.string().url().optional(),
34
+ S3_FORCE_PATH_STYLE: z.enum(["true", "false", ""]).optional().transform(v => v === "true"),
35
+ /**
36
+ * Service key for admin-level script / server-to-server authentication.
37
+ * When set, scripts can send `Authorization: Bearer <key>` to get admin access.
38
+ * Generate with: node -e "console.log(require('crypto').randomBytes(48).toString('base64'))"
39
+ */
40
+ REBASE_SERVICE_KEY: z.string().min(32, "REBASE_SERVICE_KEY must be at least 32 characters").optional()
41
+ }).superRefine((data, ctx) => {
42
+ if (data.NODE_ENV === "production" && !data.CORS_ORIGINS && !data.FRONTEND_URL) {
43
+ ctx.addIssue({
44
+ code: z.ZodIssueCode.custom,
45
+ message: "CORS_ORIGINS or FRONTEND_URL must be set in production to secure the API.",
46
+ path: ["CORS_ORIGINS"]
47
+ });
48
+ }
49
+ });
50
+
51
+ // Parse and export
52
+ export const env = envSchema.parse(process.env);