Skip to content

EspoCRM – Open Source CRM Application

TS migration

Yurii3dac8c256e51
diff --git a/client/src/collection-factory.js b/client/src/collection-factory.ts
similarity index 68%
rename from client/src/collection-factory.js
rename to client/src/collection-factory.ts
index ed8536457e..bb7da7e498 100644
--- a/client/src/collection-factory.js
+++ b/client/src/collection-factory.ts
@@ -28,57 +28,58 @@
/** @module collection-factory */
+import ModelFactory from 'model-factory';
+import Settings from 'models/settings';
+import Metadata from 'metadata';
+import Collection from 'collection';
+
/**
* A collection factory.
*/
-class CollectionFactory {
+export default class CollectionFactory {
+
+ private readonly recordListMaxSizeLimit: number
+
/**
* @param {module:model-factory} modelFactory
* @param {module:models/settings} config
* @param {module:metadata} metadata
*/
- constructor(modelFactory, config, metadata) {
- /** @private */
- this.modelFactory = modelFactory;
- /** @private */
- this.metadata = metadata;
- /** @private */
+ constructor(
+ private modelFactory: ModelFactory,
+ config: Settings,
+ private metadata: Metadata,
+ ) {
this.recordListMaxSizeLimit = config.get('recordListMaxSizeLimit') || 200;
}
/**
* Create a collection.
*
- * @param {string} entityType An entity type.
- * @param {Function} [callback] Deprecated.
- * @param {Object} [context] Deprecated.
- * @returns {Promise<module:collection>}
+ * @param entityType An entity type.
+ * @param [callback] Deprecated.
+ * @param [context] Deprecated.
+ * @returns A created collection.
*/
- create(entityType, callback, context) {
+ create(entityType: string, callback?: Function, context?: object): Promise<Collection> {
return new Promise(resolve => {
context = context || this;
this.modelFactory.getSeed(entityType, Model => {
- const orderBy = this.modelFactory.metadata
- .get(['entityDefs', entityType, 'collection', 'orderBy']);
-
- const order = this.modelFactory.metadata
- .get(['entityDefs', entityType, 'collection', 'order']);
-
- const className = this.modelFactory.metadata
- .get(['clientDefs', entityType, 'collection']) || 'collection';
-
+ const orderBy = this.metadata.get(['entityDefs', entityType, 'collection', 'orderBy']);
+ const order = this.metadata.get(['entityDefs', entityType, 'collection', 'order']);
+ const className = this.metadata .get(['clientDefs', entityType, 'collection']) || 'collection';
const defs = this.metadata.get(['entityDefs', entityType]) || {};
- Espo.loader.require(className, Collection => {
- const collection = new Collection(null, {
+ Espo.loader.require(className, (collectionClass: typeof Collection) => {
+ const collection = new collectionClass(null, {
entityType: entityType,
orderBy: orderBy,
order: order,
defs: defs,
+ model: Model,
});
- collection.model = Model;
collection.entityType = entityType;
collection.maxMaxSize = this.recordListMaxSizeLimit;
@@ -92,5 +93,3 @@ class CollectionFactory {
});
}
}
-
-export default CollectionFactory;
diff --git a/client/src/collection.ts b/client/src/collection.ts
index 91f83678c6..b1282ceaf5 100644
--- a/client/src/collection.ts
+++ b/client/src/collection.ts
@@ -90,6 +90,20 @@ export interface Data {
q?: string,
}
+/**
+ * @property model A class.
+ */
+interface Options {
+ model?: typeof Model
+ defs: import('model').Defs
+ maxSize?: number
+ entityType?: string
+ urlRoot?: string
+ url?: string
+ orderBy: string | null
+ order: 'asc' | 'desc' | boolean
+}
+
export default class Collection<TModel extends Model = Model> {
/**
@@ -222,16 +236,7 @@ export default class Collection<TModel extends Model = Model> {
*/
constructor(
models: TModel[] | Record<string, any>[] | null,
- options: {
- model?: typeof Model,
- defs: import('model').Defs,
- maxSize?: number,
- entityType?: string,
- urlRoot?: string,
- url?: string,
- orderBy: string | null,
- order: 'asc' | 'desc' | boolean,
- }
+ options: Options,
) {
options = {...options};
diff --git a/client/src/model-factory.js b/client/src/model-factory.ts
similarity index 74%
rename from client/src/model-factory.js
rename to client/src/model-factory.ts
index 9ac0cd2eec..6863147854 100644
--- a/client/src/model-factory.js
+++ b/client/src/model-factory.ts
@@ -28,26 +28,25 @@
/** @module model-factory */
+import Metadata from 'metadata';
+import Model from 'model';
+
/**
* A model factory.
*/
-class ModelFactory {
- /**
- * @param {module:metadata} metadata
- */
- constructor (metadata) {
- this.metadata = metadata;
- }
+export default class ModelFactory {
+
+ constructor(private metadata: Metadata) {}
/**
* Create a model.
*
- * @param {string} entityType An entity type.
- * @param {Function} [callback] Deprecated.
- * @param {Object} [context] Deprecated.
- * @returns {Promise<module:model>}
+ * @param entityType An entity type.
+ * @param [callback] Deprecated.
+ * @param [context] Deprecated.
+ * @returns A created model.
*/
- create(entityType, callback, context) {
+ create(entityType: string, callback?: Function, context?: object): Promise<Model> {
return new Promise(resolve => {
context = context || this;
@@ -70,14 +69,13 @@ class ModelFactory {
* Get a class.
*
* @param {string} entityType An entity type.
- * @param {function(module:model): void} callback A callback.
+ * @param callback A callback.
* @public
+ * @internal
*/
- getSeed(entityType, callback) {
- const className = this.metadata.get(['clientDefs', entityType, 'model']) || 'model';
+ getSeed(entityType: string, callback: (modelClass: typeof Model) => void) {
+ const className = (this.metadata.get(['clientDefs', entityType, 'model']) ?? 'model') as string;
- Espo.loader.require(className, modelClass => callback(modelClass));
+ Espo.loader.require(className, (modelClass: typeof Model) => callback(modelClass));
}
}
-
-export default ModelFactory;
diff --git a/client/src/model.ts b/client/src/model.ts
index 5a94ca4f3a..8e041531a1 100644
--- a/client/src/model.ts
+++ b/client/src/model.ts
@@ -75,6 +75,15 @@ interface FieldDefs {
type: string,
}
+interface Options {
+ collection?: Collection
+ entityType?: string
+ urlRoot?: string
+ url?: string
+ defs?: Defs
+ user?: User
+}
+
type Collection = import('collection').default;
type User = import('models/user').default;
@@ -147,14 +156,7 @@ export default class Model<T extends Record<string, unknown> = Record<string, an
constructor(
attributes?: Partial<T> | Model<T>,
- options?: {
- collection?: Collection;
- entityType?: string;
- urlRoot?: string;
- url?: string;
- defs?: Defs;
- user?: User;
- },
+ options?: Options,
) {
options = options || {};