uon-team / db

MongoDB module for @uon/core server applications
MIT License
0 stars 0 forks source link

bug: model id not always of type string #1

Closed sdalexandre closed 2 years ago

sdalexandre commented 3 years ago

id are exposed as a string but are not always really of string type: "id": { "type": "Buffer", "data": [ 95, 124, 143, 224, 215, 119, 113, 55, 116, 183, 176, 46 ] } So, I need to do this: if (typeof sub.id !== 'string') { sub.id = new NativeId(sub.id).toHexString(); } That is happening when there is a reference inside a sub structure.

For example:

@Model()
export class Homework {
    @ID()
    id: string;

    @ArrayMember(HomeworkStudent )
    students: HomeworkStudent [];
}

@Model()
export class Student {
    @ID()
    id: string;
}

@Model()
export class HomeworkStudent {
    @Member()
    student: Student;  // This id will be exposed as string but will actually be of type of buffer.

    @Member()
    addedOn: Date;
}

so I end up using string instead:

@Model()
export class HomeworkStudent {
    @Member()
    studentId: string;

    @Member()
    addedOn: Date;
}
uon-io commented 2 years ago

It has been fixed in commit 768f94a736298caf5fdd641b86d08e6e13dcbce4 deserialization now supports persistent models inside non-persistent model (@Model with no ID) It will be updated on NPM shortly.