Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode authored Feb 14, 2024
1 parent 31458f3 commit 6fc49cc
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
# lowdb [![](http://img.shields.io/npm/dm/lowdb.svg?style=flat)](https://www.npmjs.org/package/lowdb) [![Node.js CI](https://github.com/typicode/lowdb/actions/workflows/node.js.yml/badge.svg)](https://github.com/typicode/lowdb/actions/workflows/node.js.yml)

> Simple to use type-safe local JSON database 🦉
>
> If you know JavaScript, you know how to use lowdb.
Read or create `db.json`

```js
const db = await JSONFilePreset('db.json', { posts: [] })
```

Update data using `Array.prototype.*` and automatically write to `db.json`
Use plain JavaScript to change data

```js
const post = { id: 1, title: 'lowdb is awesome', views: 100 }

// In two steps
db.data.posts.push(post)
await db.write()

// Or in one
await db.update(({ posts }) => posts.push(post))
```

Expand All @@ -24,17 +32,18 @@ await db.update(({ posts }) => posts.push(post))
}
```

In the same spirit, query using native `Array.prototype.*`
In the same spirit, query using native `Array` functions:

```js
const { posts } = db.data
const first = posts.at(0)
const results = posts.filter((post) => post.title.includes('lowdb'))
const post1 = posts.find((post) => post.id === 1)
const sortedPosts = posts.toSorted((a, b) => a.views - b.views)

posts.at(0) // First post
posts.filter((post) => post.title.includes('lowdb')) // Filter by title
posts.find((post) => post.id === 1) // Find by id
posts.toSorted((a, b) => a.views - b.views) // Sort by views
```

It's that simple.
It's that simple. `db.data` is just a JavaScript object, no magic.

## Sponsors

Expand Down

0 comments on commit 6fc49cc

Please sign in to comment.