@pineliner/odb-client 1.0.4 → 1.0.5
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.
- package/dist/database/types.d.ts +3 -1
- package/dist/database/types.d.ts.map +1 -1
- package/dist/index.cjs +60 -11
- package/dist/index.js +60 -11
- package/package.json +1 -1
- package/src/database/adapters/bun-sqlite.ts +35 -8
- package/src/database/adapters/libsql.ts +17 -3
- package/src/database/adapters/odblite.ts +26 -2
- package/src/database/types.ts +4 -2
package/dist/database/types.d.ts
CHANGED
|
@@ -19,13 +19,15 @@ export interface QueryResult<T = any> {
|
|
|
19
19
|
* All backends implement this through adapters
|
|
20
20
|
*/
|
|
21
21
|
export interface Connection {
|
|
22
|
-
|
|
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>;
|
|
30
32
|
}
|
|
31
33
|
/**
|
|
@@ -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,EAA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AAC,EAAE,MAAM,CAAA;KAChB,CAAA;IAGD,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAC5B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,YAAY,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,GAAG,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
|
|
1123
|
+
*/ async sql(strings, ...values) {
|
|
1124
1124
|
// Build SQL from template
|
|
1125
|
-
const
|
|
1126
|
-
return this.execute(
|
|
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
|
-
|
|
1133
|
-
|
|
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(...
|
|
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(...
|
|
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
|
|
1269
|
+
*/ async sql(strings, ...values) {
|
|
1250
1270
|
// Build SQL from template
|
|
1251
|
-
const
|
|
1252
|
-
return this.execute(
|
|
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
|
|
@@ -1380,7 +1410,7 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
|
|
|
1380
1410
|
}
|
|
1381
1411
|
/**
|
|
1382
1412
|
* Template tag query (postgres.js-like)
|
|
1383
|
-
*/ async
|
|
1413
|
+
*/ async sql(strings, ...values) {
|
|
1384
1414
|
// ODBLiteClient.sql is a function that returns a Promise
|
|
1385
1415
|
const result = await this.client.sql(strings, ...values);
|
|
1386
1416
|
return {
|
|
@@ -1389,9 +1419,23 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
|
|
|
1389
1419
|
};
|
|
1390
1420
|
}
|
|
1391
1421
|
/**
|
|
1422
|
+
* Query with SQL string and parameters (alias for execute)
|
|
1423
|
+
*/ async query(sql, params = []) {
|
|
1424
|
+
return this.execute(sql, params);
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1392
1427
|
* Execute SQL with parameters
|
|
1393
1428
|
*/ async execute(sql, params = []) {
|
|
1394
1429
|
try {
|
|
1430
|
+
// Handle object format { sql, args }
|
|
1431
|
+
if ('object' == typeof sql) {
|
|
1432
|
+
const result = await this.client.sql.execute(sql.sql, sql.args || []);
|
|
1433
|
+
return {
|
|
1434
|
+
rows: result.rows,
|
|
1435
|
+
rowsAffected: result.rowsAffected || 0
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
// Handle string format
|
|
1395
1439
|
const result = await this.client.sql.execute(sql, params);
|
|
1396
1440
|
return {
|
|
1397
1441
|
rows: result.rows,
|
|
@@ -1435,6 +1479,11 @@ const external_libsql_client_namespaceObject = require("@libsql/client");
|
|
|
1435
1479
|
}
|
|
1436
1480
|
}
|
|
1437
1481
|
/**
|
|
1482
|
+
* Execute function in transaction (alias for transaction)
|
|
1483
|
+
*/ async begin(fn) {
|
|
1484
|
+
return this.transaction(fn);
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1438
1487
|
* Close connection
|
|
1439
1488
|
*/ async close() {
|
|
1440
1489
|
// ODBLiteClient connections are stateless HTTP requests
|
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
|
|
1049
|
+
*/ async sql(strings, ...values) {
|
|
1050
1050
|
// Build SQL from template
|
|
1051
|
-
const
|
|
1052
|
-
return this.execute(
|
|
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
|
-
|
|
1059
|
-
|
|
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(...
|
|
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(...
|
|
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
|
|
1194
|
+
*/ async sql(strings, ...values) {
|
|
1175
1195
|
// Build SQL from template
|
|
1176
|
-
const
|
|
1177
|
-
return this.execute(
|
|
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
|
|
@@ -1305,7 +1335,7 @@ ODBLiteClient.join;
|
|
|
1305
1335
|
}
|
|
1306
1336
|
/**
|
|
1307
1337
|
* Template tag query (postgres.js-like)
|
|
1308
|
-
*/ async
|
|
1338
|
+
*/ async sql(strings, ...values) {
|
|
1309
1339
|
// ODBLiteClient.sql is a function that returns a Promise
|
|
1310
1340
|
const result = await this.client.sql(strings, ...values);
|
|
1311
1341
|
return {
|
|
@@ -1314,9 +1344,23 @@ ODBLiteClient.join;
|
|
|
1314
1344
|
};
|
|
1315
1345
|
}
|
|
1316
1346
|
/**
|
|
1347
|
+
* Query with SQL string and parameters (alias for execute)
|
|
1348
|
+
*/ async query(sql, params = []) {
|
|
1349
|
+
return this.execute(sql, params);
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1317
1352
|
* Execute SQL with parameters
|
|
1318
1353
|
*/ async execute(sql, params = []) {
|
|
1319
1354
|
try {
|
|
1355
|
+
// Handle object format { sql, args }
|
|
1356
|
+
if ('object' == typeof sql) {
|
|
1357
|
+
const result = await this.client.sql.execute(sql.sql, sql.args || []);
|
|
1358
|
+
return {
|
|
1359
|
+
rows: result.rows,
|
|
1360
|
+
rowsAffected: result.rowsAffected || 0
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
// Handle string format
|
|
1320
1364
|
const result = await this.client.sql.execute(sql, params);
|
|
1321
1365
|
return {
|
|
1322
1366
|
rows: result.rows,
|
|
@@ -1360,6 +1404,11 @@ ODBLiteClient.join;
|
|
|
1360
1404
|
}
|
|
1361
1405
|
}
|
|
1362
1406
|
/**
|
|
1407
|
+
* Execute function in transaction (alias for transaction)
|
|
1408
|
+
*/ async begin(fn) {
|
|
1409
|
+
return this.transaction(fn);
|
|
1410
|
+
}
|
|
1411
|
+
/**
|
|
1363
1412
|
* Close connection
|
|
1364
1413
|
*/ async close() {
|
|
1365
1414
|
// ODBLiteClient connections are stateless HTTP requests
|
package/package.json
CHANGED
|
@@ -62,34 +62,54 @@ class BunSQLiteConnection implements Connection {
|
|
|
62
62
|
/**
|
|
63
63
|
* Template tag query (postgres.js-like)
|
|
64
64
|
*/
|
|
65
|
-
async
|
|
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
|
|
70
|
+
const sqlStr = strings.reduce((acc, str, i) => {
|
|
71
71
|
return acc + str + (i < values.length ? '?' : '')
|
|
72
72
|
}, '')
|
|
73
73
|
|
|
74
|
-
return this.execute(
|
|
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
|
-
|
|
83
|
-
|
|
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(...
|
|
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(...
|
|
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
|
|
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
|
|
70
|
+
const sqlStr = strings.reduce((acc, str, i) => {
|
|
71
71
|
return acc + str + (i < values.length ? '?' : '')
|
|
72
72
|
}, '')
|
|
73
73
|
|
|
74
|
-
return this.execute(
|
|
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
|
*/
|
|
@@ -97,7 +97,7 @@ class ODBLiteConnection implements Connection {
|
|
|
97
97
|
/**
|
|
98
98
|
* Template tag query (postgres.js-like)
|
|
99
99
|
*/
|
|
100
|
-
async
|
|
100
|
+
async sql<T = any>(
|
|
101
101
|
strings: TemplateStringsArray,
|
|
102
102
|
...values: any[]
|
|
103
103
|
): Promise<QueryResult<T>> {
|
|
@@ -110,11 +110,28 @@ class ODBLiteConnection implements Connection {
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Query with SQL string and parameters (alias for execute)
|
|
115
|
+
*/
|
|
116
|
+
async query<T = any>(sql: string, params: any[] = []): Promise<QueryResult<T>> {
|
|
117
|
+
return this.execute(sql, params) as Promise<QueryResult<T>>
|
|
118
|
+
}
|
|
119
|
+
|
|
113
120
|
/**
|
|
114
121
|
* Execute SQL with parameters
|
|
115
122
|
*/
|
|
116
|
-
async execute(sql: string, params: any[] = []): Promise<QueryResult> {
|
|
123
|
+
async execute(sql: string | { sql: string; args?: any[] }, params: any[] = []): Promise<QueryResult> {
|
|
117
124
|
try {
|
|
125
|
+
// Handle object format { sql, args }
|
|
126
|
+
if (typeof sql === 'object') {
|
|
127
|
+
const result = await this.client.sql.execute(sql.sql, sql.args || [])
|
|
128
|
+
return {
|
|
129
|
+
rows: result.rows,
|
|
130
|
+
rowsAffected: result.rowsAffected || 0,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Handle string format
|
|
118
135
|
const result = await this.client.sql.execute(sql, params)
|
|
119
136
|
|
|
120
137
|
return {
|
|
@@ -171,6 +188,13 @@ class ODBLiteConnection implements Connection {
|
|
|
171
188
|
}
|
|
172
189
|
}
|
|
173
190
|
|
|
191
|
+
/**
|
|
192
|
+
* Execute function in transaction (alias for transaction)
|
|
193
|
+
*/
|
|
194
|
+
async begin<T>(fn: (tx: Connection) => Promise<T>): Promise<T> {
|
|
195
|
+
return this.transaction(fn)
|
|
196
|
+
}
|
|
197
|
+
|
|
174
198
|
/**
|
|
175
199
|
* Close connection
|
|
176
200
|
*/
|
package/src/database/types.ts
CHANGED
|
@@ -31,14 +31,16 @@ export interface QueryResult<T = any> {
|
|
|
31
31
|
*/
|
|
32
32
|
export interface Connection {
|
|
33
33
|
// Template tag queries (postgres.js-like)
|
|
34
|
-
|
|
34
|
+
sql<T = any>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>>
|
|
35
35
|
|
|
36
|
-
// Standard query methods (
|
|
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>
|