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

@sqlbro/client

v0.0.5

Published

Query a public read only Sqlite database in the browser

Downloads

8

Readme

Sqlbro client

pub package

A tiny wrapper around sql.js-httpvfs to query read only public Sqlite databases from the browser:

import { initDb, query } from "@sqlbro/client";

await initDb("mydb.sqlite");
const data: Array<Record<string, any>> = await query(
  "SELECT * FROM mytable"
)

Install

npm install @sqlbro/client
# or
yarn add @sqlbro/client

Initialize the database

import { initDb } from "@sqlbro/client";

initDb()

Params and types:

function initDb(baseUrl?: string, dbName?: string): Promise<void>

Default values for the initDb params:

baseUrl = window.document.baseURI + "assets/"
dbName = "db.sqlite",

Example of initialization with different params:

import { initDb } from "@sqlbro/client";

initDb(
  window.document.baseURI + '/static',
  "db/mydb.sqlite", // the db is in /static/db/mydb.sqlite
)

Wait for the database initialization

Explicitly wait for the db to finish it's initialization:

import { isDbReady } from "@sqlbro/client";

await isDbReady;
console.log("The db is ready")

Note: the query function below will automatically wait for the db initialization to complete before running a query. So it is safe the run a query right after initDb not having to explicitly wait for the db ready state

Configure

:warning: These files have to be available from an url:

  • sqlite.worker.js
  • sql-wasm.wasm

Either copy them manualy from node_modules/sql.js-httpvfs/dist/ to a static url location or use a bundler:

Webpack

An example for Webpack 5 is available in the sql.js-httpvfs repository

Vitejs

Example to bundle the required files with Vitejs:

npm install --save-dev vite-plugin-static-copy
# or
yarn add -D vite-plugin-static-copy

Then in the vite.config.ts file:

import { defineConfig } from 'vite'
import { viteStaticCopy } from 'vite-plugin-static-copy'

export default defineConfig({
  assetsInclude: ['**/*.sqlite'],
  plugins: [
    // ...
    viteStaticCopy({
      targets: [
        {
          src: 'node_modules/sql.js-httpvfs/dist/sqlite.worker.js',
          dest: 'assets'
        },
        {
          src: 'node_modules/sql.js-httpvfs/dist/sql-wasm.wasm',
          dest: 'assets'
        },
        {
          src: 'src/assets/db.sqlite',
          dest: 'assets'
        }
      ]
    })
  ],
})

This will handle the files in dev mode and for the build

Query

Query the database from the browser:

import { query } from "@sqlbro/client"

const data: Array<Record<string, any>> = await query(
  "SELECT * FROM mytable"
)

Typing:

function query<T = Array<Record<string, any>>>(q: string): Promise<T>

Example of a query the would return typed data:

interface Product {
  id: number;
  name: string;
  description: string;
}

const data: Array<Product> = await query(
  "SELECT id,name,description FROM products"
)

Example

An example is available with a basic query and a json query