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

velox.db

v1.2.0

Published

velox.db is an open-source Node.js library that allows you to store data in a JSON file easily.

Downloads

20

Readme

Velox.db

velox.db is an open-source Node.js library that allows you to store data in a JSON file easily. The module is fully written in TypeScript to enhance types for JavaScript. This package has a built-in cache (using Map) to prevent latency when reading and writing files.

How it works?

Here is an example of how the library is saving your data (in a JSON file):

{
    "users": [
        {   
            "_id": "1ef3011f-9650-6820-b7b3-c80571feaa41",
            "name": "Tom",
            "age": 19,
            "hobbies": ["Swimming"]
        }
    ]
}

Each property from the first object represents a table name, and each property's value represents records saved for the table. The _id field is a unique field and cannot be removed or modified.

Example usage

Define a new database:

Create a new database using the class Client.

import {
    Client, 
    UUIDType,
    Flag
} from 'velox.db';

type Schemas = [{
    table: 'users',
    columns: {
        _id: string,
        name: string,
        age: number,
        hobbies: string[]
    }
}];

const client = new Client<Schemas>({
    path: '/path/to/file.json',
    uuid: UUIDType.UUIDv6,
    spaces: 4,
    flags: [
        Flag.CreateTableIfNotExist
    ]
});

The field _id must be a string, all record IDs are UUID-based.

Client options

export interface ClientOptions { path: ${string}.json, cache?: boolean, spaces?: number, uuid?: UUIDType; flags: Flag[] }

| Config | Type | Default | | ----------- | ----------- | ----------- | | path | string | | | cache | boolean? | false | | spaces | number? | undefined | | uuid | UUIDType? | UUIDType.UUIDv6 | | flags | Flag[] | [] |

The method: insert

This method adds a new record to a table.

const records = [
    { name: 'Alice', age: 23, hobbies: ['Reading', 'Music'] },
    { name: 'Bob', age: 20, hobbies: ['Sports'] },
    { name: 'Tom', age: 19, hobbies: ['Swimming'] }
];

client.insert('users', records);

The method: find

This method performs filtering based on dynamic conditions (like functions for comparisons).

client.find('users',
    {
        age: (integer) => integer > 20
    }
);

The method: delete

This method deletes filtered objects from a table.

client.delete('users',
    {
        age: (integer) => integer <= 25,
        name: (string) => string === 'Tom'
    }
);

The method: update

This method updates data for multiple records from a table.

client.update('users',
    {
        name: (string) => string === 'Alice',
        hobbies: (array) => array.includes('Music')
    },
    {
        age: 24
    }
);

Query options

This feature operates on an array of data and performs filtering, sorting, limiting, and skipping based on the provided options. It only exists in the following methods: find(), findFirst(), and count().

client.find('users',
    {
        age: (integer) => integer % 2 === 0,
        name: (string) => (/^[a-zA-Z]/g).test(string)
    },
    {
        sort: {
            age: -1
        },
        limit: 3,
        skip: 1,
        projection: ['name', 'hobbies']
    }
);

Explanation

  • Sorting: The sort object specifies the fields and the sort order (1 for ascending, -1 for descending).
  • Limit and Skip: The limit and skip options allow restricting the number of results and offsetting the results, respectively.
  • Projection: Refers to selecting specific fields from records to include in the results.

Cache

The cache feature will make your app more efficient and very fast without reading and writing the file. It saves all updated records in memory. This is how you enabled it:

const client = new Client<Schemas>({
    path: '/path/to/file.json',
    cache: true, // Set to "true" to enable
    uuid: UUIDType.UUIDv6,
    spaces: 4,
    flags: [
        Flag.CreateTableIfNotExist
    ]
});

Once you enable it, you should make an interval for saving the data to the file. Do not use a 5 seconds interval or lower, it may slow your app by writing huge data.

setInterval(() => {
    client.save();
}, 10000); // 10 seconds

Record IDs

Here are the available unique ID types for the records.

| Type | Supported? | | ----------- | ----------- | | UUIDv6 | Yes | | UUIDv4 | Yes | | ShortUUID | Yes | | NanoID | Yes | | Auto-increment | No |

Documentation

License

MIT License