activitypress/migrations/20240101055331_lookup.js

73 lines
1.9 KiB
JavaScript

import { generateKeyPair } from "node:crypto";
const SITE_USER_ID = 1_000_000_000;
/**
* Add a lookup table.
* Add an actor type to "users" table.
*/
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const up = async (knex) => {
return knex.schema
.createTable("lookup", (table) => {
table.string("key").primary();
table.string("value");
})
.then(async () => {
await knex.schema.table("users", (table) => {
table.string("actor_type");
});
const { pub, priv } = await (new Promise((resolve, reject) => {
generateKeyPair("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem"
}
}, (err, pub, priv) => {
if (err) reject(err);
else resolve({ pub, priv });
});
}));
await knex("users").insert({
id: SITE_USER_ID,
actor_type: "Application",
name: "site",
nickname: process.env.blog_host,
bio: "The site itself.",
public_key: pub,
private_key: priv,
deleted: false
});
});
;
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const down = async (knex) => {
await knex("users")
.delete()
.where("id", SITE_USER_ID)
;
return knex.schema
.dropTableIfExists("lookup")
.table("users", (table) => {
table.dropColumn("actor_type");
});
;
};