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

Update state database list method to query based on namespace match. #7742

Merged
merged 4 commits into from Jan 5, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/filebrowser/src/browser.ts
Expand Up @@ -316,8 +316,8 @@ export namespace FileBrowser {
* The default is `true`.
*
* #### Notes
* The file browser model will need to be restored before for the file
* browser to start saving its state.
* The file browser model will need to be restored manually for the file
* browser to be able to save its state.
*/
restore?: boolean;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/statedb/src/restorablepool.ts
Expand Up @@ -107,8 +107,8 @@ export class RestorablePool<
* @param obj - The object object being added.
*
* #### Notes
* The object passed into the tracker is added synchronously; its existence in
* the tracker can be checked with the `has()` method. The promise this method
* The object passed into the pool is added synchronously; its existence in
* the pool can be checked with the `has()` method. The promise this method
* returns resolves after the object has been added and saved to an underlying
* restoration connector, if one is available.
*/
Expand Down
11 changes: 7 additions & 4 deletions packages/statedb/src/statedb.ts
Expand Up @@ -177,8 +177,8 @@ export class StateDB<
/**
* Fetch a list from the database.
*/
private async _list(query?: string): Promise<{ ids: string[]; values: T[] }> {
const { ids, values } = await this._connector.list(query);
private async _list(namespace = ''): Promise<{ ids: string[]; values: T[] }> {
const { ids, values } = await this._connector.list(namespace);

return {
ids,
Expand Down Expand Up @@ -300,11 +300,14 @@ export namespace StateDB {

/**
* Retrieve the list of items available from the data connector.
*
* @param namespace - If not empty, only keys whose first token before `:`
* exactly match `namespace` will be returned, e.g. `foo` in `foo:bar`.
*/
async list(query = ''): Promise<{ ids: string[]; values: string[] }> {
async list(namespace = ''): Promise<{ ids: string[]; values: string[] }> {
return Object.keys(this._storage).reduce(
(acc, val) => {
if (val && val.indexOf(query) === 0) {
if (namespace === '' ? true : namespace === val.split(':')[0]) {
acc.ids.push(val);
acc.values.push(this._storage[val]);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/test-statedb/src/statedb.spec.ts
Expand Up @@ -120,7 +120,10 @@ describe('StateDB', () => {
'foo:qux',
'abc:def',
'abc:ghi',
'abc:jkl'
'abc:jkl',
'foo-two:bar',
'foo-two:baz',
'foo-two:qux'
];

await Promise.all(keys.map(key => db.save(key, { value: key })));
Expand Down