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

[HOLD for App onyx bump] Add getAllEntries function for Onyx. #544

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 11 additions & 0 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
} from './types';
import OnyxUtils from './OnyxUtils';
import logMessages from './logMessages';
import type {KeyValuePairList} from './storage/providers/types';

// Keeps track of the last connectionID that was used so we can keep incrementing it
let lastConnectionID = 0;
Expand Down Expand Up @@ -644,6 +645,15 @@ function update(data: OnyxUpdate[]): Promise<void> {
return clearPromise.then(() => Promise.all(promises.map((p) => p()))).then(() => undefined);
}

/**
* Get all keys and values from Onyx storage.
*
* Note: This should only be used for troubleshooting purposes. Do not use this for any production features.
*/
function getAllEntries(): Promise<KeyValuePairList> {
return Storage.getAllEntries();
}

const Onyx = {
METHOD: OnyxUtils.METHOD,
connect,
Expand All @@ -656,6 +666,7 @@ const Onyx = {
clear,
init,
registerLogger: Logger.registerLogger,
getAllEntries,
} as const;

export default Onyx;
Expand Down
5 changes: 5 additions & 0 deletions lib/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ const Storage: Storage = {
*/
getAllKeys: () => tryOrDegradePerformance(() => provider.getAllKeys()),

/**
* Returns all entries from the database
*/
getAllEntries: () => tryOrDegradePerformance(() => provider.getAllEntries()),

/**
* Gets the total bytes of the store
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/storage/providers/IDBKeyValProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {UseStore} from 'idb-keyval';
import {set, keys, getMany, setMany, get, clear, del, delMany, createStore, promisifyRequest} from 'idb-keyval';
import {set, keys, getMany, setMany, get, clear, del, delMany, createStore, promisifyRequest, entries} from 'idb-keyval';
import utils from '../../utils';
import type StorageProvider from './types';
import type {OnyxKey, OnyxValue} from '../../types';
Expand Down Expand Up @@ -47,6 +47,7 @@ const provider: StorageProvider = {
multiSet: (pairs) => setMany(pairs, idbKeyValStore),
clear: () => clear(idbKeyValStore),
getAllKeys: () => keys(idbKeyValStore),
getAllEntries: () => entries(idbKeyValStore),
getItem: (key) =>
get(key, idbKeyValStore)
// idb-keyval returns undefined for missing items, but this needs to return null so that idb-keyval does the same thing as SQLiteStorage.
Expand Down
4 changes: 4 additions & 0 deletions lib/storage/providers/MemoryOnlyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ const provider: StorageProvider = {
return Promise.resolve(_.keys(store));
},

getAllEntries() {
return Promise.resolve(Object.entries(store));
Copy link

Choose a reason for hiding this comment

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

NAB - why Object here instead of _ as above in getAllKeys()? Seems like the same effect either way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Generally we don't use underscore in TS files, and have lint rules to prevent it. but the lint config in Onyx is a bit different from E/App - most of our TS-specific lint rules are in E/App and should be moved to our ESLint plugin.

Copy link

Choose a reason for hiding this comment

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

ah okay got it. Should I go ahead and merge this or does it need to wait for the other related changes?

},

/**
* Gets the total bytes of the store.
* `bytesRemaining` will always be `Number.POSITIVE_INFINITY` since we don't have a hard limit on memory.
Expand Down
4 changes: 4 additions & 0 deletions lib/storage/providers/NoopProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ const provider: StorageProvider = {
return Promise.resolve([]);
},

getAllEntries() {
return Promise.resolve([]);
},

/**
* Gets the total bytes of the store.
* `bytesRemaining` will always be `Number.POSITIVE_INFINITY` since we don't have a hard limit on memory.
Expand Down
2 changes: 2 additions & 0 deletions lib/storage/providers/SQLiteProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const provider: StorageProvider = {
const result = rows?._array.map((row) => row.record_key);
return (result ?? []) as KeyList;
}),
// eslint-disable-next-line no-underscore-dangle
getAllEntries: () => db.executeAsync('SELECT record_key, valueJSON FROM keyvaluepairs;', []).then(({rows}) => (rows?._array ?? []).map((row) => [row.item(0), row.item(1)])),
removeItem: (key) => db.executeAsync('DELETE FROM keyvaluepairs WHERE record_key = ?;', [key]),
removeItems: (keys) => {
const placeholders = keys.map(() => '?').join(',');
Expand Down
5 changes: 5 additions & 0 deletions lib/storage/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ type StorageProvider = {
*/
getAllKeys: () => Promise<KeyList>;

/**
* Get all key-value pairs from the database
*/
getAllEntries: () => Promise<KeyValuePairList>;

/**
* Removes given key and its value from storage
*/
Expand Down
Loading