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

feature: Implement hasOne relation (issue #6) #28

Merged

Conversation

qmohitsingh
Copy link
Contributor

@qmohitsingh qmohitsingh commented Nov 16, 2023

Implement hasOne relation for issue #6

  • Added logic to handle 'hasOne' in the relation processing loop.

@qmohitsingh
Copy link
Contributor Author

qmohitsingh commented Nov 17, 2023

Please review this PR and let me know if something needs to be changed @nicoabie

@nicoabie nicoabie self-requested a review November 18, 2023 13:54
Copy link
Contributor

@nicoabie nicoabie left a comment

Choose a reason for hiding this comment

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

thanks again for contributing and sorry for the time it took me to review.
are you allowed to request my reviews?

Comment on lines 71 to 89
test('assemble hasOne', () => {
const objects = [{ 'p.personId': 1 }, { 'p.personId': 2 }];

const relations: Relations = {
person: {
contact: ['hasOne', 'contactDetails'],
},
};
const aliases = {
p: 'person',
c: 'contact',
};
const identifiers = ['p.personId', 'c.contactId'];

assert.deepStrictEqual(assemble(relations, aliases, identifiers, objects), [
{ personId: 1 },
{ personId: 2 },
]);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a valid test case? I don't think the database is going to give that output

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test deleted

Comment on lines 136 to 163
test('assemble hasOne relationship with complex data', () => {
const objects = [
{ 'p.personId': 1, 'c.contactId': 101 },
{ 'p.personId': 2, 'c.contactId': null },
{ 'p.personId': 3, 'c.contactId': 101 },
{ 'p.personId': 4, 'c.contactId': 103 },
{ 'p.personId': 5 },
];

const relations: Relations = {
person: {
contact: ['hasOne', 'contactDetails'],
},
};
const aliases = {
p: 'person',
c: 'contact',
};
const identifiers = ['p.personId', 'c.contactId'];

assert.deepStrictEqual(assemble(relations, aliases, identifiers, objects), [
{ personId: 1 },
{ personId: 2 },
{ personId: 3 },
{ personId: 4, contactDetails: { contactId: 103 } },
{ personId: 5 },
]);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct.

  1. the database will return something for person 5, see my previous comment
  2. person 1 and 3 point to the same object and that is fine from a strict point of view

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

src/assemble.ts Outdated
@@ -77,6 +78,19 @@ export const assemble: AssembleFn = <T>(
if (child[childIdentifier.split('.')[1]] !== null) {
parent[parentChildRelation[1]].push(child);
}
} else if (parentChildRelation[0] === 'hasOne') {
if (child[childIdentifier.split('.')[1]] !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we should put childIdentifier.split('.')[1] in a variable as it is being calculated 4 times

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@qmohitsingh
Copy link
Contributor Author

thanks again for contributing and sorry for the time it took me to review. are you allowed to request my reviews?

ofcoure!! no worries, and no i don't think i have permission to request the review


assert.deepStrictEqual(assemble(relations, aliases, identifiers, objects), [
{ personId: 1, contactDetails: { contactId: 101 } },
{ personId: 2 },
Copy link
Contributor

Choose a reason for hiding this comment

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

in this case contactDetails should be null

{ personId: 2, contactDetails: null },

Copy link
Contributor Author

@qmohitsingh qmohitsingh Nov 18, 2023

Choose a reason for hiding this comment

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

fixed

Comment on lines 117 to 142
test('assemble hasOne relationship with complex data', () => {
const objects = [
{ 'p.personId': 1, 'c.contactId': 101 },
{ 'p.personId': 2, 'c.contactId': null },
{ 'p.personId': 3, 'c.contactId': 101 },
{ 'p.personId': 4, 'c.contactId': 103 },
];

const relations: Relations = {
person: {
contact: ['hasOne', 'contactDetails'],
},
};
const aliases = {
p: 'person',
c: 'contact',
};
const identifiers = ['p.personId', 'c.contactId'];

assert.deepStrictEqual(assemble(relations, aliases, identifiers, objects), [
{ personId: 1 },
{ personId: 2 },
{ personId: 3 },
{ personId: 4, contactDetails: { contactId: 103 } },
]);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

we can remove this one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test removed

@nicoabie nicoabie merged commit c49a846 into agiletiger:main Nov 18, 2023
@nicoabie nicoabie mentioned this pull request Nov 18, 2023
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