link-stack/packages/bridge-common/lib/database.ts

149 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-04-21 16:59:50 +02:00
import { PostgresDialect, CamelCasePlugin } from "kysely";
2024-04-25 12:31:03 +02:00
import type {
GeneratedAlways,
Generated,
ColumnType,
Selectable,
Insertable,
Updateable,
} from "kysely";
2024-04-30 13:13:49 +02:00
import pg from "pg";
2024-03-17 12:58:25 +01:00
import { KyselyAuth } from "@auth/kysely-adapter";
2024-04-30 13:13:49 +02:00
const { Pool, types } = pg;
2024-04-21 16:59:50 +02:00
2024-04-24 21:44:05 +02:00
type Timestamp = ColumnType<Date, Date | string>;
types.setTypeParser(types.builtins.TIMESTAMPTZ, (val) =>
new Date(val).toISOString(),
);
2024-04-21 16:59:50 +02:00
type GraphileJob = {
taskIdentifier: string;
payload: Record<string, any>;
priority: number;
maxAttempts: number;
key: string;
queueName: string;
};
export const addGraphileJob = async (jobInfo: GraphileJob) => {
2024-04-21 20:47:55 +02:00
// await db.insertInto("graphile_worker.jobs").values(jobInfo).execute();
2024-04-21 16:59:50 +02:00
};
2024-03-17 12:58:25 +01:00
2024-04-23 10:31:26 +02:00
export interface Database {
2024-03-17 12:58:25 +01:00
User: {
2024-03-20 17:51:21 +01:00
id: string;
2024-03-17 12:58:25 +01:00
name: string | null;
email: string;
emailVerified: Date | null;
image: string | null;
};
Account: {
id: GeneratedAlways<string>;
userId: string;
2024-03-20 17:51:21 +01:00
type: "oidc" | "oauth" | "email" | "webauthn";
2024-03-17 12:58:25 +01:00
provider: string;
providerAccountId: string;
2024-03-20 17:51:21 +01:00
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;
2024-03-17 12:58:25 +01:00
};
Session: {
id: GeneratedAlways<string>;
userId: string;
sessionToken: string;
expires: Date;
};
VerificationToken: {
identifier: string;
token: string;
expires: Date;
};
2024-04-23 13:36:51 +02:00
WhatsappBot: {
2024-03-17 12:58:25 +01:00
id: GeneratedAlways<string>;
2024-04-21 08:11:24 +02:00
name: string;
2024-04-25 13:36:50 +02:00
description: string;
2024-04-21 08:11:24 +02:00
phoneNumber: string;
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
2024-04-25 13:36:50 +02:00
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>;
};
2024-04-21 08:11:24 +02:00
2024-04-23 13:36:51 +02:00
VoiceLine: {
2024-04-21 08:11:24 +02:00
id: GeneratedAlways<string>;
name: string;
2024-04-25 13:36:50 +02:00
description: string;
2024-04-21 08:11:24 +02:00
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
SignalBot: {
id: GeneratedAlways<string>;
name: string;
2024-04-25 13:36:50 +02:00
description: string;
2024-04-21 08:11:24 +02:00
phoneNumber: string;
createdBy: string;
createdAt: Date;
updatedAt: Date;
2024-03-17 12:58:25 +01:00
};
2024-04-23 13:36:51 +02:00
Webhook: {
id: GeneratedAlways<string>;
name: string;
2024-04-25 13:36:50 +02:00
description: string;
2024-04-29 17:27:25 +02:00
backendType: string;
backendId: string;
endpointUrl: string;
httpMethod: "post" | "put";
headers: Record<string, any>;
2024-04-23 13:36:51 +02:00
createdBy: string;
createdAt: Date;
updatedAt: Date;
};
2024-03-17 12:58:25 +01:00
}
2024-04-25 13:36:50 +02:00
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"]>;
2024-04-25 12:31:03 +02:00
2024-03-17 12:58:25 +01:00
export const db = new KyselyAuth<Database>({
dialect: new PostgresDialect({
pool: new Pool({
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME,
2024-04-23 10:31:26 +02:00
port: parseInt(process.env.DATABASE_PORT!),
2024-03-17 12:58:25 +01:00
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
}),
}),
plugins: [new CamelCasePlugin()],
});