-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread-template.tmpl
108 lines (98 loc) · 3.01 KB
/
read-template.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict"
const Sequelize = require('sequelize');
const config = {
"username": process.env.DB_USER,
"password": process.env.DB_PASSWORD,
"database": process.env.DB_NAME,
"host": process.env.DB_HOST,
"dialect": process.env.DB_DIALECT,
"port": process.env.DB_PORT,
"timeout": process.env.DB_TIMEOUT,
};
module.exports = async (event, context, callback) => {
const conditionsArray = [
isEmpty(config.database),
isEmpty(config.username),
isEmpty(config.password),
isEmpty(config.host),
isEmpty(config.port),
isEmpty(config.dialect)
]
if (conditionsArray.includes(true)) {
return context
.status(400)
.headers({"Content-Type": "application/json"})
.succeed({"message": "one of the DB configurations is empty"});
}
const client = new Sequelize(
config.database,
config.username,
config.password,
{
host: config.host,
port: config.port,
dialect: config.dialect,
dialectOptions: {
connectTimeout: Number(config.timeout)
}
});
try {
await client.sync({ force: true }).then(() => {
console.log("Connected successfully")
})
} catch (err) {
console.log("caught", err.message);
return context
.status(500)
.headers({"Content-Type": "application/json"})
.succeed({"message": err.message});
}
console.log("check url params")
{{- $combine := ""}}
{{- $datatype := .Params.Type}}
{{- range $key, $value := .Params.Schema }}
{{- range $key, $value := $value }}
{{- $data := (printf "event.query.%s" $key) }}
{{- if eq $datatype "body" }}
{{- $data = (printf "event.body.%s" $key) }}
{{- end}}
{{- if $value.Required }}
if (!{{($data)}}) {
return context
.status(400)
.headers({"Content-Type": "application/json"})
.succeed({"message": "{{$key}} is required"});
}
{{- else if eq $value.Type "string" }}
if (!{{($data)}}) {
{{ (printf "%s = '%s'" $data $value.DefaultValue) }}
}
{{ else }}
if (!{{($data)}}) {
{{ (printf "%s = %s" $data $value.DefaultValue) }}
}
{{ end }}
{{- $combine = (printf "%s %s," $combine $data) }}
{{- end}}
{{- end}}
let result
try {
result = await client.query("{{.Query}}", {
type: client.QueryTypes.SELECT,
bind: [{{$combine}}],
});
} catch (error) {
console.log("caught", error.message);
return context
.status(500)
.headers({"Content-Type": "application/json"})
.succeed({"message": error.message});
}
return context
.status(200)
.headers({"Content-Type": "application/json"})
.succeed({"data": result});
}
function isEmpty(value){
return (value == undefined || value === "");
}