typicode / lowdb

Simple and fast JSON database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to disable json beautifier when changed DB

quangnv13 opened this issue · comments

Now lowdb alway beautifier json file. But i want it's minifier for best perfomance.
image

image

How i can do that?

ps: My DB can have 100000 record and may be have 1000000 lines in json file.

commented

Well, I have the same question.
Due to JSONFile adapter has a default JSON.stringify format (

return this.#adapter.write(JSON.stringify(obj, null, 2))
), maybe you need to create a minified version of JSONFile adapter.

PS: the adapter in JSONFile is declared as "private" member, so extends the JSONFile class may be not a chance to resolve this need.

I'd recommend creating a new adapter. You can check existing ones, they're pretty simple.
Here's an example for your case:

import { Adapter } from 'lowdb'
import { TextFile } from 'lowdb/node'

class JSONFileMin<T> implements Adapter<T> {
  #adapter: TextFile

  constructor(filename: string) {
    this.#adapter = new TextFile(filename)
  }

  async read(): Promise<T | null> {
    const data = await this.#adapter.read()
    if (data === null) {
      return null
    } else {
      return JSON.parse(data) as T
    }
  }

  write(obj: T): Promise<void> {
    return this.#adapter.write(JSON.stringify(obj)) // <- minified JSON
  }
}