Closed Pixelherz23 closed 9 months ago
It works on my side with the following code (I didn't use TS to test it quicker but it shouldn't have an impact):
❯ rm file.json
❯ node index.mjs
undefined
❯ bat file.json
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: file.json
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ {
2 │ "messages": [
3 │ "HelloWorld"
4 │ ]
5 │ }
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
❯ bat index.mjs
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: index.mjs
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ import { JSONSyncPreset } from "lowdb/node";
2 │
3 │ const message = 'HelloWorld'
4 │ const defaultData = { messages: [] }
5 │ const db = JSONSyncPreset('file.json', defaultData)
6 │ db.data.messages.push(message)
7 │
8 │ db.write()
9 │ console.log(db.read());
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
/tmp/foo is 📦 v1.0.0 via v21.2.0
❯
So file.json
is created. db.read()
fills db.data
it doesn't return a value. If you want to get log the content of the db, use console.log(db.data)
.
LocalStoragePreset
is if you want to run lowdb in the browser, using window.localStorage
.
I don't know why your code doesn't work though :thinking:
ok, db.data
prints an array with the strings from previous tests I pushed in the DB. And I found the file.json. Its inD:\Users\xxx\AppData\Local\Programs\Microsoft VS Code
. Weird.
db.read() fills db.data
I am quite confused now. I used the code above without .read()
. I changed console.log(db.read());
to console.log(db.data ());
So how can it be that the db object has the data that is written in the file.json if I didnt used .read()
?
Ps: You are awesome for answering so quickly. Most repos have so many questions on read. Thx <3
ok, db.data prints an array with the strings from previous tests I pushed in the DB. And I found the file.json. Its inD:\Users\xxx\AppData\Local\Programs\Microsoft VS Code. Weird.
You can use an absolute path to ensure that file.json is where you expect it to be. You can make it relative to your source file (https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#what-do-i-use-instead-of-__dirname-and-__filename).
So how can it be that the db object has the data that is written in the file.json if I didnt used .read()?
It's not mentioned in the docs, so I understand the confusion. Presets automatically read the database so that can start using data directly.
https://github.com/typicode/lowdb/blob/main/src/presets/node.ts#L29
PS: thank you :bow: but unfortunately I have many questions that are left unanswered because of time/lack of funding ^^' so to be honest here it's a bit of luck ;)
I try to recreate/understand the examples and wrote this typescript function, called in my main:
What I expect:
What I get:
undefined
PS: I dont understand the adpater
LocalStoragePreset
. When should I use it instead of the other adapters? Thx