hello
This commit is contained in:
commit
80c6db1abd
5 changed files with 2111 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
node_modules
|
||||||
66
index.js
Normal file
66
index.js
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const express = require('express');
|
||||||
|
const session = require('express-session');
|
||||||
|
const SQLiteStore = require('better-sqlite3-session-store')(session);
|
||||||
|
const betterSqlite3 = require('better-sqlite3');
|
||||||
|
const app = express();
|
||||||
|
const PORT = 3000;
|
||||||
|
|
||||||
|
const db = betterSqlite3('prime.db');
|
||||||
|
|
||||||
|
app.use(session({
|
||||||
|
store: new SQLiteStore({ client: db, expired: { clear: true } }),
|
||||||
|
secret: 'your secret key',
|
||||||
|
resave: false,
|
||||||
|
saveUninitialized: true,
|
||||||
|
cookie: { secure: false }
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
function applyMigrations() {
|
||||||
|
db.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS migrations (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
filename TEXT NOT NULL,
|
||||||
|
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
`);
|
||||||
|
|
||||||
|
const migrationsDir = path.join(__dirname, '/migrations');
|
||||||
|
const migrationFiles = fs.readdirSync(migrationsDir).filter(file => file.endsWith('.sql'));
|
||||||
|
|
||||||
|
migrationFiles.forEach(file => {
|
||||||
|
const isApplied = db.prepare('SELECT filename FROM migrations WHERE filename = ?').get(file);
|
||||||
|
if (!isApplied) {
|
||||||
|
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf-8');
|
||||||
|
db.exec(sql);
|
||||||
|
db.prepare('INSERT INTO migrations (filename) VALUES (?)').run(file);
|
||||||
|
console.log(`Migration applied: ${file}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
applyMigrations();
|
||||||
|
|
||||||
|
app.get('*', (req, res) => {
|
||||||
|
const path = req.path;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const stmt = db.prepare('SELECT * FROM routes WHERE path = ?');
|
||||||
|
const route = stmt.get(path);
|
||||||
|
|
||||||
|
if (route) {
|
||||||
|
res.json({ success: true, message: 'Path found', data: route });
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ success: false, message: 'Path not found' });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json({ success: false, message: 'Internal server error' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`Server is running on http://localhost:${PORT}`);
|
||||||
|
});
|
||||||
59
migrations/000_bootstrap_db.sql
Normal file
59
migrations/000_bootstrap_db.sql
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
CREATE TABLE users (
|
||||||
|
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
username TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE structures (
|
||||||
|
structure_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
user_id INTEGER,
|
||||||
|
FOREIGN KEY(user_id) REFERENCES users(user_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE dbs (
|
||||||
|
db_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
belongs_to_struct INTEGER,
|
||||||
|
FOREIGN KEY(belongs_to_struct) REFERENCES structures(structure_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE templates (
|
||||||
|
template_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
user_id INTEGER,
|
||||||
|
belongs_to_struct INTEGER,
|
||||||
|
FOREIGN KEY(user_id) REFERENCES users(user_id),
|
||||||
|
FOREIGN KEY(belongs_to_struct) REFERENCES structures(structure_id)
|
||||||
|
);
|
||||||
|
CREATE TABLE routes (
|
||||||
|
route_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
verb TEXT CHECK(verb IN ('POST', 'GET', 'PUT', 'DELETE')),
|
||||||
|
path TEXT NOT NULL,
|
||||||
|
structure_id INTEGER,
|
||||||
|
user_id INTEGER,
|
||||||
|
handler TEXT NOT NULL,
|
||||||
|
FOREIGN KEY(structure_id) REFERENCES structures(structure_id),
|
||||||
|
FOREIGN KEY(user_id) REFERENCES users(user_id)
|
||||||
|
);
|
||||||
|
CREATE TABLE migrations (
|
||||||
|
migration_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
db_id INTEGER,
|
||||||
|
sql_content TEXT NOT NULL,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
FOREIGN KEY(db_id) REFERENCES dbs(db_id)
|
||||||
|
);
|
||||||
|
CREATE TABLE structure_dbs (
|
||||||
|
db_id INTEGER,
|
||||||
|
structure_id INTEGER,
|
||||||
|
PRIMARY KEY(db_id, structure_id),
|
||||||
|
FOREIGN KEY(db_id) REFERENCES dbs(db_id),
|
||||||
|
FOREIGN KEY(structure_id) REFERENCES structures(structure_id)
|
||||||
|
);
|
||||||
|
CREATE TABLE structure_templates (
|
||||||
|
template_id INTEGER,
|
||||||
|
structure_id INTEGER,
|
||||||
|
PRIMARY KEY(template_id, structure_id),
|
||||||
|
FOREIGN KEY(template_id) REFERENCES templates(template_id),
|
||||||
|
FOREIGN KEY(structure_id) REFERENCES structures(structure_id)
|
||||||
|
);
|
||||||
|
a
|
||||||
1968
package-lock.json
generated
Normal file
1968
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
17
package.json
Normal file
17
package.json
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
{
|
||||||
|
"name": "bliss",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"better-sqlite3": "^9.4.0",
|
||||||
|
"better-sqlite3-session-store": "^0.1.0",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"express-session": "^1.18.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue