@subsquid/openreader 0.2.0 → 0.3.3

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.
Files changed (128) hide show
  1. package/README.md +2 -15
  2. package/bin/main.js +2 -0
  3. package/dist/db.d.ts +28 -0
  4. package/dist/db.d.ts.map +1 -0
  5. package/dist/db.js +69 -0
  6. package/dist/db.js.map +1 -0
  7. package/dist/gql/opencrud.d.ts +1 -0
  8. package/dist/gql/opencrud.d.ts.map +1 -0
  9. package/dist/gql/opencrud.js +10 -9
  10. package/dist/gql/opencrud.js.map +1 -1
  11. package/dist/gql/schema.d.ts +1 -0
  12. package/dist/gql/schema.d.ts.map +1 -0
  13. package/dist/main.d.ts +2 -3
  14. package/dist/main.d.ts.map +1 -0
  15. package/dist/main.js +6 -30
  16. package/dist/main.js.map +1 -1
  17. package/dist/model.d.ts +1 -0
  18. package/dist/model.d.ts.map +1 -0
  19. package/dist/model.tools.d.ts +1 -0
  20. package/dist/model.tools.d.ts.map +1 -0
  21. package/dist/orderBy.d.ts +1 -0
  22. package/dist/orderBy.d.ts.map +1 -0
  23. package/dist/queryBuilder.d.ts +3 -2
  24. package/dist/queryBuilder.d.ts.map +1 -0
  25. package/dist/queryBuilder.js +28 -27
  26. package/dist/queryBuilder.js.map +1 -1
  27. package/dist/relayConnection.d.ts +1 -0
  28. package/dist/relayConnection.d.ts.map +1 -0
  29. package/dist/requestedFields.d.ts +1 -0
  30. package/dist/requestedFields.d.ts.map +1 -0
  31. package/dist/requestedFields.js +3 -3
  32. package/dist/requestedFields.js.map +1 -1
  33. package/dist/resolver.d.ts +3 -2
  34. package/dist/resolver.d.ts.map +1 -0
  35. package/dist/resolver.js +12 -11
  36. package/dist/resolver.js.map +1 -1
  37. package/dist/scalars.d.ts +2 -2
  38. package/dist/scalars.d.ts.map +1 -0
  39. package/dist/scalars.js +6 -2
  40. package/dist/scalars.js.map +1 -1
  41. package/dist/server.d.ts +5 -11
  42. package/dist/server.d.ts.map +1 -0
  43. package/dist/server.js +13 -31
  44. package/dist/server.js.map +1 -1
  45. package/dist/test/basic.test.d.ts +2 -0
  46. package/dist/test/basic.test.d.ts.map +1 -0
  47. package/dist/test/basic.test.js +286 -0
  48. package/dist/test/basic.test.js.map +1 -0
  49. package/dist/test/connection.test.d.ts +2 -0
  50. package/dist/test/connection.test.d.ts.map +1 -0
  51. package/dist/test/connection.test.js +193 -0
  52. package/dist/test/connection.test.js.map +1 -0
  53. package/dist/test/fts.test.d.ts +2 -0
  54. package/dist/test/fts.test.d.ts.map +1 -0
  55. package/dist/test/fts.test.js +110 -0
  56. package/dist/test/fts.test.js.map +1 -0
  57. package/dist/test/lists.test.d.ts +2 -0
  58. package/dist/test/lists.test.d.ts.map +1 -0
  59. package/dist/test/lists.test.js +266 -0
  60. package/dist/test/lists.test.js.map +1 -0
  61. package/dist/test/lookup.test.d.ts +2 -0
  62. package/dist/test/lookup.test.d.ts.map +1 -0
  63. package/dist/test/lookup.test.js +109 -0
  64. package/dist/test/lookup.test.js.map +1 -0
  65. package/dist/test/scalars.test.d.ts +2 -0
  66. package/dist/test/scalars.test.d.ts.map +1 -0
  67. package/dist/test/scalars.test.js +303 -0
  68. package/dist/test/scalars.test.js.map +1 -0
  69. package/dist/test/tools.test.d.ts +2 -0
  70. package/dist/test/tools.test.d.ts.map +1 -0
  71. package/dist/test/tools.test.js +49 -0
  72. package/dist/test/tools.test.js.map +1 -0
  73. package/dist/test/typed-json.test.d.ts +2 -0
  74. package/dist/test/typed-json.test.d.ts.map +1 -0
  75. package/dist/test/typed-json.test.js +75 -0
  76. package/dist/test/typed-json.test.js.map +1 -0
  77. package/dist/test/unions.test.d.ts +2 -0
  78. package/dist/test/unions.test.d.ts.map +1 -0
  79. package/dist/test/unions.test.js +84 -0
  80. package/dist/test/unions.test.js.map +1 -0
  81. package/dist/test/util/setup.d.ts +7 -0
  82. package/dist/test/util/setup.d.ts.map +1 -0
  83. package/dist/test/util/setup.js +60 -0
  84. package/dist/test/util/setup.js.map +1 -0
  85. package/dist/test/where.test.d.ts +2 -0
  86. package/dist/test/where.test.d.ts.map +1 -0
  87. package/dist/test/where.test.js +127 -0
  88. package/dist/test/where.test.js.map +1 -0
  89. package/dist/tools.d.ts +1 -0
  90. package/dist/tools.d.ts.map +1 -0
  91. package/dist/util.d.ts +1 -13
  92. package/dist/util.d.ts.map +1 -0
  93. package/dist/util.js +6 -79
  94. package/dist/util.js.map +1 -1
  95. package/dist/where.d.ts +1 -0
  96. package/dist/where.d.ts.map +1 -0
  97. package/package.json +26 -20
  98. package/src/db.ts +83 -0
  99. package/src/gql/opencrud.ts +328 -0
  100. package/src/gql/schema.ts +337 -0
  101. package/src/main.ts +51 -0
  102. package/src/model.tools.ts +173 -0
  103. package/src/model.ts +125 -0
  104. package/src/orderBy.ts +105 -0
  105. package/src/queryBuilder.ts +785 -0
  106. package/src/relayConnection.ts +80 -0
  107. package/src/requestedFields.ts +246 -0
  108. package/src/resolver.ts +199 -0
  109. package/src/scalars.ts +247 -0
  110. package/src/server.ts +115 -0
  111. package/src/test/basic.test.ts +339 -0
  112. package/src/test/connection.test.ts +195 -0
  113. package/src/test/fts.test.ts +114 -0
  114. package/src/test/lists.test.ts +278 -0
  115. package/src/test/lookup.test.ts +111 -0
  116. package/src/test/scalars.test.ts +316 -0
  117. package/src/test/tools.test.ts +27 -0
  118. package/src/test/typed-json.test.ts +76 -0
  119. package/src/test/unions.test.ts +85 -0
  120. package/src/test/util/setup.ts +63 -0
  121. package/src/test/where.test.ts +135 -0
  122. package/src/tools.ts +33 -0
  123. package/src/util.ts +39 -0
  124. package/src/where.ts +110 -0
  125. package/CHANGELOG.md +0 -16
  126. package/dist/transaction.d.ts +0 -10
  127. package/dist/transaction.js +0 -47
  128. package/dist/transaction.js.map +0 -1
package/src/main.ts ADDED
@@ -0,0 +1,51 @@
1
+ import {Pool} from "pg"
2
+ import {createPoolConfig} from "./db"
3
+ import {serve} from "./server"
4
+ import {loadModel} from "./tools"
5
+
6
+
7
+ module.exports = function main() {
8
+ let args = process.argv.slice(2)
9
+
10
+ if (args.indexOf('--help') >= 0) {
11
+ help()
12
+ process.exit(1)
13
+ }
14
+
15
+ if (args.length != 1) {
16
+ help()
17
+ process.exit(1)
18
+ }
19
+
20
+ let model = loadModel(args[0])
21
+ let db = new Pool(createPoolConfig())
22
+ let port = process.env.GRAPHQL_SERVER_PORT || 3000
23
+
24
+ serve({model, db, port}).then(
25
+ () => {
26
+ console.log('OpenReader is listening on port ' + port)
27
+ },
28
+ err => {
29
+ console.error(err)
30
+ process.exit(1)
31
+ }
32
+ )
33
+ }
34
+
35
+
36
+ function help() {
37
+ console.error(`
38
+ Usage: openreader SCHEMA
39
+
40
+ OpenCRUD GraphQL server.
41
+
42
+ Can be configured using the following environment variables:
43
+
44
+ DB_NAME
45
+ DB_USER
46
+ DB_PASS
47
+ DB_HOST
48
+ DB_PORT
49
+ GRAPHQL_SERVER_PORT
50
+ `)
51
+ }
@@ -0,0 +1,173 @@
1
+ import assert from "assert"
2
+ import {Entity, FTS_Query, JsonObject, Model, Prop, PropType} from "./model"
3
+
4
+
5
+ const UNION_MAPS = new WeakMap<Model, Record<string, JsonObject>>()
6
+
7
+
8
+ export function getUnionProps(model: Model, unionName: string): JsonObject {
9
+ let map = UNION_MAPS.get(model)
10
+ if (map == null) {
11
+ map = {}
12
+ UNION_MAPS.set(model, map)
13
+ }
14
+ if (map[unionName]) return map[unionName]
15
+ return map[unionName] = buildUnionProps(model, unionName)
16
+ }
17
+
18
+
19
+ export function buildUnionProps(model: Model, unionName: string): JsonObject {
20
+ let union = model[unionName]
21
+ assert(union.kind == 'union')
22
+ let properties: Record<string, Prop> = {}
23
+ for (let i = 0; i < union.variants.length; i++) {
24
+ let objectName = union.variants[i]
25
+ let object = model[objectName]
26
+ assert(object.kind == 'object')
27
+ Object.assign(properties, object.properties)
28
+ }
29
+ properties.isTypeOf = {
30
+ type: {kind: 'scalar', name: 'String'},
31
+ nullable: false
32
+ }
33
+ return {kind: 'object', properties}
34
+ }
35
+
36
+
37
+ export function validateModel(model: Model) {
38
+ // TODO: check all invariants we assume
39
+ validateNames(model)
40
+ validateUnionTypes(model)
41
+ validateLookups(model)
42
+ }
43
+
44
+
45
+ const TYPE_NAME_REGEX = /^[A-Z][a-zA-Z0-9]*$/
46
+ const PROP_NAME_REGEX = /^[a-z][a-zA-Z0-9]*$/
47
+
48
+
49
+ export function validateNames(model: Model) {
50
+ for (let name in model) {
51
+ let item = model[name]
52
+ if (item.kind == 'fts') {
53
+ if (!PROP_NAME_REGEX.test(name)) {
54
+ throw new Error(`Invalid fulltext search name: ${name}. It must match ${PROP_NAME_REGEX}.`)
55
+ }
56
+ } else {
57
+ if (!TYPE_NAME_REGEX.test(name)) {
58
+ throw new Error(`Invalid ${item.kind} name: ${name}. It must match ${TYPE_NAME_REGEX}`)
59
+ }
60
+ }
61
+ switch(item.kind) {
62
+ case 'entity':
63
+ case 'object':
64
+ case 'interface':
65
+ for (let prop in item.properties) {
66
+ if (!PROP_NAME_REGEX.test(prop)) {
67
+ throw new Error(`Type ${name} has a property with invalid name: ${prop}. It must match ${PROP_NAME_REGEX}.`)
68
+ }
69
+ }
70
+ break
71
+ }
72
+ }
73
+ }
74
+
75
+
76
+ export function validateUnionTypes(model: Model): void {
77
+ for (let key in model) {
78
+ let item = model[key]
79
+ if (item.kind != 'union') continue
80
+ let properties: Record<string, { objectName: string, type: PropType }> = {}
81
+ item.variants.forEach(objectName => {
82
+ let object = model[objectName]
83
+ assert(object.kind == 'object')
84
+ for (let propName in object.properties) {
85
+ let rec = properties[propName]
86
+ if (rec && !propTypeEquals(rec.type, object.properties[propName].type)) {
87
+ throw new Error(
88
+ `${rec.objectName} and ${objectName} variants of union ${key} both have property '${propName}', but types of ${rec.objectName}.${propName} and ${objectName}.${propName} are different.`
89
+ )
90
+ } else {
91
+ properties[propName] = {objectName, type: object.properties[propName].type}
92
+ }
93
+ }
94
+ })
95
+ }
96
+ }
97
+
98
+
99
+ export function validateLookups(model: Model): void {
100
+ for (let name in model) {
101
+ let item = model[name]
102
+ switch(item.kind) {
103
+ case 'object':
104
+ case 'interface':
105
+ for (let key in item.properties) {
106
+ let prop = item.properties[key]
107
+ if (prop.type.kind == 'lookup' || prop.type.kind == 'list-lookup') {
108
+ throw invalidProperty(name, key, `lookups are only supported on entity types`)
109
+ }
110
+ }
111
+ break
112
+ case 'entity':
113
+ for (let key in item.properties) {
114
+ let prop = item.properties[key]
115
+ if (prop.type.kind == 'lookup' && !prop.nullable) {
116
+ throw invalidProperty(name, key, 'one-to-one lookups must be nullable')
117
+ }
118
+ if (prop.type.kind == 'lookup' || prop.type.kind == 'list-lookup') {
119
+ let lookupEntity = getEntity(model, prop.type.entity)
120
+ let lookupProperty = lookupEntity.properties[prop.type.field]
121
+ if (lookupProperty?.type.kind != 'fk' || lookupProperty.type.foreignEntity != name) {
122
+ throw invalidProperty(name, key, `${prop.type.entity}.${prop.type.field} is not a foreign key pointing to ${name}`)
123
+ }
124
+ if (prop.type.kind == 'lookup' && !lookupProperty.unique) {
125
+ throw invalidProperty(name, key, `${prop.type.entity}.${prop.type.field} is not @unique`)
126
+ }
127
+ }
128
+ }
129
+ break
130
+ }
131
+ }
132
+ }
133
+
134
+
135
+ function invalidProperty(item: string, key: string, msg: string): Error {
136
+ return new Error(`Invalid property ${item}.${key}: ${msg}`)
137
+ }
138
+
139
+
140
+ export function propTypeEquals(a: PropType, b: PropType): boolean {
141
+ if (a.kind != b.kind) return false
142
+ if (a.kind == 'list') return propTypeEquals(a.item.type, (b as typeof a).item.type)
143
+ switch(a.kind) {
144
+ case 'fk':
145
+ return a.foreignEntity == (b as typeof a).foreignEntity
146
+ case 'lookup':
147
+ case 'list-lookup':
148
+ return a.entity == (b as typeof a).entity && a.field == (b as typeof a).field
149
+ default:
150
+ return a.name == (b as typeof a).name
151
+ }
152
+ }
153
+
154
+
155
+ export function getEntity(model: Model, name: string): Entity {
156
+ let entity = model[name]
157
+ assert(entity.kind == 'entity', `${name} expected to be an entity`)
158
+ return entity
159
+ }
160
+
161
+
162
+ export function getObject(model: Model, name: string): JsonObject {
163
+ let object = model[name]
164
+ assert(object.kind == 'object', `${name} expected to be an object`)
165
+ return object
166
+ }
167
+
168
+
169
+ export function getFtsQuery(model: Model, name: string): FTS_Query {
170
+ let query = model[name]
171
+ assert(query.kind == 'fts', `${name} expected to be FTS query`)
172
+ return query
173
+ }
package/src/model.ts ADDED
@@ -0,0 +1,125 @@
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
+ }
12
+
13
+
14
+ export interface JsonObject extends TypeMeta {
15
+ kind: 'object'
16
+ properties: Record<Name, Prop>
17
+ interfaces?: Name[]
18
+ }
19
+
20
+
21
+ export interface Interface extends TypeMeta {
22
+ kind: 'interface'
23
+ properties: Record<Name, Prop>
24
+ }
25
+
26
+
27
+ export interface Union extends TypeMeta {
28
+ kind: 'union'
29
+ variants: Name[]
30
+ }
31
+
32
+
33
+ export interface Enum extends TypeMeta {
34
+ kind: 'enum'
35
+ values: Record<string, {}>
36
+ }
37
+
38
+
39
+ export interface TypeMeta {
40
+ description?: string
41
+ }
42
+
43
+
44
+ export interface Prop {
45
+ type: PropType
46
+ nullable: boolean
47
+ description?: string
48
+ /**
49
+ * Whether the values in the column must be unique. Applicable only to entities.
50
+ */
51
+ unique?: boolean
52
+ }
53
+
54
+
55
+ export type PropType =
56
+ ScalarPropType |
57
+ EnumPropType |
58
+ ListPropType |
59
+ ObjectPropType |
60
+ UnionPropType |
61
+ FkPropType |
62
+ LookupPropType |
63
+ ListLookupPropType
64
+
65
+
66
+ export interface ScalarPropType {
67
+ kind: 'scalar'
68
+ name: Name
69
+ }
70
+
71
+
72
+ export interface EnumPropType {
73
+ kind: 'enum'
74
+ name: Name
75
+ }
76
+
77
+
78
+ export interface ObjectPropType {
79
+ kind: 'object'
80
+ name: Name
81
+ }
82
+
83
+
84
+ export interface UnionPropType {
85
+ kind: 'union'
86
+ name: Name
87
+ }
88
+
89
+
90
+ export interface ListPropType {
91
+ kind: 'list'
92
+ item: Prop
93
+ }
94
+
95
+
96
+ export interface FkPropType {
97
+ kind: 'fk'
98
+ foreignEntity: Name
99
+ }
100
+
101
+
102
+ export interface LookupPropType {
103
+ kind: 'lookup'
104
+ entity: Name
105
+ field: Name
106
+ }
107
+
108
+
109
+ export interface ListLookupPropType {
110
+ kind: 'list-lookup'
111
+ entity: Name
112
+ field: Name
113
+ }
114
+
115
+
116
+ export interface FTS_Query {
117
+ kind: 'fts'
118
+ sources: FTS_Source[]
119
+ }
120
+
121
+
122
+ export interface FTS_Source {
123
+ entity: Name
124
+ fields: Name[]
125
+ }
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
+ }