1.2.0 • Published 11 months ago

velox.db v1.2.0

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

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[] }

ConfigTypeDefault
pathstring
cacheboolean?false
spacesnumber?undefined
uuidUUIDType?UUIDType.UUIDv6
flagsFlag[][]

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.

TypeSupported?
UUIDv6Yes
UUIDv4Yes
ShortUUIDYes
NanoIDYes
Auto-incrementNo

Documentation

License

MIT License

1.2.0

11 months ago

1.1.0

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago