@pineliner/odb-client 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,10 +35,11 @@ export declare class DatabaseManager {
35
35
  * Create a new database
36
36
  * @param name - Database name (becomes filename or ODB-Lite database name)
37
37
  * @param options - Optional creation options
38
+ * @returns Connection object (contains databaseHash for ODB-Lite)
38
39
  */
39
40
  createDatabase(name: string, options?: {
40
41
  schemaContent?: string;
41
- }): Promise<void>;
42
+ }): Promise<Connection>;
42
43
  /**
43
44
  * Check if a database exists
44
45
  * @param name - Database name
@@ -1 +1 @@
1
- {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../src/database/manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,qBAAqB,EAErB,UAAU,EAIX,MAAM,SAAS,CAAA;AAOhB;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,WAAW,CAAqC;IAGxD,OAAO,CAAC,oBAAoB,CAAiC;IAC7D,OAAO,CAAC,cAAc,CAAQ;gBAElB,MAAM,EAAE,qBAAqB;IA6BzC;;;;OAIG;IACG,cAAc,CAClB,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAA;KAAE,GACnC,OAAO,CAAC,IAAI,CAAC;IA0BhB;;;;;;;OAOG;IACG,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA6BpD;;;;;;OAMG;IACG,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;IAkCtD;;;OAGG;IACG,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAiBjD;;;;;OAKG;IACG,cAAc,CAAC,IAAI,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MAAM,EAAE,CAAC;IAIxC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAa5B;;OAEG;YACW,aAAa;IAoC3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAyBzB;;OAEG;YACW,QAAQ;CAqBvB"}
@@ -19,14 +19,18 @@ export interface QueryResult<T = any> {
19
19
  * All backends implement this through adapters
20
20
  */
21
21
  export interface Connection {
22
- query<T = any>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>>;
22
+ sql<T = any>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>>;
23
+ query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>;
23
24
  execute(sql: string | {
24
25
  sql: string;
25
26
  args?: any[];
26
27
  }, params?: any[]): Promise<QueryResult>;
27
28
  prepare(sql: string): PreparedStatement;
28
29
  transaction<T>(fn: (tx: Connection) => Promise<T>): Promise<T>;
30
+ begin<T>(fn: (tx: Connection) => Promise<T>): Promise<T>;
29
31
  close(): Promise<void>;
32
+ databaseHash?: string;
33
+ databaseName?: string;
30
34
  }
31
35
  /**
32
36
  * Prepared statement interface
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/database/types.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,MAAM,WAAW,GACnB,WAAW,GACX,cAAc,GACd,WAAW,GACX,WAAW,CAAA;AAEf;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,QAAQ,GAAG,SAAS,CAAA;AAE7D;;GAEG;AACH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,GAAG;IAClC,IAAI,EAAE,CAAC,EAAE,CAAA;IACT,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,eAAe,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CAClC;AAED;;;GAGG;AACH,MAAM,WAAW,UAAU;IAEzB,KAAK,CAAC,CAAC,GAAG,GAAG,EAAE,OAAO,EAAE,oBA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EAAE,MAAM,CAAA;IACX,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,SAAS,EAAE,IAAI,CAAA;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC/B"}
package/dist/index.cjs CHANGED
@@ -1120,26 +1120,41 @@ const external_bun_sqlite_namespaceObject = require("bun:sqlite");
1120
1120
  }
1121
1121
  /**
1122
1122
  * Template tag query (postgres.js-like)
1123
- */ async query(strings, ...values) {
1123
+ */ async sql(strings, ...values) {
1124
1124
  // Build SQL from template
1125
- const sql = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1126
- return this.execute(sql, values);
1125
+ const sqlStr = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1126
+ return this.execute(sqlStr, values);
1127
+ }
1128
+ /**
1129
+ * Query with SQL string and parameters (alias for execute)
1130
+ */ async query(sql, params = []) {
1131
+ return this.execute(sql, params);
1127
1132
  }
1128
1133
  /**
1129
1134
  * Execute SQL with parameters
1130
1135
  */ async execute(sql, params = []) {
1131
1136
  try {
1132
- const stmt = this.db.query(sql);
1133
- const isSelect = sql.trim().toUpperCase().startsWith('SELECT');
1137
+ // Handle object format { sql, args }
1138
+ let sqlStr;
1139
+ let sqlParams;
1140
+ if ('object' == typeof sql) {
1141
+ sqlStr = sql.sql;
1142
+ sqlParams = sql.args || [];
1143
+ } else {
1144
+ sqlStr = sql;
1145
+ sqlParams = params;
1146
+ }
1147
+ const stmt = this.db.query(sqlStr);
1148
+ const isSelect = sqlStr.trim().toUpperCase().startsWith('SELECT') || sqlStr.trim().toUpperCase().includes('RETURNING');
1134
1149
  if (isSelect) {
1135
- const rows = stmt.all(...params);
1150
+ const rows = stmt.all(...sqlParams);
1136
1151
  return {
1137
1152
  rows: rows,
1138
1153
  rowsAffected: 0
1139
1154
  };
1140
1155
  }
1141
1156
  {
1142
- const result = stmt.run(...params);
1157
+ const result = stmt.run(...sqlParams);
1143
1158
  return {
1144
1159
  rows: [],
1145
1160
  rowsAffected: result.changes || 0,
@@ -1196,6 +1211,11 @@ const external_bun_sqlite_namespaceObject = require("bun:sqlite");
1196
1211
  }
1197
1212
  }
1198
1213
  /**
1214
+ * Execute function in transaction (alias for transaction)
1215
+ */ async begin(fn) {
1216
+ return this.transaction(fn);
1217
+ }
1218
+ /**
1199
1219
  * Close connection
1200
1220
  */ async close() {
1201
1221
  this.db.close();
@@ -1246,10 +1266,15 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1246
1266
  }
1247
1267
  /**
1248
1268
  * Template tag query (postgres.js-like)
1249
- */ async query(strings, ...values) {
1269
+ */ async sql(strings, ...values) {
1250
1270
  // Build SQL from template
1251
- const sql = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1252
- return this.execute(sql, values);
1271
+ const sqlStr = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1272
+ return this.execute(sqlStr, values);
1273
+ }
1274
+ /**
1275
+ * Query with SQL string and parameters (alias for execute)
1276
+ */ async query(sql, params = []) {
1277
+ return this.execute(sql, params);
1253
1278
  }
1254
1279
  /**
1255
1280
  * Execute SQL with parameters
@@ -1308,6 +1333,11 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1308
1333
  }
1309
1334
  }
1310
1335
  /**
1336
+ * Execute function in transaction (alias for transaction)
1337
+ */ async begin(fn) {
1338
+ return this.transaction(fn);
1339
+ }
1340
+ /**
1311
1341
  * Close connection
1312
1342
  */ async close() {
1313
1343
  // LibSQL client.close() if available in future versions
@@ -1343,7 +1373,7 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1343
1373
  apiKey: this.config.apiKey,
1344
1374
  databaseId: db.hash
1345
1375
  });
1346
- return new ODBLiteConnection(client, this.serviceClient, databaseName);
1376
+ return new ODBLiteConnection(client, this.serviceClient, databaseName, db.hash);
1347
1377
  }
1348
1378
  async disconnect(tenantId) {
1349
1379
  if (tenantId) {
@@ -1371,16 +1401,19 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1371
1401
  */ class ODBLiteConnection {
1372
1402
  client;
1373
1403
  serviceClient;
1374
- databaseName;
1375
1404
  inTransaction = false;
1376
- constructor(client, serviceClient, databaseName){
1405
+ // Public metadata fields
1406
+ databaseName;
1407
+ databaseHash;
1408
+ constructor(client, serviceClient, databaseName, databaseHash){
1377
1409
  this.client = client;
1378
1410
  this.serviceClient = serviceClient;
1379
1411
  this.databaseName = databaseName;
1412
+ this.databaseHash = databaseHash;
1380
1413
  }
1381
1414
  /**
1382
1415
  * Template tag query (postgres.js-like)
1383
- */ async query(strings, ...values) {
1416
+ */ async sql(strings, ...values) {
1384
1417
  // ODBLiteClient.sql is a function that returns a Promise
1385
1418
  const result = await this.client.sql(strings, ...values);
1386
1419
  return {
@@ -1389,9 +1422,23 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1389
1422
  };
1390
1423
  }
1391
1424
  /**
1425
+ * Query with SQL string and parameters (alias for execute)
1426
+ */ async query(sql, params = []) {
1427
+ return this.execute(sql, params);
1428
+ }
1429
+ /**
1392
1430
  * Execute SQL with parameters
1393
1431
  */ async execute(sql, params = []) {
1394
1432
  try {
1433
+ // Handle object format { sql, args }
1434
+ if ('object' == typeof sql) {
1435
+ const result = await this.client.sql.execute(sql.sql, sql.args || []);
1436
+ return {
1437
+ rows: result.rows,
1438
+ rowsAffected: result.rowsAffected || 0
1439
+ };
1440
+ }
1441
+ // Handle string format
1395
1442
  const result = await this.client.sql.execute(sql, params);
1396
1443
  return {
1397
1444
  rows: result.rows,
@@ -1435,6 +1482,11 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
1435
1482
  }
1436
1483
  }
1437
1484
  /**
1485
+ * Execute function in transaction (alias for transaction)
1486
+ */ async begin(fn) {
1487
+ return this.transaction(fn);
1488
+ }
1489
+ /**
1438
1490
  * Close connection
1439
1491
  */ async close() {
1440
1492
  // ODBLiteClient connections are stateless HTTP requests
@@ -1593,6 +1645,7 @@ var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node
1593
1645
  * Create a new database
1594
1646
  * @param name - Database name (becomes filename or ODB-Lite database name)
1595
1647
  * @param options - Optional creation options
1648
+ * @returns Connection object (contains databaseHash for ODB-Lite)
1596
1649
  */ async createDatabase(name, options) {
1597
1650
  console.log(`📦 Creating database: ${name}`);
1598
1651
  // Build database-specific config
@@ -1609,6 +1662,8 @@ var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node
1609
1662
  // Evict LRU if pool is full
1610
1663
  if (this.connections.size > this.maxConnections) await this.evictLRU();
1611
1664
  console.log(`✅ Database created: ${name}`);
1665
+ // Return connection so caller can access metadata (e.g., databaseHash for ODB-Lite)
1666
+ return conn;
1612
1667
  }
1613
1668
  /**
1614
1669
  * Check if a database exists
package/dist/index.js CHANGED
@@ -1046,26 +1046,41 @@ ODBLiteClient.join;
1046
1046
  }
1047
1047
  /**
1048
1048
  * Template tag query (postgres.js-like)
1049
- */ async query(strings, ...values) {
1049
+ */ async sql(strings, ...values) {
1050
1050
  // Build SQL from template
1051
- const sql = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1052
- return this.execute(sql, values);
1051
+ const sqlStr = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1052
+ return this.execute(sqlStr, values);
1053
+ }
1054
+ /**
1055
+ * Query with SQL string and parameters (alias for execute)
1056
+ */ async query(sql, params = []) {
1057
+ return this.execute(sql, params);
1053
1058
  }
1054
1059
  /**
1055
1060
  * Execute SQL with parameters
1056
1061
  */ async execute(sql, params = []) {
1057
1062
  try {
1058
- const stmt = this.db.query(sql);
1059
- const isSelect = sql.trim().toUpperCase().startsWith('SELECT');
1063
+ // Handle object format { sql, args }
1064
+ let sqlStr;
1065
+ let sqlParams;
1066
+ if ('object' == typeof sql) {
1067
+ sqlStr = sql.sql;
1068
+ sqlParams = sql.args || [];
1069
+ } else {
1070
+ sqlStr = sql;
1071
+ sqlParams = params;
1072
+ }
1073
+ const stmt = this.db.query(sqlStr);
1074
+ const isSelect = sqlStr.trim().toUpperCase().startsWith('SELECT') || sqlStr.trim().toUpperCase().includes('RETURNING');
1060
1075
  if (isSelect) {
1061
- const rows = stmt.all(...params);
1076
+ const rows = stmt.all(...sqlParams);
1062
1077
  return {
1063
1078
  rows: rows,
1064
1079
  rowsAffected: 0
1065
1080
  };
1066
1081
  }
1067
1082
  {
1068
- const result = stmt.run(...params);
1083
+ const result = stmt.run(...sqlParams);
1069
1084
  return {
1070
1085
  rows: [],
1071
1086
  rowsAffected: result.changes || 0,
@@ -1122,6 +1137,11 @@ ODBLiteClient.join;
1122
1137
  }
1123
1138
  }
1124
1139
  /**
1140
+ * Execute function in transaction (alias for transaction)
1141
+ */ async begin(fn) {
1142
+ return this.transaction(fn);
1143
+ }
1144
+ /**
1125
1145
  * Close connection
1126
1146
  */ async close() {
1127
1147
  this.db.close();
@@ -1171,10 +1191,15 @@ ODBLiteClient.join;
1171
1191
  }
1172
1192
  /**
1173
1193
  * Template tag query (postgres.js-like)
1174
- */ async query(strings, ...values) {
1194
+ */ async sql(strings, ...values) {
1175
1195
  // Build SQL from template
1176
- const sql = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1177
- return this.execute(sql, values);
1196
+ const sqlStr = strings.reduce((acc, str, i)=>acc + str + (i < values.length ? '?' : ''), '');
1197
+ return this.execute(sqlStr, values);
1198
+ }
1199
+ /**
1200
+ * Query with SQL string and parameters (alias for execute)
1201
+ */ async query(sql, params = []) {
1202
+ return this.execute(sql, params);
1178
1203
  }
1179
1204
  /**
1180
1205
  * Execute SQL with parameters
@@ -1233,6 +1258,11 @@ ODBLiteClient.join;
1233
1258
  }
1234
1259
  }
1235
1260
  /**
1261
+ * Execute function in transaction (alias for transaction)
1262
+ */ async begin(fn) {
1263
+ return this.transaction(fn);
1264
+ }
1265
+ /**
1236
1266
  * Close connection
1237
1267
  */ async close() {
1238
1268
  // LibSQL client.close() if available in future versions
@@ -1268,7 +1298,7 @@ ODBLiteClient.join;
1268
1298
  apiKey: this.config.apiKey,
1269
1299
  databaseId: db.hash
1270
1300
  });
1271
- return new ODBLiteConnection(client, this.serviceClient, databaseName);
1301
+ return new ODBLiteConnection(client, this.serviceClient, databaseName, db.hash);
1272
1302
  }
1273
1303
  async disconnect(tenantId) {
1274
1304
  if (tenantId) {
@@ -1296,16 +1326,19 @@ ODBLiteClient.join;
1296
1326
  */ class ODBLiteConnection {
1297
1327
  client;
1298
1328
  serviceClient;
1299
- databaseName;
1300
1329
  inTransaction = false;
1301
- constructor(client, serviceClient, databaseName){
1330
+ // Public metadata fields
1331
+ databaseName;
1332
+ databaseHash;
1333
+ constructor(client, serviceClient, databaseName, databaseHash){
1302
1334
  this.client = client;
1303
1335
  this.serviceClient = serviceClient;
1304
1336
  this.databaseName = databaseName;
1337
+ this.databaseHash = databaseHash;
1305
1338
  }
1306
1339
  /**
1307
1340
  * Template tag query (postgres.js-like)
1308
- */ async query(strings, ...values) {
1341
+ */ async sql(strings, ...values) {
1309
1342
  // ODBLiteClient.sql is a function that returns a Promise
1310
1343
  const result = await this.client.sql(strings, ...values);
1311
1344
  return {
@@ -1314,9 +1347,23 @@ ODBLiteClient.join;
1314
1347
  };
1315
1348
  }
1316
1349
  /**
1350
+ * Query with SQL string and parameters (alias for execute)
1351
+ */ async query(sql, params = []) {
1352
+ return this.execute(sql, params);
1353
+ }
1354
+ /**
1317
1355
  * Execute SQL with parameters
1318
1356
  */ async execute(sql, params = []) {
1319
1357
  try {
1358
+ // Handle object format { sql, args }
1359
+ if ('object' == typeof sql) {
1360
+ const result = await this.client.sql.execute(sql.sql, sql.args || []);
1361
+ return {
1362
+ rows: result.rows,
1363
+ rowsAffected: result.rowsAffected || 0
1364
+ };
1365
+ }
1366
+ // Handle string format
1320
1367
  const result = await this.client.sql.execute(sql, params);
1321
1368
  return {
1322
1369
  rows: result.rows,
@@ -1360,6 +1407,11 @@ ODBLiteClient.join;
1360
1407
  }
1361
1408
  }
1362
1409
  /**
1410
+ * Execute function in transaction (alias for transaction)
1411
+ */ async begin(fn) {
1412
+ return this.transaction(fn);
1413
+ }
1414
+ /**
1363
1415
  * Close connection
1364
1416
  */ async close() {
1365
1417
  // ODBLiteClient connections are stateless HTTP requests
@@ -1516,6 +1568,7 @@ ODBLiteClient.join;
1516
1568
  * Create a new database
1517
1569
  * @param name - Database name (becomes filename or ODB-Lite database name)
1518
1570
  * @param options - Optional creation options
1571
+ * @returns Connection object (contains databaseHash for ODB-Lite)
1519
1572
  */ async createDatabase(name, options) {
1520
1573
  console.log(`📦 Creating database: ${name}`);
1521
1574
  // Build database-specific config
@@ -1532,6 +1585,8 @@ ODBLiteClient.join;
1532
1585
  // Evict LRU if pool is full
1533
1586
  if (this.connections.size > this.maxConnections) await this.evictLRU();
1534
1587
  console.log(`✅ Database created: ${name}`);
1588
+ // Return connection so caller can access metadata (e.g., databaseHash for ODB-Lite)
1589
+ return conn;
1535
1590
  }
1536
1591
  /**
1537
1592
  * Check if a database exists
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pineliner/odb-client",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "Isomorphic client for ODB-Lite with postgres.js-like template string SQL support",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -62,34 +62,54 @@ class BunSQLiteConnection implements Connection {
62
62
  /**
63
63
  * Template tag query (postgres.js-like)
64
64
  */
65
- async query<T = any>(
65
+ async sql<T = any>(
66
66
  strings: TemplateStringsArray,
67
67
  ...values: any[]
68
68
  ): Promise<QueryResult<T>> {
69
69
  // Build SQL from template
70
- const sql = strings.reduce((acc, str, i) => {
70
+ const sqlStr = strings.reduce((acc, str, i) => {
71
71
  return acc + str + (i < values.length ? '?' : '')
72
72
  }, '')
73
73
 
74
- return this.execute(sql, values)
74
+ return this.execute(sqlStr, values) as Promise<QueryResult<T>>
75
+ }
76
+
77
+ /**
78
+ * Query with SQL string and parameters (alias for execute)
79
+ */
80
+ async query<T = any>(sql: string, params: any[] = []): Promise<QueryResult<T>> {
81
+ return this.execute(sql, params) as Promise<QueryResult<T>>
75
82
  }
76
83
 
77
84
  /**
78
85
  * Execute SQL with parameters
79
86
  */
80
- async execute(sql: string, params: any[] = []): Promise<QueryResult> {
87
+ async execute(sql: string | { sql: string; args?: any[] }, params: any[] = []): Promise<QueryResult> {
81
88
  try {
82
- const stmt = this.db.query(sql)
83
- const isSelect = sql.trim().toUpperCase().startsWith('SELECT')
89
+ // Handle object format { sql, args }
90
+ let sqlStr: string
91
+ let sqlParams: any[]
92
+
93
+ if (typeof sql === 'object') {
94
+ sqlStr = sql.sql
95
+ sqlParams = sql.args || []
96
+ } else {
97
+ sqlStr = sql
98
+ sqlParams = params
99
+ }
100
+
101
+ const stmt = this.db.query(sqlStr)
102
+ const isSelect = sqlStr.trim().toUpperCase().startsWith('SELECT') ||
103
+ sqlStr.trim().toUpperCase().includes('RETURNING')
84
104
 
85
105
  if (isSelect) {
86
- const rows = stmt.all(...params)
106
+ const rows = stmt.all(...sqlParams)
87
107
  return {
88
108
  rows: rows as any[],
89
109
  rowsAffected: 0,
90
110
  }
91
111
  } else {
92
- const result = stmt.run(...params)
112
+ const result = stmt.run(...sqlParams)
93
113
  return {
94
114
  rows: [],
95
115
  rowsAffected: result.changes || 0,
@@ -161,6 +181,13 @@ class BunSQLiteConnection implements Connection {
161
181
  }
162
182
  }
163
183
 
184
+ /**
185
+ * Execute function in transaction (alias for transaction)
186
+ */
187
+ async begin<T>(fn: (tx: Connection) => Promise<T>): Promise<T> {
188
+ return this.transaction(fn)
189
+ }
190
+
164
191
  /**
165
192
  * Close connection
166
193
  */
@@ -62,16 +62,23 @@ class LibSQLConnection implements Connection {
62
62
  /**
63
63
  * Template tag query (postgres.js-like)
64
64
  */
65
- async query<T = any>(
65
+ async sql<T = any>(
66
66
  strings: TemplateStringsArray,
67
67
  ...values: any[]
68
68
  ): Promise<QueryResult<T>> {
69
69
  // Build SQL from template
70
- const sql = strings.reduce((acc, str, i) => {
70
+ const sqlStr = strings.reduce((acc, str, i) => {
71
71
  return acc + str + (i < values.length ? '?' : '')
72
72
  }, '')
73
73
 
74
- return this.execute(sql, values)
74
+ return this.execute(sqlStr, values) as Promise<QueryResult<T>>
75
+ }
76
+
77
+ /**
78
+ * Query with SQL string and parameters (alias for execute)
79
+ */
80
+ async query<T = any>(sql: string, params: any[] = []): Promise<QueryResult<T>> {
81
+ return this.execute(sql, params) as Promise<QueryResult<T>>
75
82
  }
76
83
 
77
84
  /**
@@ -146,6 +153,13 @@ class LibSQLConnection implements Connection {
146
153
  }
147
154
  }
148
155
 
156
+ /**
157
+ * Execute function in transaction (alias for transaction)
158
+ */
159
+ async begin<T>(fn: (tx: Connection) => Promise<T>): Promise<T> {
160
+ return this.transaction(fn)
161
+ }
162
+
149
163
  /**
150
164
  * Close connection
151
165
  */
@@ -49,7 +49,7 @@ export class ODBLiteAdapter implements DatabaseAdapter {
49
49
  databaseId: db.hash,
50
50
  })
51
51
 
52
- return new ODBLiteConnection(client, this.serviceClient, databaseName)
52
+ return new ODBLiteConnection(client, this.serviceClient, databaseName, db.hash)
53
53
  }
54
54
 
55
55
  async disconnect(tenantId?: string): Promise<void> {
@@ -81,23 +81,28 @@ export class ODBLiteAdapter implements DatabaseAdapter {
81
81
  class ODBLiteConnection implements Connection {
82
82
  private client: ODBLiteClient
83
83
  private serviceClient: ServiceClient
84
- private databaseName: string
85
84
  private inTransaction = false
86
85
 
86
+ // Public metadata fields
87
+ public databaseName: string
88
+ public databaseHash: string
89
+
87
90
  constructor(
88
91
  client: ODBLiteClient,
89
92
  serviceClient: ServiceClient,
90
- databaseName: string
93
+ databaseName: string,
94
+ databaseHash: string
91
95
  ) {
92
96
  this.client = client
93
97
  this.serviceClient = serviceClient
94
98
  this.databaseName = databaseName
99
+ this.databaseHash = databaseHash
95
100
  }
96
101
 
97
102
  /**
98
103
  * Template tag query (postgres.js-like)
99
104
  */
100
- async query<T = any>(
105
+ async sql<T = any>(
101
106
  strings: TemplateStringsArray,
102
107
  ...values: any[]
103
108
  ): Promise<QueryResult<T>> {
@@ -110,11 +115,28 @@ class ODBLiteConnection implements Connection {
110
115
  }
111
116
  }
112
117
 
118
+ /**
119
+ * Query with SQL string and parameters (alias for execute)
120
+ */
121
+ async query<T = any>(sql: string, params: any[] = []): Promise<QueryResult<T>> {
122
+ return this.execute(sql, params) as Promise<QueryResult<T>>
123
+ }
124
+
113
125
  /**
114
126
  * Execute SQL with parameters
115
127
  */
116
- async execute(sql: string, params: any[] = []): Promise<QueryResult> {
128
+ async execute(sql: string | { sql: string; args?: any[] }, params: any[] = []): Promise<QueryResult> {
117
129
  try {
130
+ // Handle object format { sql, args }
131
+ if (typeof sql === 'object') {
132
+ const result = await this.client.sql.execute(sql.sql, sql.args || [])
133
+ return {
134
+ rows: result.rows,
135
+ rowsAffected: result.rowsAffected || 0,
136
+ }
137
+ }
138
+
139
+ // Handle string format
118
140
  const result = await this.client.sql.execute(sql, params)
119
141
 
120
142
  return {
@@ -171,6 +193,13 @@ class ODBLiteConnection implements Connection {
171
193
  }
172
194
  }
173
195
 
196
+ /**
197
+ * Execute function in transaction (alias for transaction)
198
+ */
199
+ async begin<T>(fn: (tx: Connection) => Promise<T>): Promise<T> {
200
+ return this.transaction(fn)
201
+ }
202
+
174
203
  /**
175
204
  * Close connection
176
205
  */
@@ -79,11 +79,12 @@ export class DatabaseManager {
79
79
  * Create a new database
80
80
  * @param name - Database name (becomes filename or ODB-Lite database name)
81
81
  * @param options - Optional creation options
82
+ * @returns Connection object (contains databaseHash for ODB-Lite)
82
83
  */
83
84
  async createDatabase(
84
85
  name: string,
85
86
  options?: { schemaContent?: string }
86
- ): Promise<void> {
87
+ ): Promise<Connection> {
87
88
  console.log(`📦 Creating database: ${name}`)
88
89
 
89
90
  // Build database-specific config
@@ -107,6 +108,9 @@ export class DatabaseManager {
107
108
  }
108
109
 
109
110
  console.log(`✅ Database created: ${name}`)
111
+
112
+ // Return connection so caller can access metadata (e.g., databaseHash for ODB-Lite)
113
+ return conn
110
114
  }
111
115
 
112
116
  /**
@@ -31,17 +31,23 @@ export interface QueryResult<T = any> {
31
31
  */
32
32
  export interface Connection {
33
33
  // Template tag queries (postgres.js-like)
34
- query<T = any>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>>
34
+ sql<T = any>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>>
35
35
 
36
- // Standard query methods (supports both formats for LibSQL compatibility)
36
+ // Standard query methods (same as execute)
37
+ query<T = any>(sql: string, params?: any[]): Promise<QueryResult<T>>
37
38
  execute(sql: string | { sql: string; args?: any[] }, params?: any[]): Promise<QueryResult>
38
39
  prepare(sql: string): PreparedStatement
39
40
 
40
41
  // Transaction support
41
42
  transaction<T>(fn: (tx: Connection) => Promise<T>): Promise<T>
43
+ begin<T>(fn: (tx: Connection) => Promise<T>): Promise<T>
42
44
 
43
45
  // Connection management
44
46
  close(): Promise<void>
47
+
48
+ // Metadata (optional, ODB-Lite specific)
49
+ databaseHash?: string
50
+ databaseName?: string
45
51
  }
46
52
 
47
53
  /**