diff --git a/sdk-core/src/core/core-object.ts b/sdk-core/src/core/core-object.ts index 431f7b0..a7b1994 100644 --- a/sdk-core/src/core/core-object.ts +++ b/sdk-core/src/core/core-object.ts @@ -47,6 +47,19 @@ export abstract class CoreObject { return (this.constructor).type; } + /** + * shortcut for easier chained construction of Include Queries + */ + public include(...objects: Array<(typeof CoreObject) | string>): Array { + return objects.map((object: typeof CoreObject | string) => { + if (typeof object === 'string' || object instanceof String) { + return object; + } + + return `${this.type}.${object.type}`; + }); + } + /** * Re-fills tis object instance with data from the api */ diff --git a/sdk-core/src/core/global-object-pool.ts b/sdk-core/src/core/global-object-pool.ts index a4a5acc..da5ceca 100644 --- a/sdk-core/src/core/global-object-pool.ts +++ b/sdk-core/src/core/global-object-pool.ts @@ -5,12 +5,12 @@ import { CoreObjectAttributes, type CoreObject } from './core-object'; * from the API */ export class GlobalObjectPool { - private static readonly _globalMap: Map = new Map(); + private static readonly _globalMap: Map> = new Map>(); /** * Used by the Generator for adding an object instance with a unique API key into the Registrar */ - public static register(objectInstance: typeof CoreObject): GlobalObjectPool { + public static register(objectInstance: typeof CoreObject): GlobalObjectPool { this._globalMap.set(objectInstance.type, objectInstance); return this; @@ -19,8 +19,8 @@ export class GlobalObjectPool { /** * Used by Reflective Constructors to re-generate objects from the API at runtime */ - public static get(key: string): (typeof CoreObject) | null { - const obj: typeof CoreObject | undefined = this._globalMap.get(key); + public static get(key: string): (typeof CoreObject) | null { + const obj: typeof CoreObject | undefined = this._globalMap.get(key); return obj ? obj : null; } @@ -29,7 +29,7 @@ export class GlobalObjectPool { * Generates a new instance of the object provided a key, otherwise returns null */ public static newInstance>(key: string): T | null { - const obj: typeof CoreObject | null = this.get(key); + const obj: typeof CoreObject | null = this.get(key); return obj ? obj.newInstance() : null; } diff --git a/sdk-core/src/core/query/core-query.ts b/sdk-core/src/core/query/core-query.ts index 957665b..3e688a7 100644 --- a/sdk-core/src/core/query/core-query.ts +++ b/sdk-core/src/core/query/core-query.ts @@ -4,6 +4,7 @@ import { QueryContainsOperator, ContainsQuery } from './queries/contains-query'; import { DeletedQuery } from './queries/deleted-query'; import { FieldsQuery } from './queries/fields-query'; import { FilterQuery, FilterQueryOperator } from './queries/filter-query'; +import { IncludeQuery } from './queries/include-query'; import { PaginationQuery } from './queries/pagination-query'; import { Query } from './queries/query'; import { SearchQuery, SearchQueryOperator } from './queries/search-query'; @@ -57,19 +58,29 @@ export abstract class CoreQuery, U extends CoreObjectAtt return this; } - public include(...objects: Array): this { + public include(...objects: Array<(typeof CoreObject) | string>): this { + const data: Array = objects.map((object: typeof CoreObject | string) => { + if (typeof object === 'string' || object instanceof String) { + return object; + } + + return `${this.instance.type}.${object.type}`; + }); + + this._queries.push(new IncludeQuery(data)); + return this; } - public contains(operation: QueryContainsOperator = '==', ...objects: Array): this { - const data: Array = objects.map((object: typeof CoreObject) => object.type); + public contains(operation: QueryContainsOperator = '==', ...objects: Array>): this { + const data: Array = objects.map((object: typeof CoreObject) => object.type); this._queries.push(new ContainsQuery(operation, data)); return this; } - public deleted(...objects: Array): this { - const data: Array = objects.map((object: typeof CoreObject) => object.type); + public deleted(...objects: Array>): this { + const data: Array = objects.map((object: typeof CoreObject) => object.type); this._queries.push(new DeletedQuery(data)); return this; diff --git a/sdk-core/src/core/query/queries/include-query.ts b/sdk-core/src/core/query/queries/include-query.ts index b987e0b..1a3b3e6 100644 --- a/sdk-core/src/core/query/queries/include-query.ts +++ b/sdk-core/src/core/query/queries/include-query.ts @@ -1,7 +1,26 @@ import { Query } from "./query"; export class IncludeQuery extends Query { + private readonly objects: Array; + + public constructor(objects: Array) { + super(); + + this.objects = objects ? objects : []; + } + public override toString(): string { - throw new Error("Method not implemented."); + let retData: string = ','; + + if (this.objects.length > 0) { + retData = `include=`; + + this.objects.forEach((object: string) => { + retData += `${object},`; + }); + } + + // get rid of the last element + return retData.slice(0, -1); } } \ No newline at end of file diff --git a/sdk-core/src/generator/generators/schema.ts b/sdk-core/src/generator/generators/schema.ts index 3f73608..7ced7fc 100644 --- a/sdk-core/src/generator/generators/schema.ts +++ b/sdk-core/src/generator/generators/schema.ts @@ -76,7 +76,7 @@ export class Schema { output += `\t\treturn new ${queryName}Dynamic(this, service);\n`; output += '\t}\n'; - output += `}\nGlobalObjectPool.register(${className});\n`; + output += `}\nGlobalObjectPool.register(${className});\n`; return { name: schemaInstance.type.replaceAll('_', '-'),