1.2.2 • Published 7 years ago

jsfdb v1.2.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

jsfdb

Current Version Build Status

Simple JSON File Based Database

Installation

$ npm install jsfdb

or

$ yarn add jsfdb

Usage

Constructor(rootDir: string): Object;

Always returns Object. There is error property on failure, no error property on success. rootDir is the root directory for databases, it is ./data by default.

var jsfdb = require("jsfdb")("./data");

if (jsfdb.error) {
    console.log("failed to initialize jsfdb =>", jsfdb.error);
    process.exit(1);
    return;
}

console.log("jsfdb initialized");

rootDir(): string;

Returns root directory that provided on construction.

console.log("jsfdb root is '" + jsfdb.rootDir() + "'");

genID(): string;

Returns a unique id based on process.pid ^ Date.now().

console.log("jsfdb generate ID =>", jsfdb.genID());

getDBs(cb?: (err: Error, dbs: Array) => void): void;

Gets list of available database names as array.

jsfdb.getDBs((err, dbs) => {
    if (err) {
        console.log("getDBs (failure) =>", err);
    }
    else {
        console.log("getDBs (success) =>");
        for (var i = 0; i < dbs.length; i++) {
            console.log("[" + i + "]: " + dbs[i]);
        }
    }
});

createDB(db: string, cb?: (err: Error) => void): void;

Creates database with provided name.

jsfdb.createDB("Hello", (err) => {
    if (err) {
        console.log("createDB (failure) =>", err);
    }
    else {
        console.log("createDB (success) => Hello");
    }
});

checkDB(db: string, cb?: (err: Error) => void): void;

Checks database existance.

jsfdb.checkDB("Hello", (err) => {
    if (err) {
        console.log("checkDB (failure) =>", err);
    }
    else {
        console.log("checkDB (success) => Hello");
    }
});

destroyDB(db: string, cb?: (err: Error) => void): void;

Destroys database with provided name.

jsfdb.destroyDB("Hello", (err) => {
    if (err) {
        console.log("destroyDB (failure) =>", err);
    }
    else {
        console.log("destroyDB (success) => Hello");
    }
});

getDocs(db: string, cb?: (err: Error, ids: Array) => void): void;

Gets list of document IDs as array from database.

jsfdb.getDocs("Hello", (err, ids) => {
    if (err) {
        console.log("getDocs (failure) =>", err);
    }
    else {
        console.log("getDocs (success) =>");
        for (var i = 0; i < ids.length; i++) {
            console.log("[" + i + "]: " + ids[i]);
        }
    }
});

getDoc(db: string, id: string, cb?: (err: Error, doc: Object) => void): void;

Gets document object from database with provided ID.

jsfdb.getDoc("Hello", "World", (err, doc) => {
    if (err) {
        console.log("getDoc (failure) =>", err);
    }
    else {
        console.log("getDoc (success) => Hello.World");
        console.log(JSON.stringify(doc));
    }
});

setDoc(db: string, id: string, data: Object, cb?: (err: Error) => void): void;

Sets document object in database with provided ID.

jsfdb.setDoc("Hello", "World", { id: jsfdb.genID() }, (err) => {
    if (err) {
        console.log("setDoc (failure) =>", err);
    }
    else {
        console.log("setDoc (success) => Hello.World");
    }
});

deleteDoc(db: string, id: string, cb?: (err: Error) => void): void;

Deletes document in database with provided ID.

jsfdb.deleteDoc("Hello", "World", (err) => {
    if (err) {
        console.log("deleteDoc (failure) =>", err);
    }
    else {
        console.log("deleteDoc (success) => Hello.World");
    }
});

Tests

$ npm test

or

$ yarn test

License

MIT

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.2

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago