2.0.2 • Published 2 years ago

@ioki/node-ts-cache-storage-pg v2.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

node-ts-cache-storage-pg

Postgres storage module for node-ts-cache.

This module is driver-agnostic. It expects you to bring your own raw query method - something most of the postgres available drivers provide. The driver needs to support the question-mark notation for prepared statements.

yarn add @ioki/node-ts-cache @ioki/node-ts-cache-storage-pg

In this example, node-postgres (pg) is being used. You should to provide a table with the following columns:

* key: text
value: jsonb

The query function should return an array of objects: { key: string, value: CacheItem }[].

import { PgStorage } from "@ioki/node-ts-cache-storage-pg"
import { Cache, CacheContainer } from "@ioki/node-ts-cache"
import Client from "pg"

const client = new Client()

await client.connect()

const storage = new PgStorage(tableName, async (query) => (await new Client().query(query)).rows)

const userCache = new CacheContainer(storage)

class MyService {
    @Cache(userCache, { ttl: 60 })
    public async getUsers(): Promise<string[]> {
        return ["Max", "User"]
    }
}