electron-store icon indicating copy to clipboard operation
electron-store copied to clipboard

Support dates

Open sindresorhus opened this issue 7 years ago • 15 comments

Issuehunt badges

Would be nice if you could store.set('foo', new Date()) and get back a Date object when you do store.get('foo'). Could probably make use of the second argument to JSON.parse(). We need to save some kind of information to indicate that it's a native Date format. Maybe a separate __meta__ key or something. Suggestions welcome.


IssueHunt Summary

Backers (Total: $80.00)

Submitted pull Requests


Become a backer now!

Or submit a pull request to get the deposits!

Tips

sindresorhus avatar Jun 18 '17 16:06 sindresorhus

You could serialize dates to ISO 8601 format in .set(). Then you could parse ISO 8601 dates into Date objects in .get() but override their .toString() to return the string as it was stored (rather than the default (new Date()).toString() funkiness).

That would:

  • Avoid adding extra fields
  • Be convenient, regardless of how the date was set (either as a string literal or a Date object)
  • Be safe, in case the date was set as a string literal

sholladay avatar Jul 03 '17 06:07 sholladay

Then you could parse ISO 8601 dates into Date objects in .get() but override their .toString() to return the string as it was stored (rather than the default (new Date()).toString() funkiness).

That sounds hackish. What if the user tries to do typeof store.get('date-string') and expected it to be string, not Date. I would rather store some extra metadata than having unexpected behavior.

sindresorhus avatar Jul 03 '17 10:07 sindresorhus

Just another suggestion:

Rather than a separate __meta__ key, what are your thoughts on prepending the date type to the value? Meaning:

const d = new Date();
const s = d.toString();

// Current:
conf.set('date', d);    // { "date": "Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }
conf.set('dateStr', s); // { "date": "Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }

// Suggested:
conf.set('date', d);    // { "date": "__date__,Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }
conf.set('dateStr', s); // { "date": "Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT)" }

If the value passed to .set() is an instance of Date we could prepend a special string to the serialised string, and just check for that when parsing it back from JSON.

acheronfail avatar Jan 27 '18 07:01 acheronfail

@acheronfail Can't do that. What if the user actually sets __date__,Sat Jan 27 2018 18:42:32 GMT+1100 (AEDT) themselves and expects a string back.

sindresorhus avatar Feb 05 '18 03:02 sindresorhus

conf (which is where this should be first implemented) now has a __internal__ that we could use to store this kind of metadata.

sindresorhus avatar Sep 09 '19 08:09 sindresorhus

@issuehunt has funded $80.00 to this issue.


issuehunt-oss[bot] avatar Sep 09 '19 08:09 issuehunt-oss[bot]

How about adding the possibility to store a function in the store and execute it automatically?

e.g. conf.set('foo', () => new Date());

And whenever the user fetches the value, we just evaluate if it's a function or not and run it.

rafaelramalho19 avatar Sep 09 '19 15:09 rafaelramalho19

@rafaelramalho19 I don't think that's a good idea. That would be a security nightmare. Anything on the computer could add code to the plaintext config file and have the app execute it in a trusted context.

sindresorhus avatar Sep 09 '19 19:09 sindresorhus

I guess thats true. Just wanted it to allow for broader use-cases.

rafaelramalho19 avatar Sep 09 '19 22:09 rafaelramalho19

👋 I stumbled upon this library:

https://github.com/wynntee/joss

Would it help in this case?

dnlup avatar Feb 25 '21 18:02 dnlup

v8.serialize?

Richienb avatar Mar 04 '21 06:03 Richienb

v8.serialize?

Interesting, you mean serializing just the Date or the whole db?

dnlup avatar Mar 05 '21 14:03 dnlup

By serializing the whole db, there is no longer a problem with creating non-conflicting values.

Richienb avatar Mar 05 '21 14:03 Richienb

How about just using timestamps ?

KaKi87 avatar Jul 09 '21 14:07 KaKi87

How about just using timestamps ?

The challenge would then become telling apart a timestamp from a regular number

Richienb avatar Jul 10 '21 03:07 Richienb