@keyv/postgres
Advanced tools
Comparing version 1.4.3 to 1.4.4
{ | ||
"name": "@keyv/postgres", | ||
"version": "1.4.3", | ||
"version": "1.4.4", | ||
"description": "PostgreSQL storage adapter for Keyv", | ||
@@ -62,3 +62,3 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"pg": "8.9.0" | ||
"pg": "8.10.0" | ||
}, | ||
@@ -72,4 +72,4 @@ "devDependencies": { | ||
"ts-node": "^10.9.1", | ||
"tsd": "^0.25.0", | ||
"typescript": "^4.9.5", | ||
"tsd": "^0.28.0", | ||
"typescript": "^5.0.2", | ||
"xo": "^0.53.1" | ||
@@ -76,0 +76,0 @@ }, |
@@ -10,3 +10,3 @@ # @keyv/postgres [<img width="100" align="right" src="https://jaredwray.com/images/keyv.svg" alt="keyv">](https://github.com/jaredwra/keyv) | ||
PostgreSQL storage adapter for [Keyv](https://github.com/lukechilds/keyv). | ||
PostgreSQL storage adapter for [Keyv](https://github.com/jaredwray/keyv). | ||
@@ -38,2 +38,10 @@ Requires Postgres 9.5 or newer for `ON CONFLICT` support to allow performant upserts. [Why?](https://stackoverflow.com/questions/17267417/how-to-upsert-merge-insert-on-duplicate-update-in-postgresql/17267423#17267423) | ||
You can specify the `schema` option (default is `public`). | ||
e.g: | ||
```js | ||
const keyv = new Keyv('postgresql://user:pass@localhost:5432/dbname', { schema: 'keyv' }); | ||
``` | ||
## Testing | ||
@@ -43,12 +51,10 @@ | ||
At the root of the Keyv mono repo: | ||
```shell | ||
npm run test:db | ||
yarn test:services:start | ||
``` | ||
To run each step manually do the following to start the server, and run the tests: | ||
To just test the postgres adapter go to the postgres directory (packages/postgres) and run: | ||
```shell | ||
npm run test:postgres:start | ||
npm run test | ||
npm run test:postgres:stop | ||
yarn test | ||
``` | ||
@@ -55,0 +61,0 @@ |
@@ -0,3 +1,4 @@ | ||
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */ | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */ | ||
import {EventEmitter} from 'events'; | ||
@@ -4,0 +5,0 @@ import type {Store, StoredData} from 'keyv'; |
@@ -19,8 +19,15 @@ 'use strict'; | ||
}); | ||
this.opts = {table: 'keyv', | ||
this.opts = { | ||
table: 'keyv', | ||
schema: 'public', | ||
keySize: 255, ...options}; | ||
keySize: 255, | ||
...options, | ||
}; | ||
const createTable = `CREATE TABLE IF NOT EXISTS ${this.opts.schema}.${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`; | ||
let createTable = `CREATE TABLE IF NOT EXISTS ${this.opts.schema}.${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`; | ||
if (this.opts.schema !== 'public') { | ||
createTable = `CREATE SCHEMA IF NOT EXISTS ${this.opts.schema}; ${createTable}`; | ||
} | ||
const connected = this.opts.connect() | ||
@@ -35,3 +42,3 @@ .then(query => query(createTable).then(() => query)) | ||
get(key) { | ||
const select = `SELECT * FROM ${this.opts.table} WHERE key = $1`; | ||
const select = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; | ||
return this.query(select, [key]) | ||
@@ -49,3 +56,3 @@ .then(rows => { | ||
getMany(keys) { | ||
const getMany = `SELECT * FROM ${this.opts.table} WHERE key = ANY($1)`; | ||
const getMany = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; | ||
return this.query(getMany, [keys]).then(rows => { | ||
@@ -71,3 +78,3 @@ const results = [...keys]; | ||
set(key, value) { | ||
const upsert = `INSERT INTO ${this.opts.table} (key, value) | ||
const upsert = `INSERT INTO ${this.opts.schema}.${this.opts.table} (key, value) | ||
VALUES($1, $2) | ||
@@ -80,4 +87,4 @@ ON CONFLICT(key) | ||
delete(key) { | ||
const select = `SELECT * FROM ${this.opts.table} WHERE key = $1`; | ||
const del = `DELETE FROM ${this.opts.table} WHERE key = $1`; | ||
const select = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; | ||
const del = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; | ||
return this.query(select, [key]) | ||
@@ -96,4 +103,4 @@ .then(rows => { | ||
deleteMany(key) { | ||
const select = `SELECT * FROM ${this.opts.table} WHERE key = ANY($1)`; | ||
const del = `DELETE FROM ${this.opts.table} WHERE key = ANY($1)`; | ||
const select = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; | ||
const del = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; | ||
return this.query(select, [key]) | ||
@@ -112,3 +119,3 @@ .then(rows => { | ||
clear() { | ||
const del = `DELETE FROM ${this.opts.table} WHERE key LIKE $1`; | ||
const del = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key LIKE $1`; | ||
return this.query(del, [this.namespace ? `${this.namespace}:%` : '%']) | ||
@@ -121,3 +128,3 @@ .then(() => undefined); | ||
async function * iterate(offset, options, query) { | ||
const select = `SELECT * FROM ${options.table} WHERE key LIKE $1 LIMIT $2 OFFSET $3`; | ||
const select = `SELECT * FROM ${options.schema}.${options.table} WHERE key LIKE $1 LIMIT $2 OFFSET $3`; | ||
const enteries = await query(select, [`${namespace ? namespace + ':' : ''}%`, limit, offset]); | ||
@@ -140,3 +147,3 @@ if (enteries.length === 0) { | ||
has(key) { | ||
const exists = `SELECT EXISTS ( SELECT * FROM ${this.opts.table} WHERE key = '${key}' )`; | ||
const exists = `SELECT EXISTS ( SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = '${key}' )`; | ||
return this.query(exists).then(rows => rows[0].exists); | ||
@@ -143,0 +150,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8872
172
62