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

fix: Correctly persist createdAt attribute #119

Merged
merged 2 commits into from
Oct 26, 2020
Merged

fix: Correctly persist createdAt attribute #119

merged 2 commits into from
Oct 26, 2020

Conversation

jakubkoci
Copy link
Contributor

I needed to move createAt attribute initialization up in the inheritance chain to every particular record. I'm not sure if this is a good approach in the long-term but it at least fixes the issue for now. I also aligned id attribute initialization across all records, so even if this is a wrong approach we have it wrong in the same way everywhere :)

@jakubkoci jakubkoci requested a review from a team as a code owner October 24, 2020 16:31
fixes issue #118

Signed-off-by: Jakub Koci <[email protected]>
Copy link
Contributor

@TimoGlastra TimoGlastra left a comment

Choose a reason for hiding this comment

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

Looks good, just the suggestion to use the new short circuit operator

@@ -17,7 +18,7 @@ export class BasicMessageRecord extends BaseRecord implements BasicMessageStorag
public readonly type = BasicMessageRecord.type;

public constructor(props: BasicMessageStorageProps) {
super(props.id || uuid());
super(props.id || uuid(), props.createdAt || Date.now());
Copy link
Contributor

Choose a reason for hiding this comment

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

TypeScript 4.0 introduced some new short circuit assignment operators. Which are perfect for this usage.

Suggested change
super(props.id || uuid(), props.createdAt || Date.now());
super(props.id ?? uuid(), props.createdAt ?? Date.now());

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see. That's nice. I can't even count how often I solve issues with if (number) ... 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, as I'm thinking about it. Are they really perfect for this usage? Maybe values '' and 0 are not quite valid as id or createAt

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that's perhaps the responsibility of some eventual validation. Ok, I'll change it to ??

Copy link
Contributor

Choose a reason for hiding this comment

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

Well yeah, 0 is probably not a commonly used value. However it is unexpected behaviour IMO to change the value without notic. When I pass a value of 0 and it is automatically changed to Date.now without warning or error that is weird I think. Same with empty string.

@@ -49,7 +51,7 @@ export class ConnectionRecord extends BaseRecord implements ConnectionStoragePro
public readonly type = ConnectionRecord.type;

public constructor(props: ConnectionStorageProps) {
super(props.id);
super(props.id || uuid(), props.createdAt || Date.now());
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

@@ -27,7 +28,7 @@ export class CredentialRecord extends BaseRecord implements CredentialStoragePro
public state: CredentialState;

public constructor(props: CredentialStorageProps) {
super(props.id ? props.id : uuid());
super(props.id || uuid(), props.createdAt || Date.now());
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

@@ -18,20 +19,20 @@ class TestRecord extends BaseRecord {
public readonly type = TestRecord.type;

public constructor(props: TestRecordProps) {
super(props.id || uuid());
super(props.id || uuid(), props.createdAt || Date.now());
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

@@ -15,7 +17,7 @@ export class ProvisioningRecord extends BaseRecord {
public readonly type = ProvisioningRecord.type;

public constructor(props: ProvisioningRecordProps) {
super(props.id);
super(props.id || uuid(), props.createdAt || Date.now());
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

@jakubkoci
Copy link
Contributor Author

Looks good, just the suggestion to use the new short circuit operator

Hmm, that interesting, but how does it actually work? I'm looking around but can't find anything about that 😄

@TimoGlastra
Copy link
Contributor

TimoGlastra commented Oct 26, 2020

Looks good, just the suggestion to use the new short circuit operator

Hmm, that interesting, but how does it actually work? I'm looking around but can't find anything about that 😄

I linked to the wrong thing. What I actually mean is "nullish coalescing"

Basically when you use || it will look if the left hand side results in a truthy or falsy value. This means if I provide createdAt. value of 0 it will equal to falsy and use Date.now. With ?? it will check whether the variable has a value (so not null or undefined). This means that a value of 0 will not be seen as falsy and will be used.

Also see: https://github.com/tc39/proposal-nullish-coalescing/

Signed-off-by: Jakub Koci <[email protected]>
@jakubkoci jakubkoci merged commit 797a112 into openwallet-foundation:master Oct 26, 2020
@jakubkoci jakubkoci deleted the fix/record-created-at branch October 26, 2020 14:33
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

Successfully merging this pull request may close these issues.

2 participants