Moved the nodejs agent to its own subfolder to make room for my haskell agent
This commit is contained in:
parent
0bc0df0891
commit
36cc33f9e9
22 changed files with 0 additions and 0 deletions
33
nodejs/database/db.js
Normal file
33
nodejs/database/db.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
import fs from 'fs';
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
const allMigrations = [
|
||||
'database/000_init.sql',
|
||||
'database/001_systems.sql',
|
||||
'database/002_ships.sql',
|
||||
'database/003_surveys.sql',
|
||||
];
|
||||
|
||||
const db = new Database(
|
||||
process.env.NODE_ENV === 'test' ? 'test.db' : 'spacetraders.db',
|
||||
process.env.NODE_ENV === 'development' ? { verbose: console.log } : null
|
||||
);
|
||||
db.pragma('foreign_keys = ON');
|
||||
db.pragma('journal_mode = WAL');
|
||||
|
||||
db.transaction(function migrate() {
|
||||
let version;
|
||||
try {
|
||||
version = db.prepare('SELECT version FROM schema_version').get().version;
|
||||
} catch {
|
||||
version = 0;
|
||||
}
|
||||
if (version === allMigrations.length) return;
|
||||
while (version < allMigrations.length) {
|
||||
db.exec(fs.readFileSync(allMigrations[version], 'utf8'));
|
||||
version++;
|
||||
}
|
||||
db.exec(`DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (${version});`);
|
||||
})();
|
||||
|
||||
export default db;
|
Loading…
Add table
Add a link
Reference in a new issue