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

sqlite-factory

v1.0.2

Published

`sqlite-factory` is an easy way to add Sqlite based storage to most NodeJS apps.

Downloads

2

Readme

Sqlite Factory

sqlite-factory is an easy way to add Sqlite based storage to most NodeJS apps.

Designed for command line and terminal-style apps!

Use Cases

  • Restartable tasks!
  • Logging progress or stats of long running processes.
  • Other stuff with structured data.

Notes

The library could be extended to support web based sqlite libraries. PRs welcome :).

It's not intended for use in multi-threaded apps (express server, cluster/worker module, PM2, etc.)

Examples

JavaScript Example

TypeScript Example

Folder structure

- src/index.ts
- src/models/logs.ts

src/index.ts

Important notes:

  • Remember to close the database when done.
    • In this example we listen for exit events on the process.
import Logs from "./models/logs";

Logs.insert({action: "script_started"});

// Cleanup & Final Save handler!
process.on('SIGTERM', async () => await Logs.close());
process.on('SIGINT', async () => await Logs.close());

src/models/logs.ts

Important notes:

  • The Model exported here is effectively a singleton.
  • It includes the typescript type & SQL CREATE TABLE script.
import sqliteFactory from "sqlite-factory";

interface LogRecord {
  id: number; // INTEGER PRIMARY KEY AUTOINCREMENT,
  timestamp: number; // TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  action: string; // VARCHAR(50),
  error_message: string; // TEXT,
  error_stack: string; // TEXT,
  source_file_name: string; // VARCHAR(100),
  source_line_number: number; // INTEGER,
  data: string; // TEXT
}

logService = sqliteFactory<LogRecord>({
  tableName: "logs",
  filePath: "./db.sqlite",
  createTableSql: `CREATE TABLE IF NOT EXISTS logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    action VARCHAR(50),
    error_message TEXT,
    error_stack TEXT,
    source_file_name VARCHAR(100),
    source_line_number INTEGER,
    data TEXT
  );`,
});

export default logService;

API

For the following API examples, most use this example interface:

const customerModel = sqliteFactory<{ name: string }>({
  tableName: "customers",
  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,
});

sqliteFactory(options)

options

filePath?: string Valid values are filenames, ":memory:" for an anonymous in-memory database and an empty string for an anonymous disk-based database. Anonymous databases are not persisted and when closing the database handle, their contents are lost.

tableName: string The table name to use for insert, update and delete commands.

createTableSql: string Must be a complete SQL DML Statement. Should start with CREATE TABLE IF NOT EXISTS table_name ...

In-memory example

const customers = sqliteFactory({
  tableName: "customers",
  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,
});

Disk-based example

const customers = sqliteFactory({
  tableName: "customers",
  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,
  filePath: "./customers.sqlite"
});

.close()

Closes the database. Data could be lost if you forget to call this!

.get(query, params)

Runs the SQL query with the specified parameters and resolves with the first result row afterwards. If the result set is empty, returns undefined.

The property names correspond to the column names of the result set.

It is impossible to access them by column index; the only supported way is by column name.

  • @param {string} query The SQL query to run.

  • @param {any} [params, ...] When the SQL statement contains placeholders, you can pass them in here. They will be bound to the statement before it is executed. There are three ways of passing bind parameters: directly in the function's arguments, as an array, and as an object for named parameters. This automatically sanitizes inputs.

See related method from node-sqlite3

.getAll(query, params)

Runs the SQL query with the specified parameters. The parameters are the same as the Model#run function, with the following differences:

If the result set is empty, it will be an empty array, otherwise it will have an object for each result row which in turn contains the values of that row, like the Model#get function.

Note: getAll first retrieves all result rows and stores them in memory.

  • @param {string} query The SQL query to run.
  • @param {array | object} params When the SQL statement contains placeholders, you can pass them in here. They will be bound to the statement before it is executed.

There are three ways of passing bind parameters:

  1. directly in the function's arguments,
  2. as an array,
  3. and as an object for named parameters.

Input is automatically sanitized.

const customerModel = sqliteFactory<{ name: string }>({
  tableName: "customers",
  createTableSql: `CREATE TABLE IF NOT EXISTS customers ( name VARCHAR(50) )`,
});
await customerModel.insert({ name: "Dan" });
await customerModel.insert({ name: "Rosie" });
await customerModel.insert({ name: "Sunflower" });

// 1 match found:
customerModel.getAll("SELECT * FROM customers WHERE name = :name",{ name: "Dan" });
// no matches found:
customerModel.getAll("SELECT * FROM customers WHERE name = :name",{ name: "not in the table!" });