-
Notifications
You must be signed in to change notification settings - Fork 178
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
jaredwray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.