tigrisdata-archive / tigris-client-ts

TypeScript client for Tigris
https://www.tigrisdata.com/docs/sdkstools/typescript/
Apache License 2.0
15 stars 10 forks source link

[BUG]: Document not deserializing to the type in model #360

Open adilansari opened 1 year ago

adilansari commented 1 year ago

Prerequisites

Please answer the following questions for yourself before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

Expected Behavior

@TigrisCollection('shoes')
class Shoe {
    @PrimaryKey({ order: 1 })
    id!: string

    @Field()
    purchaseDate!: string

    @Field({ timestamp: 'createdAt' })
    inserted?: Date
}

For the above collection I expect purchaseDate to be string and inserted to be a Date type in the retrieved document.

Current Behavior

Both inserted and purchaseDate are of Date type.

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

For the following collection

@TigrisCollection('shoes')
class Shoe {
    @PrimaryKey({ order: 1 })
    id!: string

    @Field()
    purchaseDate!: string

    @Field({ timestamp: 'createdAt' })
    inserted?: Date
}
describe('Date field tests', () => {
    let collection: Collection<Shoe>
    beforeEach(async () => {
        const db = getClient(ProjectCatalog).getDatabase()
        collection = await db.createOrUpdateCollection<Shoe>(Shoe)
    })

    it('deserialize date', async () => {
        const docs: Array<Shoe> = [
            {
                id: '1',
                purchaseDate: '2020-04-13T00:00:00.000+08:00',
            },
        ]
        const inserted = await collection.insertMany(docs)
        expect(inserted).toHaveLength(docs.length)

        const readDoc = await collection.findOne()
        expect(readDoc).toBeDefined()
        expect(readDoc?.inserted).toBeInstanceOf(Date) // all good

        expect(readDoc?.purchaseDate).not.toBeInstanceOf(Date) // This test fails
        expect(readDoc?.purchaseDate).toBeInstanceOf(String)
    })
})

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

You can find Tigris SDK version on your local dev project by executing npm list | grep "tigrisdata/core"

Failure Logs

Please include any relevant log snippets or files here.