1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-25 06:56:40 +07:00

#4 implemented postgres events

This commit is contained in:
Fedor Katurov 2021-05-04 13:58:05 +07:00
parent 50db549cbe
commit e73ab3cb4f
3 changed files with 55 additions and 51 deletions

View file

@ -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];
}
};
}