-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·131 lines (110 loc) · 3.58 KB
/
index.ts
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bun
import { $ } from 'bun';
import { parse as parseURL } from 'url';
import task from 'tasuku';
import pDB from './src/driver/db';
import translator from './src/driver/translator';
const dbCred = {
host: 'postgresql',
port: 5432,
user: 'pg',
password: 'pg',
database: 'db',
};
// Generate SQL scripts
async function init() {
await task('Generating SQL scripts', async ({ task }) => {
const tasks: Array<string> = [];
await $`rm -rf plv8ify-dist/*.sql`.quiet().nothrow();
for await (let file of $`ls src/backend`.lines()) {
file.length && tasks.push(file);
}
await task.group(
(task) =>
tasks.map((t) =>
task(`Generating ${t}`, async ({ setTitle }) => {
await $`bunx plv8ify generate --input-file ./src/backend/${t}`
.quiet()
.env({});
setTitle(`Generated ${t}`);
}),
),
{ concurrency: Infinity, stopOnError: false },
);
});
}
async function main(): Promise<void> {
// Connect to PostgreSQL
const db = await pDB.connect(dbCred);
await task('Connected to PostgreSQL', async () => Promise.resolve());
// Init extension if not exists
{
await task('Init PostgreSQL extension', async ({ setOutput }) => {
await db.query('CREATE EXTENSION IF NOT EXISTS plv8;');
setOutput('PostgreSQL extension initialized');
});
}
// Install backend scripts
{
await task(
'Installing backend scripts',
async ({ setStatus, task }) => {
$.cwd('./plv8ify-dist');
for await (let file of $`ls .`.lines()) {
if (!file.length) continue;
await task(`Installing ${file}`, async ({ setOutput }) => {
const fileContent = await $`cat ${file}`.text();
await db.query(fileContent);
setOutput(`Success`);
});
}
setStatus('Done ✅');
},
);
}
// Auto-migrate
{
await task('Auto-migrating database', async ({ setOutput }) => {
await db.query('SELECT autoMigrate();');
setOutput('Success');
});
}
// Ignite the server
Bun.serve({
development: false,
port: process.env.PORT || 5000,
async fetch(req) {
const { method, url } = req;
const path = parseURL(url, true).path;
console.log('Incoming request:', {
method,
url,
path,
});
return translator(req, db).catch((err) => {
return new Response(
JSON.stringify({
error: {
code: err.code || 500,
message: err.message || 'Internal Server Error',
},
}),
{
status: 500,
headers: {
'Content-Type': 'application/json',
},
},
);
});
},
});
await task(
'Server is running on port ' + (process.env.PORT || 5000),
async () => Promise.resolve(),
);
}
!(async function () {
const [, , isInit] = process.argv;
!!isInit ? await init() : await main();
})();