appy-one / acebase

A fast, low memory, transactional, index & query enabled NoSQL database engine and server for node.js and browser with realtime data change notifications
MIT License
491 stars 27 forks source link

Sorting is always ascending when using indexes and take() #124

Closed Mitsichury closed 2 years ago

Mitsichury commented 2 years ago

Hi :)

I've been through a weird behaviour when trying to sort data. Here a TU that reproduce the issue. Could you fix it please or tell me what do I do wrong please ? Thank you very much.

` /// const { DataReference, ObjectCollection } = require("acebase-core"); const { AceBase } = require(".."); const { createTempDB } = require("./tempdb");

describe("Query", () => { /* @type {AceBase} / let db; /* @type {{(): Promise}} / let removeDB;

/** @type {DataReference} */
let moviesRef;

afterAll(async () => {
    await removeDB();
});

beforeAll(async () => {
    ({ db, removeDB } = await createTempDB());
    db.indexes.create(`movies`, "name");
    db.indexes.create(`movies`, "amount");
    moviesRef = db.ref("movies");
    const entry1 = {
        id: "id1",
        name: "name",
        amount: 1,
    };
    const entry2 = {
        id: "id2",
        name: "another",
        amount: 10,
    };
    const entry3 = {
        id: "id3",
        name: "name",
        amount: 100,
    };

    const movies = [entry1, entry2, entry3];
    await moviesRef.set(ObjectCollection.from(movies));
});

it("error with indexes, order and take", async () => {
    const errors = await getDataBy(["name"]);
    expect(errors.getValues().map(({ id }) => id)).toEqual(["id3", "id1"]);
});

async function getDataBy(requestFilter) {
    const query = moviesRef.query();
    query.filter("name", "in", requestFilter);
    query.sort("amount", false);
    return await query.take(20).get();
}

}); `

appy-one commented 2 years ago

Thanks for reporting, I'll take a look!

appy-one commented 2 years ago

I found the issue, working on a fix

Mitsichury commented 2 years ago

Super fast, thank you very much ! :D

appy-one commented 2 years ago

I published the fix in v1.22.0!

Spread the word contribute Sponsor AceBase