@subsquid/openreader 0.3.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  import {toSnakeCase} from "@subsquid/util"
2
2
  import assert from "assert"
3
- import type {ClientBase, QueryArrayResult} from "pg"
4
3
  import {Database} from "./db"
5
4
  import type {Entity, JsonObject, Model} from "./model"
6
5
  import {getEntity, getFtsQuery, getObject, getUnionProps} from "./model.tools"
@@ -81,7 +80,7 @@ export class QueryBuilder {
81
80
  break
82
81
  default:
83
82
  if (columns.size()) {
84
- out += `SELECT ${columns.render()}\n`
83
+ out += `SELECT ${columns.render(true)}\n`
85
84
  }
86
85
  }
87
86
 
@@ -304,6 +303,15 @@ export class QueryBuilder {
304
303
 
305
304
  private addPropCondition(exps: string[], cursor: Cursor, field: string, op: WhereOp, arg: any): void {
306
305
  let propType = cursor.object.properties[field].type
306
+ if (op == 'isNull') {
307
+ let lhs = propType.kind == 'fk' ? cursor.fk(field) : cursor.field(field)
308
+ if (arg) {
309
+ exps.push(`${lhs} IS NULL`)
310
+ } else {
311
+ exps.push(`${lhs} IS NOT NULL`)
312
+ }
313
+ return
314
+ }
307
315
  switch(propType.kind) {
308
316
  case 'scalar':
309
317
  case 'enum': {
@@ -724,8 +732,12 @@ class ColumnSet {
724
732
  return idx
725
733
  }
726
734
 
727
- render(): string {
728
- return Array.from(this.columns.keys()).join(', ')
735
+ render(withAliases?: boolean): string {
736
+ let cols = Array.from(this.columns.keys())
737
+ if (withAliases) {
738
+ cols = cols.map((col, idx) => `${col} AS _c${idx}`)
739
+ }
740
+ return cols.join(', ')
729
741
  }
730
742
 
731
743
  size(): number {
@@ -0,0 +1,79 @@
1
+ import {useDatabase, useServer} from "./util/setup"
2
+
3
+
4
+ describe('isNull operator', function() {
5
+ useDatabase([
6
+ `create table meta (id text primary key)`,
7
+ `create table entity (id text primary key, scalar text, json jsonb, meta_id text)`,
8
+ `insert into meta (id) values ('1')`,
9
+ `insert into entity (id, json, meta_id) values ('1', '{"a": 1}', '1')`,
10
+ `insert into entity (id, scalar, meta_id) values ('2', 'foo', '1')`,
11
+ `insert into entity (id, scalar, json) values ('3', 'foo', '{"a": 2}')`,
12
+ `insert into entity (id, scalar, json, meta_id) values ('4', 'foo', '{}', '1')`,
13
+ ])
14
+
15
+ const client = useServer(`
16
+ type Meta @entity {
17
+ id: ID!
18
+ }
19
+
20
+ type Entity @entity {
21
+ id: ID!
22
+ scalar: String
23
+ json: JsonObject
24
+ meta: Meta
25
+ }
26
+
27
+ type JsonObject {
28
+ a: Int
29
+ }
30
+ `)
31
+
32
+ it("on scalar", function() {
33
+ return client.test(`
34
+ query {
35
+ entities(where: {scalar_isNull: true}) {
36
+ id
37
+ }
38
+ }
39
+ `, {
40
+ entities: [{id: '1'}]
41
+ })
42
+ })
43
+
44
+ it("on json", function() {
45
+ return client.test(`
46
+ query {
47
+ entities(where: {json_isNull: true}) {
48
+ id
49
+ }
50
+ }
51
+ `, {
52
+ entities: [{id: '2'}]
53
+ })
54
+ })
55
+
56
+ it("on nested json prop", function() {
57
+ return client.test(`
58
+ query {
59
+ entities(where: {json: {a_isNull: true}} orderBy: id_ASC) {
60
+ id
61
+ }
62
+ }
63
+ `, {
64
+ entities: [{id: '2'}, {id: '4'}]
65
+ })
66
+ })
67
+
68
+ it("on fk", function() {
69
+ return client.test(`
70
+ query {
71
+ entities(where: {meta_isNull: true}) {
72
+ id
73
+ }
74
+ }
75
+ `, {
76
+ entities: [{id: '3'}]
77
+ })
78
+ })
79
+ })
package/src/where.ts CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  export type WhereOp =
3
3
  '-' | // no operator
4
+ 'isNull' |
4
5
  'eq' | 'not_eq' |
5
6
  'gt' |
6
7
  'gte' |
@@ -19,6 +20,7 @@ export type WhereOp =
19
20
 
20
21
 
21
22
  const ENDINGS = [
23
+ 'isNull',
22
24
  'eq',
23
25
  'not_eq',
24
26
  'gt',