Closed quangnv13 closed 1 year ago
Well, I have the same question.
Due to JSONFile adapter has a default JSON.stringify
format (https://github.com/typicode/lowdb/blob/fdf004fdbe98b5e809ac2033c78d74252fe1bc48/src/adapters/JSONFile.ts#L21), 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
}
}
Now lowdb alway beautifier json file. But i want it's minifier for best perfomance.
How i can do that?
ps: My DB can have 100000 record and may be have 1000000 lines in json file.