Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

memoz

miladezzat27.4k4.2.0TypeScript support: included

Memoz is an in-memory database that persists on disk. The data model is key-value.

data, structure, database, redis, transaction, pipelining, performance, queue, nodejs, pubsub, backpressure, cache, ram, simple, storage, typescript, in-memory, disk, persist, key-value, string, list, set, sorted-set, hash

readme

Memoz

npm version  https://img.shields.io/npm/dm/memoz.svg

Memoz is an in-memory database that persists on disk, offering easy CRUD operations with a simple API. it supports document persistence to disk.

Installation

npm i memoz
# or
yarn add memoz

Usage

import { Memoz, FuzzySearchOptions } from "memoz";

interface IUser {
    readonly id?: string;
    name: string;
    age: number;
}

const memoz = new Memoz<IUser>({
    persistToDisk: true,  // to allow persist data on disk - default false
    storagePath: './data', // the location to persist data - default './data'
    useMutex: true, //  Whether to use a mutex for thread safety - default false
});


async function boot() {
    // Uncomment to create and store users in the database
    // const docs = Array.from({ length: 1000 }, (_, i) => ({ name: `User ${i}`, age: i }));
    // await memoz.createMany(docs);

    // Loop to get users with pagination and sorting to test caching
    const totalPages = 2; // Define the total number of pages to iterate over
    const usersPerPage = 10; // Number of users per page

    for (let index = 0; index < totalPages; index++) {
        try {
            const users = await memoz
                .getMany() // Retrieve all users
                .sort([{ name: 'asc' }]) // Sort users by name in ascending order
                .skip(index * usersPerPage) // Calculate the correct offset for pagination
                .limit(usersPerPage); // Limit the number of users retrieved

            console.log(`Page ${index + 1}:`, users); // Log the users for the current page
        } catch (error) {
            console.error(`Error retrieving users for page ${index + 1}:`, error); // Handle any errors
        }
    }

    // support regex 
      const user = await memoz.getOne({
        field: 'name',
        operator: '$regex',
        value: { $regex: '999', $options: 'i' }, // the $regex can be new RegExp
    });

    console.log(user);


      {
    const options: FuzzySearchOptions = {
      maxDistance: 2,
      scoringStrategy: 'default',
    };

    // Perform a fuzzy search
    const results = await memoz.fuzzySearch('User 999', ['age', 'name'], options, 5);

    console.log(results);
  }

  {
    const options: FuzzySearchOptions = {
      maxDistance: 2,
      scoringStrategy: 'tokenCount',
      fieldWeights: { title: 2, content: 1 }, // Title matches count more
    };

    // Perform a fuzzy search
    const results = await memoz.fuzzySearch('User 999', ['age', 'name'], options);
    console.log(results.slice(0, 2));
  }
  {
    const customScoringFn = (token: string, fieldToken: string, distance: number, fieldWeight: number) => {
      const baseScore = fieldWeight * (1 / (distance + 1)); // Decrease score as distance increases
      const titleBonus = fieldToken.includes(token) ? 1 : 0; // Bonus if the fieldToken contains the token
      return baseScore + titleBonus; // Total score
    };

    // Example usage
    const options: FuzzySearchOptions = {
      maxDistance: 2,
      scoringStrategy: 'custom',
      customScoringFn,
    };

    // Perform a fuzzy search
    const results = await memoz.fuzzySearch('User 999', ['age', 'name'], options);
    console.log(results.slice(0, 2));
  }
}

// Start the boot process
boot();

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.2.0 (2024-10-21)

4.1.0 (2024-10-20)

4.0.4 (2024-10-20)

4.0.3 (2024-10-20)

4.0.2 (2024-10-18)

Documentation Changes

4.0.1 (2024-10-18)

Continuous Integration

4.0.1 (2024-10-17)

4.0.0 (2024-10-17)

New Features
  • add new features like (transaction, presist on disk, etc) (4642966f)

3.1.0 (2024-10-08)

3.0.0 (2024-10-08)

2.2.0 (2024-09-16)

Continuous Integration

2.1.2 (2022-02-19)

Chores
Bug Fixes
Other Changes
  • miladezzat/memoz into gh-pages (ae7913f1)

2.1.1 (2022-02-11)

Chores
Documentation Changes

2.1.0 (2022-02-09)

Chores
New Features

2.0.1 (2021-08-04)

Chores
Documentation Changes

2.0.0 (2021-07-30)

New Features

1.1.3 (2021-07-28)

Chores
  • update package json and add husky (e9322431)
Continuous Integration
Refactors

1.1.3 (2021-07-28)

Chores
  • update package json and add husky (e9322431)
Continuous Integration
Refactors

1.1.2 (2021-07-28)

Documentation Changes

1.1.1 (2021-07-28)

Bug Fixes

1.1.0 (2021-07-27)

Chores
Documentation Changes
New Features

1.0.0 (2021-07-26)

Documentation Changes
New Features
Bug Fixes
Tests