lowdb icon indicating copy to clipboard operation
lowdb copied to clipboard

data not persisting when reading file on server restart

Open jmsbrett opened this issue 3 years ago • 0 comments

I have a project set up with nodejs and lowdb.

Nodejs version: v16.16.0 Lowdb: v3.0.0

Issue persisting data after restart:

import { join, dirname } from 'path'
import { Low, JSONFile } from 'lowdb'
import { fileURLToPath } from 'url'

const __dirname = dirname(fileURLToPath(import.meta.url));

// Use JSON file for storage
const file = join(__dirname, './db.json');
const adapter = new JSONFile(file);
const db = new Low(adapter);

db.data ||= {
    scenarios: ["Operational Scenario 1", "Operational Scenario 2"],
    tests: [
        {
            name: "Operational Scenario 1",
            command: "ls -la"
        },
        {
            name: "Operational Scenario 1",
            command: "ls -la"
        },
        {
            name: "Operational Scenario 2",
            command: "ls -la"
        },
        {
            name: "Operational Scenario 2",
            command: "ls -la"
        },
    ]
}

await db.write();

export default db;

It seem that the db.data does not actually exist and then lowdb overwrites the file.

jmsbrett avatar Sep 09 '22 16:09 jmsbrett