How to establish a mongo database connection
I want to connect to a mongo database, usually I put this at the entry point of the server, like so:
How can I achieve this in start's context?
12 Replies
like-gold•4mo ago
Hey folks, I'd like to bump this, no matter what I've tried, I can't seem to make MongoDB work with TanStack Start
correct-apricot•4mo ago
I don't think you'll want to put a database driver into the route context since it's also available on the client. If you're trying to do that you're probably leaking server code on the client and breaking the build.
As long as you make sure it's only accessed in server code there shouldn't be anything special about connecting to mongodb
like-gold•4mo ago
Of course I don't want it to be accessible from the client side. But despite my best attempts to isolate the code to the server, I run into errors.
I think I've isolated correctly, but now I get this: `ReferenceError: exports is not defined at eval (/node_modules/mongodb/lib/index.js:4:23)ˋ
correct-apricot•4mo ago
Is this in the dev environment with node? Also are you on the stable version or the alpha? I can probably try to get a simple demo going later this evening
like-gold•4mo ago
Yes in dev, I'm on 1.120.12
I think it may be due to the mongo package being shared CommonJS and ESM but I'm not sure, these were always things beyond my grasp 😅
correct-apricot•4mo ago
Yeah, it could possibly need some special config in Vite. The modules and bundling and etc are confusing for sure 😛
like-gold•4mo ago
In case you/someone want to pursue, I published a repo with my (for now broken) approach here: https://github.com/buzzb0x/tanstack-start-mongo-broken
If we get it working, would be great to open a PR to add an example to the official repo
GitHub
GitHub - buzzb0x/tanstack-start-mongo-broken
Contribute to buzzb0x/tanstack-start-mongo-broken development by creating an account on GitHub.
correct-apricot•4mo ago
I had an LLM quickly add a mongodb example to my start-basic example repo https://github.com/danecando/start-basic/commit/8bb36f3cd32f0f90d466a031043f79dfe1758b63
correct-apricot•4mo ago
It's got a docker compose to stand up mongodb, a script that adds some initial data, and it replaces the example fetch posts to use the driver and fetch posts from the db
This is on the alpha release of start. I figure it should probably work on main too but just a heads up
Should be fine to just create a db connection and export directly from a module. Just make sure to use it only in server fns or routes so that it doesn't leak into the client
like-gold•4mo ago
Thank you, I will take a look and see if I can find a difference as to why my example is not working if yours is
afraid-scarlet•3mo ago
I just came searching with a similar question.
I would have assumed that this sort of code belongs with the server entry?
https://tanstack.com/start/latest/docs/framework/react/learn-the-basics#the-server-entry-point-optional
My thinking is there is a server only util file for the db connection, which can be reused by middleware.
The connection logic is kicked off in the server entry piece.
Learn the Basics | TanStack Start React Docs
This guide will help you learn the basics behind how TanStack Start works, regardless of how you set up your project. Dependencies TanStack Start is powered by and . TanStack Router: A router for buil...
correct-apricot•3mo ago
You could just export the mongo connection from a module and import it in other ones (just make sure they aren't included on the client).
Really don't need to pass it through middleware