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

Correct usage of restore when date is within array of objects #50

Open
evallgar opened this issue May 25, 2020 · 2 comments
Open

Correct usage of restore when date is within array of objects #50

evallgar opened this issue May 25, 2020 · 2 comments

Comments

@evallgar
Copy link

Hi there!

I would like to ask how to correctly use restore for date timestamp when data looks like this:

 "wFLrjgdW7Jjmjs5sX": {
  "_id": "wFLrjgdW7Jjmjs5sX",
  "notifications": [
    {
      "date": {
        "_seconds": 1589998976,
        "_nanoseconds": 490000000
      },
      "type": "new",
      "message": "Contract has been created.",
      "user": "Philip Rawsen"
    },
    {
      "date": {
        "_seconds": 1589999521,
        "_nanoseconds": 985000000
      },
      "type": "payment",
      "message": "Customer payment for USD $ 67.92",
      "user": "Lena Perks"
    }
  ]
}

When I use the command

// Start exporting your data
firestoreService
  .restore(`./collections/${collectionName}.json`, {
    dates: ['notifications.date']
  });

I cannot get it to restore it as a timestamp. It restores as a map field.
I have also tried with 'notifications[].date' and simply 'date' unsuccessfully.

Any help is greatly appreciated. Thanks.

@dalenguyen
Copy link
Owner

Hi @evallgar, an object of data under array is not supported yet. I will figure a solution to solve this. You can check the code and make some changes to support this case if you want to contribute.

@Parranoh
Copy link

Parranoh commented Jul 29, 2020

If you put this in helper.ts

const applyConversion = (data: any, conversion: Function, key: Array<string>) => {
  if (!key || key.length === 0) return;

  if (Array.isArray(data)) {
    data.forEach(d => applyConversion(d, conversion, key));
    return;
  }

  if (!data.hasOwnProperty(key[0])) return;

  if (key.length === 1) {
    const theKey: string = key[0];
    const checkResult = conversion(data[theKey]);
    if (checkResult) data[theKey] = checkResult;
  } else if (isObject(data)) {
    applyConversion(data[key[0]], conversion, key.splice(1));
  }
}

//gives the properties of data, specified by keys, to conversion; keeps old value of conversion returns null, else replaces value with return value of conversion
export const convertProperty = (data: any, conversion: Function, keys: Array<string>) => {
  keys.forEach((key: string) => {
    applyConversion(data, conversion, key.split('.'));
  });
}

and replace the relevant part (line 132) in import.ts with

// Update date value
if (options.dates && options.dates.length > 0) {
  convertProperty(data, makeTime, options.dates)
}

(similarly for refs and and geos), that should do the trick. I've tested it only with one set of data though.

In the above example you'd use 'notifcations.date' to get the desired result; the layer of the array in the JSON tree basically gets skipped.

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

3 participants