/** * ObjectQL * Copyright (c) 2026-present ObjectStack Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import { Driver, ObjectQLError } from '@objectql/types'; export function createDriverFromConnection(connection: string): Driver { let driverPackage = ''; let driverClass = ''; let driverConfig: any = {}; if (connection.startsWith('mongodb://')) { driverPackage = '@objectql/driver-mongo'; driverClass = 'MongoDriver'; driverConfig = { url: connection }; } else if (connection.startsWith('sqlite://')) { driverPackage = '@objectql/driver-sql'; driverClass = 'SqlDriver'; const filename = connection.replace('sqlite://', ''); driverConfig = { client: 'sqlite3', connection: { filename }, useNullAsDefault: true }; } else if (connection.startsWith('postgres://') || connection.startsWith('postgresql://')) { driverPackage = '@objectql/driver-sql'; driverClass = 'SqlDriver'; driverConfig = { client: 'pg', connection: connection }; } else if (connection.startsWith('mysql://')) { driverPackage = '@objectql/driver-sql'; driverClass = 'SqlDriver'; driverConfig = { client: 'mysql2', connection: connection }; } else { throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Unsupported connection protocol: ${connection}` }); } try { const pkg = require(driverPackage); const DriverClass = pkg[driverClass]; if (!DriverClass) { throw new ObjectQLError({ code: 'DRIVER_ERROR', message: `${driverClass} not found in ${driverPackage}` }); } return new DriverClass(driverConfig); } catch (e: any) { throw new ObjectQLError({ code: 'DRIVER_ERROR', message: `Failed to load driver ${driverPackage}. Please install it: npm install ${driverPackage}. Error: ${e.message}` }); } }