-
Notifications
You must be signed in to change notification settings - Fork 47
Ensure schemas can apply defaults when inserting #209
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 64deab6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@tanstack/db-example-react-todo
commit: |
Hey it's looking good! A few failing tests to fix still... |
…into zod-schema-defaults
…into zod-schema-defaults
Hey I've fixed the previously failing tests but after merging in the new changes there's a bunch of different test failures. I'm moving houses tomorrow so I won't be able to address this for some time. If anyone has the moment to look into these conflicts I'd appreciate your help! Thanks |
👍 I'll pick it up after lunch |
@samwillis could you review my type changes? Particularly to the query builder? Don't want to mess with any guarantees you wanted. Our types are getting complicated... :-\ |
Thanks for picking this up. I have an idea that could simplify some of the collection types: export class CollectionImpl<
T extends object = Record<string, unknown>,
TKey extends string | number = string | number,
// Replace `TExplicit` `TSchema` `TFallback` with TInsertInput:
TInsertInput extends object = T,
> {
// ...
}
export class Collection<
T extends object = Record<string, unknown>,
TKey extends string | number = string | number,
TUtils extends UtilsRecord = {},
// Replace `TExplicit` `TSchema` `TFallback` with TInsertInput:
TInsertInput extends object = T,
> {
// ...
}
export function createCollection<
TExplicit = unknown,
TKey extends string | number = string | number,
TUtils extends UtilsRecord = {},
TSchema extends StandardSchemaV1 = StandardSchemaV1,
TFallback extends object = Record<string, unknown>,
>(options): Collection<
ResolveType<TExplicit, TSchema, TFallback>,
TKey,
TUtils,
// Pass the resolved type:
CollectionInsertInput<TExplicit, TSchema, TFallback>
> {
// ...
const collection = new CollectionImpl<
ResolveType<TExplicit, TSchema, TFallback>,
TKey
// Pass the resolved type:
CollectionInsertInput<TExplicit, TSchema, TFallback>
>(options)
return collection as Collection<
ResolveType<TExplicit, TSchema, TFallback>,
TKey,
TUtils
// Pass the resolved type:
CollectionInsertInput<TExplicit, TSchema, TFallback>
>
} This is similar to how ResolveType already works, only passing the resolved type instead of the 3 generics. |
ah also I forgot to update the collection jsdoc string // packages/db/src/collection.ts
/**
* Enhanced Collection interface that includes both data type T and utilities TUtils
* @template T - The type of items in the collection
* @template TKey - The type of the key for the collection
* @template TUtils - The utilities record type
* @template TSchema - The schema type for validation and type inference
*/
export interface Collection< |
Also, I think both insert / update should be using the input type instead of output. Then, since input would then not be specific to insert, it's probably worth updating the name: This might also be clearer: Here's a usecase of why it matters from gemini: A key use case is handling Calculated/Derived Fields, where the system calculates a value that should not be settable directly by the user. Here's an example: The
Although, would it be better to handle derived fields in a live query collection anyway? I guess if you want it shared across many queries, and it's not an aggregate |
Hmm DB doesn't have the concept of derived fields atm that the dev can't modify directly. Our general assumption is you'd calculate those in |
I'll try out your type idea in a bit! |
All the code in this file is completely re-written in the new query builder, so there is little point in making this perfect if its TTL is a better of days... We will need to make similar changes in the new query builder - I think they looks fine, although I haven't dug into what the new generic args are for. |
@samwillis ok let's wait until the query builder lands |
see #207 (comment) for details