QuickJS + DyBase = JavaScript persistence

module storage unifies QuickJS of Fabrice the Magnificent with DyBase engine of Konstantin the Great.

The module provides built-in data persistence – transparent data storage and retrieval using standard JavaScript means.

Think about it as of MongoDB built into the language without need of special clients, etc.

Finally we can work with objects stored in DB as with ordinary JavaScript objects: Objects, Arrays and Maps :

// module NotesDB.js 

import * as Storage from "storage"; // or "@storage" if Sciter.JS

const storage = ... open DB and optionally initialize the DB ...

// persistent Note 
class Note {

  constructor(text, date = undefined, id = undefined) {
    this.id = id || UUID.create();
    this.date = date || new Date();
    this.text = text;
    
    // adding it to the storage
    let root = storage.root;
    root.notesByDate.set(this.date, this); 
    root.notesById.set(this.id, this);
  }

  remove() {
    let root = storage.root;
    root.notesByDate.delete(this.date, this); 
    root.notesById.delete(this.id);
  }

  static getById(id) {
    return storage.root.notesById.get(id);
  }
}

Essentially: all JS objects that are accessible from Storage.root object are persistable in the Storage.

Further reading:

The Storage is a port of corresponding feature of Sciter/TIScript. Sciter.Notes application:

uses the Storage for storing its notes.

Leave a Reply

Your email address will not be published. Required fields are marked *