Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,22 @@ await db.write()
You can use TypeScript to check your data types.

```ts
import { JSONFilePreset } from 'lowdb/node'

type Data = {
messages: string[]
}

const defaultData: Data = { messages: [] }
const db = await JSONPreset<Data>('db.json', defaultData)
const db = await JSONFilePreset<Data>('db.json', defaultData)

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

### Lodash

You can extend lowdb with Lodash (or other libraries). To be able to extend it, we're not using `JSONPreset` here. Instead, we're using lower components.
You can extend lowdb with Lodash (or other libraries). To be able to extend it, we're not using `JSONFilePreset` here. Instead, we're using lower components.

```ts
import { Low } from 'lowdb'
Expand Down Expand Up @@ -214,12 +216,15 @@ db.write()

Calls `adapter.read()` and sets `db.data`.

**Note:** `JSONFile` and `JSONFileSync` adapters will set `db.data` to `null` if file doesn't exist.
**Note:** `JSONFile` and `JSONFileSync` adapters return `null` if the file doesn't exist. In that case, `db.read()` leaves `db.data` unchanged, so a new database keeps the `defaultData` passed to its constructor.

```js
db.data // === null
db.read()
db.data // !== null
const defaultData = { posts: [] }
const db = new Low(new JSONFile('db.json'), defaultData)

db.data // === defaultData
await db.read()
// If db.json doesn't exist, db.data is still defaultData.
```

#### `db.write()`
Expand Down Expand Up @@ -312,8 +317,8 @@ new DataFile(filename, {
stringify: YAML.stringify
})
new DataFile(filename, {
parse: (data) => { decypt(JSON.parse(data)) },
stringify: (str) => { encrypt(JSON.stringify(str)) }
parse: (str) => JSON.parse(decrypt(str)),
stringify: (data) => encrypt(JSON.stringify(data))
})
```

Expand Down