Skip to content

How to connect to a database? #370

Closed

Description

@lacker

In my app I have a database that stores data. It can only be connected to from the backend, not the browser, and the code to connect to that database includes some stuff like auth secrets that shouldn't be shipped to the frontend. How should I set this up in next.js, is there a recommended way? I initially thought I could stick something in getInitialProps but then it would get shipped to the client as well.

Activity

  1. nkzawa commented on Dec 9, 2016

    @nkzawa
    Contributor

    We recommend to run API server which connects to database, and access to it on getInitialProps.
    Additionally, you would be able to use database by #291

  2. lacker commented on Dec 9, 2016

    @lacker
    Author

    Ah, that makes sense, thanks for answering my question!

  3. jschr commented on Jan 2, 2017

    @jschr

    To expand this question, I'm curious how you would connect next.js to a database for your typical CRUD app.

    Are you making http requests to an api server in getInitialProps or is the api and client handled by the same server with direct access to the db in getInitialProps?

    The former would make 2 http requests to render the ui (browser -> client server -> api server) whereas the latter it seems like you'd be creating a more monolithic-style project.

  4. arashsa commented on Jan 30, 2017

    @arashsa

    Would be very good to have a tutorial on the nextjs documentation on possible ways to connect to a database, and how you could use sockets to have realtime apps that update on changes to database.

  5. sedubois commented on Jan 31, 2017

    @sedubois
    Contributor

    @ArashA you can e.g look at my app: https://github.com/relatenow/relate. Just doesn't yet have realtime subscriptions but you get the idea.

  6. locked as resolved and limited conversation to collaborators on May 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions