-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typedefinition for nested queries #245
Comments
Heyy, sorry for not getting back to this sooner. The syntax that you'll want to use is this: const filter = sift({
// path syntax like this 👇
"address.street": { $in: ["mainstreet"] }
});
const results = [
{
address: {
street: "mainstreet",
}
},
{
address: {
street: "not mainstreet",
}
}
].filter(filter); Here's a playground you can play around with: https://mongoplayground.net/p/5YoyNZLBnbf Unfortunately Sift types don't work with nested fields right now. It looks like this can work, but will require some effort. Here's what I'm looking at: https://stackoverflow.com/questions/58434389/typescript-deep-keyof-of-a-nested-object/58436959#58436959 |
this addition would be super helpful! |
this is not well tested, but on paper works fairly well: import type { NestedQuery, Query } from 'sift'
type Leaves<T> = T extends Date | Function | RegExp // Exclude special types
? never
: T extends object // Continue processing for objects
? {
[K in keyof T]: `${Exclude<K, symbol>}${Leaves<T[K]> extends never ? '' : `.${Leaves<T[K]>}`}`
}[keyof T]
: never
interface Item {
foo: string
bar: string
foobar: {
foo: string
bar: string
}
}
type QueryRecord = Record<Leaves<Item>, NestedQuery<unknown>>
const query: Query<QueryRecord> = {
foo: {
$exists: true,
},
bar: () => true,
'foobar.foo': {
$exists: true,
},
'foobar.bar': () => true,
} |
Hi, I am about to use sift with my npm module TRDB. tldr: it is a json-file db.
I wanted to add support for mongo-queries and use sift for that.
And I found some issue with the typescript definitions when using a nested query:
![Screenshot_2022-03-02_03-22-46](https://user-images.githubusercontent.com/4189801/156327500-895ad02b-8b5c-4b44-8c2a-dccd1908d9ef.png)
in the picture is the you see that
address.street
has the wrong type definition, the picture also show, that the execution is actually working correct and I find the element withaddress.streed=mainStreet
.the typescript error is
Type '{ $in: string }' is not assignable to type 'string' ts(2322)
my version is 16.0.0,
the typescript version is 4.0.3
Do you know how to get the nested type definitions right?
wand to play with the code: here is the gist.
The text was updated successfully, but these errors were encountered: