mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-24 22:46:41 +07:00
#4 added entities and synchronization to project
This commit is contained in:
parent
c5c3c013ba
commit
50db549cbe
5 changed files with 33 additions and 4 deletions
|
@ -7,7 +7,7 @@ telegram:
|
||||||
url: https://something.org:3002/webhook
|
url: https://something.org:3002/webhook
|
||||||
enabled: false
|
enabled: false
|
||||||
postgres:
|
postgres:
|
||||||
uri: postgres://user:password@db/test
|
uri: postgres://user:password@db/bot
|
||||||
logger:
|
logger:
|
||||||
level: info
|
level: info
|
||||||
vk:
|
vk:
|
||||||
|
|
16
docker-compose.yml
Normal file
16
docker-compose.yml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
version: '3.4'
|
||||||
|
services:
|
||||||
|
db:
|
||||||
|
container_name: bot-db
|
||||||
|
image: postgres:11-alpine
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: user
|
||||||
|
POSTGRES_PASSWORD: password
|
||||||
|
POSTGRES_DB: bot
|
||||||
|
volumes:
|
||||||
|
- bot-db:/var/lib/postgresql/data
|
||||||
|
- /etc/localtime:/etc/localtime:ro
|
||||||
|
volumes:
|
||||||
|
bot-db: {}
|
|
@ -1,5 +1,6 @@
|
||||||
import { VkEvent } from "../vk/types";
|
import { VkEvent } from "../vk/types";
|
||||||
import { StoredEvent, StoredLike } from "./types";
|
import { StoredEvent, StoredLike } from "./types";
|
||||||
|
import { Like } from "./postgres/entities/Like";
|
||||||
|
|
||||||
export interface Storage {
|
export interface Storage {
|
||||||
getEvent(
|
getEvent(
|
||||||
|
@ -9,7 +10,9 @@ export interface Storage {
|
||||||
channel: string
|
channel: string
|
||||||
): Promise<StoredEvent>;
|
): Promise<StoredEvent>;
|
||||||
|
|
||||||
createEvent(event: StoredEvent): Promise<StoredEvent>;
|
createEvent(event: Partial<StoredEvent>): Promise<StoredEvent>;
|
||||||
|
|
||||||
|
createOrUpdateLike(like: Partial<StoredLike>): Promise<Like>;
|
||||||
|
|
||||||
getLikesFor(channel: string, messageId: number): Promise<StoredLike[]>;
|
getLikesFor(channel: string, messageId: number): Promise<StoredLike[]>;
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import {
|
||||||
import { StoredLike } from "../../types";
|
import { StoredLike } from "../../types";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
class Like implements StoredLike {
|
export class Like implements StoredLike {
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
id: number;
|
id: number;
|
||||||
@Column()
|
@Column()
|
||||||
|
|
|
@ -3,6 +3,10 @@ import { VkEvent } from "../../vk/types";
|
||||||
import { StoredEvent } from "../types";
|
import { StoredEvent } from "../types";
|
||||||
import { PostgresConfig } from "./types";
|
import { PostgresConfig } from "./types";
|
||||||
import { Connection, createConnection } from "typeorm";
|
import { Connection, createConnection } from "typeorm";
|
||||||
|
import logger from "../../logger";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
const entities = [path.join(__dirname, "./entities/*")];
|
||||||
|
|
||||||
export class PostgresDB implements Storage {
|
export class PostgresDB implements Storage {
|
||||||
private connection: Connection;
|
private connection: Connection;
|
||||||
|
@ -12,8 +16,14 @@ export class PostgresDB implements Storage {
|
||||||
this.connection = await createConnection({
|
this.connection = await createConnection({
|
||||||
type: "postgres",
|
type: "postgres",
|
||||||
url: this.config.uri,
|
url: this.config.uri,
|
||||||
entities: ["./entities/*.ts"],
|
entities,
|
||||||
|
logging: true,
|
||||||
|
synchronize: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.connection.synchronize();
|
||||||
|
|
||||||
|
logger.info(`db connected to ${this.config.uri}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
getEvent = async (
|
getEvent = async (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue