npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@matteo.collina/sqlite-pool

v0.4.0

Published

A connection pool for better-sqlite3 compatible with atdatabases suite

Downloads

287,087

Readme

sqlite-pool

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.

Usage

import {sql, createConnectionPool} from '@matteo.collina/sqlite-pool';
// or in CommonJS:
// const { createConnectionPool, 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

API

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:

  1. it executes BEGIN just before invoking the callback function
  2. it executes COMMIT, if the callback didn't throw any error or return a rejected promise
  3. it executes ROLLBACK, if the callback did throw an error or return a rejected promise
const 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.

License

MIT