Replies: 13 comments 21 replies
-
If you want to create API endpoints it's recommended to use API routes: https://nextjs.org/docs/api-routes/introduction. API routes are automatically compiled through Next.js' compilation pipeline and allow you to use import/export syntax and a bunch of other syntax that hasn't landed in Node.js yet, similar to pages. If you use a custom server you're basically handing off a request to Next.js and Next.js is unaware of the server existing, it also boots up after the server boots up, meaning we can't compile the server, hence why you can only use Node.js features and Node.js currently does not compile |
Beta Was this translation helpful? Give feedback.
-
Definitely, I don't want to use the custom server, I want to keep all Next.js functionality as it is now. Adding the connection code to the api-routes makes me to create a new connection on each of the API calls. And that is something I would like to avoid. |
Beta Was this translation helpful? Give feedback.
-
If you add it in the global scope it's not re-initialized except for if you use serverless functions (eg when hosting on Vercel). |
Beta Was this translation helpful? Give feedback.
-
Sorry, I can't find how to define the connection in the global scope in Next.js documentation. |
Beta Was this translation helpful? Give feedback.
-
Something like this:
|
Beta Was this translation helpful? Give feedback.
-
for some reasons I always believed the same as @jmaguirrei, that /api routes were always treated as target: well, I'm happy to discover I was wrong |
Beta Was this translation helpful? Give feedback.
-
In development mode, not sure why this happens but queries only work for the first request, the next request fail of finding the Typescript classes.
In production mode, it works fine. |
Beta Was this translation helpful? Give feedback.
-
I put together this concept (tested locally for development and production). The problem: The solution:
|
Beta Was this translation helpful? Give feedback.
-
This is all super helpful. I'm trying to do the same thing and get typeorm+typegraphql+apollo mounted under /pages/api/graphql.js. I ran into the SyntaxError: Cannot use import statement outside a module` |
Beta Was this translation helpful? Give feedback.
-
I'm in the process of adding Next to an existing app (there's a lot of debt to unwind, so going entirely to Next would be a ground-up rewrite), and I've run into the I've come up with the following hack which I hope I can use in development only: const original = Connection.prototype.findMetadata;
Connection.prototype.findMetadata = function findMetadata(target) {
const result = original.call(this, target);
if (result) {
return result;
}
if (typeof target === 'function') {
return this.entityMetadatas.find(
(metadata) => metadata.name === target.name
);
}
}; Does anyone have a better solution? Or, at least confidence that things will "just work" in production with needing this hack? |
Beta Was this translation helpful? Give feedback.
-
Hi there! For those who is facing the problem of Next.js HMR and TypeORM: after researching of different approaches I've come up with the following one: typeorm/typeorm#6241 (comment) Please note you cannot use wildcard paths for entities, since Next.js compiles your entities (along with other JS/TS) files into its own internal bundles, and this path to source entity files is not already relevant: // this will not work:
createConnection({
entities: './orm/models/*.{ts,js}'
}) As well as you cannot use the following method of getting all the entities, since each HMR iteration creates new entities still keeping the old ones, and you get many duplicates of the same entity classes: import { getMetadataArgsStorage } from 'typeorm';
// this is error-prone:
createConnection({
entities: getMetadataArgsStorage().tables.map(tbl => tbl.target)
}) You should specify all entities in a straightforward way. Or create a separate JS/TS file which imports all the entities and reexports them, and then group-import them from this file and paste to the |
Beta Was this translation helpful? Give feedback.
-
Is there a way to put |
Beta Was this translation helpful? Give feedback.
-
Came up with an example repo to showcase TypeORM working with Next.js and TypeGraphQL: https://github.com/aleccool213/next-js-typeorm-typegraphql-example |
Beta Was this translation helpful? Give feedback.
-
I would like to configure TypeORM in server.js
When I run the server (node server.js), as the entities are defined in TypeScript, there is an error:
Describe the solution you'd like
I would like to have a better way to initialize other libraries that I use along with Next.js instead of writing my own server.js.
Define the convention of a file in JS or TS for initializations.
Describe alternatives you've considered
I tried to create the connection on every place that I use the database connection.
Additional context
Add any other context or screenshots about the feature request here.
Beta Was this translation helpful? Give feedback.
All reactions