Skip to content

Commit ef14f79

Browse files
committed
perf(cbjs): improve cluster types IDE experience
1 parent 9b7b981 commit ef14f79

File tree

10 files changed

+358
-131
lines changed

10 files changed

+358
-131
lines changed

packages/cbjs/src/clusterTypes/clusterTypes.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,7 @@ export type { DocDef };
4646
// prettier-ignore
4747
export type CollectionDocDef<Instance> =
4848
Instance extends Collection<infer T, infer B, infer S, infer C> ?
49-
B extends BucketName<T> ?
50-
S extends ScopeName<T, WildcardFallback<B, BucketName<T>>> ?
51-
C extends CollectionName<
52-
T,
53-
WildcardFallback<B, BucketName<T>>,
54-
WildcardFallback<S, ScopeName<T, WildcardFallback<B, BucketName<T>>>>
55-
> ?
56-
KeyspaceDocDef<T, B, S, C> :
57-
never :
58-
never :
59-
never :
49+
KeyspaceDocDef<T, B, S, C> :
6050
never
6151
;
6252

packages/cbjs/src/clusterTypes/kv/lookup/lookupIn.types.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import type {
2020
If,
2121
IsArrayLengthFixed,
2222
IsFuzzyDocument,
23+
LookupInMacroResult,
2324
LookupInMacroReturnType,
24-
OpCodeCompletionValue,
25+
SubDocument,
2526
Try,
2627
} from '@cbjsdev/shared';
2728

@@ -60,19 +61,19 @@ export type LookupInSpecResults<Options, Specs, Defs> =
6061
* Spec operation result type for a single {@link LookupInSpec}.
6162
*/
6263
export type LookupInSpecResult<Options, Spec, Def> =
63-
Def extends DocDefBodyShape ?
64-
Spec extends LookupInSpec<infer LookupDef, infer Op, infer Path> ?
65-
Op extends CppProtocolSubdocOpcode.get | CppProtocolSubdocOpcode.get_doc ?
64+
Spec extends LookupInSpec<infer LookupDef, infer Op, infer Path> ?
65+
Op extends CppProtocolSubdocOpcode.get | CppProtocolSubdocOpcode.get_doc ?
66+
Def extends DocDefBodyShape ?
6667
Path extends keyof LookupInMacroReturnType ?
6768
LookupInMacroReturnType[Path] :
6869
IsFuzzyDocument<LookupDef['Body']> extends true ?
69-
OpCodeCompletionValue<'get', Options, Def['Body'], Path> :
70-
OpCodeCompletionValue<'get', Options, LookupDef['Body'], Path> :
71-
Op extends CppProtocolSubdocOpcode.get_count ?
72-
number :
73-
Op extends CppProtocolSubdocOpcode.exists ?
74-
boolean :
70+
SubDocument<Def['Body'], Path> :
71+
SubDocument<LookupDef['Body'], Path> :
7572
never :
73+
Op extends CppProtocolSubdocOpcode.get_count ?
74+
number :
75+
Op extends CppProtocolSubdocOpcode.exists ?
76+
boolean :
7677
never :
7778
never
7879
;
@@ -110,6 +111,23 @@ type ArrayElementOrUndefined<Arr> =
110111
[]
111112
;
112113

114+
// prettier-ignore
115+
export type OptimisticGetPathCheck<Options, Def extends DocDefBodyShape, Path extends string | LookupInMacro> =
116+
Path extends string ?
117+
[SubDocument<Def['Body'], Path>] extends [never] ?
118+
LookupInGetPath<Options, Def> :
119+
Path :
120+
LookupInGetPath<Options, Def>
121+
;
122+
123+
export type LookupInGetResult<Def extends DocDefBodyShape, Path extends string | LookupInMacro> =
124+
Path extends LookupInMacro ?
125+
LookupInMacroResult<Path> :
126+
Path extends string ?
127+
SubDocument<Def['Body'], Path> :
128+
never
129+
;
130+
113131
/**
114132
* Lookup paths - Non-distributive.
115133
*/

packages/cbjs/src/clusterTypes/kv/mutation/mutationOperations.types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
IsFuzzyDocument,
2121
IsLegalPath, KvOperation,
2222
OpCodeCompletionPath,
23-
OpCodeCompletionValue,
23+
OpCodeCompletionValue, SubDocument,
2424
} from '@cbjsdev/shared';
2525

2626
import type { CompatibleMacro } from './mutateIn.types.js';
@@ -83,7 +83,7 @@ export type MutateInUpsertPath<Options, Def extends DocDefBodyShape> =
8383
* Acceptable value for an `upsert` operation at a specific path.
8484
*/
8585
export type MutateInUpsertValue<Options, Def extends DocDefBodyShape, Path extends string> =
86-
OperationValue<Def['Body'], false, OpCodeCompletionValue<'upsert', Options, Def['Body'], Path>>
86+
OperationValue<Def['Body'], false, SubDocument<Def['Body'], Path>>
8787
;
8888

8989
/**
@@ -103,7 +103,7 @@ export type MutateInReplacePath<Options, Def extends DocDefBodyShape> =
103103
* Acceptable value for a `replace` operation at a specific path.
104104
*/
105105
export type MutateInReplaceValue<Options, Def extends DocDefBodyShape, Path extends string> =
106-
OperationValue<Def['Body'], false, OpCodeCompletionValue<'replace', Options, Def['Body'], Path>>
106+
OperationValue<Def['Body'], false, SubDocument<Def['Body'], Path>>
107107
;
108108

109109
/**

packages/cbjs/src/services/kv/lookupIn/ChainableLookupIn.ts

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
import { DocDef } from '@cbjsdev/shared';
18+
1719
import { CppProtocolSubdocOpcode } from '../../../binding.js';
1820
import {
1921
CollectionDocDefMatchingKey,
@@ -22,18 +24,19 @@ import {
2224
} from '../../../clusterTypes/clusterTypes.js';
2325
import type { AnyCollection } from '../../../clusterTypes/index.js';
2426
import {
27+
LookupInGetResult,
2528
LookupInSpecResult,
2629
MakeLookupInSpec,
30+
OptimisticGetPathCheck,
2731
ValuesFromSpecResults,
2832
} from '../../../clusterTypes/kv/lookup/lookupIn.types.js';
2933
import type {
3034
LookupInCountPath,
3135
LookupInExistsPath,
32-
LookupInGetPath,
3336
} from '../../../clusterTypes/kv/lookup/lookupOperations.types.js';
3437
import { LookupInOptions } from '../../../collection.js';
3538
import { LookupInReplicaResult, LookupInResult } from '../../../crudoptypes.js';
36-
import { LookupInSpec } from '../../../sdspecs.js';
39+
import { LookupInMacro, LookupInSpec } from '../../../sdspecs.js';
3740
import type { LookupMethodName } from './types.js';
3841

3942
// prettier-ignore
@@ -57,7 +60,7 @@ type LookupResult<
5760
never
5861
;
5962

60-
type ThisAnd<T, Spec> =
63+
type ThisAnd<T, AddedSpecResult> =
6164
T extends ChainableLookupIn<
6265
infer C,
6366
infer Method,
@@ -70,7 +73,7 @@ type ThisAnd<T, Spec> =
7073
C,
7174
Method,
7275
Key,
73-
[...SpecResults, LookupInSpecResult<CollectionOptions<C>, Spec, Def>],
76+
[...SpecResults, AddedSpecResult],
7477
ThrowOnSpecError,
7578
Def
7679
>
@@ -79,13 +82,10 @@ type ThisAnd<T, Spec> =
7982
export class ChainableLookupIn<
8083
out C extends AnyCollection,
8184
out Method extends LookupMethodName,
82-
out Key extends ExtractCollectionJsonDocKey<C>,
85+
out Key extends string,
8386
in out SpecResults extends ReadonlyArray<unknown>,
8487
out ThrowOnSpecError extends boolean,
85-
in out Def extends CollectionDocDefMatchingKey<C, Key> = CollectionDocDefMatchingKey<
86-
C,
87-
Key
88-
>,
88+
in out Def extends DocDef = CollectionDocDefMatchingKey<C, Key>,
8989
> implements Promise<LookupResult<Method, SpecResults, ThrowOnSpecError>>
9090
{
9191
// Promise stuff
@@ -104,7 +104,7 @@ export class ChainableLookupIn<
104104
| undefined
105105
| null
106106
): Promise<TResult1 | TResult2> {
107-
return this.execute().then(onFulfilled, onRejected);
107+
return this.execute().then(onFulfilled as never, onRejected);
108108
}
109109

110110
catch<TResult = never>(
@@ -151,15 +151,14 @@ export class ChainableLookupIn<
151151
return new ChainableLookupIn(collection, method, key, options, []);
152152
}
153153

154-
push<Spec extends LookupInSpec>(spec: Spec): ThisAnd<this, Spec> {
154+
push<Spec extends LookupInSpec>(
155+
spec: Spec
156+
): ThisAnd<this, LookupInSpecResult<CollectionOptions<C>, Spec, Def>> {
155157
this.specs = [...this.getSpecs(), spec];
156-
return this as never as ThisAnd<
157-
this,
158-
LookupInSpecResult<CollectionOptions<C>, Spec, Def>
159-
>;
158+
return this as never;
160159
}
161160

162-
execute(): Promise<LookupResult<Method, SpecResults, ThrowOnSpecError>> {
161+
execute(): Promise<LookupInResult<[1, 2], true>> {
163162
const lookupMethod = this.collection[this.method as Method & keyof C];
164163
const lookup = lookupMethod.bind(this.collection) as any;
165164

@@ -177,15 +176,12 @@ export class ChainableLookupIn<
177176
* Whether this operation should reference the document body or the extended
178177
* attributes data for the document.
179178
*/
180-
get<Path extends LookupInGetPath<CollectionOptions<C>, Def>>(
181-
path: Path,
179+
get<const Path extends string | LookupInMacro>(
180+
path: OptimisticGetPathCheck<CollectionOptions<C>, Def, Path>,
182181
options?: { xattr?: boolean }
183-
): ThisAnd<
184-
this,
185-
MakeLookupInSpec<CollectionOptions<C>, Def, CppProtocolSubdocOpcode.get, Path>
186-
> {
182+
): ThisAnd<this, LookupInGetResult<Def, Path>> {
187183
const spec = LookupInSpec.get(path, options);
188-
return this.push(spec);
184+
return this.push(spec) as never;
189185
}
190186

191187
/**
@@ -203,7 +199,11 @@ export class ChainableLookupIn<
203199
options?: { xattr?: boolean }
204200
): ThisAnd<
205201
this,
206-
MakeLookupInSpec<CollectionOptions<C>, Def, CppProtocolSubdocOpcode.exists, Path>
202+
LookupInSpecResult<
203+
CollectionOptions<C>,
204+
MakeLookupInSpec<CollectionOptions<C>, Def, CppProtocolSubdocOpcode.exists, Path>,
205+
Def
206+
>
207207
> {
208208
const spec = LookupInSpec.exists<
209209
CollectionOptions<C>,
@@ -228,7 +228,16 @@ export class ChainableLookupIn<
228228
options?: { xattr?: boolean }
229229
): ThisAnd<
230230
this,
231-
MakeLookupInSpec<CollectionOptions<C>, Def, CppProtocolSubdocOpcode.get_count, Path>
231+
LookupInSpecResult<
232+
CollectionOptions<C>,
233+
MakeLookupInSpec<
234+
CollectionOptions<C>,
235+
Def,
236+
CppProtocolSubdocOpcode.get_count,
237+
Path
238+
>,
239+
Def
240+
>
232241
> {
233242
const spec = LookupInSpec.count<
234243
Def,
@@ -258,7 +267,9 @@ export class ChainableLookupIn<
258267
*/
259268
async values(): Promise<ValuesFromSpecResults<Method, SpecResults, ThrowOnSpecError>> {
260269
if (this.method === 'lookupInAllReplicas') {
261-
const result = (await this.execute()) as { content: { value: unknown }[] }[];
270+
const result = (await this.execute()) as never as {
271+
content: { value: unknown }[];
272+
}[];
262273
return result.map((r) => r.content.map((e) => e.value)) as never;
263274
}
264275

0 commit comments

Comments
 (0)