@keyv/postgres
Advanced tools
Comparing version 1.4.4 to 1.4.5
{ | ||
"name": "@keyv/postgres", | ||
"version": "1.4.4", | ||
"version": "1.4.5", | ||
"description": "PostgreSQL storage adapter for Keyv", | ||
@@ -62,3 +62,3 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"pg": "8.10.0" | ||
"pg": "8.11.0" | ||
}, | ||
@@ -72,5 +72,5 @@ "devDependencies": { | ||
"ts-node": "^10.9.1", | ||
"tsd": "^0.28.0", | ||
"typescript": "^5.0.2", | ||
"xo": "^0.53.1" | ||
"tsd": "^0.28.1", | ||
"typescript": "^5.0.4", | ||
"xo": "^0.54.2" | ||
}, | ||
@@ -77,0 +77,0 @@ "tsd": { |
@@ -1,4 +0,2 @@ | ||
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */ | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
import {EventEmitter} from 'events'; | ||
@@ -25,3 +23,3 @@ import type {Store, StoredData} from 'keyv'; | ||
declare namespace KeyvPostgres { | ||
interface Options { | ||
type Options = { | ||
uri?: string | undefined; | ||
@@ -32,3 +30,3 @@ table?: string | undefined; | ||
ssl?: any | undefined; | ||
} | ||
}; | ||
} |
@@ -40,78 +40,59 @@ 'use strict'; | ||
get(key) { | ||
async get(key) { | ||
const select = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1`; | ||
return this.query(select, [key]) | ||
.then(rows => { | ||
const row = rows[0]; | ||
if (row === undefined) { | ||
return undefined; | ||
} | ||
return row.value; | ||
}); | ||
const rows = await this.query(select, [key]); | ||
const row = rows[0]; | ||
return row === undefined ? undefined : row.value; | ||
} | ||
getMany(keys) { | ||
async getMany(keys) { | ||
const getMany = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`; | ||
return this.query(getMany, [keys]).then(rows => { | ||
const results = [...keys]; | ||
let i = 0; | ||
for (const key of keys) { | ||
const rowIndex = rows.findIndex(row => row.key === key); | ||
const rows = await this.query(getMany, [keys]); | ||
const results = []; | ||
if (rowIndex > -1) { | ||
results[i] = rows[rowIndex].value; | ||
} else { | ||
results[i] = undefined; | ||
} | ||
for (const key of keys) { | ||
const rowIndex = rows.findIndex(row => row.key === key); | ||
results.push(rowIndex > -1 ? rows[rowIndex].value : undefined); | ||
} | ||
i++; | ||
} | ||
return results; | ||
}); | ||
return results; | ||
} | ||
set(key, value) { | ||
async set(key, value) { | ||
const upsert = `INSERT INTO ${this.opts.schema}.${this.opts.table} (key, value) | ||
VALUES($1, $2) | ||
ON CONFLICT(key) | ||
DO UPDATE SET value=excluded.value;`; | ||
return this.query(upsert, [key, value]); | ||
VALUES($1, $2) | ||
ON CONFLICT(key) | ||
DO UPDATE SET value=excluded.value;`; | ||
await this.query(upsert, [key, value]); | ||
} | ||
delete(key) { | ||
async delete(key) { | ||
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]) | ||
.then(rows => { | ||
const row = rows[0]; | ||
if (row === undefined) { | ||
return false; | ||
} | ||
const rows = await this.query(select, [key]); | ||
return this.query(del, [key]) | ||
.then(() => true); | ||
}); | ||
if (rows[0] === undefined) { | ||
return false; | ||
} | ||
await this.query(del, [key]); | ||
return true; | ||
} | ||
deleteMany(key) { | ||
async deleteMany(keys) { | ||
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]) | ||
.then(rows => { | ||
const row = rows[0]; | ||
if (row === undefined) { | ||
return false; | ||
} | ||
const rows = await this.query(select, [keys]); | ||
return this.query(del, [key]) | ||
.then(() => true); | ||
}); | ||
if (rows[0] === undefined) { | ||
return false; | ||
} | ||
await this.query(del, [keys]); | ||
return true; | ||
} | ||
clear() { | ||
async clear() { | ||
const del = `DELETE FROM ${this.opts.schema}.${this.opts.table} WHERE key LIKE $1`; | ||
return this.query(del, [this.namespace ? `${this.namespace}:%` : '%']) | ||
.then(() => undefined); | ||
await this.query(del, [this.namespace ? `${this.namespace}:%` : '%']); | ||
} | ||
@@ -139,5 +120,6 @@ | ||
has(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); | ||
async has(key) { | ||
const exists = `SELECT EXISTS ( SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = $1 )`; | ||
const rows = await this.query(exists, [key]); | ||
return rows[0].exists; | ||
} | ||
@@ -144,0 +126,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
8602
153
+ Addedpg@8.11.0(transitive)
+ Addedpg-cloudflare@1.1.1(transitive)
- Removedpg@8.10.0(transitive)
Updatedpg@8.11.0