Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to use unlogged table on Postgres #1290

Merged
merged 3 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ import {createKeyv} from '@keyv/postgres';
const keyv = createKeyv({ uri: 'postgresql://user:pass@localhost:5432/dbname', table: 'cache', schema: 'keyv' });
```

For using of [unlogged](https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-UNLOGGED) Postgres table rather than logged pass `useUnloggedTable` option.

```js
const keyv = new Keyv(new KeyvPostgres('postgresql://user:pass@localhost:5432/dbname'), { table: 'cache', useUnloggedTable: true });
```

## Testing

When testing you can use our `docker compose` postgresql instance by having docker installed and running. This will start a postgres server, run the tests, and stop the server:
Expand Down
2 changes: 1 addition & 1 deletion packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class KeyvPostgres extends EventEmitter implements KeyvStoreAdapter {
...options,
};

let 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${this.opts.useUnloggedTable ? ' UNLOGGED ' : ' '}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}`;
Expand Down
1 change: 1 addition & 0 deletions packages/postgres/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type KeyvPostgresOptions = {
ssl?: any;
dialect?: string;
iterationLimit?: number;
useUnloggedTable?: boolean;
} & PoolConfig;

export type Query = (sqlString: string, values?: any) => Promise<any[]>;
6 changes: 6 additions & 0 deletions packages/postgres/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,9 @@ test.it('helper to create Keyv instance with postgres', async t => {
t.expect(await keyv.set('foo', 'bar')).toBe(true);
t.expect(await keyv.get('foo')).toBe('bar');
});

test.it('test unlogged table', async t => {
const keyv = createKeyv({uri: postgresUri, useUnloggedTable: true});
t.expect(await keyv.set('foo', 'bar')).toBe(true);
t.expect(await keyv.get('foo')).toBe('bar');
});
Loading