@graffy/core 0.16.20-alpha.1 → 0.16.20-alpha.11

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 +104 -29
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.1",
5
+ "version": "0.16.20-alpha.11",
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.1",
20
- "@graffy/stream": "0.16.20-alpha.1",
19
+ "@graffy/common": "0.16.20-alpha.11",
20
+ "@graffy/stream": "0.16.20-alpha.11",
21
21
  "debug": "^4.3.7"
22
22
  }
23
23
  }
package/types/index.d.ts CHANGED
@@ -1,9 +1,28 @@
1
- export type JsonObject = Record<string, unknown>;
1
+ export type AnyLeaf = string | number | boolean | null;
2
2
 
3
- type Key = unknown;
3
+ // biome-ignore lint/suspicious/noExplicitAny: This is used to match concrete types in "extends" expressions.
4
+ export type AnyObject = Record<string, any>;
4
5
 
5
- // biome-ignore lint/suspicious/noExplicitAny: "Unknown" prevents assigning a concrete type to this parameter.
6
- export type GraffyCollection<CollectionSchema extends Record<string, any>> = {
6
+ // biome-ignore lint/suspicious/noExplicitAny: Function for which types are not yet defined.
7
+ type AnyFunction = (...args: any[]) => any;
8
+
9
+ // biome-ignore lint/suspicious/noExplicitAny: Keys can be anything.
10
+ export type Key = any;
11
+ export type RangeKey =
12
+ | { $all: boolean }
13
+ | { $first: number }
14
+ | { $last: number };
15
+
16
+ // biome-ignore lint/suspicious/noExplicitAny: Any value is for results.
17
+ type AnyValue = any;
18
+
19
+ type AnyProjection =
20
+ | boolean
21
+ | { $key: Key }
22
+ | { [key: string]: AnyProjection }
23
+ | AnyProjection[];
24
+
25
+ export type GraffyCollection<CollectionSchema extends AnyObject> = {
7
26
  [name: string]: CollectionSchema;
8
27
  } & { __brand: 'GraffyCollection' };
9
28
 
@@ -20,39 +39,95 @@ export default class Graffy<S> {
20
39
  projection: Q,
21
40
  options?: GraffyReadOptions,
22
41
  ): Promise<ReadResult<S, Q>>;
42
+
43
+ // Generic mode, when the path is not known at compile time, but projection is.
44
+ read<Q extends AnyProjection>(
45
+ path: string | Key[],
46
+ projection: Q,
47
+ options?: GraffyReadOptions,
48
+ ): Promise<BlindReadResult<Q>>;
49
+
50
+ // Consider also:
51
+ // 1. Read when path is known at compile time but projection is not?
52
+ // 2. Read when neither path nor projection is known at compile time?
53
+
54
+ on: AnyFunction;
55
+ call: AnyFunction;
56
+ use: AnyFunction;
57
+ onRead: AnyFunction;
58
+ onWrite: AnyFunction;
59
+ onWatch: AnyFunction;
60
+ write: AnyFunction;
61
+ watch: AnyFunction;
62
+
63
+ static unchanged: symbol;
23
64
  }
24
65
 
25
- export type PathOf<S> = S extends GraffyCollection<BuildObject>
26
- ? string | Key
27
- : S extends JsonObject
66
+ // TODO: To avoid the "too deep" error, these need to be tail-recursive.
67
+ // This may be possible with an "accummulator" type parameter.
68
+ // https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#tail-recursion-elimination-on-conditional-types
69
+
70
+ export type PathOf<S> = S extends GraffyCollection<AnyObject>
71
+ ? Key
72
+ : S extends AnyObject
28
73
  ? keyof S | [keyof S, ...PathOf<S[keyof S]>[]]
29
74
  : never;
30
75
 
31
- export type Descend<S, P> = S extends GraffyCollection<BuildObject>
76
+ type Get<S, K> = S extends GraffyCollection<AnyObject>
32
77
  ? S[string]
33
- : P extends keyof S
34
- ? S[P]
35
- : P extends [keyof S]
36
- ? S[P[0]]
37
- : P extends [keyof S, ...infer R]
38
- ? Descend<S[P[0]], R>
39
- : S;
40
-
41
- export type Project<S> = S extends GraffyCollection<BuildObject>
42
- ?
43
- | (Project<S[string]> & { $key: Key })
44
- | (Project<S[string]> & { $key: Key })[]
45
- : S extends JsonObject
46
- ? Partial<{ [K in keyof S]: Project<S[K]> }>
47
- : boolean;
78
+ : K extends keyof S
79
+ ? S[K]
80
+ : never;
81
+
82
+ export type Descend<S, P> = P extends [Key]
83
+ ? Get<S, P[0]>
84
+ : P extends [Key, ...infer R]
85
+ ? Descend<Get<S, P[0]>, R>
86
+ : Get<S, P>;
87
+
88
+ export type Project<S> = S extends AnyLeaf
89
+ ? boolean
90
+ : S extends GraffyCollection<AnyObject>
91
+ ?
92
+ | { [key: string]: Project<S[string]> } // Object form
93
+ | (Project<S[string]> & { $key: Key }) // Single $key
94
+ | (Project<S[string]> & { $key: Key })[] // Array $key
95
+ : S extends AnyObject
96
+ ? 'string' extends keyof S // No named properties?
97
+ ? AnyProjection
98
+ : Partial<{ [K in keyof S]: Project<S[K]> }> | boolean
99
+ : never;
48
100
 
49
101
  // biome-ignore lint/suspicious/noExplicitAny: <explanation>
50
102
  type ResultArray<R> = R[] & { $page: any; $next: any; $prev: any };
51
103
 
52
- type ReadResult<S, Q> = S extends GraffyCollection<BuildObject>
53
- ? (S[string] & { $key: Key }) | ResultArray<S[string] & { $key: Key }>
54
- : S extends JsonObject
55
- ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
56
- : S;
104
+ type ReadResult<S, Q> = S extends GraffyCollection<AnyObject>
105
+ ? Q extends Array<infer QItem>
106
+ ? ResultArray<PlainReadResult<S[string], QItem> & { $key: Key }> // Array $key
107
+ : Q extends { $key: Key }
108
+ ? Q extends { $key: string }
109
+ ? { [key in Q['$key']]: PlainReadResult<S, Q> }
110
+ : ResultArray<PlainReadResult<S[string], Q> & { $key: Key }> // Single $key
111
+ : { [K in keyof Q]: PlainReadResult<S[string], Q[K]> } // Object form
112
+ : PlainReadResult<S, Q>;
113
+
114
+ // Ignore $key in Q
115
+ type PlainReadResult<S, Q> = Q extends AnyObject
116
+ ? { [K in keyof S & keyof Q]: ReadResult<S[K], Q[K]> }
117
+ : S;
118
+
119
+ // What can we tell about ReadResult when schema isn’t known?
120
+ type BlindReadResult<Q> = Q extends Array<infer QItem>
121
+ ? ResultArray<BlindPlainReadResult<QItem>>
122
+ : Q extends { $key: Key }
123
+ ? Q extends { $key: string }
124
+ ? { [key in Q['$key']]: BlindPlainReadResult<Q> }
125
+ : ResultArray<BlindPlainReadResult<Q>>
126
+ : BlindPlainReadResult<Q>;
127
+
128
+ // Ignore $key in Q
129
+ type BlindPlainReadResult<Q> = Q extends AnyObject
130
+ ? { [K in keyof Q]: BlindReadResult<Q[K]> }
131
+ : AnyValue;
57
132
 
58
- type GraffyReadOptions = JsonObject;
133
+ type GraffyReadOptions = AnyObject;