Reputation: 6260
I just discovered RxDb, which as far as I understand is running PouchDB underneath, in any case, I have defined a fairly simple schema for an entity:
let nodeSchema: RxJsonSchema<NodeDocType> = {
version: 0,
title: `node schema`,
description: `Describes a node`,
type: `object`,
properties: {
id: {
type: `string`,
primary: true,
},
type: {
type: `string`,
},
createdAt: {
type: `number`,
},
updatedAt: {
type: `number`,
},
text: {
type: `string`,
},
name: {
type: `string`,
},
originalUrl: {
type: `string`,
},
localUrl: {
type: `string`,
},
webUrl: {
type: `string`,
},
extension: {
type: `string`,
},
},
required: [`type`, `createdAt`, `updatedAt`],
}
now the problem is, when I query the collection from my component and try to apply sorting, like this:
db.nodes
.find()
.sort({
createdAt: `asc`
})
.$.subscribe((nodes) => {
if (!nodes) {
return
}
setNodes(nodes)
})
I get an error saying cannot sort on field(s) XXX when using the default index
, from what I have seen so far, this has to do with the field not being part of the selector? but it is still confusing and have not been able to get it working, does anybody have an idea what am I doing wrong?
Upvotes: 3
Views: 1293
Reputation: 851
https://rxdb.info/rx-schema.html#indexes
You need to modify your schema to include an index on properties that you want to sort by.
let nodeSchema: RxJsonSchema<NodeDocType> = {
version: 0,
title: `node schema`,
description: `Describes a node`,
type: `object`,
properties: {
createdAt: {
type: `number`,
},
},
required: [`type`, `createdAt`, `updatedAt`],
indexes: ['createdAt']
}
Upvotes: 1