Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Created January 18, 2022 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/6ce7413586a424ee706d4b718c4be8b4 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/6ce7413586a424ee706d4b718c4be8b4 to your computer and use it in GitHub Desktop.
Serverless Framework express, prisma (mysql) sample
const serverless = require('serverless-http');
const express = require('express');
const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');
const app = express();
app.use('/', indexRouter);
app.use('/users',usersRouter);
module.exports = app;
module.exports.handler = serverless(app);
var express = require('express');
var router = express.Router();
const { PrismaClient } = require('@prisma/client')
/* GET home page. */
router.get('/', function(req, res, next) {
console.log("root");
res.json({ret: 'root OK'});
});
router.get('/test', function(req, res, next) {
console.log("test");
res.json({ret: 'OK'});
});
//
router.get('/tasks',async function(req, res, next) {
console.log("tasks");
const prisma = new PrismaClient()
const items = await prisma.task.findMany()
await prisma.$disconnect()
console.log( items)
res.json({data: items});
});
//
router.post('/task_create',async function(req, res, next) {
try{
console.log("task_create");
//console.log(req.apiGateway.event.body);
const data = JSON.parse(req.apiGateway.event.body);
console.log(data);
const prisma = new PrismaClient()
const result = await prisma.task.create({
data: {
title: data.body,
userId: 0
}
})
await prisma.$disconnect();
console.log(result);
res.json({ret: 'OK'});
} catch (e) {
console.error(e);
res.json({ret: 'NG'});
}
});
module.exports = router;
{
"name": "sls-express1",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www"
},
"dependencies": {
"@prisma/client": "^3.8.1",
"aws-sdk": "^2.1058.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"hbs": "~4.0.4",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"nodemon": "^2.0.15",
"serverless-http": "^2.7.0"
},
"devDependencies": {
"prisma": "^3.8.1",
"serverless-dynamodb-local": "^0.2.40",
"serverless-offline": "^8.3.1"
}
}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String?
published Boolean @default(false)
user User @relation(fields: [userId], references: [id])
userId Int
}
model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
password String
email String @unique
name String?
posts Post[]
profile Profile?
}
model Task {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String? @db.Text
userId Int?
}
service: sls-express6
# app and org for use with dashboard.serverless.com
frameworkVersion: "2"
plugins:
- serverless-dynamodb-local
- serverless-offline
provider:
name: aws
runtime: nodejs14.x
region: ap-northeast-1
stage: dev
lambdaHashingVersion: 20201221
functions:
hello:
handler: app.handler
events:
- http:
path: /
method: ANY
- http:
path: /test
method: ANY
cors: true
- http:
path: /tasks
method: ANY
cors: true
- http:
path: /task_create
method: post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment