/sydb

Primary LanguageTypeScript

Sydb

A simple local database using the Node.js file system module.


Starting

    const Sydb = require('sydb')

    const db = new Sydb(options)
Parameter Type Optional Default Descrirption
options.path string false ./sydb.json Database File Path
options.split string true / Separator for dividing to reference properties
options.autoSave boolean true false Automatically save when you hear a change
options.spaceJson number true 4 Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.

Examples

Preparing

Json File

    {
        "users": {
            "002": {
                "name": "Pinho",
                "username": "jvopinho",
                "skills": [
                    "JavaScript",
                    "TypeScript",
                    "NodeJS",
                    "ReactJS",
                    "NextJS"
                ]
            }
        }
    }
    const Sydb = require('sydb')

    const db = Sydb(__dirname + '/sydb.json')

Applying reference:

    db.ref('reference')

    // Example

    db.ref('users/001')

Methods

Val

The val method returns the value of the path determined by the reference

    db.ref(reference).val()
    db.ref('users/002').val() // -> { name: 'Pinho', username: 'jvopinho', skills: [...] }

The val method returns the value of the path determined by the reference in map form

    db.ref('users/').val({ type: 'map' }) // -> Map(1) { '002': [Object] }

Set

The set method changes the value of the path determined by the reference.

    db.ref(reference).set(value)
    db.ref('users/002').set({
        name: 'John Pinho',
    }) // -> { users: { '002': { name: 'John Pinho' } } }

Update

The update method changes the desired values of a path determined by the reference.

Note: value must be an object.

    db.ref(reference).update(value)
    db.ref('users/002').update({
        username: 'JPinho'
    }) // -> { users: { '002': { name: 'Pinho', username: 'JPinho', skills: [...] } } }

Delete

The delete method deletes the desired values from a path determined by the reference.

    db.ref('reference').delete()
    db.ref('users/002').delete() // -> { users: {} }