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

Table query #1538

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Table query #1538

wants to merge 17 commits into from

Conversation

famence
Copy link
Contributor

@famence famence commented Nov 10, 2022

Summary:

Adds the ability to table.query() respecting models correctly. Useful for utilising the single table approach when you want to get multiple items of different models within single request.
⚠️ Preferably to be reviewed after #1517 (depends on #1517) as the current one is based on it.

Code sample:

General

Let's assume we have two models Team and TeamMember within single table:

const Team = dynamoose.model("Team", {
  "pk": {
    "type": String,
    "index": {
      "hashKey": true,
      "index": {
        "name": "primary",
        "type": "global"
      }
    },
    "map": "id"
  },
  "sk": {
    "type": String,
    "rangeKey": true
  },
  "name": String,
  "type": {"type": String, "default": "team"}
});
const TeamMember = dynamoose.model("TeamMember", {
  "pk": {
    "type": String,
    "index": {
      "hashKey": true,
      "index": {
        "name": "primary",
        "type": "global"
      }
    },
    "map": "teamId"
  },
  "sk": {
    "type": String,
    "rangeKey": true,
    "map": "id"
  },
  "name": String,
  "type": {"type": String, "default": "member"}
});
const table = new dynamoose.Table("teams", [Team, TeamMember]);

and some instances of each type:

const team1 = new Team({"id": "team_1", "name": "Team 1"});
const member1 = new TeamMember({"teamId": "team_1", "id": "member_1", "name": "Team member 1"});

now we can query the table for any global index (i.e. primary key) and get relevant items with correct models mapping:

await table.query({"pk": "team_1"}).exec()
// will return the following:
[
  Team {
    id: 'team_1',
    sk: '',
    name: 'Team 1',
    type: 'team'
  },
  TeamMember {
    teamId: 'team_1',
    id: 'member_1',
    name: 'Team member 1',
    type: 'member'
  },
]

The models of each item are identified correctly and all the attributes are mapped as we expect in our schemas.

The magic is in respecting attributes' default values which add +2 score weight and in improved Model.schemaCorrectnessScores method by adding up the scores to determine the most suitable model even if the models' schemas are same except default values.

Type (select 1):

  • Bug fix
  • Feature implementation
  • Documentation improvement
  • Testing improvement
  • Test added to report bug (GitHub issue #---- @---)
  • Something not listed here

Is this a breaking change? (select 1):

  • 🚨 YES 🚨
  • No
  • I'm not sure

Is this ready to be merged into Dynamoose? (select 1):

  • Yes
  • No

Are all the tests currently passing on this PR? (select 1):

  • Yes
  • No

Other:

  • I have read through and followed the Contributing Guidelines
  • I have searched through the GitHub pull requests to ensure this PR has not already been submitted
  • I have updated the Dynamoose documentation (if required) given the changes I made
  • I have added/updated the Dynamoose test cases (if required) given the changes I made
  • I agree that all changes made in this pull request may be distributed and are made available in accordance with the Dynamoose license
  • All of my commits and commit messages are detailed, explain what changes were made, and are easy to follow and understand
  • I have filled out all fields above

@famence famence marked this pull request as draft November 10, 2022 15:34
@github-actions
Copy link
Contributor

This pull request is stale because it has been open 7 days with no activity. Remove stale label or comment or this will be closed in 3 days.

@fishcharlie
Copy link
Member

@famence #1517 has been merged. Do you maybe want to merge develop into this branch and finalize anything else and mark it ready for review and I can take a look at it?

@famence
Copy link
Contributor Author

famence commented Apr 23, 2023

@fishcharlie i just finished the technical part so you are welcome to review the code

@famence
Copy link
Contributor Author

famence commented Apr 24, 2023

Documentation updated

@famence famence marked this pull request as ready for review April 24, 2023 13:07
@marcofailache
Copy link

Hey, guys! Thanks for the hard work!
Any timeframe for when this pull request will be merged?

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

Successfully merging this pull request may close these issues.

None yet

3 participants