0.2.1 • Published 9 months ago

@ceil-dev/persistence v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Persistence Library

Library for organizing redundancy


Table of Contents

  1. Overview
  2. Installation
  3. Usage
  4. Example
  5. License

Overview

The library serves as a straightforward interface for managing data storage across multiple levels, making it easier to handle data in systems with hierarchical structures. It simplifies the process of retrieving, updating, and deleting data, so you don’t have to worry about the intricate details of managing storage layers. Depending on availability or priority, data can be stored and accessed from different levels, offering flexibility in how information is managed and retrieved.

This approach streamlines storage operations, providing an efficient way to handle data in complex systems without dealing with the underlying complexity.


Installation

# Clone the repository
npm install @ceil-dev/persistence

Usage

import {microEnv} from '@ceil-dev/persistence';

Example

import {
  createPersistence,
  createRuntimeLevel,
  createFileSystemLevel,
} from "@ceil-dev/persistence";
import fs from "fs";

// This example uses two persistence levels: default and file system
const run = async () => {
  // Create a directory if it does not exist
  fs.mkdirSync("./tmp/", { recursive: true });

  const persistence = createPersistence({
    // If file does not exist, defaultData will be used
    defaultData: {
      firstKey: 419,
    },
    id: "test",
    levels: {
      default: createRuntimeLevel({ next: { level: "fs" } }), // Specifying file system level as the next level after default
      fs: createFileSystemLevel({ fs, folderPath: "./tmp/", prefix: "fs_" }),
    },
  });

  const testValue = (await persistence.get({ key: "firstKey" }))?.value;

  if (typeof testValue !== "number") {
    throw new Error("Invalid value for key 'firstKey'");
  }

  console.log(`Last known value of "firstKey": ${testValue}`);
  // Setting a new value for the key
  await persistence.set({ key: "firstKey", value: testValue + 1 });
  console.log(
    `Updated value of "firstKey": ${(await persistence.get({ key: 'firstKey' }))?.value}`
  );

  // Setting test object
  await persistence.set({
    key: 'testObj',
    value: { existingProp: 'hello' },
  });

  // Setting nested prop on test object
  await persistence.set({
    key: 'testObj',
    path: [{ key: 'newProp', defaultValue: [] }, 0], // = testObj.newProp[0] - newProp will be set to [] if undefined | null
    value: 'world',
  });

  console.log(
    'testObj value:',
    (
      await persistence.get({
        key: 'testObj',
      })
    )?.value
  );
};

run().catch(console.error);

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.2.1

9 months ago

0.1.8

12 months ago

0.1.7

12 months ago

0.1.6

12 months ago

0.1.5

12 months ago

0.1.4

12 months ago

0.1.3

1 year ago

0.1.0

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.2

2 years ago

0.0.1

2 years ago