This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
forked from jdeck88/phenobase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
50 lines (42 loc) · 1.4 KB
/
init.js
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
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs');
// Define the database file path
const dbFilePath = './database.sqlite';
// Initialize the SQLite database
const db = new sqlite3.Database(dbFilePath, (err) => {
if (err) {
console.error('Error opening database:', err.message);
} else {
console.log('Connected to the SQLite database.');
// Create tables if they don't already exist
initializeDatabase();
}
});
function initializeDatabase() {
// Enable foreign key constraints in SQLite
db.run("PRAGMA foreign_keys = ON;", (err) => {
if (err) {
console.error('Error enabling foreign keys:', err.message);
return;
}
});
// Read the schema from the schema.sql file
const schemaFilePath = 'schema.sql';
fs.readFile(schemaFilePath, 'utf8', (err, schemaSql) => {
if (err) {
console.error('Error reading schema file:', err.message);
return;
}
// Execute the schema SQL
db.serialize(() => {
db.exec(schemaSql, (execErr) => {
if (execErr) {
console.error('Error executing schema SQL:', execErr.message);
} else {
console.log('Database schema initialized successfully.');
}
});
});
});
}