@keyv/postgres 2.1.7 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -35,7 +35,7 @@ __export(index_exports, {
35
35
  default: () => index_default
36
36
  });
37
37
  module.exports = __toCommonJS(index_exports);
38
- var import_events = __toESM(require("events"), 1);
38
+ var import_node_events = __toESM(require("events"), 1);
39
39
  var import_keyv = __toESM(require("keyv"), 1);
40
40
 
41
41
  // src/pool.ts
@@ -51,12 +51,12 @@ var pool = (uri, options = {}) => {
51
51
  return postgresPool;
52
52
  };
53
53
  var endPool = async () => {
54
- await postgresPool.end();
54
+ await postgresPool?.end();
55
55
  globalUri = void 0;
56
56
  };
57
57
 
58
58
  // src/index.ts
59
- var KeyvPostgres = class extends import_events.default {
59
+ var KeyvPostgres = class extends import_node_events.default {
60
60
  ttlSupport;
61
61
  opts;
62
62
  query;
@@ -77,13 +77,6 @@ var KeyvPostgres = class extends import_events.default {
77
77
  ...options
78
78
  };
79
79
  }
80
- const connect = async () => {
81
- const conn = pool(options.uri, options);
82
- return async (sql, values) => {
83
- const data = await conn.query(sql, values);
84
- return data.rows;
85
- };
86
- };
87
80
  this.opts = {
88
81
  table: "keyv",
89
82
  schema: "public",
@@ -94,7 +87,7 @@ var KeyvPostgres = class extends import_events.default {
94
87
  if (this.opts.schema !== "public") {
95
88
  createTable = `CREATE SCHEMA IF NOT EXISTS ${this.opts.schema}; ${createTable}`;
96
89
  }
97
- const connected = connect().then(async (query) => {
90
+ const connected = this.connect().then(async (query) => {
98
91
  try {
99
92
  await query(createTable);
100
93
  } catch (error) {
@@ -116,13 +109,10 @@ var KeyvPostgres = class extends import_events.default {
116
109
  async getMany(keys) {
117
110
  const getMany = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`;
118
111
  const rows = await this.query(getMany, [keys]);
119
- const results = [];
120
- for (const key of keys) {
121
- const rowIndex = rows?.findIndex((row) => row.key === key);
122
- results.push(rowIndex > -1 ? rows[rowIndex].value : void 0);
123
- }
124
- return results;
112
+ const rowsMap = new Map(rows.map((row) => [row.key, row]));
113
+ return keys.map((key) => rowsMap.get(key)?.value);
125
114
  }
115
+ // biome-ignore lint/suspicious/noExplicitAny: type format
126
116
  async set(key, value) {
127
117
  const upsert = `INSERT INTO ${this.opts.schema}.${this.opts.table} (key, value)
128
118
  VALUES($1, $2)
@@ -158,7 +148,12 @@ var KeyvPostgres = class extends import_events.default {
158
148
  const limit = Number.parseInt(String(this.opts.iterationLimit), 10) || 10;
159
149
  async function* iterate(offset, options, query) {
160
150
  const select = `SELECT * FROM ${options.schema}.${options.table} WHERE key LIKE $1 LIMIT $2 OFFSET $3`;
161
- const entries = await query(select, [`${namespace ? namespace + ":" : ""}%`, limit, offset]);
151
+ const entries = await query(select, [
152
+ // biome-ignore lint/style/useTemplate: need to fix
153
+ `${namespace ? namespace + ":" : ""}%`,
154
+ limit,
155
+ offset
156
+ ]);
162
157
  if (entries.length === 0) {
163
158
  return;
164
159
  }
@@ -175,6 +170,13 @@ var KeyvPostgres = class extends import_events.default {
175
170
  const rows = await this.query(exists, [key]);
176
171
  return rows[0].exists;
177
172
  }
173
+ async connect() {
174
+ const conn = pool(this.opts.uri, this.opts);
175
+ return async (sql, values) => {
176
+ const data = await conn.query(sql, values);
177
+ return data.rows;
178
+ };
179
+ }
178
180
  async disconnect() {
179
181
  await endPool();
180
182
  }
@@ -186,3 +188,4 @@ var index_default = KeyvPostgres;
186
188
  KeyvPostgres,
187
189
  createKeyv
188
190
  });
191
+ /* v8 ignore next -- @preserve */
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import EventEmitter from 'events';
1
+ import EventEmitter from 'node:events';
2
2
  import Keyv, { KeyvStoreAdapter } from 'keyv';
3
3
  import { PoolConfig } from 'pg';
4
4
 
@@ -28,6 +28,7 @@ declare class KeyvPostgres extends EventEmitter implements KeyvStoreAdapter {
28
28
  clear(): Promise<void>;
29
29
  iterator(namespace?: string): AsyncGenerator<any, void, any>;
30
30
  has(key: string): Promise<any>;
31
+ connect(): Promise<(sql: string, values?: any) => Promise<any[]>>;
31
32
  disconnect(): Promise<void>;
32
33
  }
33
34
  declare const createKeyv: (options?: KeyvPostgresOptions) => Keyv<any>;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import EventEmitter from 'events';
1
+ import EventEmitter from 'node:events';
2
2
  import Keyv, { KeyvStoreAdapter } from 'keyv';
3
3
  import { PoolConfig } from 'pg';
4
4
 
@@ -28,6 +28,7 @@ declare class KeyvPostgres extends EventEmitter implements KeyvStoreAdapter {
28
28
  clear(): Promise<void>;
29
29
  iterator(namespace?: string): AsyncGenerator<any, void, any>;
30
30
  has(key: string): Promise<any>;
31
+ connect(): Promise<(sql: string, values?: any) => Promise<any[]>>;
31
32
  disconnect(): Promise<void>;
32
33
  }
33
34
  declare const createKeyv: (options?: KeyvPostgresOptions) => Keyv<any>;
package/dist/index.js CHANGED
@@ -15,7 +15,7 @@ var pool = (uri, options = {}) => {
15
15
  return postgresPool;
16
16
  };
17
17
  var endPool = async () => {
18
- await postgresPool.end();
18
+ await postgresPool?.end();
19
19
  globalUri = void 0;
20
20
  };
21
21
 
@@ -41,13 +41,6 @@ var KeyvPostgres = class extends EventEmitter {
41
41
  ...options
42
42
  };
43
43
  }
44
- const connect = async () => {
45
- const conn = pool(options.uri, options);
46
- return async (sql, values) => {
47
- const data = await conn.query(sql, values);
48
- return data.rows;
49
- };
50
- };
51
44
  this.opts = {
52
45
  table: "keyv",
53
46
  schema: "public",
@@ -58,7 +51,7 @@ var KeyvPostgres = class extends EventEmitter {
58
51
  if (this.opts.schema !== "public") {
59
52
  createTable = `CREATE SCHEMA IF NOT EXISTS ${this.opts.schema}; ${createTable}`;
60
53
  }
61
- const connected = connect().then(async (query) => {
54
+ const connected = this.connect().then(async (query) => {
62
55
  try {
63
56
  await query(createTable);
64
57
  } catch (error) {
@@ -80,13 +73,10 @@ var KeyvPostgres = class extends EventEmitter {
80
73
  async getMany(keys) {
81
74
  const getMany = `SELECT * FROM ${this.opts.schema}.${this.opts.table} WHERE key = ANY($1)`;
82
75
  const rows = await this.query(getMany, [keys]);
83
- const results = [];
84
- for (const key of keys) {
85
- const rowIndex = rows?.findIndex((row) => row.key === key);
86
- results.push(rowIndex > -1 ? rows[rowIndex].value : void 0);
87
- }
88
- return results;
76
+ const rowsMap = new Map(rows.map((row) => [row.key, row]));
77
+ return keys.map((key) => rowsMap.get(key)?.value);
89
78
  }
79
+ // biome-ignore lint/suspicious/noExplicitAny: type format
90
80
  async set(key, value) {
91
81
  const upsert = `INSERT INTO ${this.opts.schema}.${this.opts.table} (key, value)
92
82
  VALUES($1, $2)
@@ -122,7 +112,12 @@ var KeyvPostgres = class extends EventEmitter {
122
112
  const limit = Number.parseInt(String(this.opts.iterationLimit), 10) || 10;
123
113
  async function* iterate(offset, options, query) {
124
114
  const select = `SELECT * FROM ${options.schema}.${options.table} WHERE key LIKE $1 LIMIT $2 OFFSET $3`;
125
- const entries = await query(select, [`${namespace ? namespace + ":" : ""}%`, limit, offset]);
115
+ const entries = await query(select, [
116
+ // biome-ignore lint/style/useTemplate: need to fix
117
+ `${namespace ? namespace + ":" : ""}%`,
118
+ limit,
119
+ offset
120
+ ]);
126
121
  if (entries.length === 0) {
127
122
  return;
128
123
  }
@@ -139,6 +134,13 @@ var KeyvPostgres = class extends EventEmitter {
139
134
  const rows = await this.query(exists, [key]);
140
135
  return rows[0].exists;
141
136
  }
137
+ async connect() {
138
+ const conn = pool(this.opts.uri, this.opts);
139
+ return async (sql, values) => {
140
+ const data = await conn.query(sql, values);
141
+ return data.rows;
142
+ };
143
+ }
142
144
  async disconnect() {
143
145
  await endPool();
144
146
  }
@@ -150,3 +152,4 @@ export {
150
152
  createKeyv,
151
153
  index_default as default
152
154
  };
155
+ /* v8 ignore next -- @preserve */
package/package.json CHANGED
@@ -1,32 +1,21 @@
1
1
  {
2
2
  "name": "@keyv/postgres",
3
- "version": "2.1.7",
3
+ "version": "2.2.1",
4
4
  "description": "PostgreSQL storage adapter for Keyv",
5
5
  "type": "module",
6
- "main": "dist/index.cjs",
7
- "module": "dist/index.js",
8
- "types": "dist/index.d.ts",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "require": "./dist/index.cjs",
12
- "import": "./dist/index.js"
13
- }
14
- },
15
- "xo": {
16
- "rules": {
17
- "import/no-named-as-default": "off",
18
- "import/extensions": "off",
19
- "n/file-extension-in-import": "off",
20
- "unicorn/prefer-event-target": "off",
21
- "promise/prefer-await-to-then": "off",
22
- "unicorn/prefer-module": "off",
23
- "@typescript-eslint/use-unknown-in-catch-callback-variable": "off",
24
- "unicorn/prefer-node-protocol": "off",
25
- "@typescript-eslint/no-unsafe-return": "off",
26
- "@typescript-eslint/no-confusing-void-expression": "off",
27
- "import/no-extraneous-dependencies": "off",
28
- "@typescript-eslint/no-unnecessary-type-assertion": "off",
29
- "@typescript-eslint/no-unnecessary-type-arguments": "off"
11
+ "require": {
12
+ "types": "./dist/index.d.cts",
13
+ "default": "./dist/index.cjs"
14
+ },
15
+ "import": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
30
19
  }
31
20
  },
32
21
  "repository": {
@@ -56,16 +45,16 @@
56
45
  "pg": "^8.16.3"
57
46
  },
58
47
  "peerDependencies": {
59
- "keyv": "^5.4.0"
48
+ "keyv": "^5.5.4"
60
49
  },
61
50
  "devDependencies": {
62
- "@types/pg": "^8.15.4",
63
- "@vitest/coverage-v8": "^3.2.4",
64
- "rimraf": "^6.0.1",
65
- "tsd": "^0.32.0",
66
- "vitest": "^3.2.4",
67
- "xo": "^1.2.1",
68
- "@keyv/test-suite": "^2.0.9"
51
+ "@biomejs/biome": "^2.3.6",
52
+ "@types/pg": "^8.15.6",
53
+ "@vitest/coverage-v8": "^4.0.10",
54
+ "rimraf": "^6.1.0",
55
+ "tsd": "^0.33.0",
56
+ "vitest": "^4.0.10",
57
+ "@keyv/test-suite": "^2.1.2"
69
58
  },
70
59
  "tsd": {
71
60
  "directory": "test"
@@ -79,8 +68,10 @@
79
68
  ],
80
69
  "scripts": {
81
70
  "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
82
- "test": "xo --fix && vitest run --coverage",
83
- "test:ci": "xo && vitest --run --sequence.setupFiles=list --coverage",
71
+ "lint": "biome check --write --error-on-warnings",
72
+ "lint:ci": "biome check --error-on-warnings",
73
+ "test": "pnpm lint && vitest run --coverage",
74
+ "test:ci": "pnpm lint:ci && vitest --run --sequence.setupFiles=list --coverage",
84
75
  "clean": "rimraf ./node_modules ./coverage ./dist"
85
76
  }
86
77
  }