beingkartik
beingkartik12mo ago

I want to switch from MongoDb to convex in Node Js project. How should I start?

Please help
30 Replies
erquhart
erquhart12mo ago
Welcome! If the data needs to be preserved, and especially if it's a live application with users, you can start by streaming your data into Convex without disrupting the existing data. That way you can start experimenting with rewriting some of your backend against real data. Docs here: https://docs.convex.dev/database/import-export/streaming#streaming-import
beingkartik
beingkartikOP12mo ago
Is there any example github repo for node js?
erquhart
erquhart12mo ago
Even better, there's a quickstart: https://docs.convex.dev/quickstart/nodejs If you specifically want an example, there's a node chat app example repo too: https://github.com/get-convex/convex-node-starter
beingkartik
beingkartikOP12mo ago
Is it necessary to create functions to store data?
erquhart
erquhart12mo ago
I haven't used streaming imports myself but I don't believe it requires writing convex functions. You would use Airbyte to connect to your mongo db as a source and your convex deployment as a destination
beingkartik
beingkartikOP12mo ago
How can we define functions manually on dashboard?
erquhart
erquhart12mo ago
If you want to run a function from the dashboard you'll still need to define it in code locally. With npx convex dev running, your local functions are automatically pushed to your development environment in Convex and will appear on the dashboard.
beingkartik
beingkartikOP12mo ago
I have create a function called addUser and now in controller folder I am using it like this
const user = await convex.mutation('addUser', {
first_name,
last_name,
email,
phone_number,
role: role.toLowerCase(),
is_active: true,
hashedPassword,
})
const user = await convex.mutation('addUser', {
first_name,
last_name,
email,
phone_number,
role: role.toLowerCase(),
is_active: true,
hashedPassword,
})
but its showing Argument of type 'string' is not assignable to parameter of type 'FunctionReference<"mutation">'.
erquhart
erquhart12mo ago
convex.mutation() requires a reference to a convex mutation rather than just the string name. You'll want to import api from ../convex/_generated/api (replace ../convex with the path to your convex directory). Convex functions are accessed through the api object based on their path. For example, if your addUser mutation is defined in convex/user.ts, you would call it as:
const user = await convex.mutation(api.user.addUser, { ...args })
const user = await convex.mutation(api.user.addUser, { ...args })
Example line from the node starter repo: https://github.com/get-convex/convex-node-starter/blob/1c0e9f573ae5e123fe51bc2cc2b5c599f6a262c1/server.js#L19 And docs: https://docs.convex.dev/functions/mutation-functions#calling-mutations-from-clients
beingkartik
beingkartikOP12mo ago
@erquhart I think i should not use convex on node Js server I have because convex itself is a server. Am I right?
erquhart
erquhart12mo ago
If you can avoid it, definitely Life is much easier lol
beingkartik
beingkartikOP12mo ago
Lol. Can you tell me the issues I can face If I do ?
erquhart
erquhart12mo ago
I wouldn't expect issues per se, you just have two backend systems instead of one. It also depends on what you're using the node service for.
beingkartik
beingkartikOP12mo ago
I have all my endpoints on node js server which are interacting with the DB
erquhart
erquhart12mo ago
There's a ton of value in your application client working directly with the Convex service, getting realtime data, etc If you're migrating an existing app, effectively switching out your database for Convex while still using your node service is a forward move. From there you can continue toward working directly against the Convex service over time. Did you try streaming import yet?
beingkartik
beingkartikOP12mo ago
You mean I should use convex on the client side? Not yet
erquhart
erquhart12mo ago
Calling Convex direct from your client is a primary use case for Convex, it enables realtime data in your client with minimal latency. But if you have an existing app you may not be able to use it from your client right away, in which case you'll want to determine a migration path, or at least an experimentation path to start.
beingkartik
beingkartikOP12mo ago
gotcha. Thankyou Hey @erquhart While creating a mutation function how can i create args optional? email: v.string().optional(), title: v.string().optional(), url: v.string().optional(), I am trying this but its throwing error
erquhart
erquhart12mo ago
Hey! You’re close, just have to wrap the optional type:
v.optional(v.string())
v.optional(v.string())
erquhart
erquhart12mo ago
Good breakdown of how to use the different validators here: https://docs.convex.dev/database/schemas#validators
Schemas | Convex Developer Hub
Schema validation keeps your Convex data neat and tidy. It also gives you end-to-end TypeScript type safety!
beingkartik
beingkartikOP12mo ago
export const processInteractiveVideo = action({
args: {
videoId: v.string(),
},
handler: async (ctx,args) => {

const videoData = //TODO: I want to query the video data using args.videoId

const response = await fetch(
`http://backend-dev:8000/web-ui-process-data`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(videoData),
}
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const processedData = await response.json();
},
});
export const processInteractiveVideo = action({
args: {
videoId: v.string(),
},
handler: async (ctx,args) => {

const videoData = //TODO: I want to query the video data using args.videoId

const response = await fetch(
`http://backend-dev:8000/web-ui-process-data`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(videoData),
}
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const processedData = await response.json();
},
});
I have created this action and i want to perform a query inside it to find the video data using video id. How can I perform this? @erquhart
erquhart
erquhart12mo ago
I fed your question into the AI chat in the Convex docs and it did a pretty great job answering: --- In your action, use the runQuery field of the action context (ctx) to call a query that performs the read operation on the desired video data. This process will look like as follows: 1. First, create an internal query that fetches video data based on the video id:
export const fetchVideo = internalQuery({
args: { id: v.id("videos") },
handler: async (ctx, arg) => {
return await ctx.db.get(arg.id);
},
});
export const fetchVideo = internalQuery({
args: { id: v.id("videos") },
handler: async (ctx, arg) => {
return await ctx.db.get(arg.id);
},
});
2. Reference this internal query in your action:
export const processInteractiveVideo = action({
args: {
videoId: v.string(),
},
handler: async (ctx,args) => {
const videoData = await ctx.runQuery(internal.videos.fetchVideo, {
id: args.videoId,
});

// rest of your action logic follows...
},
});
export const processInteractiveVideo = action({
args: {
videoId: v.string(),
},
handler: async (ctx,args) => {
const videoData = await ctx.runQuery(internal.videos.fetchVideo, {
id: args.videoId,
});

// rest of your action logic follows...
},
});
This way, you're calling the fetchVideo query inside your action using the runQuery method. The videoId is provided as an argument to the query and the resulting video data is used in your function.
Do not forget to replace "videos" with the actual schema name for videos in your database if it's different.
beingkartik
beingkartikOP12mo ago
This is so cool, Thankyou Where can i see the logs if I want to log something inside action
erquhart
erquhart12mo ago
Logs from your convex code print in the dashboard logs section You can also pass the --tail-logs flag to npx convex dev to get logs in your CLI if your action is running in the dev environment.
beingkartik
beingkartikOP12mo ago
where should i pass this --tail-logs?
erquhart
erquhart12mo ago
Or you can run npx convex logs locally and get logs for any deployment, including production, just have to specify which environment you want If you want this for dev locally, you would run npx convex dev --tail-logs to run the dev server and get logs in the same command
beingkartik
beingkartikOP12mo ago
hey @erquhart How can I use query function in python code to query the data?
erquhart
erquhart12mo ago
There's a python page in the docs that should get you going: https://docs.convex.dev/client/python
Python | Convex Developer Hub
See the Python Quickstart and the
erquhart
erquhart12mo ago
You can also search for most of what you need in the Convex Search app, it searches across Discord, Stack, and the docs: https://search.convex.dev
Convex Developer Search
Search Docs, Stack, Discord all at once

Did you find this page helpful?