mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 23:16:41 +07:00
#4 added simple DB layer
This commit is contained in:
parent
f3a746efe8
commit
c0588acd26
15 changed files with 476 additions and 11 deletions
17
src/service/db/index.ts
Normal file
17
src/service/db/index.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { VkEvent } from "../vk/types";
|
||||
import { Event, Like } from "./types";
|
||||
|
||||
export interface Storage {
|
||||
getEvent(
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
): Promise<Event>;
|
||||
|
||||
createEvent(event: Event): Promise<Event>;
|
||||
|
||||
getLikesFor(channel: string, messageId: number): Promise<Like[]>;
|
||||
|
||||
getLikeBy(channel: string, messageId: number, author: number): Promise<Like>;
|
||||
}
|
47
src/service/db/postgres/index.ts
Normal file
47
src/service/db/postgres/index.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { Storage } from "../index";
|
||||
import { VkEvent } from "../../vk/types";
|
||||
import { Event } from "../types";
|
||||
import { PostgresConfig } from "./types";
|
||||
import { Connection, createConnection } from "typeorm";
|
||||
|
||||
export class PostgresDB implements Storage {
|
||||
private connection: Connection;
|
||||
constructor(private config: PostgresConfig) {}
|
||||
|
||||
connect = async () => {
|
||||
this.connection = await createConnection({
|
||||
type: "postgres",
|
||||
url: this.config.uri,
|
||||
});
|
||||
};
|
||||
|
||||
getEvent = async (
|
||||
type: VkEvent,
|
||||
id: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
) => {
|
||||
return {
|
||||
type,
|
||||
id,
|
||||
groupId,
|
||||
channel,
|
||||
tgMessageId: 0,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
};
|
||||
|
||||
createEvent = async (event: Event) => event;
|
||||
|
||||
getLikesFor = async (channel: string, messageId: number) => [];
|
||||
|
||||
getLikeBy = async (channel: string, messageId: number, author: number) => ({
|
||||
channel,
|
||||
messageId,
|
||||
author,
|
||||
text: "",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
}
|
3
src/service/db/postgres/types.ts
Normal file
3
src/service/db/postgres/types.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export interface PostgresConfig {
|
||||
uri: string;
|
||||
}
|
7
src/service/db/postgres/validation.ts
Normal file
7
src/service/db/postgres/validation.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { object, string } from "yup";
|
||||
|
||||
export const dbConfigValidatior = object().shape({
|
||||
uri: string()
|
||||
.required()
|
||||
.matches(/^postgres:/),
|
||||
});
|
20
src/service/db/types.ts
Normal file
20
src/service/db/types.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { VkEvent } from "../vk/types";
|
||||
|
||||
export interface Event {
|
||||
type: VkEvent;
|
||||
id: number;
|
||||
groupId: number;
|
||||
channel: string;
|
||||
tgMessageId: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface Like {
|
||||
messageId: number;
|
||||
channel: string;
|
||||
text: string;
|
||||
author: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue