I want to switch from MongoDb to convex in Node Js project. How should I start?
Please help
30 Replies
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
Streaming Data in and out of Convex | Convex Developer Hub
Streaming Data in and out of Convex
Is there any example github repo for node js?
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
Is it necessary to create functions to store data?
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
How can we define functions manually on dashboard?
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.I have create a function called addUser and now in controller folder I am using it like this but its showing Argument of type 'string' is not assignable to parameter of type 'FunctionReference<"mutation">'.
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:
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@erquhart I think i should not use convex on node Js server I have because convex itself is a server. Am I right?
If you can avoid it, definitely
Life is much easier lol
Lol. Can you tell me the issues I can face If I do ?
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.
I have all my endpoints on node js server which are interacting with the DB
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?
You mean I should use convex on the client side?
Not yet
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.
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
Hey! You’re close, just have to wrap the optional type:
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!
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
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
Do not forget to replace
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:
2. Reference this internal query in your action:
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.This is so cool, Thankyou
Where can i see the logs if I want to log something inside action
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.where should i pass this --tail-logs?
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 commandhey @erquhart How can I use query function in python code to query the data?
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
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