@webda/models 0.0.0-stage → 4.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +166 -0
- package/README.md +146 -2
- package/lib/index.d.ts +11 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/lib/mock.d.ts +72 -0
- package/lib/mock.d.ts.map +1 -0
- package/lib/mock.js +83 -0
- package/lib/mock.js.map +1 -0
- package/lib/model.model.d.ts +247 -0
- package/lib/model.model.d.ts.map +1 -0
- package/lib/model.model.js +277 -0
- package/lib/model.model.js.map +1 -0
- package/lib/relations.d.ts +705 -0
- package/lib/relations.d.ts.map +1 -0
- package/lib/relations.js +938 -0
- package/lib/relations.js.map +1 -0
- package/lib/repositories/abstract.d.ts +124 -0
- package/lib/repositories/abstract.d.ts.map +1 -0
- package/lib/repositories/abstract.js +249 -0
- package/lib/repositories/abstract.js.map +1 -0
- package/lib/repositories/conformance.d.ts +40 -0
- package/lib/repositories/conformance.d.ts.map +1 -0
- package/lib/repositories/conformance.js +38 -0
- package/lib/repositories/conformance.js.map +1 -0
- package/lib/repositories/event.d.ts +100 -0
- package/lib/repositories/event.d.ts.map +1 -0
- package/lib/repositories/event.js +226 -0
- package/lib/repositories/event.js.map +1 -0
- package/lib/repositories/hooks.d.ts +88 -0
- package/lib/repositories/hooks.d.ts.map +1 -0
- package/lib/repositories/hooks.js +105 -0
- package/lib/repositories/hooks.js.map +1 -0
- package/lib/repositories/memory.d.ts +208 -0
- package/lib/repositories/memory.d.ts.map +1 -0
- package/lib/repositories/memory.js +446 -0
- package/lib/repositories/memory.js.map +1 -0
- package/lib/repositories/repository.d.ts +238 -0
- package/lib/repositories/repository.d.ts.map +1 -0
- package/lib/repositories/repository.js +19 -0
- package/lib/repositories/repository.js.map +1 -0
- package/lib/storable.d.ts +200 -0
- package/lib/storable.d.ts.map +1 -0
- package/lib/storable.js +66 -0
- package/lib/storable.js.map +1 -0
- package/lib/types.d.ts +230 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +12 -0
- package/lib/types.js.map +1 -0
- package/package.json +60 -3
|
@@ -0,0 +1,705 @@
|
|
|
1
|
+
import { FilterAttributes, ArrayElement } from "@webda/tsc-esm";
|
|
2
|
+
import { PrimaryKeyType, Storable, PrimaryKey, AttributesArgument, PrimaryKeyAttributes, WEBDA_PRIMARY_KEY, ModelClass } from "./storable.js";
|
|
3
|
+
import type { JSONed, Helpers, PropertyPaths, PropertyPathType, NumericPropertyPaths } from "./types.js";
|
|
4
|
+
import type { Repository } from "./repositories/repository.js";
|
|
5
|
+
import type { QueryParameters, WebdaQLString } from "@webda/ql";
|
|
6
|
+
/** Symbol key holding the parent Storable of a relation. */
|
|
7
|
+
/** Symbol key holding the primary key value of a relation target. */
|
|
8
|
+
export declare const RelationKey: unique symbol;
|
|
9
|
+
/** Symbol key holding the repository used to resolve a relation. */
|
|
10
|
+
export declare const RelationRepository: unique symbol;
|
|
11
|
+
/** Symbol key identifying the role of a relation (Link, Related, Links, CustomLinks). */
|
|
12
|
+
export declare const RelationRole: unique symbol;
|
|
13
|
+
/** Symbol key holding custom data on a ModelRefCustom. */
|
|
14
|
+
export declare const RelationData: unique symbol;
|
|
15
|
+
/** Symbol key holding the model. */
|
|
16
|
+
export declare const RelationModel: unique symbol;
|
|
17
|
+
/** Symbol key holding the type of custom attributes on a ModelLinksArray. */
|
|
18
|
+
export declare const RelationAttributes: unique symbol;
|
|
19
|
+
/**
|
|
20
|
+
* Assign non symbol properties from source to target
|
|
21
|
+
* @param target - the destination object
|
|
22
|
+
* @param source - the source object
|
|
23
|
+
*/
|
|
24
|
+
export declare function assignNonSymbols(target: any, source: any): void;
|
|
25
|
+
/**
|
|
26
|
+
* A reference to a model
|
|
27
|
+
*/
|
|
28
|
+
export declare class ModelRef<T extends Storable> {
|
|
29
|
+
[RelationKey]?: PrimaryKeyType<T>;
|
|
30
|
+
[RelationRepository]?: Repository<ModelClass<T>>;
|
|
31
|
+
/**
|
|
32
|
+
* Create a new model reference
|
|
33
|
+
* @param key - Primary key of the referenced model
|
|
34
|
+
* @param repository - Repository to resolve the reference against
|
|
35
|
+
* @param parent - Parent storable that owns this reference
|
|
36
|
+
*/
|
|
37
|
+
constructor(key: PrimaryKeyType<T>, repository?: Repository<ModelClass<T>>, parent?: Storable);
|
|
38
|
+
/**
|
|
39
|
+
* Get the repository and ensure it's initialized
|
|
40
|
+
* @returns the repository instance
|
|
41
|
+
*/
|
|
42
|
+
protected getRepository(): Repository<ModelClass<T>>;
|
|
43
|
+
/**
|
|
44
|
+
* Get the primary key and ensure it's initialized
|
|
45
|
+
* @returns the primary key value
|
|
46
|
+
*/
|
|
47
|
+
protected getKey(): PrimaryKeyType<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Get the model
|
|
50
|
+
* @returns the primary key value
|
|
51
|
+
*/
|
|
52
|
+
getPrimaryKey(): PrimaryKeyType<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Serialize the reference to its primary key value
|
|
55
|
+
* @returns the primary key value
|
|
56
|
+
*/
|
|
57
|
+
toJSON(): PrimaryKeyType<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Set the attribute of the model
|
|
60
|
+
* @param attribute - the attribute path to set
|
|
61
|
+
* @param value - the new attribute value
|
|
62
|
+
* @returns a promise resolving when complete
|
|
63
|
+
*/
|
|
64
|
+
setAttribute<A extends PropertyPaths<T>>(attribute: A, value: JSONed<PropertyPathType<T, A>>): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Patch the model
|
|
67
|
+
* @param data - the partial data to patch
|
|
68
|
+
* @param conditionField - optional field for optimistic locking
|
|
69
|
+
* @param condition - expected value for the condition field
|
|
70
|
+
* @returns a promise resolving when complete
|
|
71
|
+
*/
|
|
72
|
+
patch<K extends keyof T, A extends PropertyPaths<T>>(data: Partial<Omit<T, PrimaryKeyAttributes<T>>>, conditionField?: A, condition?: PropertyPathType<T, A>): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Fully replace the model data (except primary key fields)
|
|
75
|
+
* @param data - Complete model data excluding primary key
|
|
76
|
+
* @param conditionField - Optional field for optimistic locking
|
|
77
|
+
* @param condition - Expected value for the condition field
|
|
78
|
+
* @returns a promise resolving when complete
|
|
79
|
+
*/
|
|
80
|
+
update<A extends PropertyPaths<T>>(data: Omit<Helpers<T>, PrimaryKeyAttributes<T>>, conditionField?: A, condition?: PropertyPathType<T, A>): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Get the model referenced by the key
|
|
83
|
+
* @returns the resolved model instance
|
|
84
|
+
*/
|
|
85
|
+
get(): Promise<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Delete the model
|
|
88
|
+
*
|
|
89
|
+
* If a condition is specified, it will be used to check if the model can be deleted
|
|
90
|
+
*
|
|
91
|
+
* @param conditionField - optional field for optimistic locking
|
|
92
|
+
* @param condition - expected value for the condition field
|
|
93
|
+
* @returns a promise resolving when complete
|
|
94
|
+
*/
|
|
95
|
+
delete<A extends PropertyPaths<T>>(conditionField?: A, condition?: PropertyPathType<T, A> | JSONed<PropertyPathType<T, A>>): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Check if the model exists in the repository
|
|
98
|
+
* @returns true if the model exists
|
|
99
|
+
*/
|
|
100
|
+
exists(): Promise<boolean>;
|
|
101
|
+
/**
|
|
102
|
+
* Increment the attributes of the model
|
|
103
|
+
* @param info The attributes to increment
|
|
104
|
+
* @param conditionField - optional field for optimistic locking
|
|
105
|
+
* @param condition - expected value for the condition field
|
|
106
|
+
* @returns a promise resolving when complete
|
|
107
|
+
*/
|
|
108
|
+
incrementAttributes(info: (NumericPropertyPaths<T> | {
|
|
109
|
+
property: NumericPropertyPaths<T>;
|
|
110
|
+
value?: number;
|
|
111
|
+
})[] | Record<NumericPropertyPaths<T>, number>, conditionField?: PropertyPaths<T>, condition?: PropertyPathType<T, PropertyPaths<T>>): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Add an item to a collection
|
|
114
|
+
* @param collection - the collection field name
|
|
115
|
+
* @param item - the item to add or update
|
|
116
|
+
* @param index - optional position to insert at
|
|
117
|
+
* @param itemWriteConditionField - optional field for item write condition
|
|
118
|
+
* @param itemWriteCondition - expected value for the item write condition
|
|
119
|
+
* @returns a promise resolving when complete
|
|
120
|
+
*/
|
|
121
|
+
upsertItemToCollection<K extends Extract<PropertyPaths<T, any[]>, keyof T>, L extends keyof ArrayElement<T[K]>>(collection: K, item: ArrayElement<T[K]> | JSONed<ArrayElement<T[K]>>, index?: number, itemWriteConditionField?: ArrayElement<T[K]> extends object ? L : never, itemWriteCondition?: ArrayElement<T[K]> extends object ? ArrayElement<T[K]>[L] : ArrayElement<T[K]> | null): Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* Remove an item from a collection
|
|
124
|
+
* @param collection - the collection field name
|
|
125
|
+
* @param index - the position to remove from
|
|
126
|
+
* @param itemWriteConditionField - optional field for item write condition
|
|
127
|
+
* @param itemWriteCondition - expected value for the item write condition
|
|
128
|
+
* @returns a promise resolving when complete
|
|
129
|
+
*/
|
|
130
|
+
deleteItemFromCollection<K extends Extract<PropertyPaths<T, any[]>, keyof T>, L extends keyof ArrayElement<T[K]>>(collection: K, index: number, itemWriteConditionField?: L, itemWriteCondition?: ArrayElement<T[K]>[L]): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* Remove an attribute from the model
|
|
133
|
+
* @param attribute - the attribute to remove
|
|
134
|
+
* @param conditionField - optional field for optimistic locking
|
|
135
|
+
* @param condition - expected value for the condition field
|
|
136
|
+
* @returns a promise resolving when complete
|
|
137
|
+
*/
|
|
138
|
+
removeAttribute<A extends PropertyPaths<T>>(attribute: Exclude<PropertyPaths<T>, PrimaryKeyAttributes<T>>, conditionField?: A, condition?: PropertyPathType<T, A>): Promise<void>;
|
|
139
|
+
/**
|
|
140
|
+
* Increment only one attribute of the model
|
|
141
|
+
* @param property - the numeric property to increment
|
|
142
|
+
* @param value - the increment amount
|
|
143
|
+
* @param conditionField - optional field for optimistic locking
|
|
144
|
+
* @param condition - expected value for the condition field
|
|
145
|
+
* @returns a promise resolving when complete
|
|
146
|
+
*/
|
|
147
|
+
incrementAttribute(property: NumericPropertyPaths<T>, value?: number, conditionField?: PropertyPaths<T>, condition?: PropertyPathType<T, PropertyPaths<T>>): Promise<void>;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Union of all relation property keys on a model (both ModelRelated and ModelLinker fields).
|
|
151
|
+
*/
|
|
152
|
+
export type ModelRelations<T extends object> = FilterAttributes<T, ModelRelated<any, any, any>> | FilterAttributes<T, ModelLinker>;
|
|
153
|
+
/**
|
|
154
|
+
* Model reference with create and upsert methods
|
|
155
|
+
*/
|
|
156
|
+
export declare class ModelRefWithCreate<T extends Storable> extends ModelRef<T> {
|
|
157
|
+
/**
|
|
158
|
+
* Upsert a model reference
|
|
159
|
+
* @param data - the model data excluding primary key
|
|
160
|
+
* @returns the upserted model instance
|
|
161
|
+
*/
|
|
162
|
+
upsert(data: Omit<JSONed<T>, T[typeof WEBDA_PRIMARY_KEY][number]>): Promise<T>;
|
|
163
|
+
/**
|
|
164
|
+
* Create a model reference
|
|
165
|
+
*
|
|
166
|
+
* It will fail if the model already exists
|
|
167
|
+
* @param data - the model data excluding primary key
|
|
168
|
+
* @returns the created model instance
|
|
169
|
+
*/
|
|
170
|
+
create(data: Omit<AttributesArgument<T>, T[typeof WEBDA_PRIMARY_KEY][number]>): Promise<T>;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Define a 1:n relation on the current model to another model
|
|
174
|
+
*
|
|
175
|
+
* T is the model to link to
|
|
176
|
+
* _K is the attribute in T to query
|
|
177
|
+
*
|
|
178
|
+
* _K is not used but is required to complete the graph
|
|
179
|
+
* _K define the attribute to use to load the related objects
|
|
180
|
+
*
|
|
181
|
+
* In SQL if current model is TableA and T is TableB, _K is the foreign key in TableB
|
|
182
|
+
*
|
|
183
|
+
* TODO Deduce attribute from the ModelParent on the other side when "" is used
|
|
184
|
+
*/
|
|
185
|
+
export declare class ModelRelated<T extends Storable, L extends Storable, K extends Extract<FilterAttributes<T, ModelLinker<L>>, string> | "" = ""> {
|
|
186
|
+
private object;
|
|
187
|
+
private attribute;
|
|
188
|
+
private repoSource;
|
|
189
|
+
/** Create a new ModelRelated
|
|
190
|
+
* @param targetClass - the target model class
|
|
191
|
+
* @param object - the source object owning the relation
|
|
192
|
+
* @param attribute - the attribute name linking the relation
|
|
193
|
+
*/
|
|
194
|
+
constructor(targetClass: ModelClass<T>, object: L, attribute?: K);
|
|
195
|
+
/**
|
|
196
|
+
* Build a WebdaQL query string that filters related objects by this model's primary key.
|
|
197
|
+
* @param query - optional additional query filter
|
|
198
|
+
* @returns the constructed query string
|
|
199
|
+
*/
|
|
200
|
+
getQuery(query?: WebdaQLString<T> | string): WebdaQLString<T>;
|
|
201
|
+
/**
|
|
202
|
+
* Query the related objects
|
|
203
|
+
* @param query - optional additional query filter, optionally with `?` or `:name` placeholders
|
|
204
|
+
* @param params - values for the placeholders: an array for `?`, an object for `:name`
|
|
205
|
+
* @returns the query results with optional continuation token
|
|
206
|
+
*/
|
|
207
|
+
query(query?: WebdaQLString<T>, params?: QueryParameters): Promise<{
|
|
208
|
+
results: T[];
|
|
209
|
+
continuationToken?: string;
|
|
210
|
+
}>;
|
|
211
|
+
/**
|
|
212
|
+
* Iterate through linked objects
|
|
213
|
+
* @param query - the query filter, optionally with `?` or `:name` placeholders
|
|
214
|
+
* @param params - values for the placeholders: an array for `?`, an object for `:name`
|
|
215
|
+
* @returns an async iterable of linked objects
|
|
216
|
+
*/
|
|
217
|
+
iterate(query: WebdaQLString<T>, params?: QueryParameters): AsyncIterable<T>;
|
|
218
|
+
/**
|
|
219
|
+
* It is a helper and should not be serialized
|
|
220
|
+
*/
|
|
221
|
+
toJSON(): void;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Marker interface for relation properties.
|
|
225
|
+
*
|
|
226
|
+
* All relation types (ModelLink, ModelLinksSimpleArray, ModelLinksArray) implement this
|
|
227
|
+
* so they can be detected and filtered at the type level.
|
|
228
|
+
*
|
|
229
|
+
* @typeParam T - The target Storable type
|
|
230
|
+
* @typeParam K - The relation role identifier
|
|
231
|
+
*/
|
|
232
|
+
export interface ModelLinker<T extends Storable = any, K extends "Link" | "Related" | "Links" | "CustomLinks" = any> {
|
|
233
|
+
readonly [RelationRole]: K;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Define a link to n:1 or 1:1 relation on the current model to another model
|
|
237
|
+
*
|
|
238
|
+
* T is the model to link to
|
|
239
|
+
*
|
|
240
|
+
* This is expected to be the symetric of ModelRelated or ModelLink in a 1:1 relation
|
|
241
|
+
*
|
|
242
|
+
* ```
|
|
243
|
+
* class Album extends Model {
|
|
244
|
+
* artist: ModelLink<Artist>;
|
|
245
|
+
* }
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
export declare class ModelLink<T extends Storable> implements ModelLinker {
|
|
249
|
+
readonly [RelationRole]: "Link";
|
|
250
|
+
protected [RelationKey]: PrimaryKeyType<T>;
|
|
251
|
+
readonly [RelationModel]: ModelClass<T>;
|
|
252
|
+
/** Create a new ModelLink
|
|
253
|
+
* @param model - the model class to link to
|
|
254
|
+
*/
|
|
255
|
+
constructor(
|
|
256
|
+
/**
|
|
257
|
+
* The repository of the object to link
|
|
258
|
+
*/
|
|
259
|
+
model: ModelClass<T>);
|
|
260
|
+
/**
|
|
261
|
+
* Resolve the link and fetch the target model from the repository
|
|
262
|
+
* @returns the linked model instance
|
|
263
|
+
*/
|
|
264
|
+
get(): Promise<T>;
|
|
265
|
+
/**
|
|
266
|
+
* Update the link to point to a different target
|
|
267
|
+
* @param id - New target: a primary key, model instance, or string UID
|
|
268
|
+
* @returns this link instance
|
|
269
|
+
* @WebdaAutoSetter
|
|
270
|
+
*/
|
|
271
|
+
set(id: PrimaryKeyType<T> | T | string): this;
|
|
272
|
+
/**
|
|
273
|
+
* Return the string representation of the linked key
|
|
274
|
+
* @returns the string representation
|
|
275
|
+
*/
|
|
276
|
+
toString(): string;
|
|
277
|
+
/**
|
|
278
|
+
* Serialize the link to its primary key value
|
|
279
|
+
* @returns the primary key value
|
|
280
|
+
*/
|
|
281
|
+
toJSON(): PrimaryKeyType<T>;
|
|
282
|
+
/**
|
|
283
|
+
* Get the primary key of the linked model
|
|
284
|
+
* @returns the primary key value
|
|
285
|
+
*/
|
|
286
|
+
getPrimaryKey(): PrimaryKeyType<T>;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Define the parent of the model
|
|
290
|
+
*
|
|
291
|
+
* Similar to @ModelLink but implies a Cascade delete
|
|
292
|
+
*/
|
|
293
|
+
export type ModelParent<T extends Storable> = ModelLink<T>;
|
|
294
|
+
/**
|
|
295
|
+
* Link to a collection of objects
|
|
296
|
+
*
|
|
297
|
+
* Uses composition instead of extending Array for better maintainability,
|
|
298
|
+
* performance, and type safety.
|
|
299
|
+
*
|
|
300
|
+
* This allows to do a n:m or 1:n relation between two models
|
|
301
|
+
*
|
|
302
|
+
* It does not require to have a symetric relation on the other side
|
|
303
|
+
* The array contains uuid of the linked objects
|
|
304
|
+
*/
|
|
305
|
+
export declare class ModelLinksSimpleArray<T extends Storable> implements ModelLinker {
|
|
306
|
+
[RelationRole]: "Links";
|
|
307
|
+
[RelationAttributes]: object;
|
|
308
|
+
protected [RelationRepository]: Repository<ModelClass<T>>;
|
|
309
|
+
private items;
|
|
310
|
+
/** Create a new ModelLinksSimpleArray
|
|
311
|
+
* @param model - the model class for linked items
|
|
312
|
+
* @param content - initial array of primary keys
|
|
313
|
+
* @param parentObject - optional parent object owning this relation
|
|
314
|
+
*/
|
|
315
|
+
constructor(model: ModelClass<T>, content?: PrimaryKeyType<T>[], parentObject?: T);
|
|
316
|
+
/**
|
|
317
|
+
* Add items to the end of the collection
|
|
318
|
+
* @param items - items to add
|
|
319
|
+
* @returns the new collection length
|
|
320
|
+
*/
|
|
321
|
+
push(...items: (string | PrimaryKeyType<T> | ModelRef<T> | T)[]): number;
|
|
322
|
+
/**
|
|
323
|
+
* Add items to the beginning of the collection
|
|
324
|
+
* @param items - items to prepend
|
|
325
|
+
* @returns the new collection length
|
|
326
|
+
*/
|
|
327
|
+
unshift(...items: (string | PrimaryKeyType<T> | ModelRef<T> | T)[]): number;
|
|
328
|
+
/**
|
|
329
|
+
* Remove and return the last item
|
|
330
|
+
* @returns the removed item, or undefined
|
|
331
|
+
*/
|
|
332
|
+
pop(): ModelRef<T> | undefined;
|
|
333
|
+
/**
|
|
334
|
+
* Remove and return the first item
|
|
335
|
+
* @returns the removed item, or undefined
|
|
336
|
+
*/
|
|
337
|
+
shift(): ModelRef<T> | undefined;
|
|
338
|
+
/**
|
|
339
|
+
* Remove/add items at specified index
|
|
340
|
+
* @param start - the start index
|
|
341
|
+
* @param deleteCount - number of items to remove
|
|
342
|
+
* @param rest - items to insert
|
|
343
|
+
* @returns the removed items
|
|
344
|
+
*/
|
|
345
|
+
splice(start: number, deleteCount?: number, ...rest: (string | PrimaryKeyType<T> | ModelRef<T> | T)[]): ModelRef<T>[];
|
|
346
|
+
/**
|
|
347
|
+
* Add a model to the collection
|
|
348
|
+
* @param model - the model to add
|
|
349
|
+
* @deprecated use push instead
|
|
350
|
+
*/
|
|
351
|
+
add(model: string | PrimaryKeyType<T> | ModelRef<T> | T): void;
|
|
352
|
+
/**
|
|
353
|
+
* Set the collection to a new set of items
|
|
354
|
+
* @param items - the new items to set
|
|
355
|
+
* @WebdaAutoSetter
|
|
356
|
+
*/
|
|
357
|
+
set(items?: (string | PrimaryKeyType<T> | ModelRef<T> | T)[]): void;
|
|
358
|
+
/**
|
|
359
|
+
* Remove item by reference or primary key
|
|
360
|
+
* @param model - the item to remove
|
|
361
|
+
* @returns true if the item was found and removed
|
|
362
|
+
*/
|
|
363
|
+
remove(model: string | ModelRef<T> | PrimaryKeyType<T> | T): boolean;
|
|
364
|
+
/**
|
|
365
|
+
* Get the number of items
|
|
366
|
+
* @returns the item count
|
|
367
|
+
*/
|
|
368
|
+
get length(): number;
|
|
369
|
+
/**
|
|
370
|
+
* Get item at index
|
|
371
|
+
* @param index - the array index
|
|
372
|
+
* @returns the item, or undefined
|
|
373
|
+
*/
|
|
374
|
+
at(index: number): ModelRef<T> | undefined;
|
|
375
|
+
/**
|
|
376
|
+
* Find item by predicate
|
|
377
|
+
* @param predicate - the search predicate
|
|
378
|
+
* @returns the found item, or undefined
|
|
379
|
+
*/
|
|
380
|
+
find(predicate: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => boolean): ModelRef<T> | undefined;
|
|
381
|
+
/**
|
|
382
|
+
* Find index by predicate
|
|
383
|
+
* @param predicate - the search predicate
|
|
384
|
+
* @returns the index, or -1
|
|
385
|
+
*/
|
|
386
|
+
findIndex(predicate: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => boolean): number;
|
|
387
|
+
/**
|
|
388
|
+
* Map over items (does NOT mutate)
|
|
389
|
+
* @param callback - the mapping function
|
|
390
|
+
* @returns the mapped array
|
|
391
|
+
*/
|
|
392
|
+
map<U>(callback: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => U): U[];
|
|
393
|
+
/**
|
|
394
|
+
* Filter items (does NOT mutate)
|
|
395
|
+
* @param predicate - the filter predicate
|
|
396
|
+
* @returns the filtered items
|
|
397
|
+
*/
|
|
398
|
+
filter(predicate: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => boolean): ModelRef<T>[];
|
|
399
|
+
/**
|
|
400
|
+
* Execute callback for each item
|
|
401
|
+
* @param callback - the function to execute for each item
|
|
402
|
+
*/
|
|
403
|
+
forEach(callback: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => void): void;
|
|
404
|
+
/**
|
|
405
|
+
* Check if any item matches predicate
|
|
406
|
+
* @param predicate - the test predicate
|
|
407
|
+
* @returns true if any item matches
|
|
408
|
+
*/
|
|
409
|
+
some(predicate: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => boolean): boolean;
|
|
410
|
+
/**
|
|
411
|
+
* Check if all items match predicate
|
|
412
|
+
* @param predicate - the test predicate
|
|
413
|
+
* @returns true if all items match
|
|
414
|
+
*/
|
|
415
|
+
every(predicate: (item: ModelRef<T>, index: number, array: ModelRef<T>[]) => boolean): boolean;
|
|
416
|
+
/**
|
|
417
|
+
* Convert to JSON
|
|
418
|
+
* @returns array of primary key values
|
|
419
|
+
*/
|
|
420
|
+
toJSON(): PrimaryKeyType<T>[];
|
|
421
|
+
/**
|
|
422
|
+
* Make the collection iterable (for...of loops)
|
|
423
|
+
* @returns an iterator over the items
|
|
424
|
+
*/
|
|
425
|
+
[Symbol.iterator](): Iterator<ModelRef<T>>;
|
|
426
|
+
/**
|
|
427
|
+
* Support indexed access like an array
|
|
428
|
+
*/
|
|
429
|
+
[index: number]: ModelRef<T>;
|
|
430
|
+
/**
|
|
431
|
+
* Convert any accepted input (string, key, ref, or model) into a ModelRef
|
|
432
|
+
* @param model - the input to convert
|
|
433
|
+
* @returns the model reference
|
|
434
|
+
*/
|
|
435
|
+
protected getModelRef(model: string | PrimaryKeyType<T> | ModelRef<T> | T): ModelRef<T>;
|
|
436
|
+
/**
|
|
437
|
+
* Mark the parent model's property for this collection as dirty
|
|
438
|
+
*/
|
|
439
|
+
protected setDirty(): void;
|
|
440
|
+
}
|
|
441
|
+
/** A ModelRef augmented with custom properties K, exposing them both at runtime and in JSON. */
|
|
442
|
+
type ModelRefCustomProperties<T extends Storable, K extends object> = ModelRef<T> & {
|
|
443
|
+
[key in keyof K]: K[key];
|
|
444
|
+
} & {
|
|
445
|
+
toJSON(): JSONed<K> & PrimaryKey<T>;
|
|
446
|
+
};
|
|
447
|
+
/**
|
|
448
|
+
* ModelRef with custom properties
|
|
449
|
+
*
|
|
450
|
+
* This is used to define a link to a collection of objects with additional data
|
|
451
|
+
*
|
|
452
|
+
* It does not require to have a symetric relation on the other side
|
|
453
|
+
* The array contains uuid of the linked objects and additional properties defined as K
|
|
454
|
+
*
|
|
455
|
+
* Sample usage:
|
|
456
|
+
* ```typescript
|
|
457
|
+
* class MyModel extends AbstractCoreModel {
|
|
458
|
+
* _links: ModelLinksArray<OtherModel, { custom: string }>;
|
|
459
|
+
* }
|
|
460
|
+
* ```
|
|
461
|
+
*/
|
|
462
|
+
declare class ModelRefCustom<T extends Storable, K> extends ModelRef<T> {
|
|
463
|
+
protected [RelationData]: K;
|
|
464
|
+
/** Create a new ModelRefCustom
|
|
465
|
+
* @param key - the primary key of the referenced object
|
|
466
|
+
* @param repo - the repository managing the referenced model
|
|
467
|
+
* @param data - custom data associated with this reference
|
|
468
|
+
* @param parent - optional parent storable
|
|
469
|
+
*/
|
|
470
|
+
constructor(key: PrimaryKeyType<T>, repo: Repository<ModelClass<T>>, data: K, parent?: Storable);
|
|
471
|
+
/**
|
|
472
|
+
* Override toJSON to include custom properties along with primary key
|
|
473
|
+
* @returns the relation data
|
|
474
|
+
*/
|
|
475
|
+
toJSON(): any;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Link to a collection of objects including some additional data
|
|
479
|
+
*
|
|
480
|
+
* Uses composition instead of extending Array for better maintainability,
|
|
481
|
+
* performance, and type safety. Provides array-like interface through
|
|
482
|
+
* explicit methods and iterators.
|
|
483
|
+
*
|
|
484
|
+
* It does not require to have a symetric relation on the other side
|
|
485
|
+
* The array contains uuid of the linked objects and additional properties defined as K
|
|
486
|
+
*
|
|
487
|
+
* Sample usage:
|
|
488
|
+
* ```typescript
|
|
489
|
+
* class MyModel extends AbstractCoreModel {
|
|
490
|
+
* _links: ModelLinksArray<OtherModel, { custom: string }>;
|
|
491
|
+
* }
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
494
|
+
export declare class ModelLinksArray<T extends Storable, K extends object> implements ModelLinker {
|
|
495
|
+
protected repo: Repository<ModelClass<T>>;
|
|
496
|
+
[RelationRole]: "CustomLinks";
|
|
497
|
+
[RelationAttributes]: K;
|
|
498
|
+
private items;
|
|
499
|
+
/** Create a new ModelLinksCustomArray
|
|
500
|
+
* @param repo - the repository for the linked model
|
|
501
|
+
* @param content - initial array of primary keys with custom data
|
|
502
|
+
*/
|
|
503
|
+
constructor(repo: Repository<ModelClass<T>>, content?: (PrimaryKey<T> & K)[]);
|
|
504
|
+
/**
|
|
505
|
+
* Add a model to the collection
|
|
506
|
+
* @param model - the model to add
|
|
507
|
+
* @deprecated use push instead
|
|
508
|
+
*/
|
|
509
|
+
add(model: JSONed<ModelRefCustomProperties<T, K>>): void;
|
|
510
|
+
/**
|
|
511
|
+
* Add items to the end of the collection
|
|
512
|
+
* @param items - items to add
|
|
513
|
+
* @returns the new collection length
|
|
514
|
+
*/
|
|
515
|
+
push(...items: (ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>)[]): number;
|
|
516
|
+
/**
|
|
517
|
+
* Remove and return the last item
|
|
518
|
+
* @returns the removed item, or undefined
|
|
519
|
+
*/
|
|
520
|
+
pop(): ModelRefCustomProperties<T, K> | undefined;
|
|
521
|
+
/**
|
|
522
|
+
* Remove and return the first item
|
|
523
|
+
* @returns the removed item, or undefined
|
|
524
|
+
*/
|
|
525
|
+
shift(): ModelRefCustomProperties<T, K> | undefined;
|
|
526
|
+
/**
|
|
527
|
+
* Add items to the beginning of the collection
|
|
528
|
+
* @param items - items to prepend
|
|
529
|
+
* @returns the new collection length
|
|
530
|
+
*/
|
|
531
|
+
unshift(...items: (ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>)[]): number;
|
|
532
|
+
/**
|
|
533
|
+
* Remove/add items at specified index
|
|
534
|
+
* @param start - the start index
|
|
535
|
+
* @param deleteCount - number of items to remove
|
|
536
|
+
* @param rest - items to insert
|
|
537
|
+
* @returns the removed items
|
|
538
|
+
*/
|
|
539
|
+
splice(start: number, deleteCount?: number, ...rest: (ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>)[]): ModelRefCustomProperties<T, K>[];
|
|
540
|
+
/**
|
|
541
|
+
* Remove item by reference or primary key
|
|
542
|
+
* @param model - the item to remove
|
|
543
|
+
* @returns true if found and removed
|
|
544
|
+
*/
|
|
545
|
+
remove(model: ModelRefCustomProperties<T, K> | PrimaryKeyType<T> | T): boolean;
|
|
546
|
+
/**
|
|
547
|
+
* Get the number of items
|
|
548
|
+
* @returns the item count
|
|
549
|
+
*/
|
|
550
|
+
get length(): number;
|
|
551
|
+
/**
|
|
552
|
+
* Get item at index
|
|
553
|
+
* @param index - the array index
|
|
554
|
+
* @returns the item, or undefined
|
|
555
|
+
*/
|
|
556
|
+
at(index: number): ModelRefCustomProperties<T, K> | undefined;
|
|
557
|
+
/**
|
|
558
|
+
* Find item by predicate
|
|
559
|
+
* @param predicate - the search predicate
|
|
560
|
+
* @returns the found item, or undefined
|
|
561
|
+
*/
|
|
562
|
+
find(predicate: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => boolean): ModelRefCustomProperties<T, K> | undefined;
|
|
563
|
+
/**
|
|
564
|
+
* Find index by predicate
|
|
565
|
+
* @param predicate - the search predicate
|
|
566
|
+
* @returns the index, or -1
|
|
567
|
+
*/
|
|
568
|
+
findIndex(predicate: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => boolean): number;
|
|
569
|
+
/**
|
|
570
|
+
* Check if item exists in collection
|
|
571
|
+
* @param search - the item to look for
|
|
572
|
+
* @param fromIndex - optional start index
|
|
573
|
+
* @returns true if found
|
|
574
|
+
*/
|
|
575
|
+
includes(search: ModelRefCustomProperties<T, K>, fromIndex?: number): boolean;
|
|
576
|
+
/**
|
|
577
|
+
* Map over items (does NOT mutate)
|
|
578
|
+
* @param callback - the mapping function
|
|
579
|
+
* @returns the mapped array
|
|
580
|
+
*/
|
|
581
|
+
map<U>(callback: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => U): U[];
|
|
582
|
+
/**
|
|
583
|
+
* Filter items (does NOT mutate)
|
|
584
|
+
* @param predicate - the filter predicate
|
|
585
|
+
* @returns the filtered items
|
|
586
|
+
*/
|
|
587
|
+
filter(predicate: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => boolean): ModelRefCustomProperties<T, K>[];
|
|
588
|
+
/**
|
|
589
|
+
* Execute callback for each item
|
|
590
|
+
* @param callback - the function to execute for each item
|
|
591
|
+
*/
|
|
592
|
+
forEach(callback: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => void): void;
|
|
593
|
+
/**
|
|
594
|
+
* Check if any item matches predicate
|
|
595
|
+
* @param predicate - the test predicate
|
|
596
|
+
* @returns true if any item matches
|
|
597
|
+
*/
|
|
598
|
+
some(predicate: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => boolean): boolean;
|
|
599
|
+
/**
|
|
600
|
+
* Check if all items match predicate
|
|
601
|
+
* @param predicate - the test predicate
|
|
602
|
+
* @returns true if all items match
|
|
603
|
+
*/
|
|
604
|
+
every(predicate: (item: ModelRefCustomProperties<T, K>, index: number, array: ModelRefCustomProperties<T, K>[]) => boolean): boolean;
|
|
605
|
+
/**
|
|
606
|
+
* Convert to JSON
|
|
607
|
+
* @returns array of serialized items
|
|
608
|
+
*/
|
|
609
|
+
toJSON(): JSONed<ModelRefCustomProperties<T, K>>[];
|
|
610
|
+
/**
|
|
611
|
+
* Make the collection iterable (for...of loops)
|
|
612
|
+
* @returns an iterator over the items
|
|
613
|
+
*/
|
|
614
|
+
[Symbol.iterator](): Iterator<ModelRefCustomProperties<T, K>>;
|
|
615
|
+
/**
|
|
616
|
+
* Support indexed access like an array
|
|
617
|
+
*/
|
|
618
|
+
[index: number]: ModelRefCustomProperties<T, K>;
|
|
619
|
+
/**
|
|
620
|
+
* Convert input into a ModelRefCustom, preserving custom properties
|
|
621
|
+
* @param model - the input to convert
|
|
622
|
+
* @returns the model reference with custom properties
|
|
623
|
+
*/
|
|
624
|
+
protected getModelRef(model: ModelRefCustomProperties<T, K> | JSONed<ModelRefCustomProperties<T, K>>): ModelRefCustomProperties<T, K>;
|
|
625
|
+
/**
|
|
626
|
+
* Mark the parent model's property for this collection as dirty
|
|
627
|
+
*/
|
|
628
|
+
protected setDirty(): void;
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Define a ModelRef with custom properties for map
|
|
632
|
+
* Exclude the PrimaryKey from the JSON
|
|
633
|
+
*
|
|
634
|
+
* @deprecated
|
|
635
|
+
*/
|
|
636
|
+
export declare class ModelRefCustomMap<T extends Storable, K> extends ModelRefCustom<T, K> {
|
|
637
|
+
/**
|
|
638
|
+
* Serialize the relation data as a plain object (excluding the primary key).
|
|
639
|
+
* @returns the relation data
|
|
640
|
+
*/
|
|
641
|
+
toJSON(): any;
|
|
642
|
+
}
|
|
643
|
+
/** Alias for a many-to-one relation (foreign key link). */
|
|
644
|
+
export type ManyToOne<T extends Storable> = ModelLink<T>;
|
|
645
|
+
/**
|
|
646
|
+
* Define a 1:n relation on the current model to another model
|
|
647
|
+
*
|
|
648
|
+
* This is an helper type that allows to query the related objects with the correct type
|
|
649
|
+
*
|
|
650
|
+
* T is the model to link to
|
|
651
|
+
* K is the attribute in T to query (not used but required to complete the graph)
|
|
652
|
+
*/
|
|
653
|
+
export type OneToMany<T extends Storable, L extends Storable, K extends Extract<FilterAttributes<T, ModelLinker<L>>, string> | ""> = ModelRelated<T, L, K>;
|
|
654
|
+
/** Alias for a one-to-one relation. */
|
|
655
|
+
export type OneToOne<T extends Storable> = ModelLink<T>;
|
|
656
|
+
/**
|
|
657
|
+
* Alias for a many-to-many relation.
|
|
658
|
+
* If K (custom properties) is provided, uses ModelLinksArray; otherwise ModelLinksSimpleArray.
|
|
659
|
+
*/
|
|
660
|
+
export type ManyToMany<T extends Storable, K extends object = object> = K extends object ? ModelLinksArray<T, K> : ModelLinksSimpleArray<T>;
|
|
661
|
+
/**
|
|
662
|
+
* Define the parent of the model
|
|
663
|
+
* Can only have one parent and implies a Cascade delete
|
|
664
|
+
*
|
|
665
|
+
* Alias for @ModelParent
|
|
666
|
+
* Similar to @ModelLink but implies a Cascade delete
|
|
667
|
+
*/
|
|
668
|
+
export type BelongTo<T extends Storable> = ModelParent<T>;
|
|
669
|
+
/** Alias for ModelLink - defines a relation to another model. */
|
|
670
|
+
export type RelateTo<T extends Storable> = ModelLink<T>;
|
|
671
|
+
/** Alias for ManyToMany - defines ownership of a collection of models. */
|
|
672
|
+
export type Contains<T extends Storable> = ManyToMany<T>;
|
|
673
|
+
/**
|
|
674
|
+
* Define a junction link between two models in a n:m relation
|
|
675
|
+
*
|
|
676
|
+
* This is used to define the junction model in a n:m relation with additional data
|
|
677
|
+
* It does not require to have a symetric relation on the other side
|
|
678
|
+
*/
|
|
679
|
+
export declare class JunctionLink<T extends Storable, K extends Storable> implements Storable {
|
|
680
|
+
[WEBDA_PRIMARY_KEY]: readonly ["linkA", "linkB"];
|
|
681
|
+
linkA: ModelParent<T>;
|
|
682
|
+
linkB: ModelLink<K>;
|
|
683
|
+
/**
|
|
684
|
+
* Return the composite primary key (linkA + linkB)
|
|
685
|
+
* @returns the composite primary key object
|
|
686
|
+
*/
|
|
687
|
+
getPrimaryKey(): {
|
|
688
|
+
linkA: PrimaryKeyType<T>;
|
|
689
|
+
linkB: PrimaryKeyType<K>;
|
|
690
|
+
};
|
|
691
|
+
/** @override */
|
|
692
|
+
load(params: any): this;
|
|
693
|
+
/**
|
|
694
|
+
* Return a string UID combining both link keys
|
|
695
|
+
* @returns the combined UID string
|
|
696
|
+
*/
|
|
697
|
+
getUUID(): string;
|
|
698
|
+
/**
|
|
699
|
+
* Return the proxied version of this model (identity for JunctionLink)
|
|
700
|
+
* @returns this instance
|
|
701
|
+
*/
|
|
702
|
+
toProxy(): this;
|
|
703
|
+
}
|
|
704
|
+
export {};
|
|
705
|
+
//# sourceMappingURL=relations.d.ts.map
|