From fcc495f919252651bc572cdcef684afa8fb05fd8 Mon Sep 17 00:00:00 2001 From: Jonas Strassel Date: Mon, 3 Oct 2022 14:54:13 +0200 Subject: [PATCH] feat: add module for postgres storage --- packages/storage-pg/package.json | 40 ++++++++++++++++++++ packages/storage-pg/src/index.ts | 26 +++++++++++++ packages/storage-pg/src/pg-storage.test.ts | 43 ++++++++++++++++++++++ packages/storage-pg/tsconfig-build.json | 8 ++++ packages/storage-pg/tsconfig.json | 7 ++++ 5 files changed, 124 insertions(+) create mode 100644 packages/storage-pg/package.json create mode 100644 packages/storage-pg/src/index.ts create mode 100644 packages/storage-pg/src/pg-storage.test.ts create mode 100644 packages/storage-pg/tsconfig-build.json create mode 100644 packages/storage-pg/tsconfig.json diff --git a/packages/storage-pg/package.json b/packages/storage-pg/package.json new file mode 100644 index 0000000..5f20c9d --- /dev/null +++ b/packages/storage-pg/package.json @@ -0,0 +1,40 @@ +{ + "name": "@boredland/node-ts-cache-storage-pg", + "description": "Postgres storage module for node-ts-cache", + "version": "0.0.1", + "private": false, + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc -p tsconfig-build.json" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/boredland/node-ts-cache.git" + }, + "keywords": [ + "node", + "nodejs", + "cache", + "typescript", + "ts", + "caching", + "cache", + "postgres", + "postgresql", + "node-cache", + "ts-cache" + ], + "license": "MIT", + "bugs": { + "url": "https://github.com/boredland/node-ts-cache/issues" + }, + "homepage": "https://github.com/boredland/node-ts-cache#readme", + "peerDependencies": { + "@boredland/node-ts-cache": "^5.1.0" + }, + "devDependencies": { + "@boredland/node-ts-cache": "^5.1.0", + "pg-mem": "^2.6.3" + } +} diff --git a/packages/storage-pg/src/index.ts b/packages/storage-pg/src/index.ts new file mode 100644 index 0000000..24bd871 --- /dev/null +++ b/packages/storage-pg/src/index.ts @@ -0,0 +1,26 @@ +import type { CachedItem, Storage } from "@boredland/node-ts-cache" + +export class PgStorage implements Storage { + constructor(private tableName: string, private rawQuery: (query: string) => Promise<{ key: string, value: CachedItem }[] | undefined>) { } + + async clear(): Promise { + await this.rawQuery(`TRUNCATE TABLE ${this.tableName};`) + } + + async getItem(key: string): Promise { + const queryResult = await this.rawQuery(`SELECT key, value FROM ${this.tableName} WHERE key = '${key}'`) + const result = queryResult; + + if (!result || result.length === 0) return undefined; + + return { ...result[0].value } + } + + async setItem(key: string, value: CachedItem): Promise { + await this.rawQuery(`INSERT INTO ${this.tableName} (key, value) VALUES ('${key}', '${JSON.stringify(value)}') ON CONFLICT (key) DO UPDATE SET value = '${JSON.stringify(value)}'`) + } + + async removeItem(key: any): Promise { + await this.rawQuery(`DELETE FROM ${this.tableName} WHERE key = '${key}'`) + } +} diff --git a/packages/storage-pg/src/pg-storage.test.ts b/packages/storage-pg/src/pg-storage.test.ts new file mode 100644 index 0000000..cdfd6fc --- /dev/null +++ b/packages/storage-pg/src/pg-storage.test.ts @@ -0,0 +1,43 @@ +import { PgStorage } from '.'; +import { DataType, IBackup, newDb } from "pg-mem"; +import { storageTestFactory } from "../../core/src/storage/storageTestFactory" +import { decoratorTestFactory } from "../../core/src/decorator/decoratorTestFactory" + +describe("pg-storage", () => { + const tableName = 'abc' + let backup: IBackup; + const storage = new PgStorage(tableName, async (query) => db.public.query(query).rows) + const db = newDb(); + + beforeAll(async () => { + db.public.declareTable({ + name: tableName, + fields: [ + { + name: "key", + type: DataType.text + }, + { + name: "value", + type: DataType.jsonb + } + ], + constraints: [ + { + type: 'primary key', + constraintName: { name: 'primary-key-node-ts-cache' }, + columns: [{ name: 'key' }] + } + ] + }) + backup = db.backup(); + }) + + + beforeEach(() => { + backup.restore(); + }) + + storageTestFactory(storage) + decoratorTestFactory(storage) +}) diff --git a/packages/storage-pg/tsconfig-build.json b/packages/storage-pg/tsconfig-build.json new file mode 100644 index 0000000..8ffe603 --- /dev/null +++ b/packages/storage-pg/tsconfig-build.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "include": ["./src"], + "exclude": ["**/*.test.ts"], + "compilerOptions": { + "sourceMap": false + } +} diff --git a/packages/storage-pg/tsconfig.json b/packages/storage-pg/tsconfig.json new file mode 100644 index 0000000..877f26a --- /dev/null +++ b/packages/storage-pg/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "include": ["./src"], + "compilerOptions": { + "outDir": "./dist" + } +}