Skip to content

Commit da2169f

Browse files
committed
feat: add presets to simplify use
1 parent 42b382e commit da2169f

File tree

10 files changed

+55
-46
lines changed

10 files changed

+55
-46
lines changed

README.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ If you like lowdb, please [sponsor](https://github.com/sponsors/typicode).
4646
- Safe atomic writes
4747
- Hackable:
4848
- Change storage, file format (JSON, YAML, ...) or add encryption via [adapters](#adapters)
49-
- Add lodash, ramda, ... for super powers!
49+
- Extend it with lodash, ramda, ... for super powers!
5050

5151
## Install
5252

@@ -59,25 +59,10 @@ npm install lowdb
5959
_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)._
6060

6161
```js
62-
// Remember to set type: module in package.json or use .mjs extension
63-
import { join, dirname } from 'node:path'
64-
import { fileURLToPath } from 'node:url'
62+
import { JSONPreset } from 'lowdb/node'
6563

66-
import { Low } from 'lowdb'
67-
import { JSONFile } from 'lowdb/node'
68-
69-
// db.json file path
70-
const __dirname = dirname(fileURLToPath(import.meta.url))
71-
const file = join(__dirname, 'db.json')
72-
73-
// Configure lowdb to write data to JSON file
74-
const adapter = new JSONFile(file)
7564
const defaultData = { posts: [] }
76-
const db = new Low(adapter, defaultData)
77-
78-
// Read data from JSON file, this will set db.data content
79-
// If JSON file doesn't exist, defaultData is used instead
80-
await db.read()
65+
const db = await JSONPreset('db.json', defaultData)
8166

8267
// Create and query items using plain JavaScript
8368
db.data.posts.push('hello world')
@@ -108,18 +93,19 @@ type Data = {
10893
}
10994

11095
const defaultData: Data = { messages: [] }
111-
const adapter = new JSONFile<Data>('db.json')
112-
const db = new Low<Data>(adapter, defaultData)
96+
const db = await JSONFile<Data>('db.json')
11397

11498
db.data.messages.push('foo') // ✅ Success
11599
db.data.messages.push(1) // ❌ TypeScript error
116100
```
117101

118102
### Lodash
119103

120-
You can also add lodash or other utility libraries to improve lowdb.
104+
You can extend lowdb with Lodash (or other libraries).
121105

122106
```ts
107+
import { Low } from 'lowdb'
108+
import { JSONFile } from 'lowdb/node'
123109
import lodash from 'lodash'
124110

125111
type Post = {
@@ -139,7 +125,7 @@ class LowWithLodash<T> extends Low<T> {
139125
const defaultData: Data = {
140126
posts: [],
141127
}
142-
const adapter = new JSONFile<Data>('db.json')
128+
const adapter = new JSONFile<Data>('db.json', defaultData)
143129
const db = new LowWithLodash(adapter)
144130
await db.read()
145131

@@ -153,6 +139,19 @@ See [`src/examples/`](src/examples) directory.
153139

154140
## API
155141

142+
### Presets
143+
144+
Lowdb provides four presets for common cases.
145+
146+
- `JSONPreset(filename, defaultData)`
147+
- `JSONSyncPreset(filename, defaultData)`
148+
- `LocalStoragePreset(name, defaultData)`
149+
- `SessionStoragePreset(name, defaultData)`
150+
151+
See [`src/examples/`](src/examples) directory for usage.
152+
153+
Lowdb is extremely flexible, if you need to extend it or modify its behavior, use the classes and adapters below instead of the presets.
154+
156155
### Classes
157156

158157
Lowdb has two classes (for asynchronous and synchronous adapters).

src/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './adapters/browser/LocalStorage.js'
22
export * from './adapters/browser/SessionStorage.js'
3-
export * from './presets/browser/WebStoragePreset.js'
3+
export * from './presets/browser.js'

src/examples/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { WebStoragePreset } from '../presets/browser/WebStoragePreset.js'
1+
import { LocalStoragePreset } from '../presets/browser.js'
22

33
type Data = {
44
messages: string[]
55
}
66

77
const defaultData: Data = { messages: [] }
8-
const db = WebStoragePreset<Data>('db', defaultData)
8+
const db = LocalStoragePreset<Data>('db', defaultData)
99

1010
db.data.messages.push('foo')
1111

src/examples/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { JSONSyncPreset } from '../presets/node/JSONPreset.js'
1+
import { JSONSyncPreset } from '../presets/node.js'
22

33
type Data = {
44
messages: string[]
55
}
66

77
const message = process.argv[2] || ''
88
const defaultData: Data = { messages: [] }
9-
const db = JSONSyncPreset('file.json', defaultData)
9+
const db = JSONSyncPreset<Data>('file.json', defaultData)
1010

1111
db.data.messages.push(message)
1212

src/examples/in-memory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare global {
1313
}
1414

1515
type Data = Record<string, unknown>
16-
const defaultData = {}
16+
const defaultData: Data = {}
1717
const adapter: SyncAdapter<Data> =
1818
process.env.NODE_ENV === 'test'
1919
? new MemorySync<Data>()

src/examples/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import express from 'express'
55
import asyncHandler from 'express-async-handler'
66

7-
import { JSONPreset } from '../presets/node/JSONPreset.js'
7+
import { JSONPreset } from '../presets/node.js'
88

99
const app = express()
1010
app.use(express.json())

src/node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './adapters/node/JSONFile.js'
22
export * from './adapters/node/TextFile.js'
3-
export * from './presets/node/JSONPreset.js'
3+
export * from './presets/node.js'

src/presets/browser.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { LocalStorage } from '../adapters/browser/LocalStorage.js'
2+
import { SessionStorage } from '../adapters/browser/SessionStorage.js'
3+
import { LowSync } from '../index.js'
4+
5+
export function LocalStoragePreset<Data>(
6+
key: string,
7+
defaultData: Data,
8+
): LowSync<Data> {
9+
const adapter = new LocalStorage<Data>(key)
10+
const db = new LowSync<Data>(adapter, defaultData)
11+
db.read()
12+
return db
13+
}
14+
15+
export function SessionStoragePreset<Data>(
16+
key: string,
17+
defaultData: Data,
18+
): LowSync<Data> {
19+
const adapter = new SessionStorage<Data>(key)
20+
const db = new LowSync<Data>(adapter, defaultData)
21+
db.read()
22+
return db
23+
}

src/presets/browser/WebStoragePreset.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/presets/node/JSONPreset.ts renamed to src/presets/node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Memory, MemorySync } from '../../adapters/Memory.js'
2-
import { JSONFile, JSONFileSync } from '../../adapters/node/JSONFile.js'
3-
import { Low, LowSync } from '../../core/Low.js'
1+
import { Memory, MemorySync } from '../adapters/Memory.js'
2+
import { JSONFile, JSONFileSync } from '../adapters/node/JSONFile.js'
3+
import { Low, LowSync } from '../core/Low.js'
44

55
export async function JSONPreset<Data>(
66
filename: string | URL,

0 commit comments

Comments
 (0)