ujjwalguptaofficial/JsStore

the order of data insertion is inconsistent with the order of query results

a1067111756 opened this issue · 1 comments

Title

the order of data insertion is inconsistent with the order of query results

Description

Question: I tried to insert data using the insert method, and then query the data using the select method, and found that the order of the inserted data was inconsistent with the order of the query result data

Table schema:
const task = { name: 'task', columns: { uuid: { primaryKey: true, dataType: DATA_TYPE.String }, userId: { notNull: true, dataType: DATA_TYPE.Number }, parentUuid: { notNull: false, dataType: DATA_TYPE.String }, projectName: { notNull: true, dataType: DATA_TYPE.String }, projectDesc: { notNull: false, default: '', dataType: DATA_TYPE.String }, version: { notNull: false, default: 0, dataType: DATA_TYPE.Number }, createTime: { notNull: false, default: 0, dataType: DATA_TYPE.String }, updateTime: { notNull: false, default: 0, dataType: DATA_TYPE.String }, isDefault: { notNull: false, default: 0, dataType: DATA_TYPE.Boolean }, deleted: { notNull: false, default: 0, dataType: DATA_TYPE.Number }, mapData: { notNull: false, default: [], dataType: DATA_TYPE.Object } } }

Code:
`
// insert data to indexDb
connection.insert({
into: 'task',
values: values
})

// query data from indexDb
connection
.select({
from: 'task',
where: {
userId: currentUserId
},
})
Performance: // insertData[
{
"uuid": "50dc6f74-99bf-42ec-b4f5-b739fe9aed06",
"userId": 336,
"parentUuid": "",
"projectName": "OvitalMap_20220119_120314",
"projectDesc": "",
"version": 0,
"isDefault": false,
"createTime": "2022-02-16 11:08:37",
"updateTime": "2022-02-16 11:08:37",
"deleted": 0,
"mapData": {
"features": []
}
},
{
"uuid": "080cbad5-275a-4621-8709-5431d028b815",
"userId": 336,
"parentUuid": "50dc6f74-99bf-42ec-b4f5-b739fe9aed06",
"projectName": "新建文件夹",
"projectDesc": "",
"version": 0,
"isDefault": false,
"createTime": "2022-02-16 11:08:37",
"updateTime": "2022-02-16 11:08:37",
"deleted": 0,
"mapData": {
"features": []
}
}
]`

// data in indexDb
[ { "uuid": "080cbad5-275a-4621-8709-5431d028b815", "userId": 336, "parentUuid": "50dc6f74-99bf-42ec-b4f5-b739fe9aed06", "projectName": "新建文件夹", "projectDesc": "", "version": 0, "isDefault": false, "createTime": "2022-02-16 11:08:37", "updateTime": "2022-02-16 11:08:37", "deleted": 0, "mapData": { "features": [] } }, { "uuid": "50dc6f74-99bf-42ec-b4f5-b739fe9aed06", "userId": 336, "parentUuid": "", "projectName": "OvitalMap_20220119_120314", "projectDesc": "", "version": 0, "isDefault": false, "createTime": "2022-02-16 11:08:37", "updateTime": "2022-02-16 11:08:37", "deleted": 0, "mapData": { "features": [] } } ]

// select result
[ { "uuid": "080cbad5-275a-4621-8709-5431d028b815", "userId": 336, "parentUuid": "50dc6f74-99bf-42ec-b4f5-b739fe9aed06", "projectName": "新建文件夹", "projectDesc": "", "version": 0, "isDefault": false, "createTime": "2022-02-16 11:08:37", "updateTime": "2022-02-16 11:08:37", "deleted": 0, "mapData": { "features": [] } }, { "uuid": "50dc6f74-99bf-42ec-b4f5-b739fe9aed06", "userId": 336, "parentUuid": "", "projectName": "OvitalMap_20220119_120314", "projectDesc": "", "version": 0, "isDefault": false, "createTime": "2022-02-16 11:08:37", "updateTime": "2022-02-16 11:08:37", "deleted": 0, "mapData": { "features": [] } } ]

My guess:
I found that when data is inserted into indexdb, the order is not in the order of inserting data, but in the order of primaryKey, which causes the order of select results to be inconsistent with the order of insertion。

Please help:
In the scenario of my case, the order of the selected results needs to be consistent with the order of my insertion. This happens because the primaryKey is a randomly generated uuid. I tried to use the order option to sort when selecting, but it didn't work well. At the same time, I think the insertion order is a more important key, and the database should not do its own logical ordering. Is there any way I can solve this problem?

Note: Description should contains the query, the current output & the expected output. You can use idbstudio to generate the example.

It's common theory now that db does their own logical ordering known as indexing. Mostly its by primary key.

So its not possible to change the stored data sorting by user. You can only use sort for sorting data to fulfill your need.

Please let me know, what is wrong with sort query.