Socket
Socket
Sign inDemoInstall

better-sqlite-pool

Package Overview
Dependencies
12
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.2 to 0.2.3

89

index.js

@@ -7,2 +7,3 @@ "use strict";

const pick = require("lodash/pick");
const releaseEvent = "release";

@@ -45,2 +46,3 @@ /**

this._closed = false;
this.path = path;

@@ -65,21 +67,24 @@ /** @type {BetterSqlite3.Database[]} */

acquire() {
if (this._closed) {
throw new Error("Database already closed");
}
return this._getAvailableConnection()
|| this._createConnection()
|| this._waitConnection();
}
_getAvailableConnection() {
for (let conn of this.connections) {
if (conn.available && conn.open) {
conn.available = false;
return Promise.resolve(conn);
}
}
return false;
}
_createConnection() {
if (this.connections.length < this.max) {
let conn = new BetterSqlite3(this.path, pick(this, [
"memory",
"readonly",
"fileMustExist",
"timeout",
"verbose"
]));
let conn = this._rawCreateConnection();
this.connections.push(conn);
conn.available = false;

@@ -90,24 +95,51 @@ conn.release = () => {

conn.available = conn.open && true;
this.emit("release");
if (this._closed) {
conn.close();
}
else {
conn.available = conn.open && true;
this.emit(releaseEvent);
}
};
if (this.onConnectionCreated) {
this.onConnectionCreated(conn);
}
this.connections.push(conn);
return Promise.resolve(conn);
} else {
return new Promise((resolve, reject) => {
let handler = () => {
clearTimeout(timer);
resolve(this.acquire());
};
let timer = setTimeout(() => {
this.removeListener("release", handler);
reject(new Error("Timeout to acquire the connection."));
}, this.timeout);
this.once("release", handler);
});
}
return false;
}
/**
* low level create connection
* TODO: this should be abstract method for universal Database Pool
*/
_rawCreateConnection() {
return new BetterSqlite3(this.path, pick(this, [
"memory",
"readonly",
"fileMustExist",
"timeout",
"verbose"
]));
}
_waitConnection() {
return new Promise((resolve, reject) => {
let handler = () => {
clearTimeout(timer);
resolve(this.acquire());
};
let timer = setTimeout(() => {
this.removeListener(releaseEvent, handler);
reject(new Error("Timeout to acquire the connection."));
}, this.timeout);
this.once(releaseEvent, handler);
});
}
/**
* Closes all connections in the pool.

@@ -117,5 +149,8 @@ * @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#close---this

close() {
this._closed = true;
for (let id in this.connections) {
if (this.connections[id].open)
this.connections[id].close();
const conn = this.connections[id];
if (conn.available && conn.open) {
conn.close();
}
}

@@ -122,0 +157,0 @@ }

{
"name": "better-sqlite-pool",
"version": "0.2.2",
"version": "0.2.3",
"description": "A connection pool for better-sqlite3.",

@@ -25,4 +25,4 @@ "main": "index.js",

"dependencies": {
"better-sqlite3": "^5.4.0",
"lodash": "^4.17.11"
"better-sqlite3": "^5.4.3",
"lodash": "^4.17.15"
},

@@ -33,5 +33,5 @@ "engines": {

"devDependencies": {
"@types/better-sqlite3": "^5.2.2",
"@types/lodash": "^4.14.122"
"@types/better-sqlite3": "^5.4.0",
"@types/lodash": "^4.14.149"
}
}

@@ -16,2 +16,6 @@ "use strict";

pool.onConnectionCreated = function (conn) {
conn.exec("ATTACH DATABASE 'log.db' AS log;");
}
pool.acquire().then(con => {

@@ -21,3 +25,3 @@ connection = con;

var ddl = [
"create table `users` (",
"create table if not exists `users` (",
" `id` integer primary key autoincrement not null,",

@@ -42,2 +46,21 @@ " `name` varchar(32) not null,",

var logDDL = [
"create table if not exists `log`.`request` (",
" `id` integer primary key autoincrement not null,",
" `text` varchar(255)",
")"
].join("\n");
con.exec(logDDL);
var res3 = con.prepare("insert into `log`.`request` (`text`) values (?)")
.run(["okay"]);
var res4 = con.prepare("select * from `log`.`request` where `id` = ?").get(res3.lastInsertRowid);
assert.deepStrictEqual(res4, {
id: res3.lastInsertRowid,
text: "okay",
});
con.release();

@@ -54,4 +77,5 @@ }).catch(err => {

con.release();
console.log("#### OK ####");
});
}, 100);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc