|
13 | 13 | * cloud (multi-project) plugin stack but backs |
14 | 14 | * it with two SQLite files (`control.db` for |
15 | 15 | * the control plane, `proj_local.db` for the |
16 | | - * single project's business data). Studio + |
17 | | - * Auth fully operational. Aliases: `local`, |
18 | | - * `single-project`. |
19 | | - * |
20 | | - * - `cloud` — multi-project, control-plane connected. See |
21 | | - * @objectstack/service-cloud for details. |
| 16 | + * single project's business data). |
| 17 | + * Aliases: `local`, `single-project`. |
| 18 | + * - `cloud` — multi-project, control-plane connected. |
22 | 19 | * Alias: `multi-project`. |
| 20 | + * - `standalone` — runtime-only (ObjectQL + REST + Driver). |
23 | 21 | * |
24 | | - * - `standalone` — runtime-only (ObjectQL + REST + Driver). No |
25 | | - * authentication, no Studio, no control plane. |
26 | | - * For embedding in other frameworks or running |
27 | | - * an internal-only data API. |
28 | | - * |
29 | | - * The legacy flag `OBJECTSTACK_MULTI_PROJECT=true` is still honoured as a |
30 | | - * deprecated alias for `OBJECTSTACK_MODE=cloud` and will be removed in a |
31 | | - * future major release. |
32 | | - * |
33 | | - * ### Common env vars |
34 | | - * |
35 | | - * AUTH_SECRET — JWT signing secret (≥32 chars) |
36 | | - * NEXT_PUBLIC_BASE_URL — public origin used by better-auth |
37 | | - * OBJECTSTACK_PROJECT_ID — local project id (default: `proj_local`) |
38 | | - * OBJECTSTACK_ARTIFACT_PATH — compiled artifact (default: ./dist/objectstack.json) |
39 | | - * |
40 | | - * ### Project / Standalone DB |
41 | | - * |
42 | | - * OBJECTSTACK_DATABASE_URL — overrides default file SQLite path |
43 | | - * OBJECTSTACK_DATABASE_AUTH_TOKEN — auth token for libSQL/Turso URLs |
44 | | - * OBJECTSTACK_DATABASE_DRIVER — driver name: sqlite | memory | turso |
45 | | - * TURSO_DATABASE_URL / TURSO_AUTH_TOKEN — fallback aliases |
| 22 | + * All boot orchestration now lives in @objectstack/service-cloud. |
| 23 | + * This file only supplies the apps/server-specific knobs (templates, |
| 24 | + * app bundle resolution). |
46 | 25 | */ |
47 | 26 |
|
48 | 27 | import { resolve as resolvePath, dirname } from 'node:path'; |
49 | 28 | import { fileURLToPath } from 'node:url'; |
50 | | -import { mkdirSync } from 'node:fs'; |
51 | | -import { readFile } from 'node:fs/promises'; |
52 | | -import { createCloudStack } from '@objectstack/service-cloud'; |
53 | | -import { createSingleProjectPlugin } from './server/single-project-plugin.js'; |
54 | | -import { templateRegistry } from './server/templates/registry.js'; |
| 29 | +import { createBootStack } from '@objectstack/service-cloud'; |
55 | 30 | import { createFsAppBundleResolver } from './server/fs-app-bundle-resolver.js'; |
56 | | - |
57 | | -function envFlag(name: string): boolean { |
58 | | - return ['1', 'true', 'yes', 'on'].includes((process.env[name] ?? '').trim().toLowerCase()); |
59 | | -} |
60 | | - |
61 | | -type Mode = 'project' | 'cloud' | 'standalone'; |
62 | | - |
63 | | -/** |
64 | | - * Resolve the deployment mode from environment. |
65 | | - * |
66 | | - * Default changed from `standalone` (legacy) to `project` (current). The |
67 | | - * legacy `standalone` semantics — single-project local dev with full |
68 | | - * Auth + Studio — moved under `project`. The new `standalone` value |
69 | | - * means runtime-only (no Auth, no Studio). |
70 | | - */ |
71 | | -function resolveMode(): Mode { |
72 | | - const raw = process.env.OBJECTSTACK_MODE?.trim().toLowerCase(); |
73 | | - if (raw === 'cloud' || raw === 'multi-project') return 'cloud'; |
74 | | - if (raw === 'standalone') return 'standalone'; |
75 | | - if (raw === 'project' || raw === 'local' || raw === 'single-project') return 'project'; |
76 | | - if (raw && raw.length > 0) { |
77 | | - // eslint-disable-next-line no-console |
78 | | - console.warn(`[objectstack] Unknown OBJECTSTACK_MODE=${raw}; falling back to "project".`); |
79 | | - } |
80 | | - if (envFlag('OBJECTSTACK_MULTI_PROJECT')) { |
81 | | - // eslint-disable-next-line no-console |
82 | | - console.warn( |
83 | | - '[objectstack] OBJECTSTACK_MULTI_PROJECT is deprecated. Use `OBJECTSTACK_MODE=cloud` instead.', |
84 | | - ); |
85 | | - return 'cloud'; |
86 | | - } |
87 | | - return 'project'; |
88 | | -} |
89 | | - |
90 | | -const mode = resolveMode(); |
91 | | - |
92 | | -const authSecret = process.env.AUTH_SECRET |
93 | | - ?? 'dev-secret-please-change-in-production-min-32-chars'; |
94 | | -const baseUrl = process.env.NEXT_PUBLIC_BASE_URL |
95 | | - ?? (process.env.VERCEL_PROJECT_PRODUCTION_URL |
96 | | - ? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}` |
97 | | - : undefined) |
98 | | - ?? (process.env.VERCEL_URL |
99 | | - ? `https://${process.env.VERCEL_URL}` |
100 | | - : undefined) |
101 | | - ?? `http://localhost:${process.env.PORT ?? 3000}`; |
| 31 | +import { templateRegistry } from './server/templates/registry.js'; |
102 | 32 |
|
103 | 33 | const serverDir = dirname(fileURLToPath(import.meta.url)); |
104 | | -const localProjectId = process.env.OBJECTSTACK_PROJECT_ID ?? 'proj_local'; |
| 34 | +const dataDir = resolvePath(serverDir, '.objectstack/data'); |
105 | 35 | const localArtifactPath = process.env.OBJECTSTACK_ARTIFACT_PATH |
106 | 36 | ?? resolvePath(serverDir, 'dist/objectstack.json'); |
107 | 37 |
|
108 | | -// ── PROJECT MODE ───────────────────────────────────────────────────────────── |
109 | | -// |
110 | | -// Reuses `createCloudStack()` with two local SQLite files. The frontend |
111 | | -// detects single-project mode via the `singleProject: true` payload from |
112 | | -// `single-project-plugin`; everything else (auth, control plane, |
113 | | -// kernel resolution, REST routing) follows the cloud code path. |
114 | | - |
115 | | -async function createProjectStack() { |
116 | | - const dataDir = resolvePath(serverDir, '.objectstack/data'); |
117 | | - mkdirSync(dataDir, { recursive: true }); |
118 | | - |
119 | | - const controlDbUrl = `file:${resolvePath(dataDir, 'control.db')}`; |
120 | | - const projectDbUrl = `file:${resolvePath(dataDir, 'proj_local.db')}`; |
121 | | - |
122 | | - const stack = await createCloudStack({ |
123 | | - authSecret, |
124 | | - baseUrl, |
125 | | - controlDriverUrl: controlDbUrl, |
| 38 | +const config = await createBootStack({ |
| 39 | + project: { |
| 40 | + dataDir, |
| 41 | + artifactPath: localArtifactPath, |
126 | 42 | appBundles: createFsAppBundleResolver(), |
127 | | - // Project-mode per-project plugins. The control plane (created by |
128 | | - // `createCloudStack`'s preset) is the sole owner of identity, |
129 | | - // authentication, security, audit, tenant catalogs, and packages — |
130 | | - // their tables live in `control.db`. Each per-project kernel only |
131 | | - // registers the engines needed to materialize that project's |
132 | | - // **business data** schemas + records. |
133 | | - basePlugins: async ({ projectId }: { projectId: string }) => { |
134 | | - const { ObjectQLPlugin } = await import('@objectstack/objectql'); |
135 | | - const { MetadataPlugin } = await import('@objectstack/metadata'); |
136 | | - const { AppPlugin } = await import('@objectstack/runtime'); |
137 | | - |
138 | | - let artifactBundle: any = null; |
139 | | - try { |
140 | | - const raw = await readFile(localArtifactPath, 'utf8'); |
141 | | - const parsed = JSON.parse(raw); |
142 | | - artifactBundle = (parsed?.schemaVersion != null && parsed?.metadata !== undefined) |
143 | | - ? parsed.metadata |
144 | | - : parsed; |
145 | | - } catch { |
146 | | - // First boot before `objectstack build` — AppPlugin skipped. |
147 | | - } |
148 | | - |
149 | | - const plugins: any[] = [ |
150 | | - new ObjectQLPlugin({ environmentId: projectId }), |
151 | | - new MetadataPlugin({ |
152 | | - watch: false, |
153 | | - environmentId: projectId, |
154 | | - artifactSource: { mode: 'local-file', path: localArtifactPath }, |
155 | | - }), |
156 | | - ]; |
157 | | - if (artifactBundle) plugins.push(new AppPlugin(artifactBundle)); |
158 | | - return plugins; |
159 | | - }, |
160 | | - }); |
161 | | - |
162 | | - // The cloud preset registers a `studio/runtime-config` route returning |
163 | | - // `{ singleProject: false }`. Hono is first-match-wins, so we drop that |
164 | | - // plugin and substitute our own which seeds local identity AND emits |
165 | | - // `{ singleProject: true, … }`. |
166 | | - const filtered = stack.plugins.filter( |
167 | | - (p: any) => p?.name !== 'com.objectstack.studio.runtime-config', |
168 | | - ); |
169 | | - filtered.push( |
170 | | - createSingleProjectPlugin({ |
171 | | - projectId: localProjectId, |
172 | | - projectDatabaseUrl: projectDbUrl, |
173 | | - projectDatabaseDriver: 'sqlite', |
174 | | - }), |
175 | | - ); |
176 | | - |
177 | | - return { |
178 | | - plugins: filtered, |
179 | | - api: stack.api, |
180 | | - }; |
181 | | -} |
182 | | - |
183 | | -// ── STANDALONE MODE ────────────────────────────────────────────────────────── |
184 | | -// |
185 | | -// Runtime-only: ObjectQL + Driver + REST. No Auth, no Studio, no control |
186 | | -// plane. Designed for embedding ObjectStack in other frameworks or |
187 | | -// internal back-end services. |
188 | | - |
189 | | -async function buildRuntimeOnlyConfig() { |
190 | | - const { ObjectQLPlugin } = await import('@objectstack/objectql'); |
191 | | - const { MetadataPlugin } = await import('@objectstack/metadata'); |
192 | | - const { DriverPlugin, AppPlugin } = await import('@objectstack/runtime'); |
193 | | - |
194 | | - const dbUrl = process.env.OBJECTSTACK_DATABASE_URL?.trim() |
195 | | - || process.env.TURSO_DATABASE_URL?.trim() |
196 | | - || `file:${resolvePath(serverDir, '.objectstack/data/standalone.db')}`; |
197 | | - const dbAuthToken = process.env.OBJECTSTACK_DATABASE_AUTH_TOKEN?.trim() |
198 | | - || process.env.TURSO_AUTH_TOKEN?.trim(); |
199 | | - const dbDriver = process.env.OBJECTSTACK_DATABASE_DRIVER?.trim() |
200 | | - || (/^(libsql|https?):\/\//i.test(dbUrl) ? 'turso' : 'sqlite'); |
201 | | - |
202 | | - let driverPlugin: any; |
203 | | - if (dbDriver === 'memory' || dbUrl.startsWith('memory://')) { |
204 | | - const { InMemoryDriver: MemoryDriver } = await import('@objectstack/driver-memory'); |
205 | | - driverPlugin = new DriverPlugin(new MemoryDriver()); |
206 | | - } else if (dbDriver === 'turso' || /^(libsql|https?):\/\//i.test(dbUrl)) { |
207 | | - const { TursoDriver } = await import('@objectstack/driver-turso'); |
208 | | - driverPlugin = new DriverPlugin( |
209 | | - new TursoDriver({ url: dbUrl, authToken: dbAuthToken }) as any, |
210 | | - ); |
211 | | - } else { |
212 | | - const { SqlDriver } = await import('@objectstack/driver-sql'); |
213 | | - const filename = dbUrl.replace(/^file:(\/\/)?/, ''); |
214 | | - mkdirSync(resolvePath(filename, '..'), { recursive: true }); |
215 | | - driverPlugin = new DriverPlugin( |
216 | | - new SqlDriver({ |
217 | | - client: 'better-sqlite3', |
218 | | - connection: { filename }, |
219 | | - useNullAsDefault: true, |
220 | | - }), |
221 | | - ); |
222 | | - } |
223 | | - |
224 | | - let artifactBundle: any = null; |
225 | | - try { |
226 | | - const raw = await readFile(localArtifactPath, 'utf8'); |
227 | | - const parsed = JSON.parse(raw); |
228 | | - artifactBundle = (parsed?.schemaVersion != null && parsed?.metadata !== undefined) |
229 | | - ? parsed.metadata |
230 | | - : parsed; |
231 | | - } catch { |
232 | | - // No artifact yet — AppPlugin skipped. |
233 | | - } |
234 | | - |
235 | | - const plugins: any[] = [ |
236 | | - driverPlugin, |
237 | | - new MetadataPlugin({ |
238 | | - watch: false, |
239 | | - environmentId: localProjectId, |
240 | | - artifactSource: { mode: 'local-file', path: localArtifactPath }, |
241 | | - }), |
242 | | - new ObjectQLPlugin({ environmentId: localProjectId }), |
243 | | - ]; |
244 | | - if (artifactBundle) plugins.push(new AppPlugin(artifactBundle)); |
245 | | - |
246 | | - return { |
247 | | - plugins, |
248 | | - api: { |
249 | | - enableProjectScoping: false, |
250 | | - projectResolution: 'none' as const, |
251 | | - }, |
252 | | - }; |
253 | | -} |
254 | | - |
255 | | -// ── Export ────────────────────────────────────────────────────────────────── |
256 | | - |
257 | | -const config = |
258 | | - mode === 'cloud' |
259 | | - ? await createCloudStack({ |
260 | | - authSecret, |
261 | | - baseUrl, |
262 | | - templates: templateRegistry, |
263 | | - appBundles: createFsAppBundleResolver(), |
264 | | - }) |
265 | | - : mode === 'standalone' |
266 | | - ? await buildRuntimeOnlyConfig() |
267 | | - : await createProjectStack(); |
| 43 | + }, |
| 44 | + cloud: { |
| 45 | + templates: templateRegistry, |
| 46 | + appBundles: createFsAppBundleResolver(), |
| 47 | + }, |
| 48 | + standalone: { |
| 49 | + artifactPath: localArtifactPath, |
| 50 | + databaseUrl: process.env.OBJECTSTACK_DATABASE_URL |
| 51 | + ?? `file:${resolvePath(dataDir, 'standalone.db')}`, |
| 52 | + }, |
| 53 | +}); |
268 | 54 |
|
269 | 55 | export default config; |
0 commit comments