Skip to content

Commit e71fe4e

Browse files
committed
Models and Collections from api-config
1 parent e7ece2a commit e71fe4e

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

projects/angular-odata/src/lib/api.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export class ODataApi {
6262
parsers: Map<string, Parser<any>>;
6363
// Schemas
6464
schemas: ODataSchema[];
65+
models: { [type: string]: typeof ODataModel<any> } = {};
66+
collections: { [type: string]: typeof ODataCollection<any, ODataModel<any>> } = {};
6567

6668
constructor(config: ODataApiConfig) {
6769
this.serviceRootUrl = config.serviceRootUrl;
@@ -85,6 +87,8 @@ export class ODataApi {
8587
this.parsers = new Map(Object.entries(config.parsers ?? EDM_PARSERS));
8688

8789
this.schemas = (config.schemas ?? []).map((schema) => new ODataSchema(schema, this));
90+
this.models = (config.models ?? {}) as {[type: string]: typeof ODataModel<any>};
91+
this.collections = (config.collections ?? {}) as {[type: string]: typeof ODataCollection<any, ODataModel<any>>};
8892
}
8993

9094
configure(
@@ -458,11 +462,11 @@ export class ODataApi {
458462
}
459463
//#endregion
460464

461-
public findModel(type: string) {
462-
return this.findStructuredType<any>(type)?.model;
465+
public findModel<T>(type: string) {
466+
return (this.models[type] ?? this.findStructuredType<any>(type)?.model) as typeof ODataModel<T> | undefined;
463467
}
464468

465-
public createModel(structured: ODataStructuredType<any>) {
469+
public createModel<T>(structured: ODataStructuredType<T>) {
466470
if (structured.model !== undefined) return structured.model;
467471
// Build Ad-hoc model
468472
const Model = class extends ODataModel<any> {} as typeof ODataModel;
@@ -478,40 +482,40 @@ export class ODataApi {
478482
}
479483
// Store New Model for next time
480484
structured.model = Model;
481-
return Model;
485+
return Model as typeof ODataModel<T>;
482486
}
483487

484-
public modelForType(type: string) {
485-
let Model = this.findModel(type);
488+
public modelForType<T>(type: string) {
489+
let Model = this.findModel<T>(type);
486490
if (Model === undefined) {
487-
const structured = this.findStructuredType<any>(type);
491+
const structured = this.findStructuredType<T>(type);
488492
if (structured === undefined) throw Error(`No structured type for ${type}`);
489-
Model = this.createModel(structured);
493+
Model = this.createModel<T>(structured);
490494
}
491495
return Model;
492496
}
493497

494-
public findCollection(type: string) {
495-
return this.findStructuredType<any>(type)?.collection;
498+
public findCollection<T>(type: string) {
499+
return (this.collections[type] ?? this.findStructuredType<any>(type)?.collection) as typeof ODataCollection<T, ODataModel<T>> | undefined;
496500
}
497501

498-
public createCollection(structured: ODataStructuredType<any>, model?: typeof ODataModel<any>) {
502+
public createCollection<T>(structured: ODataStructuredType<T>, model?: typeof ODataModel<T>) {
499503
if (structured.collection !== undefined) return structured.collection;
500504
if (model === undefined) model = this.createModel(structured);
501-
const Collection = class extends ODataCollection<any, ODataModel<any>> {
505+
const Collection = class extends ODataCollection<T, ODataModel<T>> {
502506
static override model = model!;
503507
} as typeof ODataCollection;
504508
structured.collection = Collection;
505-
return Collection;
509+
return Collection as typeof ODataCollection<T, ODataModel<T>>;
506510
}
507511

508-
public collectionForType(type: string) {
509-
let Collection = this.findCollection(type);
512+
public collectionForType<T>(type: string) {
513+
let Collection = this.findCollection<T>(type);
510514
if (Collection === undefined) {
511-
const structured = this.findStructuredType<any>(type);
515+
const structured = this.findStructuredType<T>(type);
512516
if (structured === undefined) throw Error(`No structured type for ${type}`);
513-
const Model = this.modelForType(type);
514-
Collection = this.createCollection(structured, Model);
517+
const Model = this.modelForType<T>(type);
518+
Collection = this.createCollection<T>(structured, Model) as typeof ODataCollection<T, ODataModel<T>>;
515519
}
516520
return Collection;
517521
}

projects/angular-odata/src/lib/models/collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import { ODataEntitiesAnnotations, ODataEntityAnnotations } from '../annotations';
3131

3232
export class ODataCollection<T, M extends ODataModel<T>> implements Iterable<M> {
33-
static model: typeof ODataModel | null = null;
33+
static model: typeof ODataModel<any> | null = null;
3434
_parent:
3535
| [ODataModel<any> | ODataCollection<any, ODataModel<any>>, ODataModelField<any> | null]
3636
| null = null;

projects/angular-odata/src/lib/resources/resource.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class ODataResource<T> {
127127
}: {
128128
reset?: boolean;
129129
annots?: ODataEntityAnnotations<T>;
130-
ModelType?: typeof ODataModel;
130+
ModelType?: typeof ODataModel<T>;
131131
},
132132
): M;
133133
asModel(
@@ -139,14 +139,14 @@ export class ODataResource<T> {
139139
}: {
140140
reset?: boolean;
141141
annots?: ODataEntityAnnotations<T>;
142-
ModelType?: typeof ODataModel;
142+
ModelType?: typeof ODataModel<any>;
143143
} = {},
144144
) {
145145
reset ??= annots !== undefined;
146146
let resource: ODataResource<T> = this as ODataResource<T>;
147147
const type = annots?.type ?? this.incomingType();
148148
if (type === undefined) throw Error(`No type for model`);
149-
if (ModelType === undefined) ModelType = this.api.modelForType(type);
149+
if (ModelType === undefined) ModelType = this.api.modelForType<T>(type);
150150
let entitySet = annots?.entitySet;
151151
if (entitySet !== undefined) {
152152
resource = this.api.entitySet<T>(entitySet).entity(entity as Partial<T>);
@@ -182,7 +182,7 @@ export class ODataResource<T> {
182182
}: {
183183
reset?: boolean;
184184
annots?: ODataEntitiesAnnotations<T>;
185-
CollectionType?: typeof ODataCollection;
185+
CollectionType?: typeof ODataCollection<T, M>;
186186
},
187187
): C;
188188
asCollection(
@@ -194,14 +194,14 @@ export class ODataResource<T> {
194194
}: {
195195
reset?: boolean;
196196
annots?: ODataEntitiesAnnotations<T>;
197-
CollectionType?: typeof ODataCollection;
197+
CollectionType?: typeof ODataCollection<any, ODataModel<any>>;
198198
} = {},
199199
) {
200200
reset ??= annots !== undefined;
201201
let resource: ODataResource<T> = this as ODataResource<T>;
202202
const type = annots?.type ?? this.incomingType();
203203
if (type === undefined) throw Error(`No type for collection`);
204-
if (CollectionType === undefined) CollectionType = this.api.collectionForType(type);
204+
if (CollectionType === undefined) CollectionType = this.api.collectionForType<T>(type);
205205
let entitySet = annots?.entitySet;
206206
if (entitySet !== undefined) {
207207
resource = this.api.entitySet<T>(entitySet);

projects/angular-odata/src/lib/schema/structured-type.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ export class ODataStructuredType<T> extends ODataParserSchemaElement<
1717
base?: string;
1818
parent?: ODataStructuredType<any>;
1919
children: ODataStructuredType<any>[] = [];
20-
model?: typeof ODataModel;
21-
collection?: typeof ODataCollection;
20+
model?: typeof ODataModel<any>;
21+
collection?: typeof ODataCollection<any, ODataModel<any>>;
2222

2323
constructor(config: ODataStructuredTypeConfig, schema: ODataSchema) {
2424
super(config, schema, new ODataStructuredTypeParser(config, schema.namespace, schema.alias));
2525
this.base = config.base;
26-
this.model = config.model as typeof ODataModel;
27-
this.collection = config.collection as typeof ODataCollection;
26+
this.model = config.model as typeof ODataModel<any>;
27+
this.collection = config.collection as typeof ODataCollection<any, ODataModel<any>>;
2828
}
2929

3030
configure({ options }: { options: ParserOptions }) {

projects/angular-odata/src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ export type ODataApiConfig = {
219219
parsers?: { [type: string]: Parser<any> };
220220
schemas?: ODataSchemaConfig[];
221221
references?: ODataReferenceConfig[];
222+
models?: {[type: string]: { new (...params: any[]): any }};
223+
collections?: {[type: string]: { new (...params: any[]): any }};
222224
};
223225
export type ODataAnnotationConfig = {
224226
term: string;

0 commit comments

Comments
 (0)