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

params.fields are not respected when using table.getItem() or table.queryItems() #497

Closed
ThijsJung opened this issue Aug 18, 2023 · 0 comments
Labels
bug Something isn't working fixed Issue has been fixed in the repo

Comments

@ThijsJung
Copy link

ThijsJung commented Aug 18, 2023

Describe the bug

When querying DynamoDB using Table.getItem() or Table.queryItems(), params.fields are ignored and full objects are returned. When querying using Model.get() or Model.find(), everything works as expected and only the requested fields are returned.

To Reproduce

Full details can be found the test/debug.ts file here. I'll copy/paste an outline of the issue below.

Cut/Paste

Schema

const schema = {
    version: '0.0.1',
    indexes: {
        primary: {hash: 'pk'},
    },
    params: {
        createdField: 'createdAt',
        updatedField: 'updatedAt',
        isoDates: true,
        timestamps: true,
    },
    models: {
        User: {
            pk: { type: String, value: '${id}'},

            name: { type: String },
            nickName: { type: String },
            id: { type: String, generate: "ulid" },
            email: { type: String, required: true },
        }
    } as const,
}

Test

const User = table.getModel('User')
const userProperties = {
    name: "Daenerys I Targaryen",
    nickName: "Breaker of chains",
    email: "iHeartDragons@example.com",
}
const user = await User.create(userProperties)
const userId = user.id!

test('model.get() respects the fields.', async () => {
    // Works as expected, only returns id and name.
    const user = await User.get({pk: userId}, {fields: ["id", "name"], parse: true})
    expect(user).toMatchObject({
        name: "Daenerys I Targaryen",
    });
    expect(user!.id).toMatch(Match.ulid);
    expect(user!.nickName).toBeUndefined();
    expect(user!.email).toBeUndefined();
})

test('table.getItem() does not respect the fields.', async () => {
    // Should break but doesn't because the entire record is returned.
    let tableGetItem = await table.getItem({pk: userId}, {fields: ["id", "name"], parse: true})
    expect(tableGetItem).toMatchObject(userProperties);
})

Expected behavior
Table.getItem() and Table.queryItems() should respect the params.fields and return only the requested fields.

Environment (please complete the following information):

  • OS: macOS Venture 13.4.1 (22F82)
  • Node Version: v18.16.0
  • OneTable Version: 2.6.4
  • TypeScript Version: 5.1.6
@mobsense mobsense added the bug Something isn't working label Aug 18, 2023
@mobsense mobsense added the fixed Issue has been fixed in the repo label Sep 6, 2023
@mobsense mobsense closed this as completed Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Issue has been fixed in the repo
Projects
None yet
Development

No branches or pull requests

2 participants