@coasys/ad4m 0.10.1-release-candidate-3-test-2 → 0.10.1-release-candidate-4-debug-strings

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.
@@ -1,550 +0,0 @@
1
- import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
2
- type ValueTuple = [name: string, value: any, resolve?: boolean];
3
- type WhereOps = {
4
- not: string | number | boolean | string[] | number[];
5
- between: [number, number];
6
- lt: number;
7
- lte: number;
8
- gt: number;
9
- gte: number;
10
- };
11
- type WhereCondition = string | number | boolean | string[] | number[] | {
12
- [K in keyof WhereOps]?: WhereOps[K];
13
- };
14
- type Where = {
15
- [propertyName: string]: WhereCondition;
16
- };
17
- type Order = {
18
- [propertyName: string]: "ASC" | "DESC";
19
- };
20
- export type Query = {
21
- source?: string;
22
- properties?: string[];
23
- collections?: string[];
24
- where?: Where;
25
- order?: Order;
26
- offset?: number;
27
- limit?: number;
28
- count?: boolean;
29
- };
30
- export type AllInstancesResult = {
31
- AllInstances: Ad4mModel[];
32
- TotalCount?: number;
33
- };
34
- export type ResultsWithTotalCount<T> = {
35
- results: T[];
36
- totalCount?: number;
37
- };
38
- export type PaginationResult<T> = {
39
- results: T[];
40
- totalCount?: number;
41
- pageSize: number;
42
- pageNumber: number;
43
- };
44
- /**
45
- * Base class for defining data models in AD4M.
46
- *
47
- * @description
48
- * Ad4mModel provides the foundation for creating data models that are stored in AD4M perspectives.
49
- * Each model instance is represented as a subgraph in the perspective, with properties and collections
50
- * mapped to links in that graph. The class uses Prolog-based queries to efficiently search and filter
51
- * instances based on their properties and relationships.
52
- *
53
- * Key concepts:
54
- * - Each model instance has a unique base expression that serves as its identifier
55
- * - Properties are stored as links with predicates defined by the `through` option
56
- * - Collections represent one-to-many relationships as sets of links
57
- * - Queries are translated to Prolog for efficient graph pattern matching
58
- * - Changes are tracked through the perspective's subscription system
59
- *
60
- * @example
61
- * ```typescript
62
- * // Define a recipe model
63
- * @ModelOptions({ name: "Recipe" })
64
- * class Recipe extends Ad4mModel {
65
- * // Required property with literal value
66
- * @Property({
67
- * through: "recipe://name",
68
- * resolveLanguage: "literal"
69
- * })
70
- * name: string = "";
71
- *
72
- * // Optional property with custom initial value
73
- * @Optional({
74
- * through: "recipe://status",
75
- * initial: "recipe://draft"
76
- * })
77
- * status: string = "";
78
- *
79
- * // Read-only computed property
80
- * @ReadOnly({
81
- * through: "recipe://rating",
82
- * getter: `
83
- * findall(Rating, triple(Base, "recipe://user_rating", Rating), Ratings),
84
- * sum_list(Ratings, Sum),
85
- * length(Ratings, Count),
86
- * Value is Sum / Count
87
- * `
88
- * })
89
- * averageRating: number = 0;
90
- *
91
- * // Collection of ingredients
92
- * @Collection({ through: "recipe://ingredient" })
93
- * ingredients: string[] = [];
94
- *
95
- * // Collection of comments that are instances of another model
96
- * @Collection({
97
- * through: "recipe://comment",
98
- * where: { isInstance: Comment }
99
- * })
100
- * comments: Comment[] = [];
101
- * }
102
- *
103
- * // Create and save a new recipe
104
- * const recipe = new Recipe(perspective);
105
- * recipe.name = "Chocolate Cake";
106
- * recipe.ingredients = ["flour", "sugar", "cocoa"];
107
- * await recipe.save();
108
- *
109
- * // Query recipes in different ways
110
- * // Get all recipes
111
- * const allRecipes = await Recipe.findAll(perspective);
112
- *
113
- * // Find recipes with specific criteria
114
- * const desserts = await Recipe.findAll(perspective, {
115
- * where: {
116
- * status: "recipe://published",
117
- * averageRating: { gt: 4 }
118
- * },
119
- * order: { name: "ASC" },
120
- * limit: 10
121
- * });
122
- *
123
- * // Use the fluent query builder
124
- * const popularRecipes = await Recipe.query(perspective)
125
- * .where({ averageRating: { gt: 4.5 } })
126
- * .order({ averageRating: "DESC" })
127
- * .limit(5)
128
- * .get();
129
- *
130
- * // Subscribe to real-time updates
131
- * await Recipe.query(perspective)
132
- * .where({ status: "recipe://cooking" })
133
- * .subscribe(recipes => {
134
- * console.log("Currently being cooked:", recipes);
135
- * });
136
- *
137
- * // Paginate results
138
- * const { results, totalCount, pageNumber } = await Recipe.query(perspective)
139
- * .where({ status: "recipe://published" })
140
- * .paginate(10, 1);
141
- * ```
142
- */
143
- export declare class Ad4mModel {
144
- #private;
145
- author: string;
146
- timestamp: string;
147
- private static classNamesByClass;
148
- static getClassName(perspective: PerspectiveProxy): Promise<string>;
149
- /**
150
- * Constructs a new model instance.
151
- *
152
- * @param perspective - The perspective where this model will be stored
153
- * @param baseExpression - Optional unique identifier for this instance
154
- * @param source - Optional source expression this instance is linked to
155
- *
156
- * @example
157
- * ```typescript
158
- * // Create a new recipe with auto-generated base expression
159
- * const recipe = new Recipe(perspective);
160
- *
161
- * // Create with specific base expression
162
- * const recipe = new Recipe(perspective, "recipe://chocolate-cake");
163
- *
164
- * // Create with source link
165
- * const recipe = new Recipe(perspective, undefined, "cookbook://desserts");
166
- * ```
167
- */
168
- constructor(perspective: PerspectiveProxy, baseExpression?: string, source?: string);
169
- /**
170
- * Gets the base expression of the subject.
171
- */
172
- get baseExpression(): string;
173
- /**
174
- * Protected getter for the perspective.
175
- * Allows subclasses to access the perspective while keeping it private from external code.
176
- */
177
- protected get perspective(): PerspectiveProxy;
178
- static assignValuesToInstance(perspective: PerspectiveProxy, instance: Ad4mModel, values: ValueTuple[]): Promise<void>;
179
- private getData;
180
- static queryToProlog(perspective: PerspectiveProxy, query: Query): Promise<string>;
181
- static instancesFromPrologResult<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query: Query, result: AllInstancesResult): Promise<ResultsWithTotalCount<T>>;
182
- /**
183
- * Gets all instances of the model in the perspective that match the query params.
184
- *
185
- * @param perspective - The perspective to search in
186
- * @param query - Optional query parameters to filter results
187
- * @returns Array of matching models
188
- *
189
- * @example
190
- * ```typescript
191
- * // Get all recipes
192
- * const allRecipes = await Recipe.findAll(perspective);
193
- *
194
- * // Get recipes with specific criteria
195
- * const recipes = await Recipe.findAll(perspective, {
196
- * where: {
197
- * name: "Pasta",
198
- * rating: { gt: 4 }
199
- * },
200
- * order: { createdAt: "DESC" },
201
- * limit: 10
202
- * });
203
- * ```
204
- */
205
- static findAll<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: Query): Promise<T[]>;
206
- /**
207
- * Gets all instances with count of total matches without limit applied.
208
- *
209
- * @param perspective - The perspective to search in
210
- * @param query - Optional query parameters to filter results
211
- * @returns Object containing results array and total count
212
- *
213
- * @example
214
- * ```typescript
215
- * const { results, totalCount } = await Recipe.findAllAndCount(perspective, {
216
- * where: { category: "Dessert" },
217
- * limit: 10
218
- * });
219
- * console.log(`Showing 10 of ${totalCount} dessert recipes`);
220
- * ```
221
- */
222
- static findAllAndCount<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: Query): Promise<ResultsWithTotalCount<T>>;
223
- /**
224
- * Helper function for pagination with explicit page size and number.
225
- *
226
- * @param perspective - The perspective to search in
227
- * @param pageSize - Number of items per page
228
- * @param pageNumber - Which page to retrieve (1-based)
229
- * @param query - Optional additional query parameters
230
- * @returns Paginated results with metadata
231
- *
232
- * @example
233
- * ```typescript
234
- * const page = await Recipe.paginate(perspective, 10, 1, {
235
- * where: { category: "Main Course" }
236
- * });
237
- * console.log(`Page ${page.pageNumber} of recipes, ${page.results.length} items`);
238
- * ```
239
- */
240
- static paginate<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, pageSize: number, pageNumber: number, query?: Query): Promise<PaginationResult<T>>;
241
- static countQueryToProlog(perspective: PerspectiveProxy, query?: Query): Promise<string>;
242
- /**
243
- * Gets a count of all matching instances.
244
- *
245
- * @param perspective - The perspective to search in
246
- * @param query - Optional query parameters to filter results
247
- * @returns Total count of matching entities
248
- *
249
- * @example
250
- * ```typescript
251
- * const totalRecipes = await Recipe.count(perspective);
252
- * const activeRecipes = await Recipe.count(perspective, {
253
- * where: { status: "active" }
254
- * });
255
- * ```
256
- */
257
- static count(perspective: PerspectiveProxy, query?: Query): Promise<any>;
258
- private setProperty;
259
- private setCollectionSetter;
260
- private setCollectionAdder;
261
- private setCollectionRemover;
262
- /**
263
- * Saves the model instance to the perspective.
264
- * Creates a new instance with the base expression and links it to the source.
265
- *
266
- * @throws Will throw if instance creation, linking, or updating fails
267
- *
268
- * @example
269
- * ```typescript
270
- * const recipe = new Recipe(perspective);
271
- * recipe.name = "Spaghetti";
272
- * recipe.ingredients = ["pasta", "tomato sauce"];
273
- * await recipe.save();
274
- * ```
275
- */
276
- save(): Promise<void>;
277
- /**
278
- * Updates the model instance's properties and collections.
279
- *
280
- * @throws Will throw if property setting or collection updates fail
281
- *
282
- * @example
283
- * ```typescript
284
- * const recipe = await Recipe.findAll(perspective)[0];
285
- * recipe.rating = 5;
286
- * recipe.ingredients.push("garlic");
287
- * await recipe.update();
288
- * ```
289
- */
290
- update(): Promise<void>;
291
- /**
292
- * Gets the model instance with all properties and collections populated.
293
- *
294
- * @returns The populated model instance
295
- * @throws Will throw if data retrieval fails
296
- *
297
- * @example
298
- * ```typescript
299
- * const recipe = new Recipe(perspective, existingId);
300
- * await recipe.get();
301
- * console.log(recipe.name, recipe.ingredients);
302
- * ```
303
- */
304
- get(): Promise<this>;
305
- /**
306
- * Deletes the model instance from the perspective.
307
- *
308
- * @throws Will throw if removal fails
309
- *
310
- * @example
311
- * ```typescript
312
- * const recipe = await Recipe.findAll(perspective)[0];
313
- * await recipe.delete();
314
- * ```
315
- */
316
- delete(): Promise<void>;
317
- /**
318
- * Creates a query builder for fluent query construction.
319
- *
320
- * @param perspective - The perspective to query
321
- * @param query - Optional initial query parameters
322
- * @returns A new query builder instance
323
- *
324
- * @example
325
- * ```typescript
326
- * const recipes = await Recipe.query(perspective)
327
- * .where({ category: "Dessert" })
328
- * .order({ rating: "DESC" })
329
- * .limit(5)
330
- * .run();
331
- *
332
- * // With real-time updates
333
- * await Recipe.query(perspective)
334
- * .where({ status: "cooking" })
335
- * .subscribeAndRun(recipes => {
336
- * console.log("Currently cooking:", recipes);
337
- * });
338
- * ```
339
- */
340
- static query<T extends Ad4mModel>(this: typeof Ad4mModel & (new (...args: any[]) => T), perspective: PerspectiveProxy, query?: Query): ModelQueryBuilder<T>;
341
- }
342
- /** Query builder for Ad4mModel queries.
343
- * Allows building queries with a fluent interface and either running them once
344
- * or subscribing to updates.
345
- *
346
- * @example
347
- * ```typescript
348
- * const builder = Recipe.query(perspective)
349
- * .where({ category: "Dessert" })
350
- * .order({ rating: "DESC" })
351
- * .limit(10);
352
- *
353
- * // Run once
354
- * const recipes = await builder.run();
355
- *
356
- * // Or subscribe to updates
357
- * await builder.subscribeAndRun(recipes => {
358
- * console.log("Updated recipes:", recipes);
359
- * });
360
- * ```
361
- */
362
- export declare class ModelQueryBuilder<T extends Ad4mModel> {
363
- private perspective;
364
- private queryParams;
365
- private ctor;
366
- constructor(perspective: PerspectiveProxy, ctor: typeof Ad4mModel, query?: Query);
367
- /**
368
- * Adds where conditions to the query.
369
- *
370
- * @param conditions - The conditions to filter by
371
- * @returns The query builder for chaining
372
- *
373
- * @example
374
- * ```typescript
375
- * .where({
376
- * category: "Dessert",
377
- * rating: { gt: 4 },
378
- * tags: ["vegan", "quick"],
379
- * published: true
380
- * })
381
- * ```
382
- */
383
- where(conditions: Where): ModelQueryBuilder<T>;
384
- /**
385
- * Sets the order for the query results.
386
- *
387
- * @param orderBy - The ordering criteria
388
- * @returns The query builder for chaining
389
- *
390
- * @example
391
- * ```typescript
392
- * .order({ createdAt: "DESC" })
393
- * ```
394
- */
395
- order(orderBy: Order): ModelQueryBuilder<T>;
396
- /**
397
- * Sets the maximum number of results to return.
398
- *
399
- * @param limit - Maximum number of results
400
- * @returns The query builder for chaining
401
- *
402
- * @example
403
- * ```typescript
404
- * .limit(10)
405
- * ```
406
- */
407
- limit(limit: number): ModelQueryBuilder<T>;
408
- /**
409
- * Sets the number of results to skip.
410
- *
411
- * @param offset - Number of results to skip
412
- * @returns The query builder for chaining
413
- *
414
- * @example
415
- * ```typescript
416
- * .offset(20) // Skip first 20 results
417
- * ```
418
- */
419
- offset(offset: number): ModelQueryBuilder<T>;
420
- /**
421
- * Sets the source filter for the query.
422
- *
423
- * @param source - The source to filter by
424
- * @returns The query builder for chaining
425
- *
426
- * @example
427
- * ```typescript
428
- * .source("ad4m://self")
429
- * ```
430
- */
431
- source(source: string): ModelQueryBuilder<T>;
432
- /**
433
- * Specifies which properties to include in the results.
434
- *
435
- * @param properties - Array of property names to include
436
- * @returns The query builder for chaining
437
- *
438
- * @example
439
- * ```typescript
440
- * .properties(["name", "description", "rating"])
441
- * ```
442
- */
443
- properties(properties: string[]): ModelQueryBuilder<T>;
444
- /**
445
- * Specifies which collections to include in the results.
446
- *
447
- * @param collections - Array of collection names to include
448
- * @returns The query builder for chaining
449
- *
450
- * @example
451
- * ```typescript
452
- * .collections(["ingredients", "steps"])
453
- * ```
454
- */
455
- collections(collections: string[]): ModelQueryBuilder<T>;
456
- /**
457
- * Executes the query once and returns the results.
458
- *
459
- * @returns Array of matching entities
460
- *
461
- * @example
462
- * ```typescript
463
- * const recipes = await Recipe.query(perspective)
464
- * .where({ category: "Dessert" })
465
- * .get();
466
- * ```
467
- */
468
- get(): Promise<T[]>;
469
- /**
470
- * Subscribes to the query and receives updates when results change.
471
- * Also returns initial results immediately.
472
- *
473
- * @param callback - Function to call with updated results
474
- * @returns Initial results array
475
- *
476
- * @example
477
- * ```typescript
478
- * await Recipe.query(perspective)
479
- * .where({ status: "cooking" })
480
- * .subscribe(recipes => {
481
- * console.log("Currently cooking:", recipes);
482
- * });
483
- * ```
484
- */
485
- subscribe(callback: (results: T[]) => void): Promise<T[]>;
486
- /**
487
- * Gets the total count of matching entities.
488
- *
489
- * @returns Total count
490
- *
491
- * @example
492
- * ```typescript
493
- * const totalDesserts = await Recipe.query(perspective)
494
- * .where({ category: "Dessert" })
495
- * .count();
496
- * ```
497
- */
498
- count(): Promise<number>;
499
- /**
500
- * Subscribes to count updates for matching entities.
501
- *
502
- * @param callback - Function to call with updated count
503
- * @returns Initial count
504
- *
505
- * @example
506
- * ```typescript
507
- * await Recipe.query(perspective)
508
- * .where({ status: "active" })
509
- * .countSubscribe(count => {
510
- * console.log("Active items:", count);
511
- * });
512
- * ```
513
- */
514
- countSubscribe(callback: (count: number) => void): Promise<number>;
515
- /**
516
- * Gets a page of results with pagination metadata.
517
- *
518
- * @param pageSize - Number of items per page
519
- * @param pageNumber - Which page to retrieve (1-based)
520
- * @returns Paginated results with metadata
521
- *
522
- * @example
523
- * ```typescript
524
- * const page = await Recipe.query(perspective)
525
- * .where({ category: "Main" })
526
- * .paginate(10, 1);
527
- * console.log(`Page ${page.pageNumber}, ${page.results.length} of ${page.totalCount}`);
528
- * ```
529
- */
530
- paginate(pageSize: number, pageNumber: number): Promise<PaginationResult<T>>;
531
- /**
532
- * Subscribes to paginated results updates.
533
- *
534
- * @param pageSize - Number of items per page
535
- * @param pageNumber - Which page to retrieve (1-based)
536
- * @param callback - Function to call with updated pagination results
537
- * @returns Initial pagination results
538
- *
539
- * @example
540
- * ```typescript
541
- * await Recipe.query(perspective)
542
- * .where({ category: "Main" })
543
- * .paginateSubscribe(10, 1, page => {
544
- * console.log("Updated page:", page.results);
545
- * });
546
- * ```
547
- */
548
- paginateSubscribe(pageSize: number, pageNumber: number, callback: (results: PaginationResult<T>) => void): Promise<PaginationResult<T>>;
549
- }
550
- export {};