LearnSpace: full-stack educational whiteboard platform
Node.js/Express backend + vanilla JS frontend. Features: real-time collaborative whiteboard (SSE), multi-page support, LaTeX formulas, shapes/connectors, coordinate systems, number lines, compass, zoom/pan, Catmull-Rom pencil smoothing, ruler/protractor with rotation & resize controls, minimap navigation overlay, auto-measurements, multi-page thumbnails sidebar, PNG export, page templates. Student/teacher workflows: classes, assignments, library, dashboard. Mobile responsive. SQLite (better-sqlite3). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const path = require('path');
|
||||
|
||||
const fs = require('fs');
|
||||
// Resolve DB_PATH: if absolute — use as-is; if relative — resolve from backend/ dir;
|
||||
// fallback to __dirname-relative default so CWD never matters.
|
||||
const _rawPath = process.env.DB_PATH;
|
||||
const _backendDir = path.join(__dirname, '../../');
|
||||
const dbPath = _rawPath
|
||||
? (path.isAbsolute(_rawPath) ? _rawPath : path.resolve(_backendDir, _rawPath))
|
||||
: path.join(__dirname, '../../data/learnspace.db');
|
||||
const dbDir = path.dirname(dbPath);
|
||||
if (!fs.existsSync(dbDir)) fs.mkdirSync(dbDir, { recursive: true });
|
||||
|
||||
// Auto-migrate from old location (backend/learnspace.db → backend/data/learnspace.db)
|
||||
const oldPath = path.join(__dirname, '../../learnspace.db');
|
||||
if (!fs.existsSync(dbPath) && fs.existsSync(oldPath)) {
|
||||
fs.copyFileSync(oldPath, dbPath);
|
||||
// Also copy WAL/SHM if present
|
||||
if (fs.existsSync(oldPath + '-wal')) fs.copyFileSync(oldPath + '-wal', dbPath + '-wal');
|
||||
if (fs.existsSync(oldPath + '-shm')) fs.copyFileSync(oldPath + '-shm', dbPath + '-shm');
|
||||
console.log('[db] Migrated database from', oldPath, '→', dbPath);
|
||||
}
|
||||
|
||||
const db = new DatabaseSync(dbPath);
|
||||
|
||||
db.exec('PRAGMA journal_mode = WAL');
|
||||
db.exec('PRAGMA foreign_keys = ON');
|
||||
|
||||
/**
|
||||
* Run a synchronous function inside a BEGIN/COMMIT/ROLLBACK transaction.
|
||||
* Returns the value returned by fn(), or rethrows on error.
|
||||
*/
|
||||
db.transaction = function transaction(fn) {
|
||||
return (...args) => {
|
||||
db.exec('BEGIN');
|
||||
try {
|
||||
const result = fn(...args);
|
||||
db.exec('COMMIT');
|
||||
return result;
|
||||
} catch (err) {
|
||||
db.exec('ROLLBACK');
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = db;
|
||||
Reference in New Issue
Block a user