basedb
v3.0.0
Published
basedb is a lightweight and portable database for storing data in json format
Downloads
8
Maintainers
Readme
basedb
A lightweight database
Structure
Database
→ FolderCollection
→ File ( json )Document
→{"package": "basedb"}
Installation
npm i basedb
Usage
- Require Module
const {database} = require('basedb');
- Create Database
let db = new database('DATABASE');
- Create Collection
db.createCollection('user',(err)=>{
if (err) throw err;
console.log('collection created');
});
Insert Document
- Single Document
let doc = { username: 'bit-web', email: '[email protected]', password: "********" }; db.createCollection('user',(err, collection)=>{ if (err) throw err; console.log('collection created'); collection.insert(doc,(err)=>{ if (err) throw err; console.log('document inserted'); }); });
- Multiple Documents
let docs = [ { username: 'bit-web', email: '[email protected]', password: "********" }, { username: 'bit-web2', email: '[email protected]', password: "########" } ]; db.createCollection('user',(err, collection)=>{ if (err) throw err; console.log('collection created'); collection.insert(docs,(err)=>{ if (err) throw err; console.log('document inserted'); }); });
Insert Document (if the collection already exists)
- Single Document
let doc = { username: 'bit-web', email: '[email protected]', password: "********" }; db.collection('user').insert(doc,(err)=>{ if (err) throw err; console.log('document inserted'); });
- Multiple Documents
let docs = [ { username: 'bit-web', email: '[email protected]', password: "********" }, { username: 'bit-web2', email: '[email protected]', password: "########" } ]; db.collection('user').insert(docs,(err)=>{ if (err) throw err; console.log('documents inserted'); });
Find
db.collection('user').get(item =>{
if (item.username === 'bit-web' && item.password === "********") {
console.log(item);
}else{
console.log('couldn\'t found the document');
};
});
Note: Always take the parameter
item
- Update
db.collection('user').get(item =>{
if (item.username === 'bit-web' && item.password === "********") {
item.username = 'npmjs';
item.email = '[email protected]';
}else{
console.log('couldn\'t found the document');
};
});
Note: Always take the parameter
item
- Delete Collection
db.delete('user',(err)=>{
if (err) throw err;
console.log('collection deleted');
});
- Remove Database
const {database, remove} = require('basedb');
remove('DATABASE',(err)=>{
if (err) throw err;
console.log('database removed');
});
license
ISC