SierraSoftworks / Iridium

A high performance MongoDB ORM for Node.js
http://sierrasoftworks.github.io/Iridium/
Other
569 stars 25 forks source link

Complex @Property Types #97

Open Css-IanM opened 7 years ago

Css-IanM commented 7 years ago

Could anyone point towards documentation or an example of more complex types? I'm working on migrating a complex Shopping Cart which I would like to assign sub-document ObjectIDs or some other unique identifier at the scope of the user's cart. An example i'm trying to model for below. 1.) Not sure the best way to handle an object that could be one of two types. 2.) how to specify sub document ObjectIds. 3.) I Imagine for the Endpoint denormalization i have on the line items I will have to just make a constant for that.

export class Cart {
    constructor(
        private userId: string,
        private dateCreated: string,
        private lineItems: Array<CartLineItem>,
        private _id?: string,
    ) { }
...
}

export class CartLineItem {
    constructor(
        private type: string,
        private keys: IndexedItemKeys | ArchivedItemKeys,
        private endpoint: Endpoint,
        private pages: number[],
        private description: string,
        private cost?: number,
        private _id?: string
    ) { }
}

export class IndexedItemKeys {
    constructor(
        private instrument: string,
        private documentName: string
    ) { }
}

export class ArchivedItemKeys {
    constructor(
        private bookReference: string,
        private bookNumber: string,
        private bookSuffix: string,
        private pageNumber: string,
        private pageSuffix: string
    ) { }

}
export class Endpoint {
    constructor(
        private stateId: number,
        private countyId: number,
        private stateAbbrevation: string,
        private url: string,
        private name: string,
        private status: number,
        private hasCreditModule: boolean,
        private hasArchiveModule: boolean,
        private _id?: string,
    ) { }
...
}
// Iridium Schema
@Collection('carts')
export class CartMongoSchema extends Instance<CartDTO, CartMongoSchema> {
    @ObjectID
    _id: string;
    @ObjectID
    userId: string;
    @ObjectID
    endpointId: string;
    @Property(Date)
    dateCreated: Date;
    @Property({
        $required: false,
        $type: [{
            type: String,
            // Not sure best way to handles these as depicted in class above can be of either type
            keys: Object,
            // is an entity from another collection and has an ObjectID.
           // intentional de-normalization.
            endpoint: Object,

            pages: [Number],
            description: String,
            cost: Number,
        }]})
    lineItems: Array<CartLineItem>;
}
notheotherben commented 7 years ago

Hi Ian, this isn't something currently supported out the box with Iridium's validation framework, that being said, there's no reason you can't add a custom validation plugin which allows you to specify intersect types.

I'm currently in the middle of SREcon and don't have a bunch of free time, but I'll see if I can out something together as part of the 8.x alpha to address this use case (potentially involving a rewrite of the validation framework and adding support for MongoDB native collection validation as part of that).