UNPKG

7.48 kBTypeScriptView Raw
1import { AnyObject, DataObject, Options, PrototypeOf } from './common-types';
2import { BelongsToDefinition, HasManyDefinition, HasOneDefinition, JsonSchema, ReferencesManyDefinition, RelationMetadata } from './index';
3import { TypeResolver } from './type-resolver';
4import { Type } from './types';
5/**
6 * This module defines the key classes representing building blocks for Domain
7 * Driven Design.
8 * See https://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks
9 */
10export interface JsonSchemaWithExtensions extends JsonSchema {
11 [attributes: string]: any;
12}
13export declare type PropertyType = string | Function | object | Type<any> | TypeResolver<Model>;
14/**
15 * Property definition for a model
16 */
17export interface PropertyDefinition {
18 type: PropertyType;
19 id?: boolean | number;
20 /**
21 * Used to hide this property from the response body,
22 * adding this property to the hiddenProperties array
23 */
24 hidden?: boolean;
25 json?: PropertyForm;
26 jsonSchema?: JsonSchemaWithExtensions;
27 store?: PropertyForm;
28 itemType?: PropertyType;
29 [attribute: string]: any;
30}
31/**
32 * Defining the settings for a model
33 * See https://loopback.io/doc/en/lb4/Model.html#supported-entries-of-model-definition
34 */
35export interface ModelSettings {
36 /**
37 * Description of the model
38 */
39 description?: string;
40 /**
41 * Prevent clients from setting the auto-generated ID value manually
42 */
43 forceId?: boolean;
44 /**
45 * Hides properties from response bodies
46 */
47 hiddenProperties?: string[];
48 /**
49 * Scope enables you to set a scope that will apply to every query made by the model's repository
50 */
51 scope?: object;
52 /**
53 * Specifies whether the model accepts only predefined properties or not
54 */
55 strict?: boolean | 'filter';
56 [name: string]: any;
57}
58/**
59 * See https://github.com/loopbackio/loopback-datasource-juggler/issues/432
60 */
61export interface PropertyForm {
62 in?: boolean;
63 out?: boolean;
64 name?: string;
65}
66/**
67 * A key-value map describing model relations.
68 * A relation name is used as the key, a relation definition is the value.
69 */
70export declare type RelationDefinitionMap = {
71 [relationName: string]: RelationMetadata;
72};
73/**
74 * DSL for building a model definition.
75 */
76export 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 * Definition for a model
88 */
89export 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 */
163export 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}
182export 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 */
188export declare abstract class ValueObject extends Model implements Persistable {
189}
190/**
191 * Base class for entities which have unique ids
192 */
193export 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 */
224export declare class Event {
225 source: any;
226 type: string;
227}
228export declare type EntityData = DataObject<Entity>;
229export declare 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 */
238export declare function rejectNavigationalPropertiesInData<M extends typeof Entity>(modelClass: M, data: DataObject<PrototypeOf<M>>): void;
239
\No newline at end of file