Skip to content

Commit

Permalink
fix(entity): entity filter xx != [] should set to { field, op: '!='…
Browse files Browse the repository at this point in the history
… , [{ listValue: [] }] } (#1056)
  • Loading branch information
spacedragon authored Nov 22, 2024
1 parent ee6a787 commit 9e79402
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
33 changes: 24 additions & 9 deletions packages/sdk/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StoreContext } from './context.js'
import { DatabaseSchema } from '../core/index.js'
import { BigDecimal } from '@sentio/bigdecimal'
import { AbstractEntity as Entity, Bytes, Float, ID, Int, Timestamp } from './types.js'
import type { DBRequest, Entity as EntityStruct } from '@sentio/protos'
import type { DBRequest, Entity as EntityStruct, RichValue } from '@sentio/protos'
import { DBRequest_DBOperator, DBResponse } from '@sentio/protos'
import { PluginManager } from '@sentio/runtime'
import { Cursor } from './cursor.js'
Expand Down Expand Up @@ -163,15 +163,30 @@ export class Store {
cursor,
pageSize,
filters:
filters?.map((f) => ({
field: f.field as string,
op: ops[f.op],
value: {
values: Array.isArray(f.value)
? f.value.map((v) => serializeRichValue(v))
: [serializeRichValue(f.value)]
filters?.map((f) => {
let values: RichValue[]
switch (f.op) {
case 'in':
case 'not in':
case 'has all':
case 'has any':
values = Array.isArray(f.value)
? f.value.map((v) => serializeRichValue(v))
: [serializeRichValue(f.value)]
break
default:
// = , != should set it to [[]]
values = [serializeRichValue(f.value)]
}
})) || []

return {
field: f.field as string,
op: ops[f.op],
value: {
values
}
}
}) || []
}
},
3600
Expand Down
26 changes: 26 additions & 0 deletions packages/sdk/src/store/tests/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ describe('Test Database', () => {
assert.deepEqual(tx2?.gasPrice, tx.gasPrice)
})

it('special filter test', async () => {
const store = new Store(storeContext)
await store.list(Transaction, [
{
field: 'arrayValue',
op: '!=',
value: []
}
])
assert.deepEqual(db.lastDbRequest?.list?.filters, [
{
field: 'arrayValue',
op: 1,
value: {
values: [
{
listValue: {
values: []
}
}
]
}
}
])
})

it('filter constraints', async () => {
const store = new Store(storeContext)
// wrong type won't compile
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/store/tests/memory-database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StoreContext } from '../context.js'
import { ProcessStreamResponse } from '@sentio/protos'
import { DBRequest, ProcessStreamResponse } from '@sentio/protos'

export class MemoryDatabase {
db = new Map<string, any>()

public lastDbRequest: DBRequest | undefined
constructor(readonly dbContext: StoreContext) {}

start() {
Expand All @@ -17,6 +17,7 @@ export class MemoryDatabase {

private processRequest(request: ProcessStreamResponse) {
const req = request.dbRequest
this.lastDbRequest = req
if (req) {
if (req.upsert) {
const { entityData, entity } = req.upsert
Expand Down

0 comments on commit 9e79402

Please sign in to comment.