Comments (7)
That is an excellent point! I had thought to use the Deno filesystem APIs, but you are right that all npm modules can be imported directly now. My experience is that they don't always work, but I'll try to check lowdb and report back.
from lowdb.
@typicode You were absolutely correct, in my minimal test, lowdb worked in deno without any additional messing around. Here's the test case I used in case anyone else wants to test it.
import { Low } from "npm:lowdb";
import { JSONFile } from "npm:lowdb/node";
type Data = {
posts: string[];
};
const test = async () => {
// File path
const file = "./data/db.json";
// Configure lowdb to write to JSONFile
const adapter = new JSONFile<Data>(file);
const db = new Low(adapter);
// Read data from JSON file, this will set db.data content
await db.read();
// If db.json doesn't exist, db.data will be null
// Use the code below to set default data
// db.data = db.data || { posts: [] } // For Node < v15.x
db.data ||= { posts: [] }; // For Node >= 15.x
// Create and query items using native JS API
db.data.posts.push("hello world");
const firstPost = db.data.posts[0];
console.log("#7sqNUh firstPost", firstPost);
// Alternatively, you can also use this syntax if you prefer
const { posts } = db.data;
posts.push("hello world");
// Finally write db.data content to file
await db.write();
};
// Learn more at https://deno.land/manual/examples/module_metadata#concepts
if (import.meta.main) {
await test();
}
I'll close the issue now.
from lowdb.
Hmm, I started looking into this, and I realise it's not quite as simple as I first thought. The tests are based on running in node, and so they won't work under deno. 🤔
Maybe a separate package makes more sense then?
from lowdb.
Thanks for the nice words. Aren't deno compatible with npm modules now?
from lowdb.
This used to work in Deno and Cloudflare workers, I used it when it was 1.0 and wanted to use it now, but didn't work..
Working in 1.0:
const { default: low } = await import('lowdb');
const {default: Memory } = await import('lowdb/adapters/Memory');
const db = low(new Memory())
const dbContent = await fetch('https://test.source.com/json').then((res)=>res.json());
db.defaults(dbContent)
.write()
const result = db.get('posts')
Update: Still works in Deno with lowdb 1.0 though ;-)
from lowdb.
@chmac While this is great, I was looking at getting it working in Cloudflare workers with the latest version. But when I tried the latest memory mapper, it doesn't allow writes at all.
Use case: retrieve data from a cloudflare KV value and write it to in-memory mapper.
from lowdb.
Thanks @chmac good to know :)
@tmikaeld, I don't know why it doesn't work. Lowdb with in-memory adapter is pure JS without any Node API. If you figure out the root cause, I'd be interested to know :)
from lowdb.
Related Issues (20)
- vue 安装lowdb后,用import 方式引入的时候产生问题 HOT 1
- does lowdb support dot syntax? HOT 4
- error in clustermode HOT 1
- Using lowdb in svelte causes 500 error HOT 1
- Writing data doesn't work when using the Lodash Example HOT 3
- Add new key value pair to existing database HOT 1
- Cannot find module 'lowdb/node' or its correspoing type declaractions HOT 4
- Why Json File is pretty HOT 1
- JSONFile path error HOT 2
- db.json updates but db.value() does not show it in terminal HOT 1
- Throws ERR_INVALID_URL HOT 6
- Think again if you don't want to deal with bunch of documents just because of this ESM forcing. HOT 1
- pnpm 安装后使用报错 HOT 1
- Problems with setting up a small helloWorld HOT 3
- Lowdb .db.json.tmp version 6.1.1 HOT 6
- .
- error TS1192: Module '"node:fs"' has no default export. HOT 1
- Uncaught TypeError: (0 , import_node_path.dirname) is not a function HOT 1
- Compare the advantages of localForage HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lowdb.