@live-change/simple-query 0.9.142 → 0.9.143

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 (2) hide show
  1. package/package.json +3 -3
  2. package/src/query.ts +68 -27
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/simple-query",
3
- "version": "0.9.142",
3
+ "version": "0.9.143",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "type": "module",
24
24
  "dependencies": {
25
- "@live-change/framework": "^0.9.142",
25
+ "@live-change/framework": "^0.9.143",
26
26
  "pluralize": "^8.0.0"
27
27
  },
28
28
  "devDependencies": {
@@ -30,5 +30,5 @@
30
30
  "typedoc-plugin-markdown": "^4.6.3",
31
31
  "typedoc-plugin-rename-defaults": "^0.7.3"
32
32
  },
33
- "gitHead": "efc249d458250a4d1cb9bd5ff847de066452dc1c"
33
+ "gitHead": "5d1d50252b6abcf7149a961ac0b4c521c1c53b56"
34
34
  }
package/src/query.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type {
1
+ import {
2
2
  PropertyDefinitionSpecification, ServiceDefinition, ServiceDefinitionSpecification
3
3
  } from "@live-change/framework"
4
4
 
@@ -81,10 +81,9 @@ interface QueryDefinitionSpecification {
81
81
  name: string
82
82
  properties: Record<string, PropertyDefinitionSpecification>
83
83
  returns?: PropertyDefinitionSpecification,
84
- sources: Record<string, QuerySource>,
85
- code: QueryCode,
86
- sourceName: string,
87
- update: boolean,
84
+ sources?: Record<string, QuerySource>,
85
+ code?: QueryCode,
86
+ update?: boolean,
88
87
  }
89
88
 
90
89
  class OutputMapping {
@@ -112,23 +111,31 @@ class IndexInfo {
112
111
  rules: QueryRule[]
113
112
  indexParts: OutputMapping[]
114
113
  name: string
114
+ sources: QuerySource[]
115
115
 
116
- constructor(rules: QueryRule[], indexParts: OutputMapping[], name: string = undefined) {
116
+ constructor(rules: QueryRule[], indexParts: OutputMapping[], service: ServiceDefinition<any>, name: string = undefined) {
117
117
  this.rules = rules
118
118
  this.indexParts = indexParts
119
- const allSources = rules.map(rule => rule.$_getSources()).flat()
120
- const firstSource = allSources[0]
121
- if(allSources.every(source => source === firstSource)) this.singleSource = firstSource
119
+ this.sources = rules.map(rule => rule.$_getSources()).flat()
120
+ const firstSource = this.sources[0]
121
+ if(this.sources.every(source => source === firstSource)) this.singleSource = firstSource
122
122
  if(this.singleSource) {
123
- this.name = name || (this.singleSource.input.$source.getTypeName() + "_" + (indexParts
124
- .map(part => part.path.join(".")).join("_")))
123
+ this.name = name || ''
124
+ + (this.singleSource.input.$source.serviceName === service.name ? "" : this.singleSource.input.$source.serviceName + "_")
125
+ + (
126
+ this.singleSource.input.$source.getTypeName() + "_" + (indexParts
127
+ .map(part => part.path.join(".")).join("_"))
128
+ )
125
129
  } else {
126
- this.name = name || indexParts
127
- .map(part => {
128
- const source = allSources.find(source => source.input.$alias === part.result)
130
+ this.name = name || ''
131
+ + (this.sources.every(source => source.input.$source.serviceName === service.name) ? "" : service.name + "_")
132
+ + (
133
+ indexParts.map(part => {
134
+ const source = this.sources.find(source => source.input.$alias === part.result)
129
135
  if(!source) throw new Error("Source not found for index part: " + part)
130
136
  return source.input.$source.getTypeName() + "_" + part.path.join(".")
131
137
  }).join("_")
138
+ )
132
139
  }
133
140
  }
134
141
 
@@ -160,6 +167,13 @@ class IndexInfo {
160
167
  alias: this.singleSource.input.$alias + 'Indexed'
161
168
  }
162
169
  }
170
+
171
+ $_createQuery(service: ServiceDefinition<any>) {
172
+ return new QueryDefinition(service, {
173
+ name: this.name,
174
+ properties: {}
175
+ }, this.rules)
176
+ }
163
177
  }
164
178
 
165
179
  const staticRuleSymbol = Symbol("static")
@@ -183,7 +197,9 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
183
197
  executionPlan: ExecutionStep[]
184
198
  indexPlan: ExecutionStep[]
185
199
 
186
- constructor(serviceDefinition: ServiceDefinition<SDS>, definition: QueryDefinitionSpecification) {
200
+ constructor(
201
+ serviceDefinition: ServiceDefinition<SDS>, definition: QueryDefinitionSpecification, rules: QueryRule[] = undefined
202
+ ) {
187
203
  this.service = serviceDefinition
188
204
  this.definition = definition
189
205
 
@@ -194,15 +210,20 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
194
210
  )
195
211
  )
196
212
 
197
- this.computeRules()
213
+ if(rules) {
214
+ this.rules = rules
215
+ } else {
216
+ this.computeRules()
217
+ }
218
+
198
219
  this.markStaticRules()
199
220
 
200
- this.printRules()
221
+ //this.printRules()
201
222
 
202
223
  this.computeDependencies()
203
224
  this.computeIndexes()
204
225
 
205
- this.printDependencies()
226
+ //this.printDependencies()
206
227
 
207
228
  }
208
229
 
@@ -332,7 +353,7 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
332
353
  computeIndexes() {
333
354
  this.indexes = []
334
355
  for(const ruleSource of this.ruleSources) {
335
- const potentialIndex = ruleSource.input.$_getIndexInfo(this.indexes)
356
+ const potentialIndex = ruleSource.input.$_getIndexInfo(this.indexes, this.service)
336
357
  if(!potentialIndex) continue
337
358
  const existingIndex = this.indexes.find(index => index.equals(potentialIndex))
338
359
  if(existingIndex) {
@@ -437,7 +458,7 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
437
458
  return executionPlan
438
459
  }
439
460
 
440
- computeIndexPlan() {
461
+ computeIndexPlan(endMapping: OutputMapping[] = undefined) {
441
462
  const indexSources = []
442
463
  for(const ruleSource of this.ruleSources) {
443
464
  const source = ruleSource.input.$source
@@ -469,15 +490,24 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
469
490
  }
470
491
  }).filter(s => s !== null)
471
492
 
472
- const mapping = {
493
+ const baseMapping = {
473
494
  [alias]: [firstFetch.alias],
474
495
  }
475
496
  for(const processedSource of processed) {
476
- mapping[processedSource.input.$alias] = [processedSource.input.$alias]
497
+ baseMapping[processedSource.input.$alias] = [processedSource.input.$alias]
498
+ }
499
+ let mapping = baseMapping
500
+ if(endMapping) {
501
+ mapping = {}
502
+ for(const mp of endMapping) {
503
+ const base = baseMapping[mp.result]
504
+ if(!base) throw new Error("Base mapping "+mp.result+" not found")
505
+ mapping[mp.alias] = base.concat(mp.path)
506
+ }
477
507
  }
478
508
  const execution = {
479
509
  operation: 'output',
480
- mapping
510
+ mapping
481
511
  }
482
512
  return {
483
513
  execution,
@@ -494,6 +524,11 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
494
524
  prepareQuery() {
495
525
  console.log("CREATE INDEXES", this.indexes)
496
526
 
527
+ for(const index of this.indexes) {
528
+ const indexQuery = index.$_createQuery(this.service)
529
+ indexQuery.createIndex(index.name, index.indexParts)
530
+ }
531
+
497
532
  process.exit(0)
498
533
 
499
534
  this.computeExecutionPlan()
@@ -507,9 +542,15 @@ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
507
542
  process.exit(0)
508
543
  }
509
544
 
510
- createIndex() {
511
- this.computeIndexPlan()
545
+ createIndex(name, mapping: OutputMapping[]) {
546
+ console.log("CREATE INDEX", name)
547
+ this.printRules()
548
+ console.log("OUTPUT MAPPINGS", mapping)
549
+ this.computeIndexPlan(mapping)
550
+ console.log("INDEX PLAN", JSON.stringify(this.indexPlan, null, 2))
512
551
  /// TODO: create index from query
552
+
553
+ process.exit(0)
513
554
  }
514
555
  }
515
556
 
@@ -733,7 +774,7 @@ export class QueryInputBase extends CanBeStatic {
733
774
  return this.$source === input.$source && this.$alias === input.$alias
734
775
  }
735
776
 
736
- $_getIndexInfo(indexes: IndexInfo[]): IndexInfo | null {
777
+ $_getIndexInfo(indexes: IndexInfo[], serviceDefinition: ServiceDefinition<any>): IndexInfo | null {
737
778
  if(this.$path.length === 0) return null // id is used
738
779
  if(this.$path.length === 1 && this.$path[0] === "id") return null // id is used
739
780
  return new IndexInfo([
@@ -741,7 +782,7 @@ export class QueryInputBase extends CanBeStatic {
741
782
  ], [
742
783
  new OutputMapping(this.$alias, this.$path, this.$path[this.$path.length - 1]),
743
784
  new OutputMapping(this.$alias, ['id'], 'to')
744
- ])
785
+ ], serviceDefinition)
745
786
  }
746
787
 
747
788
  $_executionJSON() {