|
13 | 13 | import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime'; |
14 | 14 | import { ObjectQLPlugin } from '@objectstack/objectql'; |
15 | 15 | import { TursoDriver } from '@objectstack/driver-turso'; |
| 16 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
16 | 17 | import { createHonoApp } from '@objectstack/hono'; |
17 | 18 | import { AuthPlugin } from '@objectstack/plugin-auth'; |
18 | 19 | import { SecurityPlugin } from '@objectstack/plugin-security'; |
@@ -62,21 +63,40 @@ async function ensureKernel(): Promise<ObjectKernel> { |
62 | 63 | // Register ObjectQL engine |
63 | 64 | await kernel.use(new ObjectQLPlugin()); |
64 | 65 |
|
65 | | - // Database driver - Turso (remote mode for Vercel) |
| 66 | + // Register Memory Driver for example apps (volatile, fast) |
| 67 | + await kernel.use(new DriverPlugin(new InMemoryDriver(), { name: 'memory' })); |
| 68 | + |
| 69 | + // Register Turso Driver for system objects (persistent, production) |
66 | 70 | const tursoUrl = process.env.TURSO_DATABASE_URL; |
67 | 71 | const tursoToken = process.env.TURSO_AUTH_TOKEN; |
68 | 72 |
|
69 | 73 | if (!tursoUrl || !tursoToken) { |
70 | | - throw new Error('Missing required environment variables: TURSO_DATABASE_URL and TURSO_AUTH_TOKEN'); |
| 74 | + console.warn('[Vercel] Turso credentials not found, falling back to file-based SQLite'); |
71 | 75 | } |
72 | 76 |
|
73 | 77 | const tursoDriver = new TursoDriver({ |
74 | | - url: tursoUrl, |
| 78 | + url: tursoUrl ?? 'file:./data/server.db', |
75 | 79 | authToken: tursoToken, |
76 | 80 | // Remote mode - no local sync needed for Vercel |
77 | 81 | }); |
78 | 82 |
|
79 | | - await kernel.use(new DriverPlugin(tursoDriver)); |
| 83 | + await kernel.use(new DriverPlugin(tursoDriver, { name: 'turso' })); |
| 84 | + |
| 85 | + // Configure datasource mapping |
| 86 | + // This must be done before loading apps, so ObjectQL can route objects correctly |
| 87 | + const ql = kernel.getService('ObjectQL'); |
| 88 | + if (ql && typeof ql.setDatasourceMapping === 'function') { |
| 89 | + ql.setDatasourceMapping([ |
| 90 | + // Example apps use in-memory driver for fast, ephemeral data |
| 91 | + { namespace: 'crm', datasource: 'memory' }, |
| 92 | + { namespace: 'todo', datasource: 'memory' }, |
| 93 | + { namespace: 'bi', datasource: 'memory' }, |
| 94 | + // System objects use Turso for persistent, production-grade storage |
| 95 | + { namespace: 'sys', datasource: 'turso' }, |
| 96 | + // Default fallback to memory driver |
| 97 | + { default: true, datasource: 'memory' }, |
| 98 | + ]); |
| 99 | + } |
80 | 100 |
|
81 | 101 | // Load app manifests (BEFORE plugins that need object schemas) |
82 | 102 | await kernel.use(new AppPlugin(CrmApp)); |
|
0 commit comments