|
1 | 1 | import { ObjectQL } from '@objectql/core'; |
2 | 2 | import { SqlDriver } from '@objectql/driver-sql'; |
3 | | -import { ObjectLoader } from '@objectql/platform-node'; |
| 3 | +import { ObjectLoader, loadModules } from '@objectql/platform-node'; |
4 | 4 | import { createNodeHandler } from '@objectql/server'; |
5 | 5 | import { createServer } from 'http'; |
6 | 6 | import * as path from 'path'; |
| 7 | +import * as fs from 'fs'; |
7 | 8 | import chalk from 'chalk'; |
8 | 9 |
|
9 | 10 | const CONSOLE_HTML = ` |
@@ -33,29 +34,89 @@ const CONSOLE_HTML = ` |
33 | 34 | </html> |
34 | 35 | `; |
35 | 36 |
|
36 | | -export async function serve(options: { port: number; dir: string }) { |
| 37 | +export async function serve(options: { |
| 38 | + port: number; |
| 39 | + dir: string; |
| 40 | + config?: string; |
| 41 | + modules?: string; |
| 42 | +}) { |
37 | 43 | console.log(chalk.blue('Starting ObjectQL Dev Server...')); |
38 | 44 |
|
39 | 45 | const rootDir = path.resolve(process.cwd(), options.dir); |
40 | 46 | console.log(chalk.gray(`Loading schema from: ${rootDir}`)); |
41 | 47 |
|
42 | | - // 1. Init ObjectQL with in-memory SQLite for Dev |
| 48 | + // Try to load configuration |
| 49 | + let config: any = null; |
| 50 | + const configPath = options.config || path.join(process.cwd(), 'objectql.config.ts'); |
| 51 | + |
| 52 | + if (fs.existsSync(configPath)) { |
| 53 | + try { |
| 54 | + console.log(chalk.gray(`Loading config from: ${configPath}`)); |
| 55 | + if (configPath.endsWith('.ts')) { |
| 56 | + require('ts-node/register'); |
| 57 | + } |
| 58 | + const loaded = require(configPath); |
| 59 | + config = loaded.default || loaded; |
| 60 | + } catch (e: any) { |
| 61 | + console.warn(chalk.yellow(`⚠️ Failed to load config: ${e.message}`)); |
| 62 | + } |
| 63 | + } else if (options.config) { |
| 64 | + console.error(chalk.red(`❌ Config file not found: ${options.config}`)); |
| 65 | + process.exit(1); |
| 66 | + } |
| 67 | + |
| 68 | + // Process modules override |
| 69 | + if (options.modules) { |
| 70 | + const moduleList = options.modules.split(',').map(p => p.trim()); |
| 71 | + if (!config) config = {}; |
| 72 | + config.modules = moduleList; |
| 73 | + console.log(chalk.yellow(`⚠️ Overriding modules: ${moduleList.join(', ')}`)); |
| 74 | + } |
| 75 | + |
| 76 | + const loadedConfig = config?.datasource?.default || config?.datasources?.default; |
| 77 | + let datasourceValue = loadedConfig; |
| 78 | + |
| 79 | + // Normalize config if it uses simplified format (type: 'sqlite') |
| 80 | + if (loadedConfig && !loadedConfig.client && loadedConfig.type === 'sqlite') { |
| 81 | + datasourceValue = { |
| 82 | + client: 'sqlite3', |
| 83 | + connection: { |
| 84 | + filename: loadedConfig.filename || ':memory:' |
| 85 | + }, |
| 86 | + useNullAsDefault: true |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + const datasource = datasourceValue || { |
| 91 | + client: 'sqlite3', |
| 92 | + connection: { |
| 93 | + filename: ':memory:' |
| 94 | + }, |
| 95 | + useNullAsDefault: true |
| 96 | + }; |
| 97 | + |
| 98 | + // 1. Init ObjectQL |
43 | 99 | const app = new ObjectQL({ |
44 | 100 | datasources: { |
45 | | - default: new SqlDriver({ |
46 | | - client: 'sqlite3', |
47 | | - connection: { |
48 | | - filename: ':memory:' // Or local file './dev.db' |
49 | | - }, |
50 | | - useNullAsDefault: true |
51 | | - }) |
52 | | - } |
| 101 | + default: new SqlDriver(datasource) |
| 102 | + }, |
| 103 | + ...config |
53 | 104 | }); |
54 | 105 |
|
55 | 106 | // 2. Load Schema |
56 | 107 | try { |
57 | 108 | const loader = new ObjectLoader(app.metadata); |
| 109 | + |
| 110 | + // Load modules first (if any) |
| 111 | + // Backwards compatibility for presets |
| 112 | + const modulesToLoad = config?.modules || config?.presets; |
| 113 | + if (modulesToLoad) { |
| 114 | + await loadModules(loader, modulesToLoad); |
| 115 | + } |
| 116 | + |
| 117 | + // Load project source |
58 | 118 | loader.load(rootDir); |
| 119 | + |
59 | 120 | await app.init(); |
60 | 121 | console.log(chalk.green('✅ Schema loaded successfully.')); |
61 | 122 | } catch (e: any) { |
|
0 commit comments