@@ -1425,77 +1425,77 @@ export const connectToLibSQL = async (credentials: LibSQLCredentials): Promise<
14251425 transactionProxy : TransactionProxy ;
14261426 }
14271427> => {
1428- if ( await checkPackage ( '@libsql/client' ) ) {
1429- const { createClient } = await import ( '@libsql/client' ) ;
1430- const { drizzle } = await import ( 'drizzle-orm/libsql' ) ;
1431- const { migrate } = await import ( 'drizzle-orm/libsql/migrator' ) ;
1428+ if ( ! ( await checkPackage ( '@libsql/client' ) ) ) {
1429+ console . log (
1430+ "Please install '@libsql/client' for Drizzle Kit to connect to LibSQL databases" ,
1431+ ) ;
1432+ process . exit ( 1 ) ;
1433+ }
14321434
1433- const client = createClient ( {
1434- url : normaliseSQLiteUrl ( credentials . url , 'libsql' ) ,
1435- authToken : credentials . authToken ,
1436- } ) ;
1437- const drzl = drizzle ( { client } ) ;
1438- const migrateFn = async ( config : MigrationConfig ) => {
1439- return migrate ( drzl , config ) ;
1440- } ;
1435+ const { createClient } = await import ( '@libsql/client' ) ;
1436+ const { drizzle } = await import ( 'drizzle-orm/libsql' ) ;
1437+ const { migrate } = await import ( 'drizzle-orm/libsql/migrator' ) ;
14411438
1442- const db : LibSQLDB = {
1443- query : async < T > ( sql : string , params ?: any [ ] ) => {
1444- const res = await client . execute ( { sql, args : params || [ ] } ) . catch ( ( e ) => {
1445- throw new QueryError ( e , sql , params || [ ] ) ;
1446- } ) ;
1447- return res . rows as T [ ] ;
1448- } ,
1449- run : async ( query : string ) => {
1450- await client . execute ( query ) . catch ( ( e ) => {
1451- throw new QueryError ( e , query , [ ] ) ;
1452- } ) ;
1453- } ,
1454- batchWithPragma : async ( queries : string [ ] ) => {
1455- await client . migrate ( queries ) ;
1456- } ,
1457- } ;
1458-
1459- type Transaction = Awaited < ReturnType < typeof client . transaction > > ;
1439+ const client = createClient ( {
1440+ url : normaliseSQLiteUrl ( credentials . url , 'libsql' ) ,
1441+ authToken : credentials . authToken ,
1442+ } ) ;
1443+ const drzl = drizzle ( { client } ) ;
1444+ const migrateFn = async ( config : MigrationConfig ) => {
1445+ return migrate ( drzl , config ) ;
1446+ } ;
14601447
1461- const proxy = async ( params : ProxyParams ) => {
1462- const preparedParams = prepareSqliteParams ( params . params || [ ] ) ;
1463- const result = await client . execute ( {
1464- sql : params . sql ,
1465- args : preparedParams ,
1448+ const db : LibSQLDB = {
1449+ query : async < T > ( sql : string , params ?: any [ ] ) => {
1450+ const res = await client . execute ( { sql, args : params || [ ] } ) . catch ( ( e ) => {
1451+ throw new QueryError ( e , sql , params || [ ] ) ;
14661452 } ) ;
1453+ return res . rows as T [ ] ;
1454+ } ,
1455+ run : async ( query : string ) => {
1456+ await client . execute ( query ) . catch ( ( e ) => {
1457+ throw new QueryError ( e , query , [ ] ) ;
1458+ } ) ;
1459+ } ,
1460+ batchWithPragma : async ( queries : string [ ] ) => {
1461+ await client . migrate ( queries ) ;
1462+ } ,
1463+ } ;
14671464
1468- if ( params . mode === 'array' ) {
1469- return result . rows . map ( ( row ) => Object . values ( row ) ) ;
1470- } else {
1471- return result . rows ;
1472- }
1473- } ;
1465+ type Transaction = Awaited < ReturnType < typeof client . transaction > > ;
14741466
1475- const transactionProxy : TransactionProxy = async ( queries ) => {
1476- const results : ( any [ ] | Error ) [ ] = [ ] ;
1477- let transaction : Transaction | null = null ;
1478- try {
1479- transaction = await client . transaction ( ) ;
1480- for ( const query of queries ) {
1481- const result = await transaction . execute ( query . sql ) ;
1482- results . push ( result . rows ) ;
1483- }
1484- await transaction . commit ( ) ;
1485- } catch ( error ) {
1486- results . push ( error as Error ) ;
1487- await transaction ?. rollback ( ) ;
1488- } finally {
1489- transaction ?. close ( ) ;
1490- }
1491- return results ;
1492- } ;
1467+ const proxy = async ( params : ProxyParams ) => {
1468+ const preparedParams = prepareSqliteParams ( params . params || [ ] ) ;
1469+ const result = await client . execute ( {
1470+ sql : params . sql ,
1471+ args : preparedParams ,
1472+ } ) ;
14931473
1494- return { ...db , packageName : '@libsql/client' , proxy, transactionProxy, migrate : migrateFn } ;
1495- }
1474+ if ( params . mode === 'array' ) {
1475+ return result . rows . map ( ( row ) => Object . values ( row ) ) ;
1476+ } else {
1477+ return result . rows ;
1478+ }
1479+ } ;
14961480
1497- console . log (
1498- "Please install '@libsql/client' for Drizzle Kit to connect to LibSQL databases" ,
1499- ) ;
1500- process . exit ( 1 ) ;
1481+ const transactionProxy : TransactionProxy = async ( queries ) => {
1482+ const results : ( any [ ] | Error ) [ ] = [ ] ;
1483+ let transaction : Transaction | null = null ;
1484+ try {
1485+ transaction = await client . transaction ( ) ;
1486+ for ( const query of queries ) {
1487+ const result = await transaction . execute ( query . sql ) ;
1488+ results . push ( result . rows ) ;
1489+ }
1490+ await transaction . commit ( ) ;
1491+ } catch ( error ) {
1492+ results . push ( error as Error ) ;
1493+ await transaction ?. rollback ( ) ;
1494+ } finally {
1495+ transaction ?. close ( ) ;
1496+ }
1497+ return results ;
1498+ } ;
1499+
1500+ return { ...db , packageName : '@libsql/client' , proxy, transactionProxy, migrate : migrateFn } ;
15011501} ;
0 commit comments