UNPKG

ice-db

Version:
145 lines (119 loc) 2.75 kB
# IceDB [![npm](https://img.shields.io/npm/dt/ice-db.svg)](https://www.npmjs.com/package/ice-db) ![npm](https://img.shields.io/npm/v/ice-db.svg) ![NpmLicense](https://img.shields.io/npm/l/ice-db.svg) [![David](https://img.shields.io/david/expressjs/express.svg)](https://www.npmjs.com/package/ice-db) [![npm bundle size (minified)](https://img.shields.io/bundlephobia/min/ice-db.svg)](https://www.npmjs.com/package/ice-db) **IceDB is a tiny and fast in-memory database for NodeJS** ## Setup 1. Run `npm i -s ice-db` 1. *(optional step)* Test the package by running `cd node_modules/ice-db && npm test` ## Quick Start 1. First import the module and instantiate the db .Thats it! Now you're ready to go :slightly_smiling_face: ```javascript const { Ice } = require('ice-db'); const db = new Ice(); ``` 2. To create a collection: ```javascript const animals = db.createCollection({ name: 'animals' }) ``` Now you can call a collection by using variable `animals` or `db.animals` 3. To add a new record: ```javascript let daisy = animals.insert({ type: 'cat', name: 'Daisy' }) /* This will return an object like this: { id: '4e6387b1-ac38-4e42-b474-1c6eaee1302d', rev: '1-1542481509127', type: 'cat', name: 'Daisy' }, but the id and rev would be unique */ ``` 4. Records can be accessed by id: ```javascript daisy = animals.get(daisy.id) /* { id: '4e6387b1-ac38-4e42-b474-1c6eaee1302d', rev: '1-1542481509127', type: 'cat', name: 'Daisy' } */ ``` 5. To list all records in a collection: ```javascript animals.list(); ``` 6. To update the record, provide the updated object: ```javascript daisy.type = 'kitty' let updatedDaisy = animals.update(daisy) /* { id: '6b45e668-698d-4d86-ae4a-bceca15af055', rev: '2-1542482143081', type: 'kitty', name: 'Daisy' } */ ``` **Notice:** make sure that the id and rev are there: ```javascript let invalid = { type: 'cat', name: 'Margaret' } try { animals.update(invalid) } catch (e) { console.error(e.message) // ID is not defined } ``` ```javascript let invalid = { type: 'cat', name: 'Margaret', id: daisy.id } try { animals.update(invalid) } catch (e) { console.error(e.message) // Rev is not defined } ``` 7. Delete an object: ```javascript animals.delete(daisy) ``` 8. To clear all data in a collection: ```javascript animals.drop() ``` or ```javascript db.dropCollection(animals) ``` or ```javascript db.dropCollection('animals') ``` 9. You can set a persistent storage for a db: ```javascript db.setStorage({ path: './storage' }) // async db.save().then(()=>{ console.log('Saved') }) // sync db.save(true) // this will create a json file with a name of a collection per each containing all data ```