summaryrefslogtreecommitdiff
path: root/database/db.js
diff options
context:
space:
mode:
Diffstat (limited to 'database/db.js')
-rw-r--r--database/db.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/database/db.js b/database/db.js
new file mode 100644
index 0000000..db71080
--- /dev/null
+++ b/database/db.js
@@ -0,0 +1,29 @@
+import fs from 'fs';
+import Database from 'better-sqlite3';
+
+const allMigrations = [
+ 'database/000_init.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.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;