Skip to content

tiered: add typescript support #722

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

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 10 additions & 12 deletions packages/tiered/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
"name": "@keyv/tiered",
"version": "1.0.4",
"description": "Tiered storage adapter for Keyv",
"main": "src/index.js",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "echo 'No build step required.'",
"build": "tsc --project tsconfig.dist.json",
"prepare": "yarn build",
"test": "xo && c8 ava --serial",
"test:ci": "xo && ava --serial",
"test:ci": "xo && ava --serial",
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite"
},
"xo": {
"rules": {
"unicorn/prefer-module": 0,
"unicorn/prefer-node-protocol": 0,
"ava/no-ignored-test-files": [
"error",
{
Expand All @@ -22,16 +21,15 @@
"ts"
]
}
]
],
"import/extensions": 0
}
},
"ava": {
"require": [
"requirable",
"ts-node/register"
],
"extensions": [
"js",
"ts"
]
},
Expand All @@ -57,12 +55,13 @@
"url": "https://github.com/jaredwray/keyv/issues"
},
"homepage": "https://github.com/jaredwray/keyv",
"dependencies": {},
"devDependencies": {
"delay": "^5.0.0",
"@keyv/test-suite": "*",
"@types/keyv": "^3.1.4",
"@keyv/sqlite": "*",
"ava": "^5.2.0",
"c8": "^7.13.0",
"eslint": "^8.35.0",
"keyv": "*",
"requirable": "^1.0.5",
"this": "^1.1.0",
Expand All @@ -74,11 +73,10 @@
"tsd": {
"directory": "test"
},
"types": "./src/index.d.ts",
"engines": {
"node": ">= 12"
},
"files": [
"src"
"dist"
]
}
27 changes: 0 additions & 27 deletions packages/tiered/src/index.d.ts

This file was deleted.

104 changes: 0 additions & 104 deletions packages/tiered/src/index.js

This file was deleted.

119 changes: 119 additions & 0 deletions packages/tiered/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import EventEmitter from 'node:events';
import Keyv from 'keyv';
import type {Options, Options_} from './types';

type KeyvTieredIndex = 'local' | 'remote';

class KeyvTiered extends EventEmitter {
opts: Options_;
remote: Keyv;
local: Keyv;
iterationLimit?: string | number;

constructor({remote = new Keyv(), local = new Keyv(), ...options}: Options) {
super();
this.opts = {
validator: () => true,
dialect: 'tiered',
...options,
};
this.remote = remote;
this.local = local;
}

async get(key: string): Promise<any> {
const localResult: unknown = await this.local.get(key);

if (localResult === undefined || !this.opts.validator(localResult, key)) {
const remoteResult: unknown = await this.remote.get(key);

if (remoteResult === localResult) {
return remoteResult;
}

await this.local.set(key, remoteResult);

return remoteResult;
}

return localResult;
}

async getMany(keys: string[]): Promise<unknown[]> {
const promises = [];
for (const key of keys) {
promises.push(this.get(key));
}

const values = await Promise.all(promises);
const data: unknown[] = [];
for (const value of values) {
data.push(value);
}

return data;
}

async set(key: string, value: any, ttl?: number) {
const toSet: KeyvTieredIndex[] = ['local', 'remote'];
return Promise.all(toSet.map(async store => this[store].set(key, value, ttl)),
);
}

async clear(): Promise<undefined> {
const toClear: KeyvTieredIndex[] = ['local'];
if (!this.opts.localOnly) {
toClear.push('remote');
}

await Promise.all(toClear
.map(async store => this[store].clear()),
);

return undefined;
}

async delete(key: string): Promise<boolean> {
const toDelete: KeyvTieredIndex[] = ['local'];
if (!this.opts.localOnly) {
toDelete.push('remote');
}

const deleted = await Promise.all(toDelete
.map(async store => this[store].delete(key)),
);

return deleted.every(Boolean);
}

async deleteMany(keys: string[]): Promise<boolean> {
const promises = [];
for (const key of keys) {
promises.push(this.delete(key));
}

const values = await Promise.all(promises);

return values.every(Boolean);
}

async has(key: string): Promise<boolean> {
const response = await this.local.has(key);

if (!response || !this.opts.validator(response, key)) {
return this.remote.has(key);
}

return response;
}

async * iterator(namespace?: string): AsyncGenerator<any, void, any> {
const limit = Number.parseInt(this.iterationLimit as string, 10) || 10;
this.remote.opts.iterationLimit = limit;
for await (const entries of this.remote.iterator(namespace)) {
yield entries;
}
}
}

export = KeyvTiered;
16 changes: 16 additions & 0 deletions packages/tiered/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type Keyv from 'keyv';

export type Options = {
local: Keyv;
remote: Keyv;
localOnly?: boolean;
iterationLimit?: number | string;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
export type Options_ = {
validator: (value: any, key: string) => boolean;
dialect: string;
iterationLimit?: number | string;
localOnly?: boolean;
};
Loading