2023-02-05 12:20:17 +01:00
|
|
|
# Mit.db
|
2023-02-05 12:15:49 +01:00
|
|
|
|
|
|
|
```js
|
2023-02-06 10:15:19 +01:00
|
|
|
const MitDB = require('mit.db');
|
|
|
|
const db = new MitDB('file.db'); // this is the save file's name + extension
|
2023-02-05 12:15:49 +01:00
|
|
|
async function sample() {
|
|
|
|
// assuming 'somekey' exists in the Map and has a value { cool: false }
|
2023-02-06 10:15:19 +01:00
|
|
|
const data = db.get('somekey');
|
2023-02-05 12:15:49 +01:00
|
|
|
// reassigning the 'cool' property a new value
|
|
|
|
data.cool = true;
|
2023-02-06 10:15:19 +01:00
|
|
|
await db.set('somekey', data);
|
2023-02-05 12:15:49 +01:00
|
|
|
// now 'somekey' has a new value { cool: true }
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Docs
|
|
|
|
|
|
|
|
#### Installation
|
|
|
|
|
|
|
|
With **npm**:
|
|
|
|
|
2023-02-05 12:20:17 +01:00
|
|
|
`npm i mit.db`
|
2023-02-05 12:15:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
#### Setup
|
|
|
|
|
|
|
|
```js
|
2023-02-23 11:46:26 +01:00
|
|
|
const MitDB = require('mit.db')
|
|
|
|
const db = new MitDB('database.json') // this is the save file's name + extension
|
2023-02-05 12:15:49 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
#### set()
|
|
|
|
|
|
|
|
```js
|
2023-02-06 10:15:19 +01:00
|
|
|
await db.set('ciao', 'hello')
|
|
|
|
await db.set('arrivederci', 'bye')
|
2023-02-05 12:15:49 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
#### get()
|
|
|
|
|
|
|
|
```js
|
2023-02-06 10:15:19 +01:00
|
|
|
var ansa = db.get('ciao') // ansa = hello
|
|
|
|
```
|
|
|
|
|
|
|
|
#### has()
|
|
|
|
|
|
|
|
```js
|
|
|
|
var asnb = db.has('arrivederci') // ansb = true
|
|
|
|
```
|
|
|
|
|
|
|
|
#### entries()
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ansc = db.entries() // ansc = [ 'ciao', 'hello' ], [ 'arrivederci', 'bye' ] ]
|
|
|
|
```
|
|
|
|
|
|
|
|
#### keys()
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ansd = db.keys() // ansd = [ 'ciao', 'arrivederci' ]
|
|
|
|
```
|
|
|
|
|
|
|
|
#### values()
|
|
|
|
|
|
|
|
```js
|
|
|
|
var anse = db.values() // anse = [ 'hello', 'bye' ]
|
|
|
|
```
|
|
|
|
|
|
|
|
#### forEach()
|
|
|
|
|
|
|
|
```js
|
|
|
|
db.forEach((value, key) => console.log(value, key)) // console.log = hello ciao
|
|
|
|
// console.log = bye arrivederci
|
|
|
|
```
|
|
|
|
|
|
|
|
#### delete()
|
|
|
|
|
|
|
|
```js
|
|
|
|
// [{"key":"ciao","value":"hello"}, {"key":"arrivederci","value":"bye"}]
|
|
|
|
await db.delete('ciao')
|
|
|
|
// [{"key":"arrivederci","value":"bye"}]
|
|
|
|
```
|
|
|
|
|
|
|
|
#### clear()
|
|
|
|
|
|
|
|
```js
|
|
|
|
// [{"key":"ciao","value":"hello"}, {"key":"arrivederci","value":"bye"}]
|
|
|
|
await db.delete('ciao')
|
|
|
|
// []
|
2023-02-05 12:15:49 +01:00
|
|
|
```
|
2023-02-06 10:15:19 +01:00
|
|
|
|
|
|
|
#### size()
|
|
|
|
|
|
|
|
```js
|
|
|
|
// [{"key":"ciao","value":"hello"}, {"key":"arrivederci","value":"bye"}]
|
|
|
|
var ansf = db.size() // size = 2
|
|
|
|
```
|