-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
89 lines (78 loc) · 3.64 KB
/
index.ts
File metadata and controls
89 lines (78 loc) · 3.64 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
/**
* 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 express from 'express';
import { ObjectQL } from '@objectql/core';
import { SqlDriver } from '@objectql/driver-sql';
import { ObjectLoader } from '@objectql/platform-node';
import { createNodeHandler, createMetadataHandler, createRESTHandler } from '@objectql/server';
import * as path from 'path';
async function main() {
// 1. Init ObjectQL
const app = new ObjectQL({
datasources: {
default: new SqlDriver({
client: 'sqlite3',
connection: {
filename: ':memory:'
},
useNullAsDefault: true
})
}
});
// 2. Load Schema
const rootDir = path.resolve(__dirname, '..');
const loader = new ObjectLoader(app.metadata);
loader.load(rootDir);
// 3. Init
app.init().then(async () => {
const objectQLHandler = createNodeHandler(app);
const restHandler = createRESTHandler(app);
const metadataHandler = createMetadataHandler(app);
// 4. Setup Express
const server = express();
const port = 3004;
// Enable CORS for development
server.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
// Mount handlers
server.all('/api/objectql*', objectQLHandler);
server.all('/api/data/*', restHandler);
server.all('/api/metadata*', metadataHandler);
// Create some sample data
const ctx = app.createContext({ isSystem: true });
await ctx.object('User').create({ name: 'Alice', email: 'alice@example.com', age: 28, status: 'active' });
await ctx.object('User').create({ name: 'Bob', email: 'bob@example.com', age: 35, status: 'active' });
await ctx.object('User').create({ name: 'Charlie', email: 'charlie@example.com', age: 42, status: 'inactive' });
await ctx.object('Task').create({ title: 'Complete project', description: 'Finish the ObjectQL console', status: 'in-progress', priority: 'high' });
await ctx.object('Task').create({ title: 'Write documentation', description: 'Document the new console feature', status: 'pending', priority: 'medium' });
await ctx.object('Task').create({ title: 'Code review', description: 'Review pull requests', status: 'pending', priority: 'low' });
await ctx.object('Task').create({ title: 'Deploy to production', description: 'Release version 1.0', status: 'pending', priority: 'high', completed: false });
server.listen(port, () => {
console.log(`\n🚀 ObjectQL Server running on http://localhost:${port}`);
console.log(`\n🔌 APIs:`);
console.log(` - JSON-RPC: http://localhost:${port}/api/objectql`);
console.log(` - REST: http://localhost:${port}/api/data`);
console.log(` - Metadata: http://localhost:${port}/api/metadata`);
console.log(`\nTest JSON-RPC:`);
console.log(`curl -X POST http://localhost:${port}/api/objectql -H "Content-Type: application/json" -d '{"op": "find", "object": "User", "args": {}}'`);
console.log(`\nTest REST API:`);
console.log(`curl http://localhost:${port}/api/data/User`);
console.log(`\nTest Metadata API:`);
console.log(`curl http://localhost:${port}/api/metadata/object`);
});
});
}
main().catch(console.error);