lowdb
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -19,3 +19,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
read() { | ||
const value = localStorage.getItem(__classPrivateFieldGet(this, _LocalStorage_key, "f")); | ||
const value = global.localStorage.getItem(__classPrivateFieldGet(this, _LocalStorage_key, "f")); | ||
if (value === null) { | ||
@@ -27,5 +27,5 @@ return null; | ||
write(obj) { | ||
localStorage.setItem(__classPrivateFieldGet(this, _LocalStorage_key, "f"), JSON.stringify(obj)); | ||
global.localStorage.setItem(__classPrivateFieldGet(this, _LocalStorage_key, "f"), JSON.stringify(obj)); | ||
} | ||
} | ||
_LocalStorage_key = new WeakMap(); |
@@ -13,3 +13,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
var _TextFile_filename, _TextFile_writer; | ||
import fs from 'fs'; | ||
import fs from 'fs/promises'; | ||
import { Writer } from 'steno'; | ||
@@ -26,3 +26,3 @@ export class TextFile { | ||
try { | ||
data = await fs.promises.readFile(__classPrivateFieldGet(this, _TextFile_filename, "f"), 'utf-8'); | ||
data = await fs.readFile(__classPrivateFieldGet(this, _TextFile_filename, "f"), 'utf-8'); | ||
} | ||
@@ -29,0 +29,0 @@ catch (e) { |
{ | ||
"name": "lowdb", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Tiny local JSON database for Node, Electron and the browser", | ||
@@ -34,6 +34,3 @@ "keywords": [ | ||
"lib", | ||
"!lib/**/*.test.*", | ||
"LICENSE-MIT", | ||
"LICENSE-PARITY", | ||
"LICENSE-PATRON" | ||
"!lib/**/*.test.*" | ||
], | ||
@@ -49,22 +46,23 @@ "scripts": { | ||
"dependencies": { | ||
"steno": "^2.1.0" | ||
"steno": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "^13.1.0", | ||
"@commitlint/config-conventional": "^13.1.0", | ||
"@commitlint/prompt-cli": "^13.1.0", | ||
"@sindresorhus/tsconfig": "^2.0.0", | ||
"@types/lodash": "^4.14.172", | ||
"@types/node": "^16.9.1", | ||
"@typicode/eslint-config": "^0.1.2", | ||
"del-cli": "^4.0.1", | ||
"husky": "^7.0.2", | ||
"@commitlint/cli": "^17.1.2", | ||
"@commitlint/config-conventional": "^17.1.0", | ||
"@commitlint/prompt-cli": "^17.1.2", | ||
"@sindresorhus/tsconfig": "^3.0.1", | ||
"@types/lodash": "^4.14.186", | ||
"@types/node": "^18.11.0", | ||
"@typicode/eslint-config": "^1.1.0", | ||
"del-cli": "^5.0.0", | ||
"eslint": "^8.25.0", | ||
"husky": "^8.0.1", | ||
"lodash": "^4.17.21", | ||
"tempy": "^2.0.0", | ||
"typescript": "^4.4.3", | ||
"xv": "^1.0.2" | ||
"tempy": "^3.0.0", | ||
"typescript": "^4.8.4", | ||
"xv": "^1.1.1" | ||
}, | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
"node": ">=14.16" | ||
} | ||
} |
# 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) | ||
> Tiny local JSON database for small projects 🦉 | ||
> Simple to use local JSON database. Powered by plain JavaScript 🦉 | ||
```js | ||
// This is pure JS, not specific to lowdb ;) | ||
db.data.posts.push({ id: 1, title: 'lowdb is awesome' }) | ||
// Edit db.json content using plain JS | ||
db.data | ||
.posts | ||
.push({ id: 1, title: 'lowdb is awesome' }) | ||
@@ -45,5 +47,5 @@ // Save to file | ||
- __Lightweight__ | ||
- __Minimalist__ and easy to learn API | ||
- Query and modify data using __plain JS__ | ||
- Improved __TypeScript__ support | ||
- __Minimalist__ | ||
- __TypeScript__ | ||
- __plain JS__ | ||
- Atomic write | ||
@@ -62,3 +64,3 @@ - Hackable: | ||
_Lowdb 2 is a pure ESM package. If you're having trouble importing it in your project, please [read this](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)._ | ||
_Lowdb 3 is a pure ESM package. If you're having trouble importing it in your project, please [read this](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c)._ | ||
@@ -80,16 +82,16 @@ ```js | ||
// If file.json doesn't exist, db.data will be null | ||
// If db.json doesn't exist, db.data will be null | ||
// Set default data | ||
db.data ||= { posts: [] } | ||
// db.data = db.data || { posts: [] } // for node < v15.x | ||
// db.data = db.data || { posts: [] } // Node < v15.x | ||
db.data ||= { posts: [] } // Node >= 15.x | ||
// Create and query items using plain JS | ||
db.data.posts.push('hello world') | ||
db.data.posts[0] | ||
const firstPost = db.data.posts[0] | ||
// You can also use this syntax if you prefer | ||
// Alternatively, you can also use this syntax if you prefer | ||
const { posts } = db.data | ||
posts.push('hello world') | ||
// Write db.data content to db.json | ||
// Finally write db.data content to file | ||
await db.write() | ||
@@ -107,14 +109,19 @@ ``` | ||
Lowdb now comes with TypeScript support. You can even type `db.data` content. | ||
You can use TypeScript to type check your data. | ||
```ts | ||
type Data = { | ||
posts: string[] // Expect posts to be an array of strings | ||
words: string[] | ||
} | ||
const adapter = new JSONFile<Data>('db.json') | ||
const db = new Low<Data>(adapter) | ||
const db = new Low(adapter) | ||
db.data | ||
.posts | ||
.push(1) // TypeScript error 🎉 | ||
.words | ||
.push('foo') // ✅ | ||
db.data | ||
.words | ||
.push(1) // ❌ | ||
``` | ||
@@ -124,16 +131,30 @@ | ||
You can easily add lodash or other utility libraries to improve lowdb. | ||
You can also add lodash or other utility libraries to improve lowdb. | ||
```js | ||
```ts | ||
import lodash from 'lodash' | ||
// ... | ||
// Note: db.data needs to be initialized before lodash.chain is called. | ||
db.chain = lodash.chain(db.data) | ||
type Post = { | ||
id: number; | ||
title: string; | ||
} | ||
// Instead of db.data, you can now use db.chain if you want to use the powerful API that lodash provides | ||
type Data = { | ||
posts: Post[] | ||
} | ||
// Extend Low class with a new `chain` field | ||
class LowWithLodash<T> extends Low<T> { | ||
chain: lodash.ExpChain<this['data']> = lodash.chain(this).get('data') | ||
} | ||
const adapter = new JSONFile<Data>('db.json') | ||
const db = new LowWithLodash(adapter) | ||
await db.read() | ||
// Instead of db.data use db.chain to access lodash API | ||
const post = db.chain | ||
.get('posts') | ||
.find({ id: 1 }) | ||
.value() // Important: value() needs to be called to execute chain | ||
.value() // Important: value() must be called to execute chain | ||
``` | ||
@@ -330,6 +351,6 @@ | ||
If you have large JavaScript objects (`~10-100MB`) you may hit some performance issues. This is because whenever you call `db.write`, the whole `db.data` is serialized and written to storage. | ||
If you have large JavaScript objects (`~10-100MB`) you may hit some performance issues. This is because whenever you call `db.write`, the whole `db.data` is serialized using `JSON.stringify` and written to storage. | ||
Depending on your use case, this can be fine or not. It can be mitigated by doing batch operations and calling `db.write` only when you need it. | ||
If you plan to scale, it's highly recommended to use databases like PostgreSQL, MongoDB, ... | ||
If you plan to scale, it's highly recommended to use databases like PostgreSQL or MongoDB instead. |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
26750
351
14
+ Addedsteno@3.2.0(transitive)
- Removedsteno@2.1.0(transitive)
Updatedsteno@^3.0.0