
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
@matteo.collina/sqlite-pool
Advanced tools
A connection pool for better-sqlite3 compatible with atdatabases suite
The @matteo.collina/sqlite-pool
library provides an asynchronous, safe and convenient
API for querying SQLite databases in node.js. Built on top of
better-sqlite3.
When using this module, consider that:
SQLite supports multiple simultaneous read transactions coming from separate database connections, possibly in separate threads or processes, but only one simultaneous write transaction - source.
import createConnectionPool, {sql} from '@matteo.collina/sqlite-pool';
// or in CommonJS:
// const createConnectionPool = require('@matteo.collina/sqlite-pool');
// const {sql} = require('@matteo.collina/sqlite-pool');
const db = createConnectionPool();
db.query(sql`SELECT * FROM users;`).then(
(results) => console.log(results),
(err) => console.error(err),
);
const createConnectionPool = require('@databases/sqlite-pool');
const {sql} = require('@databases/sqlite-pool');
const db = createConnectionPool();
db.query(sql`SELECT * FROM users;`).then(
(results) => console.log(results),
(err) => console.error(err),
);
For details on how to build queries, see Building SQL Queries
createConnectionPool(fileName)
Create a database createConnectionPoolion for a given database. You should only create one createConnectionPoolion per database for your entire applicaiton. Normally this means having one module that creates and exports the createConnectionPoolion pool.
In memory:
import createConnectionPool from '@databases/sqlite-pool';
const db = createConnectionPool();
File system:
import createConnectionPool from '@databases/sqlite-pool';
const db = createConnectionPool(FILE_NAME);
The DatabaseConnection
inherits from DatabaseTransaction
, so you call DatabaseConnection.query
directly instead of having to create a transaction for every query. Since SQLite has very limited support for actual transactions, we only support running one transaction at a time, but multiple queries can be run in parallel. You should therefore only use transactions when you actually need them.
DatabaseConnection.query(SQLQuery): Promise<any[]>
Run an SQL Query and get a promise for an array of results.
DatabaseConnection.queryStream(SQLQuery): AsyncIterable<any>
Run an SQL Query and get an async iterable of the results. e.g.
for await (const record of db.queryStream(sql`SELECT * FROM massive_table`)) {
console.log(result);
}
DatabaseConnection.tx(fn): Promise<T>
Executes a callback function as a transaction, with automatically managed createConnectionPoolion.
A transaction wraps a regular task with additional queries:
BEGIN
just before invoking the callback functionCOMMIT
, if the callback didn't throw any error or return a rejected promiseROLLBACK
, if the callback did throw an error or return a rejected promiseconst result = await db.tx(async (transaction) => {
const resultA = await transaction.query(sql`SELECT 1 + 1 AS a`);
const resultB = await transaction.query(sql`SELECT 1 + 1 AS b`);
return resultA[0].a + resultB[0].b;
});
// => 4
DatabaseConnection.dispose(): Promise<void>
Dispose the DatabaseConnection. Once this is called, any subsequent queries will fail.
MIT
FAQs
A connection pool for better-sqlite3 compatible with atdatabases suite
The npm package @matteo.collina/sqlite-pool receives a total of 23,119 weekly downloads. As such, @matteo.collina/sqlite-pool popularity was classified as popular.
We found that @matteo.collina/sqlite-pool demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.