Skip to content

feat: orderBy in the query builder #65

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

Merged
merged 7 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/optimistic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Core optimistic updates library",
"version": "0.0.3",
"dependencies": {
"@electric-sql/d2ts": "^0.1.4",
"@electric-sql/d2ts": "^0.1.5",
"@standard-schema/spec": "^1.0.0",
"@tanstack/store": "^0.7.0"
},
Expand Down
16 changes: 15 additions & 1 deletion packages/optimistic/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,21 @@ export class Collection<T extends object = Record<string, unknown>> {
// Create a derived array from the map to avoid recalculating it
this.derivedArray = new Derived({
fn: ({ currDepVals: [stateMap] }) => {
return Array.from(stateMap.values())
// Collections returned by a query that has an orderBy are annotated
// with the _orderByIndex field.
// This is used to sort the array when it's derived.
const array: Array<T & { _orderByIndex?: number }> = Array.from(
stateMap.values()
)
if (array[0] && `_orderByIndex` in array[0]) {
;(array as Array<T & { _orderByIndex: number }>).sort((a, b) => {
if (a._orderByIndex === b._orderByIndex) {
return 0
}
return a._orderByIndex < b._orderByIndex ? -1 : 1
})
}
return array
},
deps: [this.derivedState],
})
Expand Down
33 changes: 28 additions & 5 deletions packages/optimistic/src/query/order-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function processOrderBy(
}
// if a and b are both booleans, compare them
if (typeof a === `boolean` && typeof b === `boolean`) {
return a ? 1 : -1
return a === b ? 0 : a ? 1 : -1
}
// if a and b are both dates, compare them
if (a instanceof Date && b instanceof Date) {
Expand All @@ -149,11 +149,34 @@ export function processOrderBy(
}
// if a and b are both arrays, compare them element by element
if (Array.isArray(a) && Array.isArray(b)) {
for (let i = 0; i < a.length; i++) {
const result = comparator(a[i], b[i])
if (result !== 0) return result
for (let i = 0; i < Math.min(a.length, b.length); i++) {
// Get the values from the array
const aVal = a[i]
const bVal = b[i]

// Compare the values
let result: number

if (typeof aVal === `boolean` && typeof bVal === `boolean`) {
// Special handling for booleans - false comes before true
result = aVal === bVal ? 0 : aVal ? 1 : -1
} else if (typeof aVal === `number` && typeof bVal === `number`) {
// Numeric comparison
result = aVal - bVal
} else if (typeof aVal === `string` && typeof bVal === `string`) {
// String comparison
result = aVal.localeCompare(bVal)
} else {
// Default comparison using the general comparator
result = comparator(aVal, bVal)
}

if (result !== 0) {
return result
}
}
return 0
// All elements are equal up to the minimum length
return a.length - b.length
}
// if a and b are both null/undefined, return 0
if ((a === null || a === undefined) && (b === null || b === undefined)) {
Expand Down
13 changes: 13 additions & 0 deletions packages/optimistic/src/query/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
return select
})

// Ensure we have an orderByIndex in the select if we have an orderBy
// This is required if select is called after orderBy
if (this._query.orderBy) {
validatedSelects.push({ _orderByIndex: { ORDER_INDEX: `numeric` } })
}

const newBuilder = new BaseQueryBuilder<TContext>(
(this as BaseQueryBuilder<TContext>).query
)
Expand Down Expand Up @@ -704,6 +710,13 @@ export class BaseQueryBuilder<TContext extends Context<Schema>> {
// Set the orderBy clause
newBuilder.query.orderBy = orderBy

// Ensure we have an orderByIndex in the select if we have an orderBy
// This is required if select is called before orderBy
newBuilder.query.select = [
...(newBuilder.query.select ?? []),
{ _orderByIndex: { ORDER_INDEX: `numeric` } },
]

return newBuilder as QueryBuilder<TContext>
}

Expand Down
8 changes: 7 additions & 1 deletion packages/optimistic/tests/query/query-builder/key-by.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ describe(`QueryBuilder.keyBy`, () => {
expect(builtQuery.as).toBe(`e`)
expect(builtQuery.join).toBeDefined()
expect(builtQuery.where).toBeDefined()
expect(builtQuery.select).toHaveLength(3)
expect(builtQuery.select).toHaveLength(4)
expect(builtQuery.select).toEqual([
`@e.id`,
`@e.name`,
`@d.name`,
{ _orderByIndex: { ORDER_INDEX: `numeric` } }, // Added by the orderBy method
])
expect(builtQuery.orderBy).toBe(`@e.salary`)
expect(builtQuery.limit).toBe(10)
expect(builtQuery.offset).toBe(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ describe(`QueryBuilder orderBy, limit, and offset`, () => {
expect(builtQuery.as).toBe(`e`)
expect(builtQuery.join).toBeDefined()
expect(builtQuery.where).toBeDefined()
expect(builtQuery.select).toHaveLength(3)
expect(builtQuery.select).toEqual([
`@e.id`,
`@e.name`,
`@d.name`,
{ _orderByIndex: { ORDER_INDEX: `numeric` } }, // Added by the orderBy method
])
})
})
})
Loading
Loading