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-jumpstart

v0.3.6

Published

This is a small class I wrote for myself to get SQLite3 up and running faster in Node projects.

Downloads

5

Readme

node-sqlite-jumpstart

This is a small class I wrote for myself to get SQLite3 up and running faster in Node projects.

Features:

  • APIs for common used default functionalities
  • Handle DB Migrations / Patches with backups
  • Readonly and writeble DBs
  • Typed

Examples of how to use the class are in the test folder.

This is a pretty early stage project. Consider the API unstable!

How to use

Install:

npm i sqlite-jumpstart better-sqlite3
import { SQLiteDb, type SQLiteDbPatchType } from 'sqlite-jumpstart';

const patches: SQLiteDbPatchType[] = [
   {
     version: 1,
     statements: [
       `create table messages (
        id   integer primary key,
        text text
      );`,
     ],
   },
 ];

// Create a new class that extends SQLiteDb
class newDb extends SQLiteDb {
  constructor() {
    super({
      dbPath,
      readonly: false,
      patches: patches,
      logInfos: false,
      logErrors: true,
    });
  }

  // create your own APIs that extend the base functions
  addMultipleMessages(values: MessageType[]) {
    const stmnt = `
    insert into messages
      (id, text)
    values
      (:id, :text)
    ;
  `;
    // use the transactionalPreparedStatement to do multiple inserts in one transaction
    this.transactionalPreparedStatement(stmnt, values);
  }

  getMessageCount() {
    const stmnt = `
    select count(*) as cnt from messages
  `;
    const row = this.queryRow(stmnt);
    return row.cnt;
  }

  // use the queryRow function to get a single row
  getTextFromId(id: number) {
    const stmnt = `
    select text from messages where id = $id
  `;
    const row = this.queryRow(stmnt, { id } as any);
    return row.text;
  }
}

// init the DB
const db = new newDb();
await db.initDb();

const messages: MessageType[] = [
  { id: 1, text: 'Hello' },
  { id: 2, text: 'World' },
  { id: 3, text: 'I' },
  { id: 4, text: 'Like' },
  { id: 5, text: 'Tests' },
];

// use your own APIs
// note that the functions are synchronous
db.addMultipleMessages(messages);

const count = db.getMessageCount();
expect(count).toBe(5);

const text = db.getTextFromId(4);
expect(text).toBe('Like');

db.closeDb();

Constructor Params

type SQLiteDbConstructor = {
  dbPath: string; // path to the DB file
  readonly?: boolean; // if true, an exisiting DB will be opened readonly
  patches?: SQLiteDbPatchType[]; // patches to apply to the DB
  backupPath?: string; // path where the DB backups are stored
  logInfos?: boolean; // if true, log infos to the console
  logErrors?: boolean; // if true, log errors to the console
  pragmas?: string[]; // pragmas to set on the DB (overwrites defaults)
};

Patches

Patches allow you to apply changes to an existing DB. The class will check the current DB version and apply the patches if needed. The class will also create backups of the DB before applying the patches.

const patches1: SQLiteDbPatchType[] = [
  {
    version: 1,
    statements: [
      `create table messages (
         id   integer primary key,
         text text
       );`,
    ],
  },
  {
    version: 2,
    statements: [
      `alter table messages add create_date integer;`,
      `create table test1234(id integer);`,
    ],
  }
];

Pragmas

I set following default pragmas for writable DBs based on this blog post.

PRAGMA journal_mode = wal; -- different implementation of the atomicity properties
PRAGMA synchronous = normal; -- synchronise less often to the filesystem
PRAGMA foreign_keys = on; -- check foreign key reference, slightly worst performance

Additionally I run these optimizations:

PRAGMA analysis_limit=400; -- make sure pragma optimize does not take too long
PRAGMA optimize; -- gather statistics to improve query optimization
PRAGMA vacuum; -- reorganize the database file to reclaim unused space

For read only DBs I set following pragmas:

PRAGMA cache_size=-640000;
PRAGMA journal_mode=OFF;

If you don't like these you can overwrite them with the pragmas array parameter in the constructor.

SQlite Pragmas

Base Functions

  • runStatement executes a single statement
  • insertRow, updateRow, delteRow run a single statement with a descriptive function name
  • transactionalPreparedStatement executes a prepared statement in a single transaction for multiple inputs
  • queryRow returns a single row
  • queryRows returns a multiple rows
  • closeDb closes the DB connection