Skip to content

Commit

Permalink
pg
Browse files Browse the repository at this point in the history
  • Loading branch information
kabo2468 committed Jun 3, 2023
1 parent 0b66a36 commit fc8422a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 105 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"start": "node built/index.js",
"dev": "ts-node --esm src/index.ts",
"clean": "rimraf built/*",
"clean": "rimraf built",
"tsc": "tsc",
"tsc:check": "tsc --noEmit",
"build": "npm run clean && npm run tsc",
Expand All @@ -33,11 +33,11 @@
},
"devDependencies": {
"@types/eslint": "^8.37.0",
"@types/estree": "^1.0.1",
"@types/json5": "0.0.30",
"@types/moji": "^0.5.0",
"@types/ms": "^0.7.31",
"@types/node": "^20.2.1",
"@types/node-fetch": "^2.6.4",
"@types/pg": "^8.6.6",
"@types/seedrandom": "^3.0.5",
"@types/uuid": "^9.0.1",
Expand All @@ -56,4 +56,4 @@
"bufferutil": "^4.0.7",
"utf-8-validate": "^6.0.3"
}
}
}
101 changes: 14 additions & 87 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import iconv from 'iconv-lite';
import ms from 'ms';
import { Pool, QueryResult } from 'pg';
import pg from 'pg';
import ReconnectingWebSocket from 'reconnecting-websocket';
import wsConst from 'ws';
import { Config } from './config.js';
Expand Down Expand Up @@ -28,7 +28,7 @@ export class Bot {
public config: Config;
public ngWords: NGWord;
public ws: ReconnectingWebSocket.default;
private db: Pool;
private db: pg.Pool;
private rateLimit = 0;
public api: API;
public account!: User;
Expand All @@ -38,7 +38,7 @@ export class Bot {
this.config = config;
this.ngWords = ngWords;

const psql = new Pool({
const psql = new pg.Pool({
ssl: config.dbSSL,
connectionString: config.databaseUrl,
});
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Bot {
);
}

async runQuery<T extends Keys = Keys>(query: { text: string; values?: (Row[Keys] | number)[] }): Promise<QueryResult<Res<T>>> {
async runQuery<T extends Keys = Keys>(query: { text: string; values?: (Row[Keys] | number)[] }): Promise<pg.QueryResult<Res<T>>> {
return this.db.query<Res<T>>(query).catch((err) => {
console.error(err);
process.exit(1);
Expand All @@ -133,7 +133,7 @@ export class Bot {
await this.runQuery(query);
}

async removeFood(food: string, many: boolean): Promise<QueryResult<Res<'name'>>> {
async removeFood(food: string, many: boolean): Promise<pg.QueryResult<Res<'name'>>> {
const textOne = 'in (SELECT "name" FROM oishii_table WHERE LOWER("name") = LOWER($1) LIMIT 1)';
const textMany = '~* $1';
const query = {
Expand All @@ -143,7 +143,7 @@ export class Bot {
return this.runQuery<'name'>(query);
}

async removeFoodFromUserId(userId: string, learnedOnly: boolean): Promise<QueryResult<Res<'name'>>> {
async removeFoodFromUserId(userId: string, learnedOnly: boolean): Promise<pg.QueryResult<Res<'name'>>> {
const query = {
text: `DELETE FROM oishii_table WHERE "userId" = $1 ${learnedOnly ? 'AND "learned" = true' : ''} RETURNING "name"`,
values: [userId],
Expand All @@ -159,15 +159,15 @@ export class Bot {
await this.runQuery(query);
}

async getFood(name: string): Promise<QueryResult<Res>> {
async getFood(name: string): Promise<pg.QueryResult<Res>> {
const query = {
text: `SELECT * FROM oishii_table WHERE LOWER("name") = LOWER($1)`,
values: [name],
};
return this.runQuery(query);
}

async getRandomFood({ good, learned }: { good?: boolean; learned?: boolean } = {}): Promise<QueryResult<Res<'name' | 'good'>>> {
async getRandomFood({ good, learned }: { good?: boolean; learned?: boolean } = {}): Promise<pg.QueryResult<Res<'name' | 'good'>>> {
const options = [];
if (good !== undefined) options.push(`"good"=${good}`);
if (learned !== undefined) options.push(`"learned"=${learned}`);
Expand Down Expand Up @@ -203,7 +203,7 @@ export class Bot {
this.rateLimit++;
}

async getUserFoods(userId: string, page?: number): Promise<QueryResult<Res>> {
async getUserFoods(userId: string, page?: number): Promise<pg.QueryResult<Res>> {
const offset = page ? `OFFSET ${page * 10}` : 'OFFSET 0';
const query = {
text: `SELECT "name", "good" FROM oishii_table WHERE "userId" = $1 AND learned = TRUE ORDER BY updated DESC LIMIT 10 ${offset}`,
Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync } from 'fs';
import got from 'got';
import JSON5 from 'json5';
import { ClientConfig } from 'pg';
import pg from 'pg';

type Post = {
[key: string]: number;
Expand All @@ -17,11 +17,11 @@ export type MecabType = {
};

type JsonConfig = {
[key: string]: string | string[] | boolean | number | Post | MecabType | ClientConfig['ssl'];
[key: string]: string | string[] | boolean | number | Post | MecabType | pg.ClientConfig['ssl'];
url: string;
apiKey: string;
databaseUrl: string;
dbSSL: ClientConfig['ssl'];
dbSSL: pg.ClientConfig['ssl'];
ownerUsernames: string[];
post: Post;
mecab: MecabType;
Expand Down
4 changes: 2 additions & 2 deletions src/migration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Pool } from 'pg';
import pg from 'pg';
import loadConfig from './config.js';

loadConfig()
.then(async (config) => {
const pool = new Pool({
const pool = new pg.Pool({
connectionString: config.databaseUrl,
ssl: config.dbSSL,
});
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"forceConsistentCasingInFileNames": true,
"removeComments": true,
"moduleResolution": "nodenext",
"resolveJsonModule": true
"resolveJsonModule": true,
"resolvePackageJsonExports": false
},
"exclude": [
"test/**/*"
Expand Down

0 comments on commit fc8422a

Please sign in to comment.