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

better-tags

v0.1.2

Published

A way to handle SQLite with template literal tags that works seamlessly with both bun and better-sqlite3

Downloads

31

Readme

better-tags

build status Coverage Status

Social Media Photo by Angèle Kamp on Unsplash

A way to handle SQLite with template literal tags that works seamlessly with both bun and Nodejs' better-sqlite3 module, or even other modules exposing the same API, offering both a greedly cached variant or a non cached (default) one.

// for cached variant:
// import createSQLiteTags from 'better-tags/cached';
import createSQLiteTags from 'better-tags';

// for better-sqlite3 variant:
// import Database from 'better-sqlite3';
import {Database} from 'bun:sqlite';

const {
  db,         // the db passed along

  // retrieve results
  get,        // as single row
  all,        // as all rows
  values,     // as rows with array of values

  // just execute queries
  exec,       // alias for run
  run,        // alias for exec

  // transactions related
  entries,    // passes along fields
  transaction // execut a transaction
} = createSQLiteTags(new Database);

exec`CREATE TABLE people (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT,
  age INTEGER
)`;

// entries helps passing along homogeneous
// collections as fields per each transaction
// statements that will be run later on
const people = entries([
  {name: 'a', age: 0},
  {name: 'b', age: 1}
]);

// will execute multiple times the statement
// first with 'a' and 0, then with 'b' and 1
transaction`
  INSERT INTO people (name, age)
  VALUES (${people.name}, ${people.age})
`.default();
// transaction`...`():void
// transaction`...`.default():void
// transaction`...`.deferred():void
// transaction`...`.exclusive():void
// transaction`...`.immediate():void

console.log(
  all`SELECT * FROM people`
);
// [{id: 1, name: "a", age: 0}, {id: 2, name: "b", "age": 1}]

const name = 'b';
console.log(
  get`SELECT * FROM people WHERE name = ${name}`
);
// {id: 2, name: "b", "age": 1}

db.close();

Cached VS Non-cached

If your project uses one or few databases all the time, and there is enough heap/memory to deal with all possible queries and resulting statements, the better-tags/cached export will grant faster repeated execution in either bun or Nodejs.

If your project uses randomly defined databases though, the default export tries not to cache anything, neither via the module itself, nor through the internal cache bun, as example, could use. This is ideal for general purpose targets such as IoT devices and projects that cannot know AOT how many databases or queries should be handled.

Please note that the cached variant accepts a Map or WeakMap as second argument, so that you can control/fine-tune the caching exposed through this module, but bun internally doesn't offer this ability so that cached statements migt remain, or sature, the available cache.

What about pragma?

I am afraid .pragma(...) is not equally available in bun and beter-sqlite3 so that using exec or run, among regular SELECT options is the way to set or retrieve pragma details.

// nope
all`PRAGMA table_info(${name})`;

// yup
all`SELECT * FROM pragma_table_info(${name})`;

Is it safe or is this SQLInjection prone?

Every interpolation results into a bound ? parameter so that if these are not exactly the same amount of expected parameters the module will throw an error.