mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
#4 implemented postgres events
This commit is contained in:
parent
50db549cbe
commit
e73ab3cb4f
3 changed files with 55 additions and 51 deletions
|
@ -1,6 +1,6 @@
|
|||
import { VkEvent } from "../vk/types";
|
||||
import { StoredEvent, StoredLike } from "./types";
|
||||
import { Like } from "./postgres/entities/Like";
|
||||
import { Event } from "./postgres/entities/Event";
|
||||
|
||||
export interface Storage {
|
||||
getEvent(
|
||||
|
@ -8,17 +8,13 @@ export interface Storage {
|
|||
id: number,
|
||||
groupId: number,
|
||||
channel: string
|
||||
): Promise<StoredEvent>;
|
||||
): Promise<Event>;
|
||||
|
||||
createEvent(event: Partial<StoredEvent>): Promise<StoredEvent>;
|
||||
createEvent(event: Partial<Event>): Promise<Event>;
|
||||
|
||||
createOrUpdateLike(like: Partial<StoredLike>): Promise<Like>;
|
||||
createOrUpdateLike(like: Partial<Like>): Promise<Like>;
|
||||
|
||||
getLikesFor(channel: string, messageId: number): Promise<StoredLike[]>;
|
||||
getLikesFor(channel: string, messageId: number): Promise<Like[]>;
|
||||
|
||||
getLikeBy(
|
||||
channel: string,
|
||||
messageId: number,
|
||||
author: number
|
||||
): Promise<StoredLike>;
|
||||
getLikeBy(channel: string, messageId: number, author: number): Promise<Like>;
|
||||
}
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
import { Storage } from "../index";
|
||||
import { VkEvent } from "../../vk/types";
|
||||
import { StoredEvent } from "../types";
|
||||
import { PostgresConfig } from "./types";
|
||||
import { Connection, createConnection } from "typeorm";
|
||||
import { Connection, createConnection, Repository } from "typeorm";
|
||||
import logger from "../../logger";
|
||||
import path from "path";
|
||||
import { Like } from "./entities/Like";
|
||||
import { Event } from "./entities/Event";
|
||||
|
||||
const entities = [path.join(__dirname, "./entities/*")];
|
||||
|
||||
export class PostgresDB implements Storage {
|
||||
private connection: Connection;
|
||||
private events: Repository<Event>;
|
||||
private likes: Repository<Like>;
|
||||
|
||||
constructor(private config: PostgresConfig) {}
|
||||
|
||||
connect = async () => {
|
||||
|
@ -23,6 +27,9 @@ export class PostgresDB implements Storage {
|
|||
|
||||
await this.connection.synchronize();
|
||||
|
||||
this.events = this.connection.getRepository(Event);
|
||||
this.likes = this.connection.getRepository(Like);
|
||||
|
||||
logger.info(`db connected to ${this.config.uri}`);
|
||||
};
|
||||
|
||||
|
@ -32,28 +39,50 @@ export class PostgresDB implements Storage {
|
|||
groupId: number,
|
||||
channel: string
|
||||
) => {
|
||||
return {
|
||||
type,
|
||||
id,
|
||||
groupId,
|
||||
channel,
|
||||
tgMessageId: 0,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
return await this.events.findOne({ type, id, groupId, channel });
|
||||
};
|
||||
|
||||
createEvent = async (event: StoredEvent) => event;
|
||||
createEvent = async (event) => {
|
||||
const result = this.events.create({
|
||||
...event,
|
||||
});
|
||||
|
||||
getLikesFor = async (channel: string, messageId: number) => [];
|
||||
return result[0];
|
||||
};
|
||||
|
||||
getLikeBy = async (channel: string, messageId: number, author: number) => ({
|
||||
id: 0,
|
||||
getLikesFor = async (channel, messageId) => {
|
||||
return await this.likes.find({
|
||||
where: { channel, messageId },
|
||||
});
|
||||
};
|
||||
|
||||
getLikeBy = async (channel, messageId, author) => {
|
||||
return this.likes.findOne({
|
||||
channel,
|
||||
messageId,
|
||||
author,
|
||||
});
|
||||
};
|
||||
|
||||
createOrUpdateLike = async ({
|
||||
channel,
|
||||
messageId,
|
||||
author,
|
||||
text: "",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
});
|
||||
text,
|
||||
messageId,
|
||||
}: Partial<Like>) => {
|
||||
const like = await this.likes.findOne({ channel, author, messageId });
|
||||
|
||||
if (like) {
|
||||
like.text = text;
|
||||
return await this.likes.save(like);
|
||||
} else {
|
||||
const created = await this.likes.create({
|
||||
channel,
|
||||
author,
|
||||
text,
|
||||
messageId,
|
||||
});
|
||||
return created[0];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
import { VkEvent } from "../vk/types";
|
||||
|
||||
export interface StoredEvent {
|
||||
type: VkEvent;
|
||||
id: number;
|
||||
groupId: number;
|
||||
channel: string;
|
||||
tgMessageId: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface StoredLike {
|
||||
id: number;
|
||||
messageId: number;
|
||||
channel: string;
|
||||
text: string;
|
||||
author: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue