1.2.0 • Published 3 years ago

monkeyhouse v1.2.0

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

Monkeyhouse - a simple, powerful in-memory & file database management

Welcome to Monkeyhouse!

Monkeyhouse lets you to manage your data in a simple, powerful way.Start by typing in

npm i monkeyhouse

in your CLI with npm.Then, you can use the full features of Monkeyhouse as shown below:Before you copy & paste the code, make sure you have

require

d the package in your code.

const DB = require('monkeyhouse')

Setting up database location

You can set a custom folder location for the data to be stored in. This will only be used when you use the DB.SAVE method.

DB.location = 'database'
// also supports
DB.LOCATION = 'database'

You can also use

DB.setLocation('database')

to set the location.

Saving data

You can save data to the database using the DB.SET method. This will save the data in the memory.So, be careful not to overflow your ram!The following code is an example of how to save data to the database:

DB.SET('users', [
    {
        name: 'John',
        age: 20
    },
    {
        name: 'Jane',
        age: 21
    }
])

Getting data

Now you have saved the data, it's time to get it back. You can get the data using the DB.GET method.Below is an example of how to get data from the in-memory database:

DB.GET('users')

The above code will return the data saved in the database, like this:

[ { name: 'John', age: 20 }, { name: 'Jane', age: 21 } ]

Appending data

You can also append data to the database using the DB.ADD method.This is an example of how to append data to the database:

DB.ADD('users', {
    name: 'Jack',
    age: 22
})

This will return the following output:

[
  { name: 'John', age: 20 },
  { name: 'Jane', age: 21 },
  { name: 'Jack', age: 22 }
]

Remember, this method appends data to the database, not replacing it.

Databases on-disk

Monkeyhouse also supports saving data to / importing data from the disk. This is useful when you want to save data to the disk for later use.

Saving data to disk

DB.SAVE('users') will save

{"users":[{"name":"John","age":20},{"name":"Jane","age":21},{"name":"Jack","age":22}]}

to the disk.You can also save data which is not in the memory.For example, you can save your custom JSON like this:

DB.SAVE('address', '{"address":"123 Main St"}')

This will save

{"address":"123 Main St"}

to ./database/address.json

Importing data from a JSON file

You can also use the DB.LOAD method to import data from the disk.The following code is an example of how to import data from the disk:

DB.LOAD('users')

The above code will return the data saved in the database from ./database/users.json , like this:

[ { name: 'John', age: 20 }, { name: 'Jane', age: 21 }, { name: 'Jack', age: 22 } ]

Make sure you don't include the .json extension in the file name.

Update : Username support

Now you can use specific usernames to limit access to the database.TL:DR:Now you can put specific usernames as the parameter like so:

DB.SET('user', {
    superSecretUsersList: ['AgEnT#1 Jack', 'aGeEt#2 Lina']
})
DB.GET('user', 'anotherUser') // this will return an empty object
DB.ALLOW('user', 'anotherUser')
DB.GET('user', 'anotherUser') // now this will return the data

The full example is shown at the bottom of this README.

Finishing

If you have any questions, feel free to contact me at perfect2315shlim@gmail.com.This project is just a toy project which I made, in like 1 hour, so don't rely on this so much.

Examples

const DB = require('../index.js')
DB.location = 'database'
// OR
// DB.LOCATION('database')
// DB.setLocation('database')

DB.SET('users', [
    {
        name: 'John',
        age: 20
    },
    {
        name: 'Jane',
        age: 21
    }
])
console.log(DB.GET('users')) // [ { name: 'John', age: 20 }, { name: 'Jane', age: 21 } ]

DB.ADD('users', {
    name: 'Jack',
    age: 22
})
console.log(DB.GET('users'))
/*
[
  { name: 'John', age: 20 },
  { name: 'Jane', age: 21 },
  { name: 'Jack', age: 22 }
]
*/

DB.SAVE('users') // {"users":[{"name":"John","age":20},{"name":"Jane","age":21},{"name":"Jack","age":22}]}
DB.SAVE('address', '{"address":"123 Main St"}') // {"address":"123 Main St"}

Username support example

const DB = require('../index.js')
DB.SET('user', [{
    name: 'Arthur'
}])
console.log(DB.GET('user'))
console.log('accessDenied', DB.GET('user', 'anotherUser'))
DB.ADD('user', {
    name: 'Eric'
}, 'anotherUser')
console.log('editDenied', DB.GET('user'))

DB.ALLOW('user', 'anotherUser')
console.log('accessGranted', DB.GET('user', 'anotherUser'))

DB.ADD('user', {
    name: 'Kyle'
})
console.log(DB.GET('user'))

DB.ADD('user', {
    name: 'Eric'
}, 'anotherUser')
console.log('editGranted', DB.GET('user'))
1.2.0

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago