Memoz
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
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,
storagePath: './data',
useMutex: true,
});
async function boot() {
const totalPages = 2;
const usersPerPage = 10;
for (let index = 0; index < totalPages; index++) {
try {
const users = await memoz
.getMany()
.sort([{ name: 'asc' }])
.skip(index * usersPerPage)
.limit(usersPerPage);
console.log(`Page ${index + 1}:`, users);
} catch (error) {
console.error(`Error retrieving users for page ${index + 1}:`, error);
}
}
const user = await memoz.getOne({
field: 'name',
operator: '$regex',
value: { $regex: '999', $options: 'i' },
});
console.log(user);
{
const options: FuzzySearchOptions = {
maxDistance: 2,
scoringStrategy: 'default',
};
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 },
};
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));
const titleBonus = fieldToken.includes(token) ? 1 : 0;
return baseScore + titleBonus;
};
const options: FuzzySearchOptions = {
maxDistance: 2,
scoringStrategy: 'custom',
customScoringFn,
};
const results = await memoz.fuzzySearch('User 999', ['age', 'name'], options);
console.log(results.slice(0, 2));
}
}
boot();