-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobjectstack.config.ts
More file actions
151 lines (143 loc) · 5.99 KB
/
objectstack.config.ts
File metadata and controls
151 lines (143 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* ObjectQL
* Copyright (c) 2026-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { createRequire } from 'module';
import * as path from 'path';
import * as fs from 'fs';
// Load .env file into process.env (zero-dependency, Node-native approach)
const envPath = path.resolve(path.dirname(new URL(import.meta.url).pathname), '.env');
if (fs.existsSync(envPath)) {
for (const line of fs.readFileSync(envPath, 'utf-8').split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const eqIdx = trimmed.indexOf('=');
if (eqIdx === -1) continue;
const key = trimmed.slice(0, eqIdx).trim();
const val = trimmed.slice(eqIdx + 1).trim();
if (!process.env[key]) process.env[key] = val;
}
}
// Polyfill require and __dirname for ESM
if (typeof globalThis.require === 'undefined') {
const require = createRequire(import.meta.url);
(globalThis as any).require = require;
}
if (typeof globalThis.__dirname === 'undefined') {
(globalThis as any).__dirname = path.dirname(new URL(import.meta.url).pathname);
}
import { ObjectQLSecurityPlugin } from '@objectql/plugin-security';
import { GraphQLPlugin } from '@objectql/protocol-graphql';
import { ODataV4Plugin } from '@objectql/protocol-odata-v4';
import { JSONRPCPlugin } from '@objectql/protocol-json-rpc';
import { HonoServerPlugin } from '@objectstack/plugin-hono-server';
import { AuthPlugin } from '@objectstack/plugin-auth';
import { ConsolePlugin } from '@object-ui/console';
import { ObjectQLPlugin } from '@objectstack/objectql';
import { QueryPlugin } from '@objectql/plugin-query';
import { ValidatorPlugin } from '@objectql/plugin-validator';
import { FormulaPlugin } from '@objectql/plugin-formula';
import { createApiRegistryPlugin } from '@objectstack/core';
import { MemoryDriver } from '@objectql/driver-memory';
import { createTursoDriver } from '@objectql/driver-turso';
import pkg from '@objectql/platform-node';
const { createAppPlugin } = pkg;
// Choose driver based on environment — Turso when TURSO_DATABASE_URL is set,
// MemoryDriver otherwise (zero-config fallback for quick starts).
function createDefaultDriver() {
const tursoUrl = process.env.TURSO_DATABASE_URL;
if (tursoUrl) {
console.log(`🗄️ Driver: Turso (${tursoUrl})`);
const syncUrl = process.env.TURSO_SYNC_URL;
return createTursoDriver({
url: tursoUrl,
authToken: process.env.TURSO_AUTH_TOKEN,
syncUrl,
sync: syncUrl
? {
intervalSeconds: Number(process.env.TURSO_SYNC_INTERVAL) || 60,
onConnect: true,
}
: undefined,
});
}
console.log('🗄️ Driver: Memory (in-memory, non-persistent)');
return new MemoryDriver();
}
// Shared driver instance — registered as 'driver.default' service for
// upstream ObjectQLPlugin discovery and passed to QueryPlugin for query execution.
const defaultDriver = createDefaultDriver();
// App plugins: each business module is loaded via createAppPlugin.
// ObjectLoader recursively scans for *.object.yml, *.view.yml, *.permission.yml, etc.
// The assembled manifest is registered as an `app.<id>` service.
// Upstream ObjectQLPlugin auto-discovers all `app.*` services during start().
const projectTrackerPlugin = createAppPlugin({
id: 'project-tracker',
dir: path.join(__dirname, 'examples/showcase/project-tracker/src'),
label: 'Project Tracker',
description: 'A showcase of ObjectQL capabilities including all field types.',
});
export default {
metadata: {
name: 'objectos',
version: '1.0.0'
},
// Runtime plugins (instances only)
// No manual `objects:` field — metadata is auto-loaded via AppPlugin.
plugins: [
createApiRegistryPlugin(),
new HonoServerPlugin({}),
new ConsolePlugin(),
// Register the active driver as 'driver.default' service so upstream
// ObjectQLPlugin can discover it during start() phase.
{
name: 'driver-default',
init: async (ctx: any) => {
ctx.registerService('driver.default', defaultDriver);
},
start: async () => {
// Connect Turso driver if applicable (MemoryDriver has no connect method)
if ('connect' in defaultDriver && typeof (defaultDriver as { connect: () => Promise<void> }).connect === 'function') {
await (defaultDriver as { connect: () => Promise<void> }).connect();
}
},
},
// App plugins: register app metadata as `app.*` services.
// Must be before ObjectQLPlugin so services are available during start().
projectTrackerPlugin,
// Upstream ObjectQLPlugin from @objectstack/objectql:
// - Registers objectql, metadata, data, protocol services
// - Discovers driver.* and app.* services and calls ql.registerApp()
// - Registers audit hooks (created_by/updated_by) and tenant isolation middleware
new ObjectQLPlugin(),
new QueryPlugin({ datasources: { default: defaultDriver } }),
new ValidatorPlugin(),
new FormulaPlugin(),
new ObjectQLSecurityPlugin({
enableAudit: false
}),
new AuthPlugin({
secret: process.env.AUTH_SECRET || 'objectql-dev-secret-change-me-in-production',
trustedOrigins: ['http://localhost:*'],
}),
new GraphQLPlugin({
basePath: '/graphql',
introspection: true,
enableSubscriptions: true
}),
new ODataV4Plugin({
basePath: '/odata',
enableBatch: true,
enableSearch: true,
enableETags: true
}),
new JSONRPCPlugin({
basePath: '/rpc',
enableIntrospection: true,
enableSessions: true
})
]
};