@libsql/client
Advanced tools
Comparing version 0.3.5 to 0.3.6-pre.1
@@ -60,6 +60,8 @@ "use strict"; | ||
executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened", config.intMode); | ||
return new Sqlite3Client(db, config.intMode); | ||
return new Sqlite3Client(path, options, db, config.intMode); | ||
} | ||
exports._createClient = _createClient; | ||
class Sqlite3Client { | ||
#path; | ||
#options; | ||
#db; | ||
@@ -70,3 +72,5 @@ #intMode; | ||
/** @private */ | ||
constructor(db, intMode) { | ||
constructor(path, options, db, intMode) { | ||
this.#path = path; | ||
this.#options = options; | ||
this.#db = db; | ||
@@ -79,20 +83,21 @@ this.#intMode = intMode; | ||
this.#checkNotClosed(); | ||
return executeStmt(this.#db, stmt, this.#intMode); | ||
return executeStmt(this.#getDb(), stmt, this.#intMode); | ||
} | ||
async batch(stmts, mode = "deferred") { | ||
this.#checkNotClosed(); | ||
const db = this.#getDb(); | ||
try { | ||
executeStmt(this.#db, (0, util_js_1.transactionModeToBegin)(mode), this.#intMode); | ||
executeStmt(db, (0, util_js_1.transactionModeToBegin)(mode), this.#intMode); | ||
const resultSets = stmts.map((stmt) => { | ||
if (!this.#db.inTransaction) { | ||
if (!db.inTransaction) { | ||
throw new api_js_1.LibsqlError("The transaction has been rolled back", "TRANSACTION_CLOSED"); | ||
} | ||
return executeStmt(this.#db, stmt, this.#intMode); | ||
return executeStmt(db, stmt, this.#intMode); | ||
}); | ||
executeStmt(this.#db, "COMMIT", this.#intMode); | ||
executeStmt(db, "COMMIT", this.#intMode); | ||
return resultSets; | ||
} | ||
finally { | ||
if (this.#db.inTransaction) { | ||
executeStmt(this.#db, "ROLLBACK", this.#intMode); | ||
if (db.inTransaction) { | ||
executeStmt(db, "ROLLBACK", this.#intMode); | ||
} | ||
@@ -102,13 +107,16 @@ } | ||
async transaction(mode = "write") { | ||
executeStmt(this.#db, (0, util_js_1.transactionModeToBegin)(mode), this.#intMode); | ||
return new Sqlite3Transaction(this.#db, this.#intMode); | ||
const db = this.#getDb(); | ||
executeStmt(db, (0, util_js_1.transactionModeToBegin)(mode), this.#intMode); | ||
this.#db = null; // A new connection will be lazily created on next use | ||
return new Sqlite3Transaction(db, this.#intMode); | ||
} | ||
async executeMultiple(sql) { | ||
this.#checkNotClosed(); | ||
const db = this.#getDb(); | ||
try { | ||
return executeMultiple(this.#db, sql); | ||
return executeMultiple(db, sql); | ||
} | ||
finally { | ||
if (this.#db.inTransaction) { | ||
executeStmt(this.#db, "ROLLBACK", this.#intMode); | ||
if (db.inTransaction) { | ||
executeStmt(db, "ROLLBACK", this.#intMode); | ||
} | ||
@@ -119,7 +127,9 @@ } | ||
this.#checkNotClosed(); | ||
await this.#db.sync(); | ||
await this.#getDb().sync(); | ||
} | ||
close() { | ||
this.closed = true; | ||
this.#db.close(); | ||
if (this.#db !== null) { | ||
this.#db.close(); | ||
} | ||
} | ||
@@ -131,2 +141,9 @@ #checkNotClosed() { | ||
} | ||
// Lazily creates the database connection and returns it | ||
#getDb() { | ||
if (this.#db === null) { | ||
this.#db = new libsql_1.default(this.#path, this.#options); | ||
} | ||
return this.#db; | ||
} | ||
} | ||
@@ -133,0 +150,0 @@ exports.Sqlite3Client = Sqlite3Client; |
@@ -13,3 +13,3 @@ import Database from "libsql"; | ||
/** @private */ | ||
constructor(db: Database.Database, intMode: IntMode); | ||
constructor(path: string, options: Database.Options, db: Database.Database, intMode: IntMode); | ||
execute(stmt: InStatement): Promise<ResultSet>; | ||
@@ -16,0 +16,0 @@ batch(stmts: Array<InStatement>, mode?: TransactionMode): Promise<Array<ResultSet>>; |
@@ -39,5 +39,7 @@ import Database from "libsql"; | ||
executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened", config.intMode); | ||
return new Sqlite3Client(db, config.intMode); | ||
return new Sqlite3Client(path, options, db, config.intMode); | ||
} | ||
export class Sqlite3Client { | ||
#path; | ||
#options; | ||
#db; | ||
@@ -48,3 +50,5 @@ #intMode; | ||
/** @private */ | ||
constructor(db, intMode) { | ||
constructor(path, options, db, intMode) { | ||
this.#path = path; | ||
this.#options = options; | ||
this.#db = db; | ||
@@ -57,20 +61,21 @@ this.#intMode = intMode; | ||
this.#checkNotClosed(); | ||
return executeStmt(this.#db, stmt, this.#intMode); | ||
return executeStmt(this.#getDb(), stmt, this.#intMode); | ||
} | ||
async batch(stmts, mode = "deferred") { | ||
this.#checkNotClosed(); | ||
const db = this.#getDb(); | ||
try { | ||
executeStmt(this.#db, transactionModeToBegin(mode), this.#intMode); | ||
executeStmt(db, transactionModeToBegin(mode), this.#intMode); | ||
const resultSets = stmts.map((stmt) => { | ||
if (!this.#db.inTransaction) { | ||
if (!db.inTransaction) { | ||
throw new LibsqlError("The transaction has been rolled back", "TRANSACTION_CLOSED"); | ||
} | ||
return executeStmt(this.#db, stmt, this.#intMode); | ||
return executeStmt(db, stmt, this.#intMode); | ||
}); | ||
executeStmt(this.#db, "COMMIT", this.#intMode); | ||
executeStmt(db, "COMMIT", this.#intMode); | ||
return resultSets; | ||
} | ||
finally { | ||
if (this.#db.inTransaction) { | ||
executeStmt(this.#db, "ROLLBACK", this.#intMode); | ||
if (db.inTransaction) { | ||
executeStmt(db, "ROLLBACK", this.#intMode); | ||
} | ||
@@ -80,13 +85,16 @@ } | ||
async transaction(mode = "write") { | ||
executeStmt(this.#db, transactionModeToBegin(mode), this.#intMode); | ||
return new Sqlite3Transaction(this.#db, this.#intMode); | ||
const db = this.#getDb(); | ||
executeStmt(db, transactionModeToBegin(mode), this.#intMode); | ||
this.#db = null; // A new connection will be lazily created on next use | ||
return new Sqlite3Transaction(db, this.#intMode); | ||
} | ||
async executeMultiple(sql) { | ||
this.#checkNotClosed(); | ||
const db = this.#getDb(); | ||
try { | ||
return executeMultiple(this.#db, sql); | ||
return executeMultiple(db, sql); | ||
} | ||
finally { | ||
if (this.#db.inTransaction) { | ||
executeStmt(this.#db, "ROLLBACK", this.#intMode); | ||
if (db.inTransaction) { | ||
executeStmt(db, "ROLLBACK", this.#intMode); | ||
} | ||
@@ -97,7 +105,9 @@ } | ||
this.#checkNotClosed(); | ||
await this.#db.sync(); | ||
await this.#getDb().sync(); | ||
} | ||
close() { | ||
this.closed = true; | ||
this.#db.close(); | ||
if (this.#db !== null) { | ||
this.#db.close(); | ||
} | ||
} | ||
@@ -109,2 +119,9 @@ #checkNotClosed() { | ||
} | ||
// Lazily creates the database connection and returns it | ||
#getDb() { | ||
if (this.#db === null) { | ||
this.#db = new Database(this.#path, this.#options); | ||
} | ||
return this.#db; | ||
} | ||
} | ||
@@ -111,0 +128,0 @@ export class Sqlite3Transaction { |
{ | ||
"name": "@libsql/client", | ||
"version": "0.3.5", | ||
"version": "0.3.6-pre.1", | ||
"keywords": [ | ||
@@ -103,3 +103,3 @@ "libsql", | ||
"js-base64": "^3.7.5", | ||
"libsql": "^0.1.22" | ||
"libsql": "^0.1.24" | ||
}, | ||
@@ -106,0 +106,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147199
3571
+ Added@types/node@22.9.3(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@types/node@22.10.0(transitive)
- Removedundici-types@6.20.0(transitive)
Updatedlibsql@^0.1.24