jeremydaly / lambda-api

Lightweight web framework for your serverless applications
https://serverless-api.com
MIT License
1.41k stars 125 forks source link

Add a context #197

Open tcarac opened 2 years ago

tcarac commented 2 years ago

Hey, im loving lambda-api so far. Quick question, is there a way to add a context to handlers ? Let's say

app.get("/posts", getPosts);
async function getPosts(req,res,context){
const prisma = context.prisma;
return prisma.posts.findMany({});
}

And this is basically for code splitting

warapitiya commented 2 years ago

You can use req.context to retrieve the context object.

GuidoNebiolo commented 1 year ago

Hi @tcarac you can do this assigning the service you want directly to app during initialization.

example: const api = require('lambda-api')() api.services = {prisma} api.post('/', async (req, res) => { const prisma = api.services.prisma; return prisma.posts.findMany({});}