Skip to content
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

fix bug when sorting falsyBool + cursor #41

Merged
merged 2 commits into from
Nov 18, 2024
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
1 change: 0 additions & 1 deletion src/EventSourcingDB/EventSourcingDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
const reducers = {}
const migrationOptions = {queue: this.queue}

const dispatch = async (type, data, ts) => {

Check warning on line 275 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Move async arrow function 'dispatch' to the outer scope
if (this._processing) {
const store = this._alsDispatch.getStore()
dbg({store})
Expand Down Expand Up @@ -412,9 +412,9 @@
if (wantVersion > this._minVersion) this._minVersion = wantVersion
} else if (!this._isPolling) {
this._isPolling = true
// @ts-ignore

Check warning on line 415 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (module.hot) {
// @ts-ignore

Check warning on line 417 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
module.hot.dispose(() => {
this.stopPolling()
})
Expand Down Expand Up @@ -461,7 +461,7 @@
* @param {number} [ts] The timestamp of the event.
* @returns {Promise<Event>} The processed event.
*/
dispatch = makeDispatcher('dispatch', async (type, data, ts) => {

Check warning on line 464 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Move async arrow function to the outer scope
const event = await this.queue.add(type, data, ts)
return this.handledVersion(event.v)
})
Expand Down Expand Up @@ -543,7 +543,7 @@
}
}
if (o && process.env.NODE_ENV === 'test') {
// @ts-ignore

Check warning on line 546 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (!this.__BE_QUIET)
// eslint-disable-next-line no-console
console.error(
Expand Down Expand Up @@ -633,7 +633,7 @@
return _resultQueue.set(result)
})
.catch(error => {
// @ts-ignore

Check warning on line 636 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (!this.__BE_QUIET)
// eslint-disable-next-line no-console
console.error(
Expand All @@ -652,7 +652,7 @@

if (resultEvent.error) {
errorCount++
// @ts-ignore

Check warning on line 655 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (!this.__BE_QUIET) {
let path, error
// find the deepest error
Expand Down Expand Up @@ -697,7 +697,6 @@
}
}
return lastV
/* eslint-enable no-await-in-loop */
}

async _preprocessor(cache, event, isMainEvent) {
Expand Down Expand Up @@ -775,7 +774,7 @@
}
let out
try {
// @ts-ignore

Check warning on line 777 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
out = await model.reducer(helpers)
} catch (error) {
out = {
Expand Down Expand Up @@ -877,7 +876,7 @@
if (event.error) return event

// Allow GC
// @ts-ignore

Check warning on line 879 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
cache = null

event = await this._applyEvent(event, isMainEvent)
Expand Down Expand Up @@ -966,7 +965,7 @@
return addEvent(...args)
}
await settleAll(this._deriverModels, async model =>
// @ts-ignore

Check warning on line 968 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (16.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
model.deriver({
event,
model,
Expand Down
50 changes: 50 additions & 0 deletions src/JsonModel/JM-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,56 @@ describe('search cursor', () => {
total: 10,
})
})

test('search with sort, cursor, limit - limit < total, some sorted values = falsyBool', async () => {
const totalCount = await m.count()
const falsyBoolFalseCount = await m.count({x2: false})
const falsyBoolTrueCount = await m.count({x2: true})

const pageCount = falsyBoolTrueCount + 1
// to make sure there are results on page2
expect(falsyBoolFalseCount - pageCount).toBeTruthy()

const searchOptions = {
// true first
sort: {x2: -1},
limit: pageCount,
cols: ['id', 'x2'],
}
const page1 = await m.search({}, searchOptions)

expect(page1).toEqual({
cursor: '!_N~1',
items: [
{id: 2, x2: 1},
{id: 4, x2: 1},
{id: 6, x2: 1},
{id: 8, x2: 1},
{id: 10, x2: 1},
{id: 12, x2: 1},
{id: 14, x2: 1},
{id: 1},
],
prevCursor: '!!1~2',
total: 14,
})

const page2Count = await m.count(
{},
{...searchOptions, cursor: page1.cursor}
)
const remainingAfterPage1 = totalCount - pageCount
expect(page2Count).toEqual(
remainingAfterPage1 < pageCount ? remainingAfterPage1 : pageCount
)
const page2 = await m.search({}, {...searchOptions, cursor: page1.cursor})
expect(page2).toEqual({
cursor: undefined,
items: [{id: 3}, {id: 5}, {id: 7}, {id: 9}, {id: 11}, {id: 13}],
prevCursor: '!!_N~3',
total: 14,
})
})
})

test('search itemsOnly', async () => {
Expand Down
12 changes: 9 additions & 3 deletions src/JsonModel/JsonModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,15 @@ class JsonModelImpl {
cursorQ = `${cursorColAliases[len]}${getDir(len)}?`
cursorArgs = [cursorVals[len]] // ID added at first
for (let i = len - 1; i >= 0; i--) {
cursorQ =
`(${cursorColAliases[i]}${getDir(i)}=?` +
` AND (${cursorColAliases[i]}!=? OR ${cursorQ}))`
const colAlias = cursorColAliases[i]
const isFalsyBool = Object.values(this.columns).find(
c => c.alias === colAlias
).falsyBool
cursorQ = isFalsyBool
? `(COALESCE(${colAlias}, 0)${getDir(i)}=COALESCE(?, 0)` +
` AND (COALESCE(${colAlias},0)!=COALESCE(?, 0) OR ${cursorQ}))`
: `(${cursorColAliases[i]}${getDir(i)}=?` +
` AND (${cursorColAliases[i]}!=? OR ${cursorQ}))`
const val = cursorVals[i]
cursorArgs.unshift(val, val)
}
Expand Down
Loading