1.1.0-beta.0 • Published 1 year ago

@pvermeer/dexie-boolean-null-index-addon v1.1.0-beta.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Dexie Boolean Null Index Addon

NPM Version NPM Version master lerna Conventional Commits

This addon can be used as a stand-alone addon for Dexie.js, yet is also part of dexie-addon-suite NPM Version that combines a number of addons for Dexie. It contains code to combine some addons like populated rxjs observables.

Install over npm

npm install @pvermeer/dexie-boolean-null-index-addon

Dexie.js

Dexie Null Index Addon depends on Dexie.js v3. NPM Version

npm install dexie

Documentation

How to use

Addon

Add booleanNullIndex() to your Dexie database. See below examples and https://dexie.org for more info.

This addon uses hooks to update read and write operations. It save null, true and false as a binary value (ArrayBuffer) that IndexedDB can index. It maps the binary value back to null, true and false on read.

Some methods of Table, WhereClause and Collection are overwritten so everything works as expected per the Dexie documentation.

Wait for open

Always open the database yourself. Dexie does not wait for all hooks to be subscribed (bug?).

await db.open();

To help with this, the option 'autoOpen' has been disabled.

Create Dexie database

ESM

import Dexie from 'dexie';
import { booleanNullIndex } from '@pvermeer/dexie-boolean-null-index-addon';

// Declare Database
const db = new Dexie("FriendDatabase", {
    addons: [booleanNullIndex]
});
db.version(1).stores({
    friends: '++id, firstName, lastName, shoeSize, age'
});

// Open the database
db.open()
    .then(() => {
        console.log('DB loaded! :D')
        // Use Dexie
    });

Typescript

import Dexie from 'dexie';
import { booleanNullIndex } from '@pvermeer/dexie-boolean-null-index-addon';

// Declare Database
class FriendsDatabase extends Dexie {

    public friends: Dexie.Table<Friend, number>;

    constructor(name: string) {
        super(name);

        booleanNullIndex(this);

        this.version(1).stores({
            friends: '++id, firstName, lastName, shoeSize, age'
        });
    }
}

const db = new FriendDatabase('FriendsDatabase');

// Open the database
db.open()
    .then(() => {
        console.log('DB loaded! :D')
        // Use Dexie
    });

HTML import

Bundled & minified package: https://unpkg.com/@pvermeer/dexie-boolean-null-index-addon@latest/dist/dexie-boolean-null-index-addon.min.js.

Addon is exported as namespace DexieBooleanNullIndexAddon.

<!doctype html>
<html>
    <head>
        <!-- Include Dexie (@next if v3 is still in RC) -->
        <script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script> 

        <!-- Include DexieBooleanNullIndexAddon (always after Dexie, it's a dependency) -->
        <script src="https://unpkg.com/@pvermeer/dexie-boolean-null-index-addon@latest/dist/dexie-boolean-null-index-addon.min.js"></script>

        <script>
            // Define your database
            const db = new Dexie("FriendDatabase", {
                addons: [DexieBooleanNullIndexAddon.booleanNullIndex]
            });
            db.version(1).stores({
                friends: '++id, firstName, lastName, shoeSize, age'
            });

            // Open the database
            db.open()
                .then(() => {
                    console.log('DB loaded! :D')
                    // Do Dexie stuff
                });
        </script>
    </head>
</html>

API

The packet exposes two exports:

booleanNullIndex - addon function

function booleanNullIndex(db: Dexie): void;

IndexValueEncoder

Constants and encode / decode methods that this addon uses.

class IndexValueEncoder { };

Dexie.js

Dexie.js is a wrapper library for indexedDB - the standard database in the browser. https://dexie.org