W
Wasp-lang2mo ago
Mohan

how do write to my entity from custom api

How do i write to my entity from custom api
6 Replies
kapa.ai
kapa.ai2mo ago
Hi @Mohan. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To write to your entity from a custom API in Wasp, you need to define the API and implement it as a NodeJS function. Here's a step-by-step guide: 1. Define the API: In your main.wasp file, define the API and include the entity you want to write to in the entities array. For example:
api fooBar {
fn: import { fooBar } from "@src/apis",
entities: [Task],
httpRoute: (GET, "/foo/bar")
}
api fooBar {
fn: import { fooBar } from "@src/apis",
entities: [Task],
httpRoute: (GET, "/foo/bar")
}
In this example, Task is the entity you want to write to. Replace Task with the name of your entity. 2. Implement the API: In your src/apis.js or src/apis.ts file, implement the API as a NodeJS function. This function should take three arguments: req (the Express Request object), res (the Express Response object), and context (an additional context object injected into the API by Wasp). The context object contains information about entities. Here's an example of how you might implement the API:
export const fooBar = (req, res, context) => {
res.json({ count: await context.entities.Task.count() });
};
export const fooBar = (req, res, context) => {
res.json({ count: await context.entities.Task.count() });
};
In this example, context.entities.Task is used to access the Task entity. Replace Task with the name of your entity. Remember, the context.entities.Task object exposes prisma.task from Prisma's CRUD API, so you can use it to perform create, read, update, and delete operations on your entity. For more information, check out the Wasp documentation on using entities in APIs (https://wasp-lang.dev/docs/advanced/apis#using-entities-in-apis). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Mohan, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Mohan
MohanOP2mo ago
oh yeh got it
MEE6
MEE62mo ago
Wohooo @Mohan, you just became a Waspeteer level 3!
Mohan
MohanOP2mo ago
@kapa.ai how do i run my client in a custom port @kapa.ai how do i run my client in a custom port
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To run your client on a custom port in Wasp, you can customize the Vite config. Here's how you can do it: 1. Create a vite.config.js or vite.config.ts file: This file should be in the root directory of your project. 2. Set the port option: In the vite.config.js or vite.config.ts file, set the port option to your desired port number. For example, if you want to use port 4000, your file would look like this: For JavaScript:
export default {
server: {
port: 4000,
},
}
export default {
server: {
port: 4000,
},
}
For TypeScript:
import { defineConfig } from 'vite'

export default defineConfig({
server: {
port: 4000,
},
})
import { defineConfig } from 'vite'

export default defineConfig({
server: {
port: 4000,
},
})
3. Update the WASP_WEB_CLIENT_URL environment variable: In your .env.server file, update the WASP_WEB_CLIENT_URL environment variable to reflect the new port number. For example:
WASP_WEB_CLIENT_URL=<http://localhost:4000>
WASP_WEB_CLIENT_URL=<http://localhost:4000>
Please be careful when changing the dev server port, you'll need to update the WASP_WEB_CLIENT_URL env var in your .env.server file. For more information, check out the Wasp documentation on customizing the Vite config (https://wasp-lang.dev/docs/project/custom-vite-config#examples). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Mohan, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Filip
Filip2mo ago
Hey @Mohan. Unfortunately, this isn't natively supported that (I"ve added your request to the relevant issue: https://github.com/wasp-lang/wasp/issues/730#issuecomment-2344123571). I can help you hack it together if you really need it, but my advice is to just stick with the default port in development. If you're talking about production, then you can run it on any port you want. Let me know what you need and I'll help out the best I can.
GitHub
Make it easy to use different ports (than 3000 and 3001) during Was...
Right now, Wasp has it hardcoded to use 3001 port for api server (backend) and 3000 for web app (frontend). If those ports are already taken, wasp start will fail. This causes problems in different...
Want results from more Discord servers?
Add your server