sensedeep / dynamodb-onetable

DynamoDB access and management for one table designs with NodeJS
https://doc.onetable.io/
MIT License
689 stars 109 forks source link

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

Closed ThijsJung closed 1 year ago

ThijsJung commented 1 year ago

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):