@subsquid/openreader 0.2.1 → 0.4.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.
- package/README.md +2 -15
- package/bin/main.js +2 -0
- package/dist/db.d.ts +28 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +69 -0
- package/dist/db.js.map +1 -0
- package/dist/gql/opencrud.d.ts +1 -0
- package/dist/gql/opencrud.d.ts.map +1 -0
- package/dist/gql/opencrud.js +10 -9
- package/dist/gql/opencrud.js.map +1 -1
- package/dist/gql/schema.d.ts +1 -0
- package/dist/gql/schema.d.ts.map +1 -0
- package/dist/gql/schema.js +116 -17
- package/dist/gql/schema.js.map +1 -1
- package/dist/main.d.ts +2 -3
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +6 -30
- package/dist/main.js.map +1 -1
- package/dist/model.d.ts +9 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.tools.d.ts +2 -0
- package/dist/model.tools.d.ts.map +1 -0
- package/dist/model.tools.js +27 -1
- package/dist/model.tools.js.map +1 -1
- package/dist/orderBy.d.ts +1 -0
- package/dist/orderBy.d.ts.map +1 -0
- package/dist/queryBuilder.d.ts +3 -2
- package/dist/queryBuilder.d.ts.map +1 -0
- package/dist/queryBuilder.js +28 -27
- package/dist/queryBuilder.js.map +1 -1
- package/dist/relayConnection.d.ts +1 -0
- package/dist/relayConnection.d.ts.map +1 -0
- package/dist/requestedFields.d.ts +1 -0
- package/dist/requestedFields.d.ts.map +1 -0
- package/dist/requestedFields.js +3 -3
- package/dist/requestedFields.js.map +1 -1
- package/dist/resolver.d.ts +3 -2
- package/dist/resolver.d.ts.map +1 -0
- package/dist/resolver.js +12 -11
- package/dist/resolver.js.map +1 -1
- package/dist/scalars.d.ts +2 -2
- package/dist/scalars.d.ts.map +1 -0
- package/dist/scalars.js.map +1 -1
- package/dist/server.d.ts +5 -11
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +13 -31
- package/dist/server.js.map +1 -1
- package/dist/test/basic.test.d.ts +2 -0
- package/dist/test/basic.test.d.ts.map +1 -0
- package/dist/test/basic.test.js +286 -0
- package/dist/test/basic.test.js.map +1 -0
- package/dist/test/connection.test.d.ts +2 -0
- package/dist/test/connection.test.d.ts.map +1 -0
- package/dist/test/connection.test.js +193 -0
- package/dist/test/connection.test.js.map +1 -0
- package/dist/test/fts.test.d.ts +2 -0
- package/dist/test/fts.test.d.ts.map +1 -0
- package/dist/test/fts.test.js +110 -0
- package/dist/test/fts.test.js.map +1 -0
- package/dist/test/lists.test.d.ts +2 -0
- package/dist/test/lists.test.d.ts.map +1 -0
- package/dist/test/lists.test.js +266 -0
- package/dist/test/lists.test.js.map +1 -0
- package/dist/test/lookup.test.d.ts +2 -0
- package/dist/test/lookup.test.d.ts.map +1 -0
- package/dist/test/lookup.test.js +109 -0
- package/dist/test/lookup.test.js.map +1 -0
- package/dist/test/scalars.test.d.ts +2 -0
- package/dist/test/scalars.test.d.ts.map +1 -0
- package/dist/test/scalars.test.js +303 -0
- package/dist/test/scalars.test.js.map +1 -0
- package/dist/test/tools.test.d.ts +2 -0
- package/dist/test/tools.test.d.ts.map +1 -0
- package/dist/test/tools.test.js +49 -0
- package/dist/test/tools.test.js.map +1 -0
- package/dist/test/typed-json.test.d.ts +2 -0
- package/dist/test/typed-json.test.d.ts.map +1 -0
- package/dist/test/typed-json.test.js +75 -0
- package/dist/test/typed-json.test.js.map +1 -0
- package/dist/test/unions.test.d.ts +2 -0
- package/dist/test/unions.test.d.ts.map +1 -0
- package/dist/test/unions.test.js +84 -0
- package/dist/test/unions.test.js.map +1 -0
- package/dist/test/util/setup.d.ts +7 -0
- package/dist/test/util/setup.d.ts.map +1 -0
- package/dist/test/util/setup.js +60 -0
- package/dist/test/util/setup.js.map +1 -0
- package/dist/test/where.test.d.ts +2 -0
- package/dist/test/where.test.d.ts.map +1 -0
- package/dist/test/where.test.js +127 -0
- package/dist/test/where.test.js.map +1 -0
- package/dist/tools.d.ts +1 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/util.d.ts +1 -13
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +6 -79
- package/dist/util.js.map +1 -1
- package/dist/where.d.ts +1 -0
- package/dist/where.d.ts.map +1 -0
- package/package.json +26 -20
- package/src/db.ts +83 -0
- package/src/gql/opencrud.ts +328 -0
- package/src/gql/schema.ts +463 -0
- package/src/main.ts +51 -0
- package/src/model.tools.ts +201 -0
- package/src/model.ts +137 -0
- package/src/orderBy.ts +105 -0
- package/src/queryBuilder.ts +785 -0
- package/src/relayConnection.ts +80 -0
- package/src/requestedFields.ts +246 -0
- package/src/resolver.ts +199 -0
- package/src/scalars.ts +247 -0
- package/src/server.ts +115 -0
- package/src/test/basic.test.ts +339 -0
- package/src/test/connection.test.ts +195 -0
- package/src/test/fts.test.ts +114 -0
- package/src/test/lists.test.ts +278 -0
- package/src/test/lookup.test.ts +111 -0
- package/src/test/scalars.test.ts +316 -0
- package/src/test/tools.test.ts +27 -0
- package/src/test/typed-json.test.ts +76 -0
- package/src/test/unions.test.ts +85 -0
- package/src/test/util/setup.ts +63 -0
- package/src/test/where.test.ts +135 -0
- package/src/tools.ts +33 -0
- package/src/util.ts +39 -0
- package/src/where.ts +110 -0
- package/CHANGELOG.md +0 -20
- package/dist/transaction.d.ts +0 -10
- package/dist/transaction.js +0 -47
- package/dist/transaction.js.map +0 -1
package/src/model.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
export type Name = string
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export type Model = Record<Name, Entity | JsonObject | Interface | Union | Enum | FTS_Query>
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export interface Entity extends TypeMeta {
|
|
8
|
+
kind: 'entity'
|
|
9
|
+
properties: Record<Name, Prop>
|
|
10
|
+
interfaces?: Name[]
|
|
11
|
+
indexes?: Index[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export interface JsonObject extends TypeMeta {
|
|
16
|
+
kind: 'object'
|
|
17
|
+
properties: Record<Name, Prop>
|
|
18
|
+
interfaces?: Name[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export interface Interface extends TypeMeta {
|
|
23
|
+
kind: 'interface'
|
|
24
|
+
properties: Record<Name, Prop>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
export interface Union extends TypeMeta {
|
|
29
|
+
kind: 'union'
|
|
30
|
+
variants: Name[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
export interface Enum extends TypeMeta {
|
|
35
|
+
kind: 'enum'
|
|
36
|
+
values: Record<string, {}>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
export interface TypeMeta {
|
|
41
|
+
description?: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export interface Prop {
|
|
46
|
+
type: PropType
|
|
47
|
+
nullable: boolean
|
|
48
|
+
description?: string
|
|
49
|
+
/**
|
|
50
|
+
* Whether the values in the column must be unique. Applicable only to entities.
|
|
51
|
+
*/
|
|
52
|
+
unique?: boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
export type PropType =
|
|
57
|
+
ScalarPropType |
|
|
58
|
+
EnumPropType |
|
|
59
|
+
ListPropType |
|
|
60
|
+
ObjectPropType |
|
|
61
|
+
UnionPropType |
|
|
62
|
+
FkPropType |
|
|
63
|
+
LookupPropType |
|
|
64
|
+
ListLookupPropType
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
export interface ScalarPropType {
|
|
68
|
+
kind: 'scalar'
|
|
69
|
+
name: Name
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
export interface EnumPropType {
|
|
74
|
+
kind: 'enum'
|
|
75
|
+
name: Name
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
export interface ObjectPropType {
|
|
80
|
+
kind: 'object'
|
|
81
|
+
name: Name
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
export interface UnionPropType {
|
|
86
|
+
kind: 'union'
|
|
87
|
+
name: Name
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
export interface ListPropType {
|
|
92
|
+
kind: 'list'
|
|
93
|
+
item: Prop
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
export interface FkPropType {
|
|
98
|
+
kind: 'fk'
|
|
99
|
+
foreignEntity: Name
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
export interface LookupPropType {
|
|
104
|
+
kind: 'lookup'
|
|
105
|
+
entity: Name
|
|
106
|
+
field: Name
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
export interface ListLookupPropType {
|
|
111
|
+
kind: 'list-lookup'
|
|
112
|
+
entity: Name
|
|
113
|
+
field: Name
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
export interface FTS_Query {
|
|
118
|
+
kind: 'fts'
|
|
119
|
+
sources: FTS_Source[]
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
export interface FTS_Source {
|
|
124
|
+
entity: Name
|
|
125
|
+
fields: Name[]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
export interface Index {
|
|
130
|
+
fields: IndexField[]
|
|
131
|
+
unique?: boolean
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
export interface IndexField {
|
|
136
|
+
name: string
|
|
137
|
+
}
|
package/src/orderBy.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import assert from "assert"
|
|
2
|
+
import type {Model} from "./model"
|
|
3
|
+
import {getUnionProps} from "./model.tools"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export type SortOrder = 'ASC' | 'DESC'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export interface OrderBy {
|
|
10
|
+
[field: string]: SortOrder | OrderBy
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* OpenCRUD orderBy enum value (e.g. foo_ASC)
|
|
16
|
+
*/
|
|
17
|
+
export type OpenCrudOrderByValue = string
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A mapping between OpenCRUD enum variants and OrderBy specs
|
|
22
|
+
*/
|
|
23
|
+
export type OpenCrud_OrderBy_Mapping = ReadonlyMap<OpenCrudOrderByValue, OrderBy>
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const MAPPING_CACHE = new WeakMap<Model, Record<string, OpenCrud_OrderBy_Mapping>>()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
export function getOrderByMapping(model: Model, entityName: string): OpenCrud_OrderBy_Mapping {
|
|
30
|
+
let cache = MAPPING_CACHE.get(model)
|
|
31
|
+
if (cache == null) {
|
|
32
|
+
cache = {}
|
|
33
|
+
MAPPING_CACHE.set(model, cache)
|
|
34
|
+
}
|
|
35
|
+
if (cache[entityName]) return cache[entityName]
|
|
36
|
+
return cache[entityName] = buildOrderByMapping(model, entityName, 2)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
function buildOrderByMapping(model: Model, typeName: string, depth: number): OpenCrud_OrderBy_Mapping {
|
|
41
|
+
if (depth <= 0) return new Map()
|
|
42
|
+
let object = model[typeName]
|
|
43
|
+
if (object.kind == 'union') {
|
|
44
|
+
object = getUnionProps(model, typeName)
|
|
45
|
+
}
|
|
46
|
+
assert(object.kind == 'entity' || object.kind == 'object')
|
|
47
|
+
let m = new Map<string, OrderBy>()
|
|
48
|
+
for (let key in object.properties) {
|
|
49
|
+
let propType = object.properties[key].type
|
|
50
|
+
switch(propType.kind) {
|
|
51
|
+
case 'scalar':
|
|
52
|
+
case 'enum':
|
|
53
|
+
m.set(key + '_ASC', {[key]: 'ASC'})
|
|
54
|
+
m.set(key + '_DESC', {[key]: 'DESC'})
|
|
55
|
+
break
|
|
56
|
+
case 'object':
|
|
57
|
+
case 'union':
|
|
58
|
+
for (let [name, spec] of buildOrderByMapping(model, propType.name, depth - 1)) {
|
|
59
|
+
m.set(key + '_' + name, {[key]: spec})
|
|
60
|
+
}
|
|
61
|
+
break
|
|
62
|
+
case 'fk':
|
|
63
|
+
for (let [name, spec] of buildOrderByMapping(model, propType.foreignEntity, depth - 1)) {
|
|
64
|
+
m.set(key + '_' + name, {[key]: spec})
|
|
65
|
+
}
|
|
66
|
+
break
|
|
67
|
+
case 'lookup':
|
|
68
|
+
for (let [name, spec] of buildOrderByMapping(model, propType.entity, depth - 1)) {
|
|
69
|
+
m.set(key + '_' + name, {[key]: spec})
|
|
70
|
+
}
|
|
71
|
+
break
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return m
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
export function parseOrderBy(model: Model, entityName: string, input: OpenCrudOrderByValue[]): OrderBy {
|
|
79
|
+
let mapping = getOrderByMapping(model, entityName)
|
|
80
|
+
return mergeOrderBy(
|
|
81
|
+
input.map(value => {
|
|
82
|
+
let spec = mapping.get(value)
|
|
83
|
+
assert(spec != null)
|
|
84
|
+
return spec
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
export function mergeOrderBy(list: OrderBy[]): OrderBy {
|
|
91
|
+
let result: OrderBy = {}
|
|
92
|
+
list.forEach(item => {
|
|
93
|
+
for (let key in item) {
|
|
94
|
+
let current = result[key]
|
|
95
|
+
if (current == null ) {
|
|
96
|
+
result[key] = item[key]
|
|
97
|
+
} else if (typeof current != 'string') {
|
|
98
|
+
let it = item[key]
|
|
99
|
+
assert(typeof it == 'object')
|
|
100
|
+
result[key] = mergeOrderBy([current, it])
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
return result
|
|
105
|
+
}
|