Skip to content

Commit

Permalink
feat: add module for postgres storage
Browse files Browse the repository at this point in the history
  • Loading branch information
boredland committed Oct 3, 2022
1 parent adc14cc commit fcc495f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/storage-pg/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
26 changes: 26 additions & 0 deletions packages/storage-pg/src/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await this.rawQuery(`TRUNCATE TABLE ${this.tableName};`)
}

async getItem(key: string): Promise<CachedItem | undefined> {
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<void> {
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<void> {
await this.rawQuery(`DELETE FROM ${this.tableName} WHERE key = '${key}'`)
}
}
43 changes: 43 additions & 0 deletions packages/storage-pg/src/pg-storage.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
8 changes: 8 additions & 0 deletions packages/storage-pg/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": ["./src"],
"exclude": ["**/*.test.ts"],
"compilerOptions": {
"sourceMap": false
}
}
7 changes: 7 additions & 0 deletions packages/storage-pg/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"include": ["./src"],
"compilerOptions": {
"outDir": "./dist"
}
}

0 comments on commit fcc495f

Please sign in to comment.