-
-
Notifications
You must be signed in to change notification settings - Fork 307
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
Initial non searchable fields support on missing schema/doc key match #174
Initial non searchable fields support on missing schema/doc key match #174
Conversation
Hey hey thank you so much for opening this! Let me know if you need any help at any point! |
Thanks @micheleriva ❤️ Basically, I don't think that ATM While I was expecting that Something like the following crashes const db = create({
schema: {
quote: "string",
author: {
name: "string",
surname: "string",
},
tag: {
name: "string",
description: "string",
},
},
});
insert(db, {
quote: "Be yourself; everyone else is already taken.",
author: {
name: "Oscar",
surname: "Wild",
},
tag: {
name: "inspirational",
description: "Inspirational quotes",
// @ts-expect-error test error case
cadrega: "so... the apple is the cadrega",
},
}); However, this other doc that have the same problem but const db = create({
schema: {
quote: "string",
author: {
name: "string",
surname: "string",
},
tag: {
name: "string",
description: "string",
},
},
});
insert(db, {
quote: "A room without books is like a body without a soul.",
author: {
name: "Marcus",
surname: "Tullius Cicero",
},
// @ts-expect-error test error case
furio: "Madga, do you adore me? Then you see it's a mutual thing, do ya?",
tag: {
name: "books",
description: "Quotes about books",
},
}); The same applies on wrong supported scalar schema types and doc type check Is this intended to work like this? It's not strictly related to this feature but (if you confirm my doubt) it could prevent the way I tried to implement this from working as expected Also, if all of that turns out to be something real, the interface for the document I really hope to be wrong and that I'm missing something 🤞 😓 |
Hey @LBRDan thank you so much for the analysis! So, as for the current version of Lyra ( For instance, given: import { create, insert } from '@lyrasearch/lyra'
const db = create({
quote: 'string'
meta: {
rating: 'number'
}
}) this should throw: insert({
quote: 'foo bar baz',
meta: {
rating: 10,
randomProperty: 'foo' // <-- this should throw
}
}) This is certainly a bug and I'd kindly ask you to open a new issue for it, we can fix it separately after merging this PR (it will be released in Regarding this PR, I'd ask you to add more test cases, also with deeply nested properties and different types. Thanks a lot! |
Hi @LBRDan! |
Sorry @micheleriva , I'm pushing the tests you requested just now and opening the PR to reviews! Anyway, I was wondering if a normal user should be able to enhance the schema (preventing type checking errors) via TS module augmentation (or something else), or we should also change the import type { PropertiesSchema } from "./lyra";
export type Nullable<T> = T | null;
type ResolveTypes<TType> = TType extends "string"
? string
: TType extends "boolean"
? boolean
: TType extends "number"
? number
: TType extends PropertiesSchema
? { [P in keyof TType]: ResolveTypes<TType[P]> } & { [key: string]: unknown }
: never;
export type ResolveSchema<T extends PropertiesSchema> = {
[P in keyof T]: ResolveTypes<T[P]>;
} & { [key: string]: unknown };
export type SearchProperties<
TSchema extends PropertiesSchema,
TKey extends keyof TSchema = keyof TSchema,
> = TKey extends string
? TSchema[TKey] extends PropertiesSchema
? `${TKey}.${SearchProperties<TSchema[TKey]>}`
: TKey
: never; What do you think about this? Also, it took me a while to merge the frequency feature that landed on master. Some of the tests that were marked there as to skip, currently do not pass in my branch and I believe they should... Let me know if something slipped unnoticed after the conflict resolution from the merge |
@LBRDan I just tested this locally, and it seems to work great! About the typings, we could consider it as a nice-to-have for a future release. Thank you so much for your contribution |
Hello there!
Thanks for this amazing piece of open source software! It should be an example for everyone out there as it is for me ❤️
First of all, let me apologize for being so rushy in opening this PR as a draft, but I'm currently running out of battery (hopefully be able to work again on this tonight or tomorrow (CET))
This PR contains a first implementation attempt for #171 issue. I wonder if this is the desired way of implementing this feature, it would be awesome to hear feedback from you
Thanks!
--- Edit:
This PR tries to implement the #171 request for non-searchable fields when they are included inside a document but they're not included inside the schema definition
I tried to implement the feature by:
recursiveCheckDocSchema
functionrecursiveTrieInsertion
to be schema-aware when including a document field as searchable by traversing the doc and the schema at the same pace