simple-offline-first

1.0.66 • Public • Published

Simple Offline-First Library

This is a minimal example library implementing an offline-first infrastructure supporting developers to apply the offline-first philosophy to their existing synchronized projects. The Client implements an stateful auto synchronization to a server with conflict resolution using an own json-based CRDT datatype, while the Server provides the necessary RESTful API interface for the server. It also utilizes privacy reinforcing techniques to hide sensitive information to third parties.

Installation

You can install the library (which is still in an early development stage) using the node package manager (npm):

npm init -y && npm install simple-offline-first

Alternatively, pull the project using git:

cd my-project && git clone https://github.com/david-prv/offline-first-lib && cd offline-first-lib && npm install

Example Usage

Import & setup Client from library:

const { Client: OfflineFirstClient } = require("simple-offline-first");
const client = new OfflineFirstClient(
  // server to sync with -> MUST use our server API
  "localhost:3600",
  // the database name to work on
  "todo_application"
);

Log changes to local CRDT:

// I don't have any milk...
let change = new TodoWrapper(TodoType.SHOPPING, "Buy some milk");
client.insert(change.toString());

// Hm, this wasn't polite, let's change that...
let updatedChange = new TodoWrapper(TodoType.SHOPPING, "Buy some milk, please");
client.update(updatedChange.toString(), (entry) => entry.hash == hashToChange);

// Know what, I've found some, let's delete that...
client.delete((entry) => entry.hash == hashToChange);

It's as easy as that!

Changelog using CRDT

{
  "example": [
    {
      "hash": "ad35e2a737129acd2146bf2bfa09e0ff62c55fe029e5c34672bfae0f1195981c",
      "type": "insert",
      "timestamp": "2024-01-21T17:11:52.214Z",
      "data": "{\"title\":\"Entry title\",\"content\":\"This is an example\"}"
    },
    {
      "hash": "5b33bf76622b22b2318826d913f3050f65a7d5f433944edd6f656c610348ac19",
      "type": "update(ad35e2a737129acd2146bf2bfa09e0ff62c55fe029e5c34672bfae0f1195981c)",
      "timestamp": "2024-01-21T17:11:52.214Z",
      "data": "{\"title\":\"Entry title\",\"content\":\"This is an changed example\"}"
    },
    {
      "hash": "4ea13a0c514c91814cfc61108f789e64701ef9b5b3caa855c89eed2c16ec0e93",
      "type": "delete(ad35e2a737129acd2146bf2bfa09e0ff62c55fe029e5c34672bfae0f1195981c)",
      "timestamp": "2024-01-21T17:11:57.964Z",
      "data": null
    },
    {
      "hash": "05caab73cb56ecd0197454ef22dc77c5a6fddcba1f55d51c4650de6bfa18280e",
      "type": "insert",
      "timestamp": "2024-01-21T17:10:25.343Z",
      "data": "{\"title\":\"Entry title\",\"content\":\"This is another example\"}"
    }
  ]
}

Readme

Keywords

none

Package Sidebar

Install

npm i simple-offline-first

Weekly Downloads

0

Version

1.0.66

License

ISC

Unpacked Size

45.5 kB

Total Files

9

Last publish

Collaborators

  • david-prv