summaryrefslogtreecommitdiff
path: root/node_modules/lowdb/examples/README.md
blob: 8e7022bde9c59328e2eee90e7337c1acd701c0f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Examples

## CLI

```js
// cli.js
const low = require('lowdb')
const db = low('db.json')

db.defaults({ posts: [] })
  .value()

const result = db.get('posts')
  .push({ name: process.argv[2] })
  .value()

console.log(result)
```

```sh
$ node cli.js hello
# [ { title: 'hello' } ]
```

## Browser

```js
import low from 'lowdb'
const db = low('db')

db.defaults({ posts: [] })
  .value()

// Data is automatically saved to localStorage
db.get('posts')
  .push({ title: 'lowdb' })
  .value()
```

## Server

Please __note__ that if you're developing a local server and don't expect to get concurrent requests, it's often easier to use `file-sync` storage, which is the default.

But if you need to avoid blocking requests, you can do so by using `file-async` storage.

```js
const express = require('express')
const low = require('lowdb')
const fileAsync = require('lowdb/lib/file-async')

// Create server
const app = express()

// Start database using file-async storage
const db = low('db.json', {
  storage: fileAsync
})

// Init
db.defaults({ posts: [] })
  .value()

// Define posts
const posts = db.get('posts')

// Routes
// GET /posts/:id
app.get('/posts/:id', (req, res) => {
  const post = posts
    .find({ id: req.params.id })
    .value()

  res.send(post)
})

// POST /posts
app.post('/posts', (req, res) => {
  // Some basic id generation, use uuid ideally
  req.body.id = Date.now()

  // post will be created asynchronously in the background
  const post = posts
    .push(req.body)
    .last()
    .value()

  res.send(post)
})

app.listen(3000, () => console.log('Server is listening'))
```

In the example above, data is written asynchronously in the background. If you want to send the response only after it has been written, set `writeOnChange` to `false` and call `db.write()` manually.

```js
const db = low('db.json', {
  storage: fileAsync,
  writeOnChange: false
})

//...
app.post('/posts', (req, res) => {
  const post = posts
    .push(req.body)
    .last()
    .value()

  db.write()
    .then(() => res.send(post))
})
// ...
```

## In-memory

In this mode, no storage is used. Everything is done in memory.

You can still persist data but you'll have to do it yourself. Here's an example:

```js
const fs = require('fs')
const db = low()

db.defaults({ posts: [] })
  .value()

db.get('posts')
  .push({ title: 'lowdb' })
  .value()

// Manual writing
fs.writeFileSync('db.json', JSON.stringify(db.getState()))
```

In this case, it's recommended to create a custom storage.