|
| 1 | +/** |
| 2 | + * ObjectQL |
| 3 | + * Copyright (c) 2026-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { Hono } from 'hono'; |
| 10 | +import { serve } from '@hono/node-server'; |
| 11 | +import { ObjectQL } from '@objectql/core'; |
| 12 | +import { SqlDriver } from '@objectql/driver-sql'; |
| 13 | +import { ObjectLoader } from '@objectql/platform-node'; |
| 14 | +import { createHonoAdapter } from '@objectql/plugin-server'; |
| 15 | +import * as path from 'path'; |
| 16 | + |
| 17 | +async function main() { |
| 18 | + // 1. Init ObjectQL |
| 19 | + const app = new ObjectQL({ |
| 20 | + datasources: { |
| 21 | + default: new SqlDriver({ |
| 22 | + client: 'sqlite3', |
| 23 | + connection: { |
| 24 | + filename: ':memory:' |
| 25 | + }, |
| 26 | + useNullAsDefault: true |
| 27 | + }) |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + // 2. Load Schema |
| 32 | + const rootDir = path.resolve(__dirname, '..'); |
| 33 | + const loader = new ObjectLoader(app.metadata); |
| 34 | + loader.load(rootDir); |
| 35 | + |
| 36 | + // 3. Init |
| 37 | + await app.init(); |
| 38 | + |
| 39 | + // 4. Create Hono server with ObjectQL adapter |
| 40 | + const server = new Hono(); |
| 41 | + const port = 3005; |
| 42 | + |
| 43 | + // Add CORS middleware |
| 44 | + server.use('*', async (c, next) => { |
| 45 | + c.header('Access-Control-Allow-Origin', '*'); |
| 46 | + c.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); |
| 47 | + c.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); |
| 48 | + |
| 49 | + if (c.req.method === 'OPTIONS') { |
| 50 | + return c.text('', 200); |
| 51 | + } |
| 52 | + |
| 53 | + await next(); |
| 54 | + }); |
| 55 | + |
| 56 | + // Mount ObjectQL handler |
| 57 | + const objectqlHandler = createHonoAdapter(app); |
| 58 | + server.all('/api/*', objectqlHandler); |
| 59 | + |
| 60 | + // Welcome page |
| 61 | + server.get('/', (c) => { |
| 62 | + return c.html(` |
| 63 | +<!DOCTYPE html> |
| 64 | +<html> |
| 65 | +<head> |
| 66 | + <title>ObjectQL Hono Server</title> |
| 67 | + <style> |
| 68 | + body { |
| 69 | + font-family: system-ui, -apple-system, sans-serif; |
| 70 | + max-width: 800px; |
| 71 | + margin: 50px auto; |
| 72 | + padding: 20px; |
| 73 | + line-height: 1.6; |
| 74 | + } |
| 75 | + h1 { color: #333; } |
| 76 | + .endpoint { |
| 77 | + background: #f5f5f5; |
| 78 | + padding: 10px; |
| 79 | + margin: 10px 0; |
| 80 | + border-left: 3px solid #007acc; |
| 81 | + } |
| 82 | + code { |
| 83 | + background: #e8e8e8; |
| 84 | + padding: 2px 6px; |
| 85 | + border-radius: 3px; |
| 86 | + } |
| 87 | + </style> |
| 88 | +</head> |
| 89 | +<body> |
| 90 | + <h1>🚀 ObjectQL Hono Server</h1> |
| 91 | + <p>Welcome to the ObjectQL Hono integration example!</p> |
| 92 | + |
| 93 | + <h2>Available APIs</h2> |
| 94 | + <div class="endpoint"> |
| 95 | + <strong>JSON-RPC:</strong> <code>POST /api/objectql</code><br> |
| 96 | + Example: <code>{"op": "find", "object": "User", "args": {}}</code> |
| 97 | + </div> |
| 98 | + <div class="endpoint"> |
| 99 | + <strong>REST:</strong> <code>GET /api/data/:object</code><br> |
| 100 | + Example: <code>GET /api/data/User</code> |
| 101 | + </div> |
| 102 | + <div class="endpoint"> |
| 103 | + <strong>Metadata:</strong> <code>GET /api/metadata/object</code><br> |
| 104 | + Get schema information |
| 105 | + </div> |
| 106 | + |
| 107 | + <h2>Test Commands</h2> |
| 108 | + <pre><code>curl -X POST http://localhost:${port}/api/objectql \\ |
| 109 | + -H "Content-Type: application/json" \\ |
| 110 | + -d '{"op": "find", "object": "User", "args": {}}' |
| 111 | +
|
| 112 | +curl http://localhost:${port}/api/data/User |
| 113 | +
|
| 114 | +curl http://localhost:${port}/api/metadata/object</code></pre> |
| 115 | +</body> |
| 116 | +</html> |
| 117 | + `); |
| 118 | + }); |
| 119 | + |
| 120 | + // Create some sample data |
| 121 | + const ctx = app.createContext({ isSystem: true }); |
| 122 | + await ctx.object('User').create({ |
| 123 | + name: 'Alice', |
| 124 | + email: 'alice@example.com', |
| 125 | + age: 28, |
| 126 | + status: 'active' |
| 127 | + }); |
| 128 | + await ctx.object('User').create({ |
| 129 | + name: 'Bob', |
| 130 | + email: 'bob@example.com', |
| 131 | + age: 35, |
| 132 | + status: 'active' |
| 133 | + }); |
| 134 | + await ctx.object('User').create({ |
| 135 | + name: 'Charlie', |
| 136 | + email: 'charlie@example.com', |
| 137 | + age: 42, |
| 138 | + status: 'inactive' |
| 139 | + }); |
| 140 | + |
| 141 | + await ctx.object('Task').create({ |
| 142 | + title: 'Complete project', |
| 143 | + description: 'Finish the ObjectQL console', |
| 144 | + status: 'in-progress', |
| 145 | + priority: 'high' |
| 146 | + }); |
| 147 | + await ctx.object('Task').create({ |
| 148 | + title: 'Write documentation', |
| 149 | + description: 'Document the new console feature', |
| 150 | + status: 'pending', |
| 151 | + priority: 'medium' |
| 152 | + }); |
| 153 | + await ctx.object('Task').create({ |
| 154 | + title: 'Code review', |
| 155 | + description: 'Review pull requests', |
| 156 | + status: 'pending', |
| 157 | + priority: 'low' |
| 158 | + }); |
| 159 | + |
| 160 | + // Start Hono server |
| 161 | + console.log(`\n🚀 ObjectQL Hono Server running on http://localhost:${port}`); |
| 162 | + console.log(`\n🔌 APIs:`); |
| 163 | + console.log(` - JSON-RPC: http://localhost:${port}/api/objectql`); |
| 164 | + console.log(` - REST: http://localhost:${port}/api/data`); |
| 165 | + console.log(` - Metadata: http://localhost:${port}/api/metadata`); |
| 166 | + console.log(` - Web UI: http://localhost:${port}/`); |
| 167 | + |
| 168 | + serve({ |
| 169 | + fetch: server.fetch, |
| 170 | + port |
| 171 | + }); |
| 172 | +} |
| 173 | + |
| 174 | +main().catch(console.error); |
0 commit comments