1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17+ import { DocDef } from '@cbjsdev/shared' ;
18+
1719import { CppProtocolSubdocOpcode } from '../../../binding.js' ;
1820import {
1921 CollectionDocDefMatchingKey ,
@@ -22,18 +24,19 @@ import {
2224} from '../../../clusterTypes/clusterTypes.js' ;
2325import type { AnyCollection } from '../../../clusterTypes/index.js' ;
2426import {
27+ LookupInGetResult ,
2528 LookupInSpecResult ,
2629 MakeLookupInSpec ,
30+ OptimisticGetPathCheck ,
2731 ValuesFromSpecResults ,
2832} from '../../../clusterTypes/kv/lookup/lookupIn.types.js' ;
2933import type {
3034 LookupInCountPath ,
3135 LookupInExistsPath ,
32- LookupInGetPath ,
3336} from '../../../clusterTypes/kv/lookup/lookupOperations.types.js' ;
3437import { LookupInOptions } from '../../../collection.js' ;
3538import { LookupInReplicaResult , LookupInResult } from '../../../crudoptypes.js' ;
36- import { LookupInSpec } from '../../../sdspecs.js' ;
39+ import { LookupInMacro , LookupInSpec } from '../../../sdspecs.js' ;
3740import 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> =
7982export 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