11import express from 'express' ;
22import { ObjectQL } from '@objectql/core' ;
33import { KnexDriver } from '@objectql/driver-knex' ;
4- import { createNodeHandler } from '@objectql/server' ;
4+ import { createNodeHandler , createMetadataHandler , createConsoleHandler } from '@objectql/server' ;
55import * as path from 'path' ;
66
77async function main ( ) {
@@ -22,24 +22,49 @@ async function main() {
2222 app . loadFromDirectory ( path . join ( __dirname ) ) ;
2323 await app . init ( ) ;
2424
25- // 3. Create Handler
25+ // 3. Create Handlers
2626 const objectQLHandler = createNodeHandler ( app ) ;
27+ const metadataHandler = createMetadataHandler ( app ) ;
28+ const consoleHandler = createConsoleHandler ( ) ;
2729
2830 // 4. Setup Express
2931 const server = express ( ) ;
3032 const port = 3004 ;
3133
32- // Optional: Parse JSON body globally (or strict to other routes)
33- // server.use(express.json());
34+ // Enable CORS for development
35+ server . use ( ( req , res , next ) => {
36+ res . header ( 'Access-Control-Allow-Origin' , '*' ) ;
37+ res . header ( 'Access-Control-Allow-Methods' , 'GET, POST, OPTIONS' ) ;
38+ res . header ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
39+ if ( req . method === 'OPTIONS' ) {
40+ res . sendStatus ( 200 ) ;
41+ } else {
42+ next ( ) ;
43+ }
44+ } ) ;
3445
35- // Mount ObjectQL handler
36- // Supports both /api/objectql (POST) or similar
46+ // Mount handlers
3747 server . all ( '/api/objectql' , objectQLHandler ) ;
48+ server . all ( '/api/metadata*' , metadataHandler ) ;
49+ server . get ( '/console*' , consoleHandler ) ;
50+
51+ // Create some sample data
52+ const ctx = app . createContext ( { isSystem : true } ) ;
53+ await ctx . object ( 'User' ) . create ( { name : 'Alice' , email : 'alice@example.com' , age : 28 , status : 'active' } ) ;
54+ await ctx . object ( 'User' ) . create ( { name : 'Bob' , email : 'bob@example.com' , age : 35 , status : 'active' } ) ;
55+ await ctx . object ( 'User' ) . create ( { name : 'Charlie' , email : 'charlie@example.com' , age : 42 , status : 'inactive' } ) ;
56+
57+ await ctx . object ( 'Task' ) . create ( { title : 'Complete project' , description : 'Finish the ObjectQL console' , status : 'in-progress' , priority : 'high' } ) ;
58+ await ctx . object ( 'Task' ) . create ( { title : 'Write documentation' , description : 'Document the new console feature' , status : 'pending' , priority : 'medium' } ) ;
59+ await ctx . object ( 'Task' ) . create ( { title : 'Code review' , description : 'Review pull requests' , status : 'pending' , priority : 'low' } ) ;
60+ await ctx . object ( 'Task' ) . create ( { title : 'Deploy to production' , description : 'Release version 1.0' , status : 'pending' , priority : 'high' , completed : false } ) ;
3861
3962 server . listen ( port , ( ) => {
40- console . log ( `Express app listening on port ${ port } ` ) ;
41- console . log ( `Test CURL:` ) ;
42- console . log ( `curl -X POST http://localhost:${ port } /api/objectql -H "Content-Type: application/json" -d '{"op": "create", "object": "User", "args": { "data": { "name": "ExpressUser", "email": "express@example.com" }}}'` ) ;
63+ console . log ( `\n🚀 ObjectQL Server running on http://localhost:${ port } ` ) ;
64+ console . log ( `\n📊 Console UI: http://localhost:${ port } /console` ) ;
65+ console . log ( `\n🔌 API Endpoint: http://localhost:${ port } /api/objectql` ) ;
66+ console . log ( `\nTest CURL:` ) ;
67+ console . log ( `curl -X POST http://localhost:${ port } /api/objectql -H "Content-Type: application/json" -d '{"op": "find", "object": "User", "args": {}}'` ) ;
4368 } ) ;
4469}
4570
0 commit comments