added db intialization on server start
This commit is contained in:
parent
d21f53fa9e
commit
040e2226df
43
server/db.js
Normal file
43
server/db.js
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
const initSqlJs = require('sql.js');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
let db = null;
|
||||||
|
const DB_PATH = './ka.db';
|
||||||
|
|
||||||
|
async function initDb() {
|
||||||
|
const SQL = await initSqlJs();
|
||||||
|
|
||||||
|
console.log('Initializing Database...');
|
||||||
|
|
||||||
|
// Loading Database if it already exists
|
||||||
|
if (fs.existsSync(DB_PATH)) {
|
||||||
|
const buffer = fs.readFilySync(DB_PATH);
|
||||||
|
db = new SQL.Database(buffer);
|
||||||
|
console.log('Database Loaded')
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// Creating a new one if it does not
|
||||||
|
console.log('Database not found. Creating new instance...')
|
||||||
|
db = new SQL.Database();
|
||||||
|
|
||||||
|
// Creating Knowledge Article table
|
||||||
|
db.run(`
|
||||||
|
CREATE TABLE articles (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
ka_number TEXT UNIQUE,
|
||||||
|
title TEXT,
|
||||||
|
content TEXT,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Saving the created db to file
|
||||||
|
const data = db.export();
|
||||||
|
fs.writeFileSync(DB_PATH, Buffer.from(data));
|
||||||
|
console.log(`Database created at ${DB_PATH}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {initDb};
|
||||||
BIN
server/ka.db
Normal file
BIN
server/ka.db
Normal file
Binary file not shown.
9
server/package-lock.json
generated
9
server/package-lock.json
generated
@ -9,7 +9,8 @@
|
|||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^5.1.0"
|
"express": "^5.1.0",
|
||||||
|
"sql.js": "^1.13.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/accepts": {
|
"node_modules/accepts": {
|
||||||
@ -754,6 +755,12 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/sql.js": {
|
||||||
|
"version": "1.13.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/sql.js/-/sql.js-1.13.0.tgz",
|
||||||
|
"integrity": "sha512-RJbVP1HRDlUUXahJ7VMTcu9Rm1Nzw+EBpoPr94vnbD4LwR715F3CcxE2G2k45PewcaZ57pjetYa+LoSJLAASgA==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/statuses": {
|
"node_modules/statuses": {
|
||||||
"version": "2.0.2",
|
"version": "2.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
|
||||||
|
|||||||
@ -13,6 +13,7 @@
|
|||||||
"author": "Matt Taylor",
|
"author": "Matt Taylor",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^5.1.0"
|
"express": "^5.1.0",
|
||||||
|
"sql.js": "^1.13.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,19 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
const { initDb } = require('./db');
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 9000;
|
const PORT = 9000;
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
initDb().then(() => {
|
||||||
res.json({message : 'Server is running'});
|
|
||||||
});
|
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.get('/', (req, res) => {
|
||||||
console.log(`Server running on http://localhost:${PORT}`);
|
res.json({message : 'Server is running'});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`Server running on http://localhost:${PORT}`);
|
||||||
|
});
|
||||||
|
}).catch(err => {
|
||||||
|
console.error('Failed to initialize database:', err);
|
||||||
});
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user