Skip to content

Commit 47c4cd3

Browse files
committed
Create server initialization
1 parent 17cb3bb commit 47c4cd3

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

packages/backend/db/create.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE DATABASE taskdb;

packages/backend/db/setup.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE IF NOT EXISTS tasks (
2+
id SERIAL PRIMARY KEY,
3+
name VARCHAR(255) NOT NULL,
4+
etag INT NOT NULL DEFAULT 0
5+
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
import { pgconfig } from '@/config.js';
4+
import { readFile } from 'fs/promises';
5+
import pg from 'pg';
6+
export async function runSQLFile(filePath: string) {
7+
const config = pgconfig(process.env);
8+
const client = new pg.Client(config);
9+
10+
try {
11+
await client.connect();
12+
const sql = await readFile(filePath, { encoding: 'utf8' });
13+
await client.query(sql);
14+
console.log(`Executed SQL file: ${filePath}`);
15+
} catch (err) {
16+
console.error(`Error executing SQL file ${filePath}:`, err);
17+
} finally {
18+
await client.end();
19+
}
20+
}
21+
22+
await runSQLFile(process.argv[2]);

0 commit comments

Comments
 (0)