From 6fc49ccb69666ad9159d1403514549a6af5fa8ef Mon Sep 17 00:00:00 2001 From: typicode Date: Wed, 14 Feb 2024 12:00:47 +0100 Subject: [PATCH] Update README.md --- README.md | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 08a0c0a..88c789f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # 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` @@ -8,10 +10,16 @@ Read or create `db.json` 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)) ``` @@ -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