1 | import { AnyObject, DataObject, Options, PrototypeOf } from './common-types';
|
2 | import { BelongsToDefinition, HasManyDefinition, HasOneDefinition, JsonSchema, ReferencesManyDefinition, RelationMetadata } from './index';
|
3 | import { TypeResolver } from './type-resolver';
|
4 | import { Type } from './types';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export interface JsonSchemaWithExtensions extends JsonSchema {
|
11 | [attributes: string]: any;
|
12 | }
|
13 | export type PropertyType = string | Function | object | Type<any> | TypeResolver<Model>;
|
14 |
|
15 |
|
16 |
|
17 | export interface PropertyDefinition {
|
18 | type: PropertyType;
|
19 | id?: boolean | number;
|
20 | |
21 |
|
22 |
|
23 |
|
24 | hidden?: boolean;
|
25 | json?: PropertyForm;
|
26 | jsonSchema?: JsonSchemaWithExtensions;
|
27 | store?: PropertyForm;
|
28 | itemType?: PropertyType;
|
29 | [attribute: string]: any;
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | export interface ModelSettings {
|
36 | |
37 |
|
38 |
|
39 | description?: string;
|
40 | |
41 |
|
42 |
|
43 | forceId?: boolean;
|
44 | |
45 |
|
46 |
|
47 | hiddenProperties?: string[];
|
48 | |
49 |
|
50 |
|
51 | scope?: object;
|
52 | |
53 |
|
54 |
|
55 | strict?: boolean | 'filter';
|
56 | [name: string]: any;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | export interface PropertyForm {
|
62 | in?: boolean;
|
63 | out?: boolean;
|
64 | name?: string;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | export type RelationDefinitionMap = {
|
71 | [relationName: string]: RelationMetadata;
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 | export interface ModelDefinitionSyntax {
|
77 | name: string;
|
78 | properties?: {
|
79 | [name: string]: PropertyDefinition | PropertyType;
|
80 | };
|
81 | settings?: ModelSettings;
|
82 | relations?: RelationDefinitionMap;
|
83 | jsonSchema?: JsonSchemaWithExtensions;
|
84 | [attribute: string]: any;
|
85 | }
|
86 |
|
87 |
|
88 |
|
89 | export declare class ModelDefinition {
|
90 | readonly name: string;
|
91 | properties: {
|
92 | [name: string]: PropertyDefinition;
|
93 | };
|
94 | settings: ModelSettings;
|
95 | relations: RelationDefinitionMap;
|
96 | [attribute: string]: any;
|
97 | constructor(nameOrDef: string | ModelDefinitionSyntax);
|
98 | /**
|
99 | * Add a property
|
100 | * @param name - Property definition or name (string)
|
101 | * @param definitionOrType - Definition or property type
|
102 | */
|
103 | addProperty(name: string, definitionOrType: PropertyDefinition | PropertyType): this;
|
104 | /**
|
105 | * Add a setting
|
106 | * @param name - Setting name
|
107 | * @param value - Setting value
|
108 | */
|
109 | addSetting(name: string, value: any): this;
|
110 | /**
|
111 | * Define a new relation.
|
112 | * @param definition - The definition of the new relation.
|
113 | */
|
114 | addRelation(definition: RelationMetadata): this;
|
115 | /**
|
116 | * Define a new belongsTo relation.
|
117 | * @param name - The name of the belongsTo relation.
|
118 | * @param definition - The definition of the belongsTo relation.
|
119 | */
|
120 | belongsTo(name: string, definition: Omit<BelongsToDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
121 | /**
|
122 | * Define a new hasOne relation.
|
123 | * @param name - The name of the hasOne relation.
|
124 | * @param definition - The definition of the hasOne relation.
|
125 | */
|
126 | hasOne(name: string, definition: Omit<HasOneDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
127 | /**
|
128 | * Define a new hasMany relation.
|
129 | * @param name - The name of the hasMany relation.
|
130 | * @param definition - The definition of the hasMany relation.
|
131 | */
|
132 | hasMany(name: string, definition: Omit<HasManyDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
133 | /**
|
134 | * Define a new referencesMany relation.
|
135 | * @param name - The name of the referencesMany relation.
|
136 | * @param definition - The definition of the referencesMany relation.
|
137 | */
|
138 | referencesMany(name: string, definition: Omit<ReferencesManyDefinition, 'name' | 'type' | 'targetsMany'>): this;
|
139 | /**
|
140 | * Get an array of names of ID properties, which are specified in
|
141 | * the model settings or properties with `id` attribute.
|
142 | *
|
143 | * @example
|
144 | * ```ts
|
145 | * {
|
146 | * settings: {
|
147 | * id: ['id']
|
148 | * }
|
149 | * properties: {
|
150 | * id: {
|
151 | * type: 'string',
|
152 | * id: true
|
153 | * }
|
154 | * }
|
155 | * }
|
156 | * ```
|
157 | */
|
158 | idProperties(): string[];
|
159 | }
|
160 | /**
|
161 | * Base class for models
|
162 | */
|
163 | export declare class Model {
|
164 | static get modelName(): string;
|
165 | static definition: ModelDefinition;
|
166 | /**
|
167 | * Serialize into a plain JSON object
|
168 | */
|
169 | toJSON(): Object;
|
170 | /**
|
171 | * Convert to a plain object as DTO
|
172 | *
|
173 | * If `ignoreUnknownProperty` is set to false, convert all properties in the
|
174 | * model instance, otherwise only convert the ones defined in the model
|
175 | * definitions.
|
176 | *
|
177 | * See function `asObject` for each property's conversion rules.
|
178 | */
|
179 | toObject(options?: Options): Object;
|
180 | constructor(data?: DataObject<Model>);
|
181 | }
|
182 | export interface Persistable {
|
183 | }
|
184 | /**
|
185 | * Base class for value objects - An object that contains attributes but has no
|
186 | * conceptual identity. They should be treated as immutable.
|
187 | */
|
188 | export declare abstract class ValueObject extends Model implements Persistable {
|
189 | }
|
190 | /**
|
191 | * Base class for entities which have unique ids
|
192 | */
|
193 | export declare class Entity extends Model implements Persistable {
|
194 | /**
|
195 | * Get the names of identity properties (primary keys).
|
196 | */
|
197 | static getIdProperties(): string[];
|
198 | /**
|
199 | * Get the identity value for a given entity instance or entity data object.
|
200 | *
|
201 | * @param entityOrData - The data object for which to determine the identity
|
202 | * value.
|
203 | */
|
204 | static getIdOf(entityOrData: AnyObject): any;
|
205 | /**
|
206 | * Get the identity value. If the identity is a composite key, returns
|
207 | * an object.
|
208 | */
|
209 | getId(): any;
|
210 | /**
|
211 | * Get the identity as an object, such as `{id: 1}` or
|
212 | * `{schoolId: 1, studentId: 2}`
|
213 | */
|
214 | getIdObject(): Object;
|
215 | /**
|
216 | * Build the where object for the given id
|
217 | * @param id - The id value
|
218 | */
|
219 | static buildWhereForId(id: any): any;
|
220 | }
|
221 | /**
|
222 | * Domain events
|
223 | */
|
224 | export declare class Event {
|
225 | source: any;
|
226 | type: string;
|
227 | }
|
228 | export type EntityData = DataObject<Entity>;
|
229 | export type EntityResolver<T extends Entity> = TypeResolver<T, typeof Entity>;
|
230 | /**
|
231 | * Check model data for navigational properties linking to related models.
|
232 | * Throw a descriptive error if any such property is found.
|
233 | *
|
234 | * @param modelClass Model constructor, e.g. `Product`.
|
235 | * @param entityData Model instance or a plain-data object,
|
236 | * e.g. `{name: 'pen'}`.
|
237 | */
|
238 | export declare function rejectNavigationalPropertiesInData<M extends typeof Entity>(modelClass: M, data: DataObject<PrototypeOf<M>>): void;
|
239 |
|
\ | No newline at end of file |