npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

memoz

v4.2.0

Published

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

Downloads

26,891

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();