How to disable json beautifier when changed DB
quangnv13 opened this issue · comments
Quang Nguyễn commented
Wenbo commented
Well, I have the same question.
Due to JSONFile adapter has a default JSON.stringify
format (
lowdb/src/adapters/JSONFile.ts
Line 21 in fdf004f
PS: the adapter in JSONFile is declared as "private" member, so extends the JSONFile class may be not a chance to resolve this need.
typicode commented
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
}
}