Refactoring 2

This commit is contained in:
Darren Clarke 2024-04-30 11:39:16 +02:00
parent dd14dfe72e
commit e4b78ceec2
76 changed files with 870 additions and 734 deletions

View file

@ -0,0 +1,93 @@
import * as path from "path";
import { fileURLToPath } from "url";
import { promises as fs } from "fs";
import {
Kysely,
Migrator,
MigrationResult,
FileMigrationProvider,
PostgresDialect,
CamelCasePlugin,
} from "kysely";
import pkg from "pg";
const { Pool } = pkg;
import * as dotenv from "dotenv";
interface Database {}
export const migrate = async (arg: string) => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
if (process.env.NODE_ENV !== "production") {
dotenv.config({ path: path.join(__dirname, "../.env.local") });
}
const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME,
port: parseInt(process.env.DATABASE_PORT!),
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
}),
}),
plugins: [new CamelCasePlugin()],
});
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: path.join(__dirname, "migrations"),
}),
});
let error = null;
let results: MigrationResult[] = [];
if (arg === "up:all") {
const out = await migrator.migrateToLatest();
results = out.results ?? [];
error = out.error;
} else if (arg === "up:one") {
const out = await migrator.migrateUp();
results = out.results ?? [];
error = out.error;
} else if (arg === "down:all") {
const migrations = await migrator.getMigrations();
for (const _ of migrations) {
const out = await migrator.migrateDown();
if (out.results) {
results = results.concat(out.results);
error = out.error;
}
}
} else if (arg === "down:one") {
const out = await migrator.migrateDown();
if (out.results) {
results = out.results ?? [];
error = out.error;
}
}
results?.forEach((it) => {
if (it.status === "Success") {
console.log(
`Migration "${it.migrationName} ${it.direction.toLowerCase()}" was executed successfully`,
);
} else if (it.status === "Error") {
console.error(`Failed to execute migration "${it.migrationName}"`);
}
});
if (error) {
console.error("Failed to migrate");
console.error(error);
process.exit(1);
}
await db.destroy();
};
const arg = process.argv.slice(2).pop();
migrate(arg as string);

View file

@ -0,0 +1,72 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("User")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("name", "text")
.addColumn("email", "text", (col) => col.unique().notNull())
.addColumn("emailVerified", "timestamptz")
.addColumn("image", "text")
.execute();
await db.schema
.createTable("Account")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("userId", "uuid", (col) =>
col.references("User.id").onDelete("cascade").notNull(),
)
.addColumn("type", "text", (col) => col.notNull())
.addColumn("provider", "text", (col) => col.notNull())
.addColumn("providerAccountId", "text", (col) => col.notNull())
.addColumn("refresh_token", "text")
.addColumn("access_token", "text")
.addColumn("expires_at", "bigint")
.addColumn("token_type", "text")
.addColumn("scope", "text")
.addColumn("id_token", "text")
.addColumn("session_state", "text")
.execute();
await db.schema
.createTable("Session")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("userId", "uuid", (col) =>
col.references("User.id").onDelete("cascade").notNull(),
)
.addColumn("sessionToken", "text", (col) => col.notNull().unique())
.addColumn("expires", "timestamptz", (col) => col.notNull())
.execute();
await db.schema
.createTable("VerificationToken")
.addColumn("identifier", "text", (col) => col.notNull())
.addColumn("token", "text", (col) => col.notNull().unique())
.addColumn("expires", "timestamptz", (col) => col.notNull())
.execute();
await db.schema
.createIndex("Account_userId_index")
.on("Account")
.column("userId")
.execute();
await db.schema
.createIndex("Session_userId_index")
.on("Session")
.column("userId")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("Account").ifExists().execute();
await db.schema.dropTable("Session").ifExists().execute();
await db.schema.dropTable("User").ifExists().execute();
await db.schema.dropTable("VerificationToken").ifExists().execute();
}

View file

@ -0,0 +1,35 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("SignalBot")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("phone_number", "text")
.addColumn("token", "text", (col) => col.unique().notNull())
.addColumn("user_id", "uuid")
.addColumn("name", "text")
.addColumn("description", "text")
.addColumn("auth_info", "text")
.addColumn("is_verified", "boolean", (col) =>
col.notNull().defaultTo(false),
)
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("SignalBotToken")
.on("SignalBot")
.column("token")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("SignalBot").ifExists().execute();
}

View file

@ -0,0 +1,35 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("WhatsappBot")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("phone_number", "text")
.addColumn("token", "text", (col) => col.unique().notNull())
.addColumn("user_id", "uuid")
.addColumn("name", "text")
.addColumn("description", "text")
.addColumn("auth_info", "text")
.addColumn("is_verified", "boolean", (col) =>
col.notNull().defaultTo(false),
)
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("WhatsappBotToken")
.on("WhatsappBot")
.column("token")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("WhatsappBot").ifExists().execute();
}

View file

@ -0,0 +1,77 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("VoiceProvider")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("kind", "text", (col) => col.notNull())
.addColumn("name", "text", (col) => col.notNull())
.addColumn("description", "text")
.addColumn("credentials", "jsonb", (col) => col.notNull())
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("VoiceProviderName")
.on("VoiceProvider")
.column("name")
.execute();
await db.schema
.createTable("VoiceLine")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("provider_id", "uuid", (col) =>
col.notNull().references("VoiceProvider.id").onDelete("cascade"),
)
.addColumn("provider_line_sid", "text", (col) => col.notNull())
.addColumn("number", "text", (col) => col.notNull())
.addColumn("name", "text", (col) => col.notNull())
.addColumn("description", "text")
.addColumn("language", "text", (col) => col.notNull())
.addColumn("voice", "text", (col) => col.notNull())
.addColumn("prompt_text", "text")
.addColumn("prompt_audio", "jsonb")
.addColumn("audio_prompt_enabled", "boolean", (col) =>
col.notNull().defaultTo(false),
)
.addColumn("audio_converted_at", "timestamptz")
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("VoiceLineProviderId")
.on("VoiceLine")
.column("provider_id")
.execute();
await db.schema
.createIndex("VoiceLineProviderLineSid")
.on("VoiceLine")
.column("provider_line_sid")
.execute();
await db.schema
.createIndex("VoiceLineNumber")
.on("VoiceLine")
.column("number")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("VoiceLine").ifExists().execute();
await db.schema.dropTable("VoiceProvider").ifExists().execute();
}

View file

@ -0,0 +1,38 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("FacebookBot")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("name", "text")
.addColumn("description", "text")
.addColumn("token", "text")
.addColumn("page_access_token", "text")
.addColumn("app_secret", "text")
.addColumn("verify_token", "text")
.addColumn("page_id", "text")
.addColumn("app_id", "text")
.addColumn("user_id", "uuid")
.addColumn("is_verified", "boolean", (col) =>
col.notNull().defaultTo(false),
)
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("FacebookBotToken")
.on("FacebookBot")
.column("token")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("FacebookBot").ifExists().execute();
}

View file

@ -0,0 +1,41 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("Webhook")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("name", "text", (col) => col.notNull())
.addColumn("description", "text")
.addColumn("backend_type", "text", (col) => col.notNull())
.addColumn("backend_id", "uuid", (col) => col.notNull())
.addColumn("endpoint_url", "text", (col) =>
col.notNull().check(sql`endpoint_url ~ '^https?://[^/]+'`),
)
.addColumn("http_method", "text", (col) =>
col
.notNull()
.defaultTo("post")
.check(sql`http_method in ('post', 'put')`),
)
.addColumn("headers", "jsonb")
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("WebhookBackendTypeBackendId")
.on("Webhook")
.column("backend_type")
.column("backend_id")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("Webhook").ifExists().execute();
}

View file

@ -0,0 +1,28 @@
import { Kysely, sql } from "kysely";
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("Setting")
.addColumn("id", "uuid", (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn("name", "text")
.addColumn("value", "jsonb")
.addColumn("created_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn("updated_at", "timestamptz", (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex("SettingName")
.on("Setting")
.column("name")
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable("Setting").ifExists().execute();
}

View file

@ -0,0 +1,2 @@
export { db, type Database } from "./lib/database";
export { getWorkerUtils } from "./lib/utils";

View file

@ -0,0 +1,147 @@
import { PostgresDialect, CamelCasePlugin } from "kysely";
import type {
GeneratedAlways,
Generated,
ColumnType,
Selectable,
Insertable,
Updateable,
} from "kysely";
import { Pool, types } from "pg";
import { KyselyAuth } from "@auth/kysely-adapter";
type Timestamp = ColumnType<Date, Date | string>;
types.setTypeParser(types.builtins.TIMESTAMPTZ, (val) =>
new Date(val).toISOString(),
);
type GraphileJob = {
taskIdentifier: string;
payload: Record<string, any>;
priority: number;
maxAttempts: number;
key: string;
queueName: string;
};
export const addGraphileJob = async (jobInfo: GraphileJob) => {
// await db.insertInto("graphile_worker.jobs").values(jobInfo).execute();
};
export interface Database {
User: {
id: string;
name: string | null;
email: string;
emailVerified: Date | null;
image: string | null;
};
Account: {
id: GeneratedAlways<string>;
userId: string;
type: "oidc" | "oauth" | "email" | "webauthn";
provider: string;
providerAccountId: string;
refresh_token: string | undefined;
access_token: string | undefined;
expires_at: number | undefined;
token_type: Lowercase<string> | undefined;
scope: string | undefined;
id_token: string | undefined;
session_state: string | undefined;
};
Session: {
id: GeneratedAlways<string>;
userId: string;
sessionToken: string;
expires: Date;
};
VerificationToken: {
identifier: string;
token: string;
expires: Date;
};
WhatsappBot: {
id: GeneratedAlways<string>;
name: string;
description: string;
phoneNumber: string;
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
FacebookBot: {
id: GeneratedAlways<string>;
name: string | null;
description: string | null;
token: string | null;
pageAccessToken: string | null;
appSecret: string | null;
verifyToken: string | null;
pageId: string | null;
appId: string | null;
userId: string | null;
isVerified: Generated<boolean>;
createdAt: GeneratedAlways<Timestamp>;
updatedAt: GeneratedAlways<Timestamp>;
};
VoiceLine: {
id: GeneratedAlways<string>;
name: string;
description: string;
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
SignalBot: {
id: GeneratedAlways<string>;
name: string;
description: string;
phoneNumber: string;
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
Webhook: {
id: GeneratedAlways<string>;
name: string;
description: string;
backendType: string;
backendId: string;
endpointUrl: string;
httpMethod: "post" | "put";
headers: Record<string, any>;
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
}
export type FacebookBot = Selectable<Database["FacebookBot"]>;
export type SignalBot = Selectable<Database["SignalBot"]>;
export type WhatsappBot = Selectable<Database["WhatsappBot"]>;
export type VoiceLine = Selectable<Database["VoiceLine"]>;
export type Webhook = Selectable<Database["Webhook"]>;
export type User = Selectable<Database["User"]>;
export const db = new KyselyAuth<Database>({
dialect: new PostgresDialect({
pool: new Pool({
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME,
port: parseInt(process.env.DATABASE_PORT!),
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
}),
}),
plugins: [new CamelCasePlugin()],
});

View file

@ -0,0 +1,13 @@
import { makeWorkerUtils, WorkerUtils } from "graphile-worker";
let workerUtils: WorkerUtils;
export const getWorkerUtils = async () => {
if (!workerUtils) {
workerUtils = await makeWorkerUtils({
connectionString: process.env.DATABASE_URL,
});
}
return workerUtils;
};

View file

@ -0,0 +1,26 @@
{
"name": "bridge-common",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"author": "Darren Clarke <darren@redaranj.com>",
"license": "AGPL-3.0-or-later",
"scripts": {
"build": "tsc -p tsconfig.json",
"dev": "NODE_OPTIONS=\"--loader ts-node/esm\" graphile-worker"
},
"dependencies": {
"@auth/kysely-adapter": "^1.0.0",
"kysely": "^0.26",
"pg": "^8.11.5"
},
"devDependencies": {
"@babel/core": "7.24.4",
"@babel/preset-env": "7.24.4",
"@babel/preset-typescript": "7.24.1",
"eslint": "^9.0.0",
"prettier": "^3.2.5",
"ts-config": "*",
"typescript": "^5.4.5"
}
}

View file

@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": ".",
"paths": {
"@/*": ["./*", "../../node_modules/*"]
},
"plugins": [
{
"name": "next"
}
]
},
"include": ["**.d.ts", "**/*.ts", "**/*.tsx", "**/*.png, **/*.svg"],
"exclude": ["node_modules", "babel__core"]
}

File diff suppressed because one or more lines are too long