-
Notifications
You must be signed in to change notification settings - Fork 79
Refactor select improving spread (...obj
) and enabling nested projection.
#389
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
Open
samwillis
wants to merge
24
commits into
ref-refactor
Choose a base branch
from
select-refactor
base: ref-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7f69f04
fix spread types
samwillis b53892f
more spread
samwillis f88bd13
nested spread working
samwillis b82cb54
nested select projection
samwillis 50e5203
checkpoint
samwillis b3adf79
fixed subquery select
samwillis 0a40f1d
tidy
samwillis b9a4d73
changeset
samwillis b74a5de
Merge branch 'ref-refactor' into select-refactor
samwillis 88817e8
mostly working select types...
samwillis 4cf78b0
mostly working select types...
samwillis d7455bc
checkpoint - nearly there
samwillis 175efdf
funcs working
samwillis 66764f0
spread partialy working
samwillis 425664c
more
samwillis 320978f
wip
samwillis 1e5985c
one failing test
samwillis 9888c5a
working types!
samwillis c2b7c54
address first part of review
samwillis 0db2f0c
refactor processSelect
samwillis c79de35
finish addressing review
samwillis 4ec1f68
formatting
samwillis 254df36
remove redundent types
samwillis a4585f0
Merge branch 'ref-refactor' into select-refactor
samwillis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tanstack/db": patch | ||
--- | ||
|
||
Refactored `select` improving spread (`...obj`) support and enabling nested projection. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { PropRef, Value } from "../ir.js" | ||
import type { BasicExpression } from "../ir.js" | ||
import type { Ref } from "./types.js" | ||
import type { RefLeaf } from "./types.js" | ||
|
||
export interface RefProxy<T = any> { | ||
/** @internal */ | ||
|
@@ -20,7 +20,7 @@ export type SingleRowRefProxy<T> = | |
? { | ||
[K in keyof T]: T[K] extends Record<string, any> | ||
? SingleRowRefProxy<T[K]> & RefProxy<T[K]> | ||
: Ref<T[K]> | ||
: RefLeaf<T[K]> | ||
} & RefProxy<T> | ||
: RefProxy<T> | ||
|
||
|
@@ -84,7 +84,7 @@ export function createRefProxy<T extends Record<string, any>>( | |
aliases: Array<string> | ||
): RefProxy<T> & T { | ||
const cache = new Map<string, any>() | ||
const spreadSentinels = new Set<string>() // Track which aliases have been spread | ||
let accessId = 0 // Monotonic counter to record evaluation order | ||
|
||
function createProxy(path: Array<string>): any { | ||
const pathKey = path.join(`.`) | ||
|
@@ -110,10 +110,14 @@ export function createRefProxy<T extends Record<string, any>>( | |
}, | ||
|
||
ownKeys(target) { | ||
// If this is a table-level proxy (path length 1), mark it as spread | ||
if (path.length === 1) { | ||
const aliasName = path[0]! | ||
spreadSentinels.add(aliasName) | ||
const id = ++accessId | ||
const sentinelKey = `__SPREAD_SENTINEL__${path.join(`.`)}__${id}` | ||
if (!Object.prototype.hasOwnProperty.call(target, sentinelKey)) { | ||
Object.defineProperty(target, sentinelKey, { | ||
enumerable: true, | ||
configurable: true, | ||
value: true, | ||
}) | ||
} | ||
return Reflect.ownKeys(target) | ||
}, | ||
|
@@ -136,7 +140,6 @@ export function createRefProxy<T extends Record<string, any>>( | |
if (prop === `__refProxy`) return true | ||
if (prop === `__path`) return [] | ||
if (prop === `__type`) return undefined // Type is only for TypeScript inference | ||
if (prop === `__spreadSentinels`) return spreadSentinels // Expose spread sentinels | ||
if (typeof prop === `symbol`) return Reflect.get(target, prop, receiver) | ||
|
||
const propStr = String(prop) | ||
|
@@ -148,28 +151,18 @@ export function createRefProxy<T extends Record<string, any>>( | |
}, | ||
|
||
has(target, prop) { | ||
if ( | ||
prop === `__refProxy` || | ||
prop === `__path` || | ||
prop === `__type` || | ||
prop === `__spreadSentinels` | ||
) | ||
if (prop === `__refProxy` || prop === `__path` || prop === `__type`) | ||
return true | ||
if (typeof prop === `string` && aliases.includes(prop)) return true | ||
return Reflect.has(target, prop) | ||
}, | ||
|
||
ownKeys(_target) { | ||
return [...aliases, `__refProxy`, `__path`, `__type`, `__spreadSentinels`] | ||
return [...aliases, `__refProxy`, `__path`, `__type`] | ||
}, | ||
|
||
getOwnPropertyDescriptor(target, prop) { | ||
if ( | ||
prop === `__refProxy` || | ||
prop === `__path` || | ||
prop === `__type` || | ||
prop === `__spreadSentinels` | ||
) { | ||
if (prop === `__refProxy` || prop === `__path` || prop === `__type`) { | ||
return { enumerable: false, configurable: true } | ||
} | ||
if (typeof prop === `string` && aliases.includes(prop)) { | ||
|
@@ -190,7 +183,8 @@ export function toExpression<T = any>(value: T): BasicExpression<T> | |
export function toExpression(value: RefProxy<any>): BasicExpression<any> | ||
export function toExpression(value: any): BasicExpression<any> { | ||
if (isRefProxy(value)) { | ||
return new PropRef(value.__path) | ||
const expr = new PropRef(value.__path) | ||
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. nit: let's write this on one line: |
||
return expr | ||
} | ||
// If it's already an Expression (Func, Ref, Value) or Agg, return it directly | ||
if ( | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
toExpression(value)
is the default case when none of the if tests match so we can remove this if test altogether.