11// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
22
3+ /**
4+ * Shared ObjectStack Server Configuration
5+ *
6+ * Single source of truth for all plugins — used by both:
7+ * - `objectstack serve` (local dev via CLI)
8+ * - `server/index.ts` (Vercel serverless deployment)
9+ */
10+
311import { defineStack } from '@objectstack/spec' ;
412import { AppPlugin , DriverPlugin } from '@objectstack/runtime' ;
513import { ObjectQLPlugin } from '@objectstack/objectql' ;
614import { InMemoryDriver } from '@objectstack/driver-memory' ;
15+ import { TursoDriver } from '@objectstack/driver-turso' ;
716import { AuthPlugin } from '@objectstack/plugin-auth' ;
17+ import { SecurityPlugin } from '@objectstack/plugin-security' ;
18+ import { AuditPlugin } from '@objectstack/plugin-audit' ;
19+ import { SetupPlugin } from '@objectstack/plugin-setup' ;
20+ import { FeedServicePlugin } from '@objectstack/service-feed' ;
21+ import { MetadataPlugin } from '@objectstack/metadata' ;
22+ import { AIServicePlugin } from '@objectstack/service-ai' ;
23+ import { AutomationServicePlugin } from '@objectstack/service-automation' ;
24+ import { AnalyticsServicePlugin } from '@objectstack/service-analytics' ;
825import CrmApp from '../../examples/app-crm/objectstack.config' ;
926import TodoApp from '../../examples/app-todo/objectstack.config' ;
1027import BiPluginManifest from '../../examples/plugin-bi/objectstack.config' ;
1128
12- // Production Server
13- // This project acts as a "Platform Server" that loads multiple apps and plugins.
14- // It effectively replaces the manual composition in `src/index.ts`.
29+ // Resolve base URL: explicit env > Vercel production URL > Vercel preview URL > localhost
30+ const baseUrl = process . env . NEXT_PUBLIC_BASE_URL
31+ ?? ( process . env . VERCEL_PROJECT_PRODUCTION_URL
32+ ? `https://${ process . env . VERCEL_PROJECT_PRODUCTION_URL } ` : undefined )
33+ ?? ( process . env . VERCEL_URL
34+ ? `https://${ process . env . VERCEL_URL } ` : undefined )
35+ ?? 'http://localhost:3000' ;
1536
16- // Shared authentication plugin — reads secrets from environment variables so the
17- // same config works both locally and on Vercel (where VERCEL_URL is injected).
18- const authPlugin = new AuthPlugin ( {
19- secret : process . env . AUTH_SECRET ?? 'dev-secret-please-change-in-production-min-32-chars' ,
20- baseUrl : process . env . NEXT_PUBLIC_BASE_URL ?? ( process . env . VERCEL_URL
21- ? `https://${ process . env . VERCEL_URL } `
22- : 'http://localhost:3000' ) ,
23- } ) ;
37+ // Turso persistent storage — enabled when env vars are configured
38+ const tursoUrl = process . env . TURSO_DATABASE_URL ;
39+ const tursoToken = process . env . TURSO_AUTH_TOKEN ;
40+ const useTurso = ! ! ( tursoUrl && tursoToken ) ;
41+
42+ const driverPlugins = useTurso
43+ ? [
44+ new DriverPlugin ( new InMemoryDriver ( ) , 'memory' ) ,
45+ new DriverPlugin ( new TursoDriver ( { url : tursoUrl ! , authToken : tursoToken ! } ) , 'turso' ) ,
46+ ]
47+ : [ new DriverPlugin ( new InMemoryDriver ( ) , 'memory' ) ] ;
2448
2549export default defineStack ( {
2650 manifest : {
@@ -31,18 +55,30 @@ export default defineStack({
3155 description : 'Production server aggregating CRM, Todo and BI plugins' ,
3256 type : 'app' ,
3357 } ,
34-
35- // Explicitly Load Plugins and Apps
36- // The Runtime CLI will iterate this list and call kernel.use()
3758 plugins : [
3859 new ObjectQLPlugin ( ) ,
39- // Register Default Driver (Memory)
40- new DriverPlugin ( new InMemoryDriver ( ) ) ,
41- // Authentication — required for production (Vercel) deployments
42- authPlugin ,
43- // Wrap Manifests/Stacks in AppPlugin adapter
60+ ...driverPlugins ,
4461 new AppPlugin ( CrmApp ) ,
4562 new AppPlugin ( TodoApp ) ,
46- new AppPlugin ( BiPluginManifest )
47- ]
63+ new AppPlugin ( BiPluginManifest ) ,
64+ new SetupPlugin ( ) ,
65+ new AuthPlugin ( {
66+ secret : process . env . AUTH_SECRET ?? 'dev-secret-please-change-in-production-min-32-chars' ,
67+ baseUrl,
68+ } ) ,
69+ new SecurityPlugin ( ) ,
70+ new AuditPlugin ( ) ,
71+ new FeedServicePlugin ( ) ,
72+ new MetadataPlugin ( { watch : ! process . env . VERCEL } ) ,
73+ new AIServicePlugin ( ) ,
74+ new AutomationServicePlugin ( ) ,
75+ new AnalyticsServicePlugin ( ) ,
76+ ] ,
77+ // When Turso is configured, route sys namespace to persistent storage
78+ ...( useTurso && {
79+ datasourceMapping : [
80+ { namespace : 'sys' , datasource : 'turso' } ,
81+ { default : true , datasource : 'memory' } ,
82+ ] ,
83+ } ) ,
4884} ) ;
0 commit comments