Skip to content

Commit

Permalink
add store service to pagination service, update kv imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hellobontempo committed Oct 9, 2024
1 parent 3527274 commit 66cae12
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
62 changes: 30 additions & 32 deletions ui/app/services/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: BUSL-1.1
*/

import Store from '@ember-data/store';
import Service, { inject as service } from '@ember/service';
import { run, schedule } from '@ember/runloop';
import { resolve, Promise } from 'rsvp';
import { dasherize } from '@ember/string';
Expand Down Expand Up @@ -32,7 +32,8 @@ export function keyForCache(query) {
return JSON.stringify(cacheKeyObject);
}

export default class PaginationService extends Store {
export default class PaginationService extends Service {
@service store;
lazyCaches = new Map();

setLazyCacheForModel(modelName, key, value) {
Expand Down Expand Up @@ -71,7 +72,7 @@ export default class PaginationService extends Store {
const skipCache = query.skipCache;
// We don't want skipCache to be part of the actual query key, so remove it
delete query.skipCache;
const adapter = this.adapterFor(modelType);
const adapter = this.store.adapterFor(modelType);
const modelName = normalizeModelName(modelType);
const dataCache = skipCache ? this.clearDataset(modelName) : this.getDataset(modelName, query);
const responsePath = query.responsePath;
Expand All @@ -87,7 +88,7 @@ export default class PaginationService extends Store {
return adapter
.query(this, { modelName }, query, null, adapterOptions)
.then((response) => {
const serializer = this.serializerFor(modelName);
const serializer = this.store.serializerFor(modelName);
const datasetHelper = serializer.extractLazyPaginatedData;
const dataset = datasetHelper
? datasetHelper.call(serializer, response)
Expand Down Expand Up @@ -150,32 +151,28 @@ export default class PaginationService extends Store {
forceUnload(modelName) {
// Hack to get unloadAll to work correctly until we update to ember-data@4.12
// so that all the records are properly unloaded and we don't get ghost records
this.peekAll(modelName).length;
this.store.peekAll(modelName).length;
// force destroy queue to flush https://github.com/emberjs/data/issues/5447
run(() => this.unloadAll(modelName));
run(() => this.store.unloadAll(modelName));
}

// pushes records into the store and returns the result
fetchPage(modelName, query) {
const response = this.constructResponse(modelName, query);
this.forceUnload(modelName);
// Hack to ensure the pushed records below all get in the store. remove with update to ember-data@4.12
this.peekAll(modelName).length;
this.store.peekAll(modelName).length;
return new Promise((resolve) => {
// push subset of records into the store
schedule('destroy', () => {
this.push(
this.serializerFor(modelName).normalizeResponse(
this,
this.modelFor(modelName),
response,
null,
'query'
)
this.store.push(
this.store
.serializerFor(modelName)
.normalizeResponse(this, this.store.modelFor(modelName), response, null, 'query')
);
// Hack to make sure all records get in model correctly. remove with update to ember-data@4.12
this.peekAll(modelName).length;
const model = this.peekAll(modelName).slice();
this.store.peekAll(modelName).length;
const model = this.store.peekAll(modelName).slice();
model.set('meta', response.meta);
resolve(model);
});
Expand Down Expand Up @@ -207,6 +204,7 @@ export default class PaginationService extends Store {
this.lazyCaches.clear();
}

// TODO can we just remove this and use clearDataset??
clearAllDatasets() {
this.clearDataset();
}
Expand All @@ -221,19 +219,19 @@ export default class PaginationService extends Store {
* the following fixes the issue by explicitly unloading the mount-config models associated to the parent
* this should be looked into further to find the root cause, at which time these overrides may be removed
*/
unloadAll(modelName) {
const hasMountConfig = ['auth-method', 'secret-engine'];
if (hasMountConfig.includes(modelName)) {
this.peekAll(modelName).forEach((record) => this.unloadRecord(record));
} else {
super.unloadAll(modelName);
}
}
unloadRecord(record) {
const hasMountConfig = ['auth-method', 'secret-engine'];
if (record && hasMountConfig.includes(record.constructor.modelName) && record.config) {
super.unloadRecord(record.config);
}
super.unloadRecord(record);
}
// unloadAll(modelName) {
// const hasMountConfig = ['auth-method', 'secret-engine'];
// if (hasMountConfig.includes(modelName)) {
// this.store.peekAll(modelName).forEach((record) => this.unloadRecord(record));
// } else {
// super.unloadAll(modelName);
// }
// }
// unloadRecord(record) {
// const hasMountConfig = ['auth-method', 'secret-engine'];
// if (record && hasMountConfig.includes(record.constructor.modelName) && record.config) {
// super.unloadRecord(record.config);
// }
// super.unloadRecord(record);
// }
}
4 changes: 2 additions & 2 deletions ui/lib/kv/addon/components/page/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import { pathIsDirectory } from 'kv/utils/kv-breadcrumbs';

export default class KvListPageComponent extends Component {
@service flashMessages;
@service pagination;
@service('app-router') router;
@service store;

@tracked secretPath;
@tracked metadataToDelete = null; // set to the metadata intended to delete
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class KvListPageComponent extends Component {
try {
// The model passed in is a kv/metadata model
await model.destroyRecord();
this.store.clearDataset('kv/metadata'); // Clear out the store cache so that the metadata/list view is updated.
this.pagination.clearDataset('kv/metadata'); // Clear out the store cache so that the metadata/list view is updated.
const message = `Successfully deleted the metadata and all version data of the secret ${model.fullSecretPath}.`;
this.flashMessages.success(message);
// if you've deleted a secret from within a directory, transition to its parent directory.
Expand Down
3 changes: 2 additions & 1 deletion ui/lib/kv/addon/components/page/secret/metadata/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import errorMessage from 'vault/utils/error-message';
export default class KvSecretMetadataDetails extends Component {
@service controlGroup;
@service flashMessages;
@service pagination;
@service('app-router') router;
@service store;

Expand All @@ -54,7 +55,7 @@ export default class KvSecretMetadataDetails extends Component {
const adapter = this.store.adapterFor('kv/metadata');
try {
await adapter.deleteMetadata(backend, path);
this.store.clearDataset('kv/metadata'); // Clear out the store cache so that the metadata/list view is updated.
this.pagination.clearDataset('kv/metadata'); // Clear out the store cache so that the metadata/list view is updated.
this.flashMessages.success(
`Successfully deleted the metadata and all version data for the secret ${path}.`
);
Expand Down
4 changes: 2 additions & 2 deletions ui/lib/kv/addon/components/page/secrets/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import errorMessage from 'vault/utils/error-message';
export default class KvSecretCreate extends Component {
@service controlGroup;
@service flashMessages;
@service pagination;
@service('app-router') router;
@service store;

@tracked showJsonView = false;
@tracked errorMessage;
Expand Down Expand Up @@ -60,7 +60,7 @@ export default class KvSecretCreate extends Component {
try {
// try saving secret data first
yield secret.save();
this.store.clearDataset('kv/metadata'); // Clear out the store cache so that the metadata/list view is updated.
this.pagination.clearDataset('kv/metadata'); // Clear out the store cache so that the metadata/list view is updated.
this.flashMessages.success(`Successfully saved secret data for: ${secret.path}.`);
} catch (error) {
let message = errorMessage(error);
Expand Down

0 comments on commit 66cae12

Please sign in to comment.