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

lupdo-sqlite

v2.0.0

Published

Sqlite Driver For Lupdo

Downloads

11

Readme

Lupdo-sqlite

Lupdo Driver For Sqlite. Api

Supported Databases

Third Party Library

Lupdo-sqlite, under the hood, uses stable and performant npm packages:

Usage

Base Example

const { createSqlitePdo } = require('lupdo-sqlite');
// ES6 or Typescrypt
import { createSqlitePdo } from 'ludpo-sqlite';

const pdo = createSqlitePdo({ path: ':memory' }, { min: 2, max: 3 });
const run = async () => {
    const statement = await pdo.query('SELECT 2');
    const res = statement.fetchArray().all();
    console.log(res);
    await pdo.disconnect();
};

run();

Driver Options

https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#new-databasepath-options

new required option added:

  • path: string

new optional option added

  • wal: boolean [default false]
  • synchronous: string [default NORMAL works only when WAL enabled]
  • maxSize: number [default undefined] (MB)
  • onWalError: (err) => void [default undefined]

When WAL is disabled default journal_mode will be delete for database file and memory for memory database.
When WAL is enabled and maxSize is defined every 5 seconds lupdo-sqlite will check if WAL file is bigger than maxSize, if size is greater than maxSize wal_checkpoint(TRUNCATE) is called.
When WAL watcher get an error it will call your custom onWalError callback with the original error.
Sqlite only creates the WAL file when it is needed, which is why it may not exist, here is an example:

const pdo = createSqlitePdo(
    {
        path: 'sqlitefile.db',
        wal: true,
        maxSize: 100,
        onWalError: (err: any) => {
            if (err.code !== 'ENOENT') {
                // log the error on your application
            }
        }
    },
    { min: 2, max: 3 }
);

Better Sqlite Overrides

By default Ludpo-sqlite enable db.defaultSafeIntegers(true) and statement.safeIntegers(true). https://github.com/WiseLibs/better-sqlite3/blob/2194095aa1183e9c21d28eafadeac0d4d4d42625/docs/integer.md#getting-bigints-from-the-database

Internally lupdo-sqlite convert bigint to normal number if precision will be preserved.

Note Custom Aggregate and Function must be adapted as required if using numbers.

Parameters Binding

Lupdo-sqlite ignore type definition of TypeBinding parameter.
Lupdo-sqlite does not support array of parameters.

Not Integer Numbers

Warning All non-integer numbers are returned as strings, no precision is guaranteed, you can choose which cast to apply in your application.

Kill Connection

Lupdo-sqlite do not support kill query, if you need to perform very slow queries, you should implement worker threads by yourself.

Note you can use better-sqlite3 native api, retrieving a raw connection from the pool with pdo.getRawPoolConnection(). Do not forget to release rawPoolConnection before stop the job, otherwise the pool will be stuck.

SqliteDriver Create Function & Aggregate

SqliteDriver expose two static Method in order to register custom aggregates and functions.

Here You can find more details on aggregate and functions Options.

Note The SqliteDriver.createFunction(name, options) differs from the original better-sqlite3.function(name, [options], function), it accepts only a name and a config, config must contains execute function.

const { createSqlitePdo, SqliteDriver } = require('lupdo-sqlite');
// ES6 or Typescrypt
import { createSqlitePdo, SqliteDriver } from 'ludpo-sqlite';

SqliteDriver.createAggregate('max_len', {
    start: 0,
    step: (context: number, nextValue: string) => {
        if (nextValue.length > context) {
            return nextValue.length;
        }
        return context;
    }
});

SqliteDriver.createFunction('add2', {
    execute(a, b) {
        return a + b;
    }
});

const pdo = createSqlitePdo({ path: './test.db' });

await pdo.query('SELECT max_len(name) FROM companies;');
await pdo.query('SELECT add2(name, gender) FROM users;');