All Projects → WebReflection → Sqlite Worker

WebReflection / Sqlite Worker

Licence: isc
A simple, and persistent, SQLite database for Web and Workers.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Sqlite Worker

Pwa Cookbook
personally website
Stars: ✭ 107 (+52.86%)
Mutual labels:  worker, service-worker
Electron Angular4 Sqlite3
Sample project to show how to build a desktop app using Electron, Angular 4 and Sqlite3
Stars: ✭ 60 (-14.29%)
Mutual labels:  sqlite3
Parcel Plugin Sw Cache
📦👷 Parcel plugin for caching using a service worker
Stars: ✭ 45 (-35.71%)
Mutual labels:  service-worker
Flight Ticket Booksystem
大三下数据库课设 - 机票预订系统 - Django
Stars: ✭ 55 (-21.43%)
Mutual labels:  sqlite3
Sw 101 Gdgdf
Slides: Service Worker 101 @ GDG DevFest 2016
Stars: ✭ 48 (-31.43%)
Mutual labels:  service-worker
Esp32 Idf Sqlite3
Sqlite library for esp-idf (esp32) framework
Stars: ✭ 57 (-18.57%)
Mutual labels:  sqlite3
Pushkit
All the required components to set up independent web push notifications 🎈
Stars: ✭ 45 (-35.71%)
Mutual labels:  service-worker
D2sqlite3
A small wrapper around SQLite for the D programming language
Stars: ✭ 67 (-4.29%)
Mutual labels:  sqlite3
Expenses
💰Expense tracker using Google Sheets 📉 as a storage written in React
Stars: ✭ 1,105 (+1478.57%)
Mutual labels:  service-worker
Navi
Open Source Project for Grow with Google Udacity Scholarship Challenge - Navigation app using offline first strategy and google maps api - To get started please refer to the README.md - CONTRIBUTING.md and the project Wiki
Stars: ✭ 51 (-27.14%)
Mutual labels:  service-worker
Fluent Sqlite Driver
Fluent driver for SQLite
Stars: ✭ 51 (-27.14%)
Mutual labels:  sqlite3
Laravel Elasticbeanstalk Queue Worker
Stars: ✭ 48 (-31.43%)
Mutual labels:  worker
Yinyue
🏖Version Of Progressive Web App ( Serverless )
Stars: ✭ 57 (-18.57%)
Mutual labels:  service-worker
Service Mocker
🚀 Next generation frontend API mocking framework
Stars: ✭ 1,033 (+1375.71%)
Mutual labels:  service-worker
Sqlite orm
❤️ SQLite ORM light header only library for modern C++
Stars: ✭ 1,121 (+1501.43%)
Mutual labels:  sqlite3
Python crawler
It's designed to be a simple, tiny, pratical python crawler using json and sqlite instead of mysql or mongdb. The destination website is Zhihu.com.
Stars: ✭ 45 (-35.71%)
Mutual labels:  sqlite3
Tasktiger
Python task queue using Redis
Stars: ✭ 1,053 (+1404.29%)
Mutual labels:  worker
Tossit
Library for distributed job/worker logic.
Stars: ✭ 56 (-20%)
Mutual labels:  worker
Pangres
SQL upsert using pandas DataFrames for PostgreSQL, SQlite and MySQL with extra features
Stars: ✭ 68 (-2.86%)
Mutual labels:  sqlite3
Leo Editor
Leo is an Outliner, Editor, IDE and PIM written in 100% Python.
Stars: ✭ 1,136 (+1522.86%)
Mutual labels:  sqlite3

sqlite-worker

Social Media Photo by Alexander Sinn on Unsplash

A simple, and persistent, SQLite database for Web and Workers, based on sql.js and sqlite-tag.

How to use this module

The most important thing for this module to work, is being able to reach its pre-built, and pre-optimized files, via its own dist folder.

The resolution is done automatically, whenever this modules is imported via native ESM, but due to a long standing bug that involves both Web and Service Workers across browsers, such dist folder must be specified manually, whenever this module is used directly within either a Service Worker, or a generic Web Worker.

Importing on Web pages via ESM

In any generic page, it is possible to import this module via native ESM with, or without, the help of a CDN:

<script type="module">
// no ?module needed, it's the main export in unpkg
import {SQLiteWorker} from '//unpkg.com/sqlite-worker';

// `dist` option resolved automatically via import.meta.url
SQLiteWorker({name: 'my-db'})
  .then(async ({all, get, query}) => {
    await query`CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY, value TEXT)`;
    const {total} = await get`SELECT COUNT(id) as total FROM todos`;
    if (total < 1) {
      console.log('Inserting some value');
      await query`INSERT INTO todos (value) VALUES (${'a'})`;
      await query`INSERT INTO todos (value) VALUES (${'b'})`;
      await query`INSERT INTO todos (value) VALUES (${'c'})`;
    }
    console.log(await all`SELECT * FROM todos`);
  });
</script>

If the current dist folder is pre-installed though, import {SQLiteWorker} from './js/sqlite-worker/dist/index.js'; would work too.

While above example would run sqlite-worker through a Web Worker, which is recommended, it is also possible to bootstrap this module right away in the main thread.

<script type="module">
// no ?module needed, it's the main export in unpkg
import {init} from '//unpkg.com/sqlite-worker';

// `dist` option resolved automatically via import.meta.url
init({name: 'my-db'}).then(async ({all, get, query}) => {
  // ... same code as before ...
});
</script>

Beside being slightly faster, avoiding the worker postMessage dance, the main difference between SQLiteWorker and init is that init accepts an extra update option, that could be used to synchronize remotely the local database, whenever it's needed.

import {init} from 'sqlite-worker';

init({name: 'my-db', update(uInt8Array) {
  // store the latest uInt8Array somewhere
}});

The very same stored buffer could be used in the future to start from last stored update, in case the client erased its data (changed phone, IndexedDB cleared data, etc.).

This functionality could also be used in a Service Worker, but the initialization in there would be slightly different.

Importing on Service Worker

Instead of import, we must use importScripts to have cross browser compatibility, but this is not an issue, as this module provides, through its dist folder, everything needed to do so, as long as such folder is reachable:

// will add a `sqliteWorker` "global" initiator
importScripts('./dist/sw.js');

/* ⚠ IMPORTANT ⚠ */
const dist = './dist/';

sqliteWorker({dist, name: 'my-db'})
  .then(async ({all, get, query}) => {
    await query`CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY, value TEXT)`;
    const {total} = await get`SELECT COUNT(id) as total FROM todos`;
    if (total < 1) {
      console.log('Inserting some value');
      await query`INSERT INTO todos (value) VALUES (${'a'})`;
      await query`INSERT INTO todos (value) VALUES (${'b'})`;
      await query`INSERT INTO todos (value) VALUES (${'c'})`;
    }
    console.table(await all`SELECT * FROM todos`);
  });

The dist option could also be used from generic pages, but usually with import.meta.url such information can be easily, automatically, retrieved by the module itself.

ℹ About Bundlers

Because of its own folder dependencies, including the WASM file, and the module, needed to bootstripe SQLite 3, importing this module via bundlers might break its actual execution if:

  • all files are not also included in the bundle folder
  • the bundler transform import.meta.url is a "too smart" way, breaking its native functionality
  • something else some bundler might do

However, as previously mentioned, if the dist option is provided, everything should be fine, even if bundled.

Initialization Options

Both init([options]) and SQLiteWorker([options]) optionally accept a configuration/options object with the following fields:

  • name: the persistent database name. By default it's the string 'sqlite-worker'
  • dist: the folder, as string, containing all distribution files of this module. This is resolved automatically on pages that are not workers, but it must be provided within workers when importScripts is used instead.
  • database: an initial SQLite database, as Uint8Array instance. This is used only the very first time, and it fallbacks to new Uint8Array(0).
  • timeout: minimum interval, in milliseconds, between saves, to make storing, and exporting, the database, less greedy. By default it's the number 250.

Direct init Extra Options

These options work only with direct initialization, so either in the main thread or via Service Worker (once fixed in Chrome) after importing its init export.

  • update: a function that receives latest version of the database, as Uint8Array, whenever some query executed an INSERT, a DELETE, or an UPDATE.

SQLiteWorker Extra Options

These options work only with SQLiteWorker initialization.

  • worker: the string path where the JS worker to use is located. By default, this is the dist/worker.js file, which is a pre-optimized version of this source.
  • credentials: the optional credentials string between omit, same-origin, or include, defaulting to omit, or better, undefined credentials.

After Initialization Helpers

Both init(...) and SQLiteWorker(...) resolves with the sqlite-tag API, except for the raw utility, which is not implemented via the Worker interface, as it requires a special instance that won't survive postMessage dance, but it's exported within the direct init(...), hence in the main thread or via Service Worker.

The API in a nutshell is:

  • all: a template literal tag to retrieve all rows that match the query
  • get: a template literal tag to retrieve one row that matches the query
  • query: a template literal tag to simply query the database (no result returned)

All tags are asynchronous, so that it's possible to await their result.

Extra Initialization Helpers

The sqlite-worker/tables export helps defining, or modifying, tables at runtime, without needing to write complex logic, or queries.

All it's needed, is a tables property that describe the table name and its fields, handled via sqlite-tables-handler, before returning all module helpers.

import {init, SQLiteWorker} from 'sqlite-worker/tables';

init({
  name: 'test-db',
  // the tables schema
  tables: {
    todos: {
      id: 'INTEGER PRIMARY KEY',
      value: 'TEXT'
    }
  }
}).then(async ({all, get, query, raw}) => {
  const {total} = await get`SELECT COUNT(id) as total FROM todos`;
  if (total < 1) {
    console.log('Inserting some value');
    await query`INSERT INTO todos (value) VALUES (${'a'})`;
    await query`INSERT INTO todos (value) VALUES (${'b'})`;
    await query`INSERT INTO todos (value) VALUES (${'c'})`;
  }
  console.table(await all`SELECT * FROM todos`);
});

For Service Worker one must use the dist/sw-tables.js file instead of dist/sw.js.

importScripts('./dist/sw-tables.js');

sqliteWorker({
  dist: './dist',
  name: 'my-db',
  tables: {
    todos: {
      id: 'INTEGER PRIMARY KEY',
      value: 'TEXT'
    }
  }
})
  .then(async ({all, get, query}) => {
    // ...
  });

Compatibility

This module requires a browser compatible with WASM and native ESM import.

This module won't work in old Edge or IE.

Live Demo - please note if you read two OK after the list of expected errors (due code coverage) it means everything is fine and your browser works as expected.

CodePen - will show the table result, as JSON, in the body.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].