Skip to content

Commit 8331e09

Browse files
authored
v7 (#585)
* feat: add new features * chore: update Node version requirement to >=18 * chore: update dependencies in package.json * feat: refactor code to use db.update() instead of directly modifying data * fix: update JSON presets in CLI and server examples * fix: node version typo * fix: refactor file imports in TextFile and TextFileSync classes
1 parent 5769a84 commit 8331e09

File tree

17 files changed

+1528
-668
lines changed

17 files changed

+1528
-668
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [16.x, 18.x, 20.x]
15+
node-version: [18.x, 20.x]
1616

1717
steps:
1818
- uses: actions/checkout@v3

README.md

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
44
55
```js
66
// Read or create db.json
7-
const db = await JSONPreset('db.json', { posts: [] })
7+
const db = await JSONFilePreset('db.json', { posts: [] })
88

9-
// Edit db.json content using plain JavaScript
10-
db.data
11-
.posts
12-
.push({ id: 1, title: 'lowdb is awesome' })
9+
// Update data using Array.prototype.push
10+
// and automatically write to db.json
11+
const post = { id: 1, title: 'lowdb is awesome', views: 100 }
12+
await db.update(({ posts }) => posts.push(post))
1313

14-
// Save to file
15-
db.write()
14+
// Query using Array.prototype.*
15+
const { posts } = db.data
16+
const first = posts.at(0)
17+
const results = posts.filter((post) => post.title.includes('lowdb'))
18+
const post1 = posts.find((post) => post.id === 1)
19+
const sortedPosts = posts.toSorted((a, b) => a.views - b.views)
1620
```
1721

1822
```js
1923
// db.json
2024
{
2125
"posts": [
22-
{ "id": 1, "title": "lowdb is awesome" }
26+
{ "id": 1, "title": "lowdb is awesome", "views": 100 }
2327
]
2428
}
2529
```
@@ -50,6 +54,7 @@ db.write()
5054
- Hackable:
5155
- Change storage, file format (JSON, YAML, ...) or add encryption via [adapters](#adapters)
5256
- Extend it with lodash, ramda, ... for super powers!
57+
- Automatically switches to fast in-memory mode during tests
5358

5459
## Install
5560

@@ -62,21 +67,18 @@ npm install lowdb
6267
_Lowdb is a pure ESM package. If you're having trouble using it in your project, please [read this](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)._
6368

6469
```js
65-
import { JSONPreset } from 'lowdb/node'
70+
import { JSONFilePreset } from 'lowdb/node'
6671

6772
// Read or create db.json
6873
const defaultData = { posts: [] }
69-
const db = await JSONPreset('db.json', defaultData)
74+
const db = await JSONFilePreset('db.json', defaultData)
7075

71-
// Create and query items using plain JavaScript
72-
db.data.posts.push('hello world')
73-
const firstPost = db.data.posts[0]
74-
75-
// If you don't want to type db.data everytime, you can use destructuring assignment
76-
const { posts } = db.data
77-
posts.push('hello world')
76+
// Update db.json
77+
await db.update(({ posts }) => posts.push('hello world'))
7878

79-
// Finally write db.data content to file
79+
// Alternatively you can call db.write() explicitely later
80+
// to write to db.json
81+
db.data.posts.push('hello world')
8082
await db.write()
8183
```
8284

@@ -148,8 +150,8 @@ See [`src/examples/`](src/examples) directory.
148150

149151
Lowdb provides four presets for common cases.
150152

151-
- `JSONPreset(filename, defaultData)`
152-
- `JSONSyncPreset(filename, defaultData)`
153+
- `JSONFilePreset(filename, defaultData)`
154+
- `JSONFileSyncPreset(filename, defaultData)`
153155
- `LocalStoragePreset(name, defaultData)`
154156
- `SessionStoragePreset(name, defaultData)`
155157

@@ -208,6 +210,18 @@ db.data = {}
208210
db.write() // file.json will be {}
209211
```
210212

213+
#### `db.update(fn)`
214+
215+
Calls `fn()` then `db.write()`.
216+
217+
```js
218+
db.update((data) => {
219+
// make changes to data
220+
// ...
221+
})
222+
// files.json will be updated
223+
```
224+
211225
### Properties
212226

213227
#### `db.data`
@@ -258,10 +272,28 @@ new LowSync(new LocalStorage(name), {})
258272
new LowSync(new SessionStorage(name), {})
259273
```
260274

275+
### Utility adapters
276+
261277
#### `TextFile` `TextFileSync`
262278

263279
Adapters for reading and writing text. Useful for creating custom adapters.
264280

281+
#### `DataFile` `DataFileSync`
282+
283+
Adapters for easily supporting other data formats or adding behaviors (encrypt, compress...).
284+
285+
```js
286+
import { DataFile } from 'lowdb'
287+
new DataFile(filename, {
288+
parse: YAML.parse,
289+
stringify: YAML.stringify
290+
})
291+
new DataFile(filename, {
292+
parse: (data) => { decypt(JSON.parse(data)) },
293+
stringify: (str) => { encrypt(JSON.stringify(str)) }
294+
})
295+
```
296+
265297
### Third-party adapters
266298

267299
If you've published an adapter for lowdb, feel free to create a PR to add it here.

0 commit comments

Comments
 (0)