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

Export a single model and related models? #366

Open
superlou opened this issue Dec 4, 2022 · 3 comments
Open

Export a single model and related models? #366

superlou opened this issue Dec 4, 2022 · 3 comments

Comments

@superlou
Copy link

superlou commented Dec 4, 2022

It looks like the export function will export the entire local-storage database. Is there a way to serialize only a specific model and its relationships?

Since the add-on serializer extends JSONAPISerializer, I expected it to include the related models data, but it only appears to include the related IDs and model names.

@superlou
Copy link
Author

superlou commented Dec 6, 2022

I think I figured out how to export a single model and its relationships by putting all the related records into the root data attribute.

  createImportableJson(model) {
    let json = model.serialize({ includeId: true });
    json.data = [json.data];
    
    let related = model.children;
    json.data = json.data.concat(related.map(
      (model) => model.serialize({ includeId: true }).data
    ));
    
    return json;
  }

This imports, however it looks like it deletes everything in the storage except for what was imported. Is there a way to have it add and/or overwrite, rather than start from scratch?

Edit: That might be as simple as setting {truncate: false} in the import options. Are there any side-effects of that I might be missing?

@fsmanuel
Copy link
Member

fsmanuel commented Dec 6, 2022

Hey @superlou you are right. truncate: false is the option you are looking for. If set to true it will wipe out all records for record types that are included in the import:

if (options.truncate) {
content.data.forEach((record) => {
truncateTypes.addObject(record.type);
});
truncateTypes.forEach((type) => {
const singularType = singularize(type);
const adapter = store.adapterFor(singularType);
adapter._getIndex(type).forEach((storageKey) => {
delete get(adapter, '_storage')[storageKey];
});
adapter._getIndex(type).reset();
// unload from store
store.unloadAll(singularType);
});
}

@superlou
Copy link
Author

superlou commented Dec 6, 2022

Thanks. This is working, but would it make sense to leave this open as a feature request (export a single model or subset of the records)? Otherwise, I guess it can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants