-
Notifications
You must be signed in to change notification settings - Fork 152
composable queries #216
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
composable queries #216
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { BaseQueryBuilder } from "./index.js" | ||
| import type { InitialQueryBuilder, QueryBuilder } from "./index.js" | ||
| import type { NamespacedRow } from "../../types.js" | ||
| import type { RefProxyForNamespaceRow, SelectObject } from "./types.js" | ||
|
|
||
| /** | ||
| * Create a reusable query builder that can be used in multiple places | ||
| * | ||
| * @param fn - A function that receives an initial query builder and returns a configured query | ||
| * @returns A reusable query builder that can be used directly or extended further | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const activeUsersQuery = defineQuery((q) => | ||
| * q.from({ user: usersCollection }) | ||
| * .where(({ user }) => eq(user.active, true)) | ||
| * ) | ||
| * | ||
| * // Use directly | ||
| * const users = useLiveQuery(activeUsersQuery) | ||
| * | ||
| * // Extend further | ||
| * const activeAdults = activeUsersQuery.where(({ user }) => gt(user.age, 18)) | ||
| * ``` | ||
| */ | ||
| export function defineQuery<TQueryBuilder extends QueryBuilder<any>>( | ||
| fn: (builder: InitialQueryBuilder) => TQueryBuilder | ||
| ): TQueryBuilder { | ||
| return fn(new BaseQueryBuilder()) | ||
| } | ||
|
|
||
| /** | ||
| * Create reusable, type-safe query components for specific table schemas | ||
| * | ||
| * @returns An object with `callback` and `select` methods for creating reusable components | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Create reusable predicates | ||
| * const userIsAdult = defineForRow<{ user: User }>().callback(({ user }) => | ||
| * gt(user.age, 18) | ||
| * ) | ||
| * | ||
| * // Create reusable select transformations | ||
| * const userInfo = defineForRow<{ user: User }>().select(({ user }) => ({ | ||
| * id: user.id, | ||
| * name: upper(user.name) | ||
| * })) | ||
| * | ||
| * // Use in queries | ||
| * const query = useLiveQuery((q) => | ||
| * q.from({ user: usersCollection }) | ||
| * .where(userIsAdult) | ||
| * .select(userInfo) | ||
| * ) | ||
| * ``` | ||
| */ | ||
| export function defineForRow<TNamespaceRow extends NamespacedRow>() { | ||
| /** | ||
| * Create a reusable callback function for WHERE, HAVING, or other expression contexts | ||
| * | ||
| * @param fn - A function that receives table references and returns an expression | ||
| * @returns A reusable callback function that can be used in query builders | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const userIsActive = defineForRow<{ user: User }>().callback(({ user }) => | ||
| * eq(user.active, true) | ||
| * ) | ||
| * | ||
| * // Use in WHERE clauses | ||
| * query.where(userIsActive) | ||
| * ``` | ||
| */ | ||
| const callback = <TResult>( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function as well as Since they are identity functions, it seems that your example from the PR description is equivalent to: type User = {
name: string
age: number
}
const userIsAdult = ({ user: User }) => gt(user.age, 18)
export const userNameUpper = ({ user }) => ({
name: upper(user.name),
})
const users = useLiveQuery((q) =>
q
.from({ user: usersCollection }) // usersCollection is a Collection<User>
.where(userIsAdult)
.select(userNameUpper)
)This seems more compact and simpler than the original code in the PR description. |
||
| fn: (refs: RefProxyForNamespaceRow<TNamespaceRow>) => TResult | ||
| ) => fn | ||
|
|
||
| /** | ||
| * Create a reusable select transformation for projecting and transforming data | ||
| * | ||
| * @param fn - A function that receives table references and returns a select object | ||
| * @returns A reusable select transformation that can be used in query builders | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const userBasicInfo = defineForRow<{ user: User }>().select(({ user }) => ({ | ||
| * id: user.id, | ||
| * name: user.name, | ||
| * displayName: upper(user.name) | ||
| * })) | ||
| * | ||
| * // Use in SELECT clauses | ||
| * query.select(userBasicInfo) | ||
| * ``` | ||
| */ | ||
| const select = <TSelectObject extends SelectObject>( | ||
| fn: (refs: RefProxyForNamespaceRow<TNamespaceRow>) => TSelectObject | ||
| ) => fn | ||
|
|
||
| return { | ||
| callback, | ||
| select, | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
defineForRowfunction seems to be used only for typing purposes. I'm against introducing runtime functions for compile time concerns (like typing).