Initial Commit

Added: app.js basic express http listener.
Added: migrate-tool, to automatically apply migrations, including a base migration to create a database table to track migrations.
This commit is contained in:
Andrew Pietila 2024-01-21 18:52:24 -06:00
commit 8f178392f9
7 changed files with 1577 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules
brainz-social.sqlite3

5
app.js Normal file
View file

@ -0,0 +1,5 @@
const express = require('express');
const app = express();
app.listen(3000);

21
migrate-tool.js Normal file
View file

@ -0,0 +1,21 @@
const migrationConfig = require("./migration-config");
const {globSync} = require('glob');
migrationConfig.getAppliedMigrations(migrationConfig.migrationTarget).then((res) => {
const appliedMigrations = res;
const availableMigrations = globSync("*", {cwd: process.cwd()+"/migrations"}).sort((a, b) => {
a.localeCompare(b);
});
const unAppliedMigrations = availableMigrations.filter((migration) => {
return appliedMigrations.indexOf(migration) === -1;
});
(async () => {
for ( migration of unAppliedMigrations ) {
var migrationImport = require(`${process.cwd()}/migrations/${migration}`);
await migrationImport.up(migrationConfig.migrationTarget);
await migrationConfig.addAppliedMigration(migrationConfig.migrationTarget, migration);
}
})();
});

25
migration-config.js Normal file
View file

@ -0,0 +1,25 @@
const sqlite3 = require("better-sqlite3");
module.exports = {
migrationTarget: new sqlite3("./brainz-social.sqlite3"),
getAppliedMigrations: (target) => {
return new Promise((res, rej) => {
var rows = target.prepare("SELECT * FROM brainz_migrations;").all();
res(rows.map((row) => row.name));
}).catch((rej) => {
if ( rej.code === "SQLITE_ERROR" && rej.message === "no such table: brainz_migrations")
return [];
throw new Error("Unhandled SQLITE error", {cause: rej});
})
},
addAppliedMigration: (target, migrationName) => {
return new Promise((res, rej) => {
target.prepare("INSERT INTO brainz_migrations (name) VALUES (?);").run(migrationName);
});
},
removeAppliedMigration: (target, migrationName) => {
return new Promise((res, rej) => {
target.prepare("DELETE FROM brainz_migrations WHERE name = ?;").run(migrationName);
});
}
}

View file

@ -0,0 +1,9 @@
module.exports = {
up: async (target) => {
target.prepare("CREATE TABLE brainz_migrations (name TEXT NOT NULL);").run();
},
down: async (target) => {
target.prepare("DROP TABLE brainz_migrations;");
}
}

1499
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

16
package.json Normal file
View file

@ -0,0 +1,16 @@
{
"name": "brainz-aggregator",
"version": "0.0.1",
"description": "Brainz Social Aggregator",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Andrew Pietila <a.pietila@protonmail.com>",
"license": "WTFPL",
"dependencies": {
"better-sqlite3": "^9.3.0",
"express": "^4.18.2",
"glob": "^10.3.10"
}
}