Skip to content

Commit 8a698ae

Browse files
committed
Add 'db' export support and alias in REPL command
Updated the REPL CLI to recognize 'db' as a valid export from config files, in addition to 'app' and 'objectql'. Also added 'db' as an alias in the REPL context for easier access, and updated example config to use 'db' instead of 'app'.
1 parent f4486f7 commit 8a698ae

4 files changed

Lines changed: 84 additions & 23 deletions

File tree

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
import { ObjectQL, MetadataLoader } from '@objectql/core';
2-
import { KnexDriver } from '@objectql/driver-knex';
1+
import { ObjectQL } from '@objectql/core';
32
import * as path from 'path';
43

5-
const app = new ObjectQL({
6-
datasources: {
7-
default: new KnexDriver({
8-
client: 'sqlite3',
9-
connection: {
10-
filename: path.join(__dirname, 'dev.sqlite3')
11-
},
12-
useNullAsDefault: true
13-
})
14-
}
4+
const db = new ObjectQL({
5+
connection: `sqlite://${path.join(__dirname, 'dev.sqlite3')}`,
6+
source: path.join(__dirname, 'src')
157
});
168

17-
// Load objects from src
18-
const loader = new MetadataLoader(app.metadata);
19-
loader.load(path.join(__dirname, 'src'));
20-
21-
export default app;
9+
export default db;

packages/cli/src/commands/repl.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ export async function startRepl(configPath?: string) {
3737

3838
try {
3939
const configModule = require(path.join(cwd, configFile));
40-
// Support default export or named export 'app' or 'objectql'
41-
const app = configModule.default || configModule.app || configModule.objectql;
40+
// Support default export or named export 'app' or 'objectql' or 'db'
41+
const app = configModule.default || configModule.app || configModule.objectql || configModule.db;
4242

4343
if (!(app instanceof ObjectQL)) {
44-
console.error("❌ The config file must export an instance of 'ObjectQL' as default or 'app'.");
44+
console.error("❌ The config file must export an instance of 'ObjectQL' as default or 'app'/'db'.");
4545
process.exit(1);
4646
}
4747

@@ -75,6 +75,7 @@ export async function startRepl(configPath?: string) {
7575

7676
// 4. Inject Context
7777
r.context.app = app;
78+
r.context.db = app; // Alias for db
7879
r.context.object = (name: string) => app.getObject(name);
7980

8081
// Helper to get a repo quickly: tasks.find() instead of app.object('tasks').find()
@@ -108,6 +109,9 @@ export async function startRepl(configPath?: string) {
108109

109110
console.log(`\nAvailable Objects: ${objects.map((o: any) => o.name).join(', ')}`);
110111
console.log(`Usage: tasks.find() (Auto-await enabled)`);
112+
113+
// Fix for REPL sometimes not showing prompt immediately
114+
r.displayPrompt();
111115

112116
} catch (error) {
113117
console.error("Failed to load or start:", error);

packages/core/src/index.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ export class ObjectQL implements IObjectQL {
2626
constructor(config: ObjectQLConfig) {
2727
this.metadata = config.registry || new MetadataRegistry();
2828
this.loader = new MetadataLoader(this.metadata);
29-
this.datasources = config.datasources;
29+
this.datasources = config.datasources || {};
30+
31+
if (config.connection) {
32+
this.loadDriverFromConnection(config.connection);
33+
}
34+
35+
if (config.source) {
36+
this.loader.load(config.source);
37+
}
3038

3139
if (config.objects) {
3240
for (const [key, obj] of Object.entries(config.objects)) {
@@ -175,6 +183,59 @@ export class ObjectQL implements IObjectQL {
175183
}
176184
}
177185
}
186+
187+
private loadDriverFromConnection(connection: string) {
188+
let driverPackage = '';
189+
let driverClass = '';
190+
let driverConfig: any = {};
191+
192+
if (connection.startsWith('mongodb://')) {
193+
driverPackage = '@objectql/driver-mongo';
194+
driverClass = 'MongoDriver';
195+
driverConfig = { url: connection };
196+
}
197+
else if (connection.startsWith('sqlite://')) {
198+
driverPackage = '@objectql/driver-knex';
199+
driverClass = 'KnexDriver';
200+
const filename = connection.replace('sqlite://', '');
201+
driverConfig = {
202+
client: 'sqlite3',
203+
connection: { filename },
204+
useNullAsDefault: true
205+
};
206+
}
207+
else if (connection.startsWith('postgres://') || connection.startsWith('postgresql://')) {
208+
driverPackage = '@objectql/driver-knex';
209+
driverClass = 'KnexDriver';
210+
driverConfig = {
211+
client: 'pg',
212+
connection: connection
213+
};
214+
}
215+
else if (connection.startsWith('mysql://')) {
216+
driverPackage = '@objectql/driver-knex';
217+
driverClass = 'KnexDriver';
218+
driverConfig = {
219+
client: 'mysql2',
220+
connection: connection
221+
};
222+
}
223+
else {
224+
throw new Error(`Unsupported connection protocol: ${connection}`);
225+
}
226+
227+
try {
228+
// eslint-disable-next-line @typescript-eslint/no-var-requires
229+
const pkg = require(driverPackage);
230+
const DriverClass = pkg[driverClass];
231+
if (!DriverClass) {
232+
throw new Error(`${driverClass} not found in ${driverPackage}`);
233+
}
234+
this.datasources['default'] = new DriverClass(driverConfig);
235+
} catch (e: any) {
236+
throw new Error(`Failed to load driver ${driverPackage}. Please install it: npm install ${driverPackage}. Error: ${e.message}`);
237+
}
238+
}
178239
}
179240

180241
export * from './repository';

packages/types/src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@ export interface IObjectRepository {
2727
}
2828

2929
export interface ObjectQLConfig {
30-
3130
registry?: MetadataRegistry;
32-
datasources: Record<string, Driver>;
31+
datasources?: Record<string, Driver>;
32+
/**
33+
* Optional connection string for auto-configuration.
34+
* e.g. "sqlite://dev.db", "postgres://localhost/db", "mongodb://localhost/db"
35+
*/
36+
connection?: string;
37+
/**
38+
* Path to the directory containing schema files (*.object.yml).
39+
*/
40+
source?: string;
3341
objects?: Record<string, ObjectConfig>;
3442
packages?: string[];
3543
}

0 commit comments

Comments
 (0)