Skip to content
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

Open
TobiasNickel opened this issue Mar 2, 2022 · 3 comments
Open

typedefinition for nested queries #245

TobiasNickel opened this issue Mar 2, 2022 · 3 comments

Comments

@TobiasNickel
Copy link

TobiasNickel commented Mar 2, 2022

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:
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 with address.streed=mainStreet.
Screenshot_2022-03-02_03-22-46

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.

@crcn
Copy link
Owner

crcn commented Oct 29, 2022

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

@mr-rpl
Copy link

mr-rpl commented Dec 5, 2024

this addition would be super helpful!

@mr-rpl
Copy link

mr-rpl commented Dec 5, 2024

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,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants