@live-change/simple-query 0.9.138 → 0.9.141

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/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019-2024 Michał Łaszczewski
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -27,9 +27,9 @@ const channelMessagesWithUsersAndIdentificationByTime = query({ // definition
27
27
  code(props, { user, message, identification }) => {
28
28
  const { channel, ...range } = props
29
29
  message.time.inside(range)
30
- message.channel.eq(channel)
31
- user.id.eq(message.author)
32
- identification.id.eq(user.id)
30
+ message.channel.eqals(channel)
31
+ user.id.equals(message.au thor)
32
+ identification.id.equals(user.id)
33
33
  }
34
34
  })
35
35
  ```
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import queryFactory from "./query.js"
2
+
3
+ export default queryFactory
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/simple-query",
3
- "version": "0.9.138",
3
+ "version": "0.9.141",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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": "b1b605b7f1fa4fc3de4720afbb401e2cfff080cf"
33
+ "gitHead": "2e34ef5af52ba93eaeb317a8a062b898db0673b8"
34
34
  }
package/query.ts ADDED
@@ -0,0 +1,133 @@
1
+ import type {
2
+ PropertyDefinitionSpecification, ServiceDefinition, ServiceDefinitionSpecification,
3
+ ModelDefinition, ForeignModelDefinition
4
+ } from "@live-change/framework"
5
+
6
+ import { PropertyDefinition } from "@live-change/framework"
7
+
8
+ interface QueryParameters {
9
+ [key: string]: any
10
+ }
11
+
12
+ interface QueryInputs {
13
+ [key: string]: QueryInput
14
+ }
15
+
16
+ type QueryCode = ((parameters: QueryParameters, inputs: QueryInputs) => any)
17
+
18
+ interface QueryDefinitionSpecification {
19
+ name: string
20
+ properties: Record<string, PropertyDefinitionSpecification>
21
+ returns?: PropertyDefinitionSpecification,
22
+ code: QueryCode,
23
+ sourceName: string,
24
+ update: boolean,
25
+ }
26
+
27
+ export class QueryDefinition<SDS extends ServiceDefinitionSpecification> {
28
+
29
+ service: ServiceDefinition<SDS>
30
+ definition: QueryDefinitionSpecification
31
+ properties: Record<string, PropertyDefinition<any>>
32
+
33
+ constructor(serviceDefinition: ServiceDefinition<SDS>, definition: QueryDefinitionSpecification) {
34
+ this.service = serviceDefinition
35
+ this.definition = definition
36
+
37
+ this.properties = Object.fromEntries(
38
+ Object.entries(definition.properties)
39
+ .map(
40
+ ([propertyName, propertyDefinition]) => [propertyName, new PropertyDefinition(propertyDefinition)]
41
+ )
42
+ )
43
+
44
+ const queryProperties = {}
45
+ for(const propertyName in definition.properties) {
46
+ const propertyDefinition = definition.properties[propertyName]
47
+ const base = new QueryPropertyBase([propertyName])
48
+ queryProperties[propertyName] = createQueryPropertyProxy(base, propertyName, this.properties[propertyName])
49
+ }
50
+
51
+ const queryInputs = {}
52
+ for(const propertyName in definition.properties) {
53
+ const propertyDefinition = definition.properties[propertyName]
54
+ const base = new QueryInputBase(this, [propertyName])
55
+ queryInputs[propertyName] = createQueryInputProxy(base, propertyName, this.properties[propertyName])
56
+ }
57
+
58
+ // run the code to collect relations
59
+ this.definition.code(queryProperties, queryInputs)
60
+
61
+ /// TODO: use collected relations to create indexes and prepared query
62
+ }
63
+ }
64
+
65
+
66
+ export type QueryFactoryFunction<SDS extends ServiceDefinitionSpecification> =
67
+ (definition: QueryDefinitionSpecification) => QueryDefinition<SDS>
68
+
69
+ export default function queryFactory<SDS extends ServiceDefinitionSpecification>(
70
+ serviceDefinition: ServiceDefinition<SDS>
71
+ ) {
72
+ const queryFactoryFunction: QueryFactoryFunction<SDS> =
73
+ (definition: QueryDefinitionSpecification) => new QueryDefinition<SDS>(serviceDefinition, definition)
74
+ return queryFactoryFunction
75
+ }
76
+
77
+
78
+ type QuerySource = ModelDefinition<any> | ForeignModelDefinition | any /// Query Definition will be recursive definition, so use any for now
79
+
80
+ export class QueryInputBase {
81
+ $source: QuerySource
82
+ $path: string[]
83
+
84
+ constructor(source: QuerySource, path: string[]) {
85
+ this.$source = source
86
+ this.$path = path
87
+ }
88
+ }
89
+
90
+ export class QueryInput extends QueryInputBase {
91
+ [key: string]: QueryInputBase | any /// Proxy class will be added to this
92
+ }
93
+
94
+
95
+ export function createQueryInputProxy(
96
+ base: QueryInputBase, propertyName: string, propertyDefinition: PropertyDefinition<any>
97
+ ) {
98
+ return new Proxy(base, {
99
+ get(target, prop, receiver) {
100
+ const foundInBase = Reflect.get(target, prop, receiver)
101
+ if(foundInBase) return foundInBase
102
+ const propertyBase = new QueryInputBase(base.$source, [...base.$path, propertyName])
103
+ const propertyProxy = createQueryInputProxy(propertyBase, propertyName, propertyDefinition)
104
+ return propertyProxy
105
+ }
106
+ })
107
+ }
108
+
109
+ export class QueryPropertyBase {
110
+ $path: string[]
111
+
112
+ constructor(path: string[]) {
113
+ this.$path = path
114
+ }
115
+ }
116
+
117
+ export class QueryProperty extends QueryPropertyBase {
118
+ [key: string]: QueryPropertyBase | any /// Proxy class will be added to this
119
+ }
120
+
121
+ export function createQueryPropertyProxy(
122
+ base: QueryPropertyBase, propertyName: string, propertyDefinition: PropertyDefinition<any>
123
+ ) {
124
+ return new Proxy(base, {
125
+ get(target, prop, receiver) {
126
+ const foundInBase = Reflect.get(target, prop, receiver)
127
+ if(foundInBase) return foundInBase
128
+ const propertyBase = new QueryPropertyBase([...base.$path, propertyName])
129
+ const propertyProxy = createQueryPropertyProxy(propertyBase, propertyName, propertyDefinition)
130
+ return propertyProxy
131
+ }
132
+ })
133
+ }
@@ -0,0 +1,2 @@
1
+ import type { QueryDefinition, QueryInputBase } from "./query.js"
2
+ import type { PropertyDefinition } from "@live-change/framework"