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

add support for all hash field expiration commands #2742

Open
wants to merge 7 commits into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
40 changes: 40 additions & 0 deletions packages/client/lib/commands/HEXPIRE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HEXPIRE from './HEXPIRE';

describe('HEXPIRE', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HEXPIRE.transformArguments('key', 'field', 1),
['HEXPIRE', 'key', '1', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HEXPIRE.transformArguments('key', ['field1', 'field2'], 1),
['HEXPIRE', 'key', '1', '2', 'field1', 'field2']
);
});

it('with set option', () => {
assert.deepEqual(
HEXPIRE.transformArguments('key', 'field1', 1, 'NX'),
['HEXPIRE', 'key', '1', 'NX', '1', 'field1']
);
});
});

testUtils.testAll('hExpire', async client => {
assert.equal(
await client.hExpire('key', ['field1'], 0),
null
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
34 changes: 34 additions & 0 deletions packages/client/lib/commands/HEXPIRE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Command, NullReply, RedisArgument } from "../RESP/types";
import { RedisVariadicArgument, pushVariadicArgument } from "./generic-transformers";

export const HASH_EXPIRATION = {
/** The field does not exist */
FIELD_NOT_EXISTS: -2,
/** Specified NX | XX | GT | LT condition not met */
CONDITION_NOT_MET: 0,
/** Expiration time was set or updated */
UPDATED: 1,
/** Field deleted because the specified expiration time is in the past */
DELETED: 2
} as const;

export type HashExpiration = typeof HASH_EXPIRATION[keyof typeof HASH_EXPIRATION];

export default {
FIRST_KEY_INDEX: 1,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
seconds: number,
mode?: 'NX' | 'XX' | 'GT' | 'LT',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do

interface HExpireOptions {
  mode?: 'NX' | 'XX' | 'GT' | 'LT';
}

for "future proofing" (in case there will be more optional arguments)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't really future proof, it just makes it easier if these options are shared between multiple commands. Unsure it adds that much.

) {
const args = ['HEXPIRE', key, seconds.toString()];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => NullReply | Array<HashExpiration>
} as const satisfies Command;
48 changes: 48 additions & 0 deletions packages/client/lib/commands/HEXPIREAT.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HEXPIREAT from './HEXPIREAT';

describe('HEXPIREAT', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string + number', () => {
assert.deepEqual(
HEXPIREAT.transformArguments('key', 'field', 1),
['HEXPIREAT', 'key', '1', '1', 'field']
);
});

it('array + number', () => {
assert.deepEqual(
HEXPIREAT.transformArguments('key', ['field1', 'field2'], 1),
['HEXPIREAT', 'key', '1', '2', 'field1', 'field2']
);
});

it('date', () => {
const d = new Date();
assert.deepEqual(
HEXPIREAT.transformArguments('key', ['field1'], d),
['HEXPIREAT', 'key', Math.floor(d.getTime() / 1000).toString(), '1', 'field1']
);
});

it('with set option', () => {
assert.deepEqual(
HEXPIREAT.transformArguments('key', 'field1', 1, 'GT'),
['HEXPIREAT', 'key', '1', 'GT', 1, 'field1']
);
});
});

testUtils.testAll('expireAt', async client => {
assert.equal(
await client.hExpireAt('key', 'field1', 1),
null
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
23 changes: 23 additions & 0 deletions packages/client/lib/commands/HEXPIREAT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisArgument, Command, NullReply } from '../RESP/types';
import { HashExpiration } from './HEXPIRE';
import { RedisVariadicArgument, pushVariadicArgument, transformEXAT } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
timestamp: number | Date,
mode?: 'NX' | 'XX' | 'GT' | 'LT'
) {
const args = ['HEXPIREAT', key, transformEXAT(timestamp)];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => NullReply | Array<HashExpiration>
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HEXPIRETIME.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HEXPIRETIME from './HEXPIRETIME';

describe('HEXPIRETIME', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HEXPIRETIME.transformArguments('key', 'field'),
['HEXPIRETIME', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HEXPIRETIME.transformArguments('key', ['field1', 'field2']),
['HEXPIRETIME', 'key', '2', 'field1', 'field2']
);
});
})

testUtils.testAll('hExpireTime', async client => {
assert.equal(
await client.hExpireTime('key', 'field'),
null
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
11 changes: 11 additions & 0 deletions packages/client/lib/commands/HEXPIRETIME.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RedisArgument, NumberReply, Command, NullReply, ArrayReply } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HEXPIRETIME', key], fields);
},
transformReply: undefined as unknown as () => NullReply | ArrayReply<NumberReply>
leibale marked this conversation as resolved.
Show resolved Hide resolved
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HPERSIST.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPERSIST from './HPERSIST';

describe('HPERSIST', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HPERSIST.transformArguments('key', 'field'),
['HPERSIST', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HPERSIST.transformArguments('key', ['field1', 'field2']),
['HPERSIST', 'key', '2', 'field1', 'field2']
);
});
})

testUtils.testAll('hPersist', async client => {
assert.equal(
await client.hPersist('key', 'field1'),
null
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
10 changes: 10 additions & 0 deletions packages/client/lib/commands/HPERSIST.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RedisArgument, NumberReply, Command, NullReply, ArrayReply } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HPERSIST', key], fields);
},
transformReply: undefined as unknown as () => NullReply | ArrayReply<NumberReply>
} as const satisfies Command;
40 changes: 40 additions & 0 deletions packages/client/lib/commands/HPEXPIRE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPEXPIRE from './HPEXPIRE';

describe('HPEXPIRE', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HPEXPIRE.transformArguments('key', 'field', 1),
['HPEXPIRE', 'key', '1', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HPEXPIRE.transformArguments('key', ['field1', 'field2'], 1),
['HPEXPIRE', 'key', '1', '2', 'field1', 'field2']
);
});

it('with set option', () => {
assert.deepEqual(
HPEXPIRE.transformArguments('key', ['field1'], 1, 'NX'),
['HPEXPIRE', 'key', '1', 'NX', '1', 'field1']
);
});
});

testUtils.testAll('hpRxpire', async client => {
assert.equal(
await client.hpExpire('key', ['field'], 0),
null
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
22 changes: 22 additions & 0 deletions packages/client/lib/commands/HPEXPIRE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Command, NullReply, RedisArgument } from "../RESP/types";
import { HashExpiration } from "./HEXPIRE";
import { RedisVariadicArgument, pushVariadicArgument } from "./generic-transformers";

export default {
FIRST_KEY_INDEX: 1,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
ms: number,
mode?: 'NX' | 'XX' | 'GT' | 'LT',
) {
const args = ['HPEXPIRE', key, ms.toString()];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => NullReply | Array<HashExpiration>
} as const satisfies Command;
48 changes: 48 additions & 0 deletions packages/client/lib/commands/HPEXPIREAT.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPEXPIREAT from './HPEXPIREAT';

describe('HPEXPIREAT', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string + number', () => {
assert.deepEqual(
HPEXPIREAT.transformArguments('key', 'field', 1),
['HPEXPIREAT', 'key', '1', '1', 'field']
);
});

it('array + number', () => {
assert.deepEqual(
HPEXPIREAT.transformArguments('key', ['field1', 'field2'], 1),
['HPEXPIREAT', 'key', '1', '2', 'field1', 'field2']
);
});

it('date', () => {
const d = new Date();
assert.deepEqual(
HPEXPIREAT.transformArguments('key', ['field1'], d),
['HPEXPIREAT', 'key', d.getTime().toString(), '1', 'field1']
);
});

it('with set option', () => {
assert.deepEqual(
HPEXPIREAT.transformArguments('key', ['field1'], 1, 'XX'),
['HPEXPIREAT', 'key', '1', 'XX', '1', 'field1']
);
});
});

testUtils.testAll('hpExpireAt', async client => {
assert.equal(
await client.hpExpireAt('key', ['field1'], 1),
null,
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
23 changes: 23 additions & 0 deletions packages/client/lib/commands/HPEXPIREAT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisArgument, Command, NullReply } from '../RESP/types';
import { HashExpiration } from './HEXPIRE';
import { RedisVariadicArgument, pushVariadicArgument, transformEXAT } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
timestamp: number | Date,
mode?: 'NX' | 'XX' | 'GT' | 'LT'
) {
const args = ['HPEXPIREAT', key, transformEXAT(timestamp)];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => NullReply | Array<HashExpiration>
} as const satisfies Command;
Loading