@@ -262,59 +262,85 @@ Convert introspected schema to ObjectQL object configurations.
262262
263263## Complete Example
264264
265- Here's a complete working example combining all features :
265+ Here's a complete working example with error handling and best practices :
266266
267267``` typescript
268268import { ObjectQL } from ' @objectql/core' ;
269269import { SqlDriver } from ' @objectql/driver-sql' ;
270270import * as fs from ' fs' ;
271+ import * as path from ' path' ;
271272import * as yaml from ' js-yaml' ;
272273
273274async function connectExistingDatabase() {
274- // Create driver for existing database
275- const driver = new SqlDriver ({
276- client: ' postgresql' ,
277- connection: {
278- host: ' localhost' ,
279- database: ' my_existing_db' ,
280- user: ' username' ,
281- password: ' password'
275+ try {
276+ // Create driver for existing database
277+ const driver = new SqlDriver ({
278+ client: ' postgresql' ,
279+ connection: {
280+ host: process .env .DB_HOST || ' localhost' ,
281+ database: process .env .DB_NAME || ' my_existing_db' ,
282+ user: process .env .DB_USER || ' username' ,
283+ password: process .env .DB_PASSWORD || ' password'
284+ }
285+ });
286+
287+ // Initialize ObjectQL
288+ const app = new ObjectQL ({
289+ datasources: { default: driver }
290+ });
291+
292+ console .log (' Connecting to database...' );
293+
294+ // Introspect and register all tables
295+ const objects = await app .introspectAndRegister (' default' , {
296+ excludeTables: [' migrations' , ' schema_version' ]
297+ });
298+
299+ console .log (` ✅ Discovered ${objects .length } tables ` );
300+
301+ // Initialize and use
302+ await app .init ();
303+ const ctx = app .createContext ({ isSystem: true });
304+
305+ // Query existing data
306+ const users = await ctx .object (' users' ).find ({
307+ top: 10
308+ });
309+
310+ console .log (` Found ${users .length } users ` );
311+
312+ // Optionally export metadata to files
313+ const outputDir = ' src/objects' ;
314+
315+ // Ensure directory exists
316+ if (! fs .existsSync (outputDir )) {
317+ fs .mkdirSync (outputDir , { recursive: true });
282318 }
283- });
284-
285- // Initialize ObjectQL
286- const app = new ObjectQL ({
287- datasources: { default: driver }
288- });
289-
290- // Introspect and register all tables
291- const objects = await app .introspectAndRegister (' default' , {
292- excludeTables: [' migrations' , ' schema_version' ]
293- });
294-
295- console .log (` Discovered ${objects .length } tables ` );
296-
297- // Initialize and use
298- await app .init ();
299- const ctx = app .createContext ({ isSystem: true });
300-
301- // Query existing data
302- const users = await ctx .object (' users' ).find ({
303- top: 10
304- });
305-
306- console .log (' Users:' , users );
307-
308- // Optionally export metadata to files
309- objects .forEach (obj => {
310- fs .writeFileSync (
311- ` src/objects/${obj .name }.object.yml ` ,
312- yaml .dump (obj )
313- );
314- });
319+
320+ objects .forEach (obj => {
321+ const filePath = path .join (outputDir , ` ${obj .name }.object.yml ` );
322+ try {
323+ fs .writeFileSync (filePath , yaml .dump (obj ));
324+ console .log (` ✅ Exported ${obj .name } to ${filePath } ` );
325+ } catch (err ) {
326+ console .error (` ❌ Failed to export ${obj .name }: ` , err .message );
327+ }
328+ });
329+
330+ console .log (' ✅ Database connection successful!' );
331+
332+ } catch (error ) {
333+ console .error (' ❌ Error connecting to database:' , error .message );
334+ if (error .code === ' ECONNREFUSED' ) {
335+ console .error (' Make sure the database server is running' );
336+ } else if (error .code === ' 28P01' ) {
337+ console .error (' Authentication failed - check username and password' );
338+ }
339+ process .exit (1 );
340+ }
315341}
316342
317- connectExistingDatabase (). catch ( console . error ) ;
343+ connectExistingDatabase ();
318344```
319345
320346## Chinese Documentation (中文文档)
0 commit comments