bucky.db-local
v1.2.1
Published
An npm that creates a local database in your json project
Downloads
18
Maintainers
Readme
Installation:
npm install bucky.db-local --save
yarn add bucky.db-local
Important informations:
- This npm uses node v16.6.0 or higher, another version below this may result in unexpected errors.
- To better understand how to use the functions, take a good look at the examples below the list of available functions!
- All npm functions have the option for you to use a callback return, it is not mandatory.
- The set, update, delete and push functions have a function called “save”, if you do not use this function, the data will only be in the database cache, to save, just run the function.
Setting up database:
There are two ways to configure:
const
{ Database } = require('bucky.db-local'),
db = new Database({
// [...]
});
// Or
const
{ Database, Config } = require('bucky.db-local'),
db = new Database(new Config({
// [...]
}));
Options to configure:
| Name | Description | Default |
| ---- | ----------- | ------- |
| directory | File path where the database should be.
| ./Database.json
|
| objectNotation | Define which object notation.
| /
|
| Serialize | You can use this function to encrypt information.
| false
|
| Deserialize | You can use this function to decrypt the information.
| false
|
| Defaults | When the database is created, it will be created with these values.
| false
|
Functions:
| Name | Params |
| ------ | -------- |
| set | path
values
callback
|
| get | path
callback
|
| update | path
values
callback
|
| delete | path
callback
|
| push | path
values
callback
|
| has | path
callback
|
| all | path
callback
|
| keys | path
callback
|
| values | path
callback
|
| entries | path
callback
|
| toJSON | path
callback
|
Examples:
Set:
const
{ Database } = require('bucky.db-local'),
db = new Database();
let data = db.set('db', 'local');
// It will show the data information along with a function called "save".
// If you don't use the save function, the data will be saved in the cache.
console.log(data);
console.log(data.save()); // Output: true. Value: Boolean
Get:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', 'local').save();
let data = db.get('db');
console.log(data); // Output: local
Update:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', { type: 'local' }).save();
let data = db.update('db', { name: 'bucky.db-local' });
console.log(data.dataValues); // { db: { type: 'local', name: 'bucky.db-local' } }
console.log(data.save()); // Output: true
Delete:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', { name: 'bucky.db-local', type: 'local' }).save();
let data = db.delete('db/name');
console.log(data.save()); // Output: true
Push:
const
{ Database } = require('bucky.db-local'),
db = new Database();
let data = db.push('db', [{ type: 'local' }, { name: 'bucky.db-local' }]);
console.log(data.dataValues); // Output: { db: [ { type: 'local' }, { name: 'bucky.db-local' } ] }
console.log(data.save()); // Output: true
Has:
const
{ Database } = require('bucky.db-local'),
db = new Database();
let data1 = db.has('db');
db.set('db', 'local').save();
let date2 = db.has('db');
console.log(data1); // Output: false
console.log(data2); // Output: true
All:
const
{ Database } = require('bucky.db-local'),
db = new Database();
let data = db.all();
console.log(data); // It will show all values from your database.
Keys:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', { name: 'bucky.db-local', type: 'local' });
let data = db.keys('db');
console.log(data); // Output: [ 'name', 'type' ]
Values:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', { name: 'bucky.db-local', type: 'local' });
let data = db.values('db');
console.log(data); // Output: [ 'bucky.db-local', 'local' ]
Entries:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', { name: 'bucky.db-local', type: 'local' });
let data = db.entries('db');
console.log(data); // Output: [ [ 'name', 'bucky.db-local' ], [ 'type', 'local' ] ]
ToJSON:
const
{ Database } = require('bucky.db-local'),
db = new Database();
db.set('db', { name: 'bucky.db-local', type: 'local' });
let data = db.toJSON('db');
console.log(data); // Output: {"name":"bucky.db-local","type":"local"}