ian
ian2y ago

DB access from local script?

@Ethan Brown: Is there an example or any documentation for programmatically connecting to the database from a script I can run locally? That is, I want to have some script foo.js that makes db.<whatever> calls and then run it locally...any advice?
4 Replies
ian
ianOP2y ago
Short answer: access to the DB is limited to server functions (on purpose). Some thoughts on achieving a similar result: 1. You can play around in the node shell with the ConvexHTTPClient, calling queries, mutations, actions, etc. 2. While you’re running npx convex dev, edits to convex functions are synced automatically. Depending how much code you’re bundling it could just take ~200ms to deploy, so the iteration loop is similar to editing a file locally and running locally. 3. If you’re editing the file locally and don’t want to run the functions from a node shell / execute a script, you can run the functions from the dashboard. You can pass parameters, act as a user (from auth pov), and queries you run are live-updating. Does that help? Or what’s the use case that you’d like support for? Some benefits of having the db accessed from functions only: 1. You don’t end up with code that does a bunch of slow round trips to the server for waterfall requests. 2. You can implement your permissions checks in a trusted environment and not rely on setting permissions on the documents themselves. Also, you can avoid surfacing all the fields in a document, return a materialized view of many documents, etc. 3. You have transactions with strong ACID guarantees by default, and don’t hold open a transaction across a network connection, which is really important to keep db access fast and conflict-free.
ballingt
ballingt2y ago
@Ethan Brown here's an example repo showing what 1) above would look like, with scripts for reading and writing to the database in src/download.js and src/write.js. Note that these are still mediated by Convex functions in convex/listMessages.js and convex/writeArbitraryData: https://github.com/thomasballinger/convex-tutorial-write-data-from-node
GitHub
GitHub - thomasballinger/convex-tutorial-write-data-from-node
Contribute to thomasballinger/convex-tutorial-write-data-from-node development by creating an account on GitHub.
ballingt
ballingt2y ago
As Ian says, it requires writing some Convex functions so that writes to and reads from the database are protected. Running these from a local script.js looks like this:
const { ConvexHttpClient } = require("convex/browser");

require("dotenv").config({
// change this to ".env" to connect to the production database
path: ".env.local",
});

const client = new ConvexHttpClient(process.env["VITE_CONVEX_URL"]);

async function getMessages() {
const results = await client.query("listMessages")();
console.log(results);
}

getMessages();
const { ConvexHttpClient } = require("convex/browser");

require("dotenv").config({
// change this to ".env" to connect to the production database
path: ".env.local",
});

const client = new ConvexHttpClient(process.env["VITE_CONVEX_URL"]);

async function getMessages() {
const results = await client.query("listMessages")();
console.log(results);
}

getMessages();
Ethan Brown
Ethan Brown2y ago
This is all very helpful; thanks to everyone who answered!