Skip to content

Commit

Permalink
- Added further generics to certain methods and functions to ensure t…
Browse files Browse the repository at this point in the history
…ype compatibility
  • Loading branch information
DavidArayan committed Nov 22, 2023
1 parent a0e1c79 commit 4b13699
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
13 changes: 13 additions & 0 deletions sdk-core/src/core/core-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ export abstract class CoreObject<Attributes extends CoreObjectAttributes> {
return (<any>this.constructor).type;
}

/**
* shortcut for easier chained construction of Include Queries
*/
public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | string>): Array<string> {
return objects.map<string>((object: typeof CoreObject<CoreObjectAttributes> | string) => {
if (typeof object === 'string' || object instanceof String) {
return <string>object;
}

return `${this.type}.${object.type}`;
});
}

/**
* Re-fills tis object instance with data from the api
*/
Expand Down
10 changes: 5 additions & 5 deletions sdk-core/src/core/global-object-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { CoreObjectAttributes, type CoreObject } from './core-object';
* from the API
*/
export class GlobalObjectPool {
private static readonly _globalMap: Map<string, typeof CoreObject> = new Map<string, typeof CoreObject>();
private static readonly _globalMap: Map<string, typeof CoreObject<CoreObjectAttributes>> = new Map<string, typeof CoreObject<CoreObjectAttributes>>();

/**
* 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<CoreObjectAttributes>): GlobalObjectPool {
this._globalMap.set(objectInstance.type, objectInstance);

return this;
Expand All @@ -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<CoreObjectAttributes>) | null {
const obj: typeof CoreObject<CoreObjectAttributes> | undefined = this._globalMap.get(key);

return obj ? obj : null;
}
Expand All @@ -29,7 +29,7 @@ export class GlobalObjectPool {
* Generates a new instance of the object provided a key, otherwise returns null
*/
public static newInstance<T extends CoreObject<CoreObjectAttributes>>(key: string): T | null {
const obj: typeof CoreObject | null = this.get(key);
const obj: typeof CoreObject<CoreObjectAttributes> | null = this.get(key);

return obj ? obj.newInstance<T>() : null;
}
Expand Down
21 changes: 16 additions & 5 deletions sdk-core/src/core/query/core-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -57,19 +58,29 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
return this;
}

public include(...objects: Array<typeof CoreObject>): this {
public include(...objects: Array<(typeof CoreObject<CoreObjectAttributes>) | string>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject<CoreObjectAttributes> | string) => {
if (typeof object === 'string' || object instanceof String) {
return <string>object;
}

return `${this.instance.type}.${object.type}`;
});

this._queries.push(new IncludeQuery(data));

return this;
}

public contains(operation: QueryContainsOperator = '==', ...objects: Array<typeof CoreObject>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject) => object.type);
public contains(operation: QueryContainsOperator = '==', ...objects: Array<typeof CoreObject<CoreObjectAttributes>>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject<CoreObjectAttributes>) => object.type);
this._queries.push(new ContainsQuery(operation, data));

return this;
}

public deleted(...objects: Array<typeof CoreObject>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject) => object.type);
public deleted(...objects: Array<typeof CoreObject<CoreObjectAttributes>>): this {
const data: Array<string> = objects.map<string>((object: typeof CoreObject<CoreObjectAttributes>) => object.type);
this._queries.push(new DeletedQuery(data));

return this;
Expand Down
21 changes: 20 additions & 1 deletion sdk-core/src/core/query/queries/include-query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import { Query } from "./query";

export class IncludeQuery extends Query {
private readonly objects: Array<string>;

public constructor(objects: Array<string>) {
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);
}
}
2 changes: 1 addition & 1 deletion sdk-core/src/generator/generators/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Schema {
output += `\t\treturn new ${queryName}Dynamic(this, service);\n`;
output += '\t}\n';

output += `}\nGlobalObjectPool.register(<any>${className});\n`;
output += `}\nGlobalObjectPool.register(${className});\n`;

return {
name: schemaInstance.type.replaceAll('_', '-'),
Expand Down

0 comments on commit 4b13699

Please sign in to comment.