@graffy/core 0.16.20-alpha.4 → 0.16.20-alpha.6

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/types/index.d.ts +42 -8
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/core",
3
3
  "description": "The main module for Graffy, a library for intuitive real-time data APIs.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.16.20-alpha.4",
5
+ "version": "0.16.20-alpha.6",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,8 +16,8 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.16.20-alpha.4",
20
- "@graffy/stream": "0.16.20-alpha.4",
19
+ "@graffy/common": "0.16.20-alpha.6",
20
+ "@graffy/stream": "0.16.20-alpha.6",
21
21
  "debug": "^4.3.7"
22
22
  }
23
23
  }
package/types/index.d.ts CHANGED
@@ -9,6 +9,15 @@ type AnyFunction = (...args: any[]) => any;
9
9
  // biome-ignore lint/suspicious/noExplicitAny: Keys can be anything.
10
10
  type Key = any;
11
11
 
12
+ // biome-ignore lint/suspicious/noExplicitAny: Any value is for results.
13
+ type AnyValue = any;
14
+
15
+ type AnyProjection =
16
+ | boolean
17
+ | { $key: Key }
18
+ | Record<string, AnyProjection>
19
+ | AnyProjection[];
20
+
12
21
  export type GraffyCollection<CollectionSchema extends AnyObject> = {
13
22
  [name: string]: CollectionSchema;
14
23
  } & { __brand: 'GraffyCollection' };
@@ -27,6 +36,13 @@ export default class Graffy<S> {
27
36
  options?: GraffyReadOptions,
28
37
  ): Promise<ReadResult<S, Q>>;
29
38
 
39
+ // Generic one, when the path is not known at compile time.
40
+ read<Q extends AnyProjection>(
41
+ path: string | Key[],
42
+ projection: Q,
43
+ options?: GraffyReadOptions,
44
+ ): Promise<BlindReadResult<Q>>;
45
+
30
46
  on: AnyFunction;
31
47
  call: AnyFunction;
32
48
  use: AnyFunction;
@@ -63,8 +79,9 @@ export type Project<S> = S extends AnyLeaf
63
79
  ? boolean
64
80
  : S extends GraffyCollection<AnyObject>
65
81
  ?
66
- | (Project<S[string]> & { $key: Key })
67
- | (Project<S[string]> & { $key: Key })[]
82
+ | { [key: string]: Project<S[string]> } // Object form
83
+ | (Project<S[string]> & { $key: Key }) // Single $key
84
+ | (Project<S[string]> & { $key: Key })[] // Array $key
68
85
  : S extends AnyObject
69
86
  ? Partial<{ [K in keyof S]: Project<S[K]> }> | boolean
70
87
  : never;
@@ -73,11 +90,28 @@ export type Project<S> = S extends AnyLeaf
73
90
  type ResultArray<R> = R[] & { $page: any; $next: any; $prev: any };
74
91
 
75
92
  type ReadResult<S, Q> = S extends GraffyCollection<AnyObject>
76
- ? Q extends Array<AnyObject> | { $key: Key }
77
- ? ResultArray<S[string] & { $key: Key }>
78
- : S[string] & { $key: Key }
79
- : S extends AnyObject
80
- ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
81
- : S;
93
+ ? Q extends Array<infer QItem>
94
+ ? ResultArray<PlainReadResult<S[string], QItem> & { $key: Key }> // Array $key
95
+ : Q extends { $key: Key }
96
+ ? ResultArray<PlainReadResult<S[string], Q> & { $key: Key }> // Single $key
97
+ : { [K in keyof Q]: PlainReadResult<S[string], Q[K]> } // Object form
98
+ : PlainReadResult<S, Q>;
99
+
100
+ // Ignore $key in Q
101
+ type PlainReadResult<S, Q> = Q extends AnyObject
102
+ ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
103
+ : S;
104
+
105
+ // What can we tell about ReadResult when schema isn’t known?
106
+ type BlindReadResult<Q> = Q extends Array<infer QItem>
107
+ ? ResultArray<BlindPlainReadResult<QItem>>
108
+ : Q extends { $key: Key }
109
+ ? ResultArray<BlindPlainReadResult<Q>>
110
+ : BlindPlainReadResult<Q>;
111
+
112
+ // Ignore $key in Q
113
+ type BlindPlainReadResult<Q> = Q extends AnyObject
114
+ ? { [K in keyof Q]: BlindReadResult<Q[K]> }
115
+ : AnyValue;
82
116
 
83
117
  type GraffyReadOptions = AnyObject;