1.1.1 • Published 3 years ago

neudb v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

NeuDB

A simple in-memory database

NeuDb is a simple in-memory database package I made as I needed it in some of my projects, it works very well with Electron, but it also works in any other application, it would not be ideal as a proper database, most likely just as local storage alternative.

I released it publicly for feedback, and for fun. hope helps!

documentation is WIP
read example.js for specifics.

Basic example

Setup

const NeuDB = require('neudb');

const template = {
    name: "",
    last_name: "Doe",//set default value
    age: 0,
    location: {
        continent: "",
        house_number: 0
    },
    notes: []
};

const db = new NeuDB({
    data: template, //set up template data for expected data
});

//OR
const db = new NeuDB({
    data: {//shorten version for readability
        name: "",
        last_name: "Doe",
        notes: []
    },
});

Get Data

db.get('name');

Set Data

db.set('name','John');

Chaining Data

you can also chain most basic functions (get, set, put) ex.

db.get('location').set('house_number', 5);
//or
db.get('location').get('house_number');

When chaining you will (should) receive the last object accessed.

Constructor Properties

data

  • required
  • default: {}
  • Data is a required property used to set the base data for the database, this is useful if you always expect a certain dataset for your database.

autoSave

  • default: true
  • Autosave is if it automatically saves the database on any change.

asBinary

  • default: false
  • asBinary is if it will save as a readable json file. or unreadable (sortof) binary file.

filePath

  • default: __dirname + "/db" (+ file extension) //not ideal path, but it works
  • filepath is the path of the data file it will write the data to (without the file extension).
  • cache

  • default: false
  • with cache set to true, the data will never be stored to disk (unless you call db.save()), it can still load the data if the file exists this will overwrite the value for autoSave to false.

  • customParser

  • details

Functions

Get

Get is the basic function to get data from the database. you can use it very straight forward like:

db.get('property_name');

This function is chainable to get embedded property values.

Set

Set is the basic function to set data on the database. you can use it like:

db.set('property_name', property_value);

This function is also chainable to set embedded property values.

Put (previously push)

Put is the basic function to put data into an array property. If a property is an array, you can put an object onto it using put ex.

db.put('notes', "make readme file");

Put is also chainable to put onto embedded properties. in this case you do not need to add the property name. ex.

//template: {user: {name: "John", notes: []}}

db.get('user').put( "Make readme file")

put does not allow duplicates by default, you can force put a value into the array using the 3rd property force, like so:

db.put('notes', "make readme file", true);

Force is false by default.

Save

Save is needed to save the data, this will be done automatically if you enable autosave, so in that case you don't need to worry about this. Otherwise, you can call it like this:

db.save(); //returns db object (for chaining)

Load

Load is used to load the data on initialization, you can always call it. This could be useful if you want to undo changes and you haven't saved the changes yet (with autosave off).

You use it like:

db.load(); //returns db object for chaining

This will automatically set the data to the loaded data, and return the db object.

Reset

Reset is an advanced function to reset the database structure

You use it like:

db.load(template);

it needs the parameter to be an object with the same properties as the original, otherwise it will throw an error you can overwrite this with a second parameter like this:

db.load({newObject: "this is an example"}, true);

this would overwrite the template to the new structure.

you can do an unsafe reset using:

db.load(db.get());

but this is not recommended

Custom parser

Using the custom parser object you can save and load using custom functions. for example if you want to serialize to a different format as json (ex. yaml)

There's a few properties that come with this function

  • enabled

  • default: false
  • enable the custom parser
  • resetOnError

  • default: true
  • will reset the db if the parser errors (ex. old parser is yaml and you use json parser)
  • stringify

  • default: null
  • function that returns stringified data to write to disk ex: (t)=>{return JSON.stringify(t, null, 4)}
  • parser

  • default: null
  • function that returns parsed object from string (t)=>{return JSON.parse(t)}
  • ext

  • default: 'cst'
  • file extension to save to

Example:

example using the js-yaml library to save as yaml

const NeuDB = require('neudb');
const yaml = require('js-yaml');

const db = new NeuDB({
    data: { sample:true },
    customParser: {
        enabled: true,
        stringify: yaml.dump,
        parser: yaml.load,
        ext: 'yaml'
    }
});
1.1.1

3 years ago

1.1.0

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago