generated from azures04/Base-REST-API
Add SQLite database and repositories, remove old routes
Introduces a new SQLite database setup using better-sqlite3, with initialization scripts and repository modules for users, permissions, and licenses. Removes legacy user and register routes and related test files. Updates dependencies to include better-sqlite3, bcryptjs, and ftp-srv.
This commit is contained in:
5
modules/database.js
Normal file
5
modules/database.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const path = require("node:path")
|
||||
const Database = require("better-sqlite3")
|
||||
const databasePath = path.join(process.cwd(), "data", "database.db")
|
||||
|
||||
module.exports = new Database(databasePath)
|
||||
83
modules/databaseGlobals.js
Normal file
83
modules/databaseGlobals.js
Normal file
@@ -0,0 +1,83 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
const Database = require("better-sqlite3")
|
||||
const databasePath = path.join(process.cwd(), "data", "database.db")
|
||||
|
||||
if (!fs.existsSync(path.parse(databasePath).dir)) {
|
||||
fs.mkdirSync(path.parse(databasePath).dir, { recursive: true })
|
||||
}
|
||||
|
||||
const db = new Database(databasePath)
|
||||
|
||||
function initializeDatabase() {
|
||||
db.exec("PRAGMA foreign_keys = ON")
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
firstName TEXT,
|
||||
lastName TEXT,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password TEXT NOT NULL,
|
||||
createdAt TEXT DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`)
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
owner INTEGER NOT NULL,
|
||||
createdAt TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
FOREIGN KEY(owner)
|
||||
REFERENCES accounts(id)
|
||||
ON DELETE CASCADE
|
||||
)
|
||||
`)
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
userId INTEGER NOT NULL,
|
||||
productId INTEGER NOT NULL,
|
||||
channel TEXT DEFAULT '*',
|
||||
permission TEXT NOT NULL,
|
||||
|
||||
FOREIGN KEY(userId)
|
||||
REFERENCES accounts(id)
|
||||
ON DELETE CASCADE,
|
||||
FOREIGN KEY(productId)
|
||||
REFERENCES products(id)
|
||||
ON DELETE CASCADE
|
||||
|
||||
UNIQUE(
|
||||
userId,
|
||||
channel,
|
||||
productId,
|
||||
permission
|
||||
)
|
||||
)
|
||||
`)
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS licenses (
|
||||
id INTEGER PRIMARY KEY,
|
||||
key TEXT NOT NULL UNIQUE,
|
||||
productId INTEGER NOT NULL,
|
||||
targetIhannel TEXT NOT NULL,
|
||||
userId INTEGER DEFAULT NULL,
|
||||
usedAt TEXT,
|
||||
|
||||
FOREIGN KEY(productId)
|
||||
REFERENCES products(id)
|
||||
ON DELETE CASCADE,
|
||||
|
||||
FOREIGN KEY(userId)
|
||||
REFERENCES accounts(id)
|
||||
ON DELETE SET NULL
|
||||
)
|
||||
`)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initializeDatabase
|
||||
}
|
||||
Reference in New Issue
Block a user