0.2.0 • Published 4 years ago

@dev-your-ops/firelight v0.2.0

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

Welcome to FireLight !

FireLight is a very simple and light database that allows you to share an object with several processes. It is more a shared object than a real database DO NOT use it to process massive or sensitive data!

Get Started

Install

yarn add @dev-your-ops/firelight

or

npm i @dev-your-ops/firelight

Usage

By default, the database file is created at the root of the project with the name "db.json".

const db = require('@dev-your-ops/firelight')
db.load()

you can give a path as an option to store the db wherever you want :

db.load('/tmp/db.json')

get a ref

const user = db.ref('/users/1')

it will take the user if it exists or will create the space for it

set data

user.set({firstname: 'john'});

if you pass true as the second argument, the new data and the old data will be merged together:

user.set({lastname: 'doe'}, true); // user.data() = {firstname: 'john', lastname: 'doe'}

access data

You can access to the ref data by using the association function:

const userData = user.data()

console.log(userData) // => {firstname: 'john'}

get ref childs

You have access to all the children of a ref.

const userChilds = db.ref('/users').childs()

console.log(userChilds) // => [Ref,...]
console.log(userChilds[0].data()) // => {firstname: 'john'}

delete ref

if you want to remove a ref and ALL OF THESE CHILDREN.

// only a user
db.ref('/users/1').delete()

// all users
db.ref('/users').delete()

Example

const db = require('@dev-your-ops/firelight')
db.load()

// subsribe to change
db.on('onload', (e) => console.log('onload:', e));
db.on('set', (e) => console.log('set:', e));
db.on('delete', (e) => console.log('delete:', e));

// create a user
const user = db.ref('/users/1')

// set user data
user.set({ name: 'test' })

// update user data
user.set({ tab: [1, 2, 3] }, true)

console.log(user.data())
// => { name: 'test', tab: [1, 2, 3] }

// Advence
console.log(db.ref("/users/2").set({name: 'user 2'}).data())
console.log(db.ref("/users").childs()[1].data())

// delete the user
db.deleteRef(user.path)

! important

if you are using nodemon or any other hot reload method, then you will need to ignore the db.json file. for example, for nodemon, add a file named "nodemon.json" with:

// nodemon.json
{
	"ignore": ["db.json"]
}

Parameters

the db object:

PropertyTypeWhat is it ?
ref(path)FunctionGet or create a new space to the db associeted with the gived path.
deleteRef(path)FunctionDelete the space on the db at the gived path.
load()FunctionRead the file from the dbPath location an load it into dbData
save()FunctionWrite the stringified data in the json db file.
slugify(str)FunctionReplace all '/' by '-' in str
UID()Functionreturn a uuid as xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
dbDataObjectIs an object with containe the bd json file parsed.
dbPathStringThe file path to save the data from thedatabasedata.

the ref object:

PropertyTypeReturnWhat is it ?
set(data, merge)Functionthe refSet data of ref space (if merge is true the new and old data will be merged).
delete()Functionthe refDelete the current ref
data()Functionthe ref dataGet the ref data.
childs()FunctionRef, Ref...Get all ref childs as an array of ref.
pathObjectthe ref pathThe current ref path.
contextObjectthe db ObjectThe parent db object.

Events

you can subsribe to db event to know whats happened with the data and share it between other process

// fire only at first instansiation
firelight.on( 'onload', (data) => console.log('data:', data));  // => data

firelight.on( 'set',    (e)    => console.log('set:', e));      // => {path, data, merge}
firelight.on( 'delete', (e)    => console.log('delete:', e));   // => path

you can then recreate the db object on another process

import firelight from "@dev-your-ops/firelight"

// on load
firelight.dbData = data

// on set
firelight.set({path, data, merge})

// on delete
firelight.deleteRef(path)
0.2.0

4 years ago

0.1.8

4 years ago

0.1.9

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago