Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kearfy/locdb
Json db for nodejs
https://github.com/kearfy/locdb
Last synced: about 1 month ago
JSON representation
Json db for nodejs
- Host: GitHub
- URL: https://github.com/kearfy/locdb
- Owner: kearfy
- License: mpl-2.0
- Created: 2019-07-26T14:02:47.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-09T15:36:06.000Z (almost 5 years ago)
- Last Synced: 2024-05-01T19:39:11.005Z (8 months ago)
- Language: JavaScript
- Size: 245 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# LocDB
LocDB is a local in-memory database module for NodeJS. It stores data in json files.
LocDB is more reliable than you would think, since it does not constantly read from the json files, it only does that once you initialize the module, after that it is stored in memory and only written to the json files when updated.## Installation
Use the package manager [npm](https://npmjs.com) to install LocDB.
```bash
npm i locdb
```## Usage
initialize the module
```javascript
var Locdb = require('locdb');
var database = new LocDB('/path/to/directory');
```retrieve, define or update and remove data.
```javascript
//Your data is stored as an Object or Array (based on what you stored in your json file.), so you can treat it just like any javascript Array or Object.
//All databases are stored within .dbconsole.log(database.db.databaseName.someKey); //undefined
database.db.databaseName.someKey = 'value';
console.log(database.db.databaseName.someKey); //'value'database.db.databaseName.someKey = 'otherValue';
console.log(database.db.databaseName.someKey); //'otherValue'remove database.db.databaseName.someKey;
console.log(database.db.databaseName.someKey); //undefined
```**setUpdateInterval(Number: interval):** Change the interval (milliseconds) in which the json files are update for changes. Default is 500ms. (half a second)
```javascript
database.setUpdateInterval(500);
```**exists(String: db, [OPTIONAL] Boolean: file):** Check if a database is registered or if the JSON file for the database exists.
```javascript
//db is registered and file exists.
database.exists('databaseName') //true
database.exists('databaseName', true) //true//db is registered but file is missing.
database.exists('databaseName') //true
database.exists('databaseName', true) //false//db is note registered but file exists.
database.exists('databaseName') //false
database.exists('databaseName', true) //true//db is not registered and file is missing.
database.exists('databaseName') //false
database.exists('databaseName', true) //false
```**register(String: db):** Register a new database. If a JSON file for the database already exists, it won't be overwritten.
```javascript
database.register('otherDatabase'); //won't return anything.
```**purge(String: db):** Removes a database, you will have to make a backup yourself.
```javascript
database.purge('otherDatabase'); //won't return anything.
```**log(String: msg):** system function, puts out a message in the console and log file.
```javascript
database.log('Log message');
```config.json: stores configuration and a list of databases.
```json
{
"updateInterval": 500,
"list": [
"example"
]
}
```Directory structure:
```
database_directory/
├───files
│ └───example.json
├───logs
│ └───date.locdb.log
└───config.json
```## Contributing
If you want to make an addition to the project, please make a pull request or for major changes, open an issue.## License
[MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/)