powersync 0.1.0 → 0.1.2

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 (191) hide show
  1. package/README.md +14 -14
  2. package/dist/commands/init.d.ts +0 -1
  3. package/dist/commands/init.js +11 -8
  4. package/dist/commands/instance/sync-rules/generate-schema.js +6 -6
  5. package/dist/constants.d.ts +2 -5
  6. package/dist/constants.js +4 -2
  7. package/dist/service/accounts.client.js +11129 -25
  8. package/dist/service/powersync.client.js +37453 -113
  9. package/dist/service/types.js +1 -1
  10. package/dist/service/utils.js +6 -6
  11. package/dist/utils.js +5 -7
  12. package/oclif.manifest.json +1 -1
  13. package/package.json +28 -20
  14. package/dist/powersync-sdk/accounts/api.d.ts +0 -717
  15. package/dist/powersync-sdk/accounts/api.js +0 -182
  16. package/dist/powersync-sdk/accounts/client.d.ts +0 -1096
  17. package/dist/powersync-sdk/accounts/client.js +0 -124
  18. package/dist/powersync-sdk/accounts/codecs.d.ts +0 -32
  19. package/dist/powersync-sdk/accounts/codecs.js +0 -93
  20. package/dist/powersync-sdk/accounts/index.d.ts +0 -4
  21. package/dist/powersync-sdk/accounts/index.js +0 -4
  22. package/dist/powersync-sdk/accounts/models.d.ts +0 -260
  23. package/dist/powersync-sdk/accounts/models.js +0 -96
  24. package/dist/powersync-sdk/accounts/routes.d.ts +0 -44
  25. package/dist/powersync-sdk/accounts/routes.js +0 -45
  26. package/dist/powersync-sdk/accounts/source.d.ts +0 -76
  27. package/dist/powersync-sdk/accounts/source.js +0 -32
  28. package/dist/powersync-sdk/errors/errors.d.ts +0 -62
  29. package/dist/powersync-sdk/errors/errors.js +0 -115
  30. package/dist/powersync-sdk/errors/index.d.ts +0 -2
  31. package/dist/powersync-sdk/errors/index.js +0 -2
  32. package/dist/powersync-sdk/errors/utils.d.ts +0 -4
  33. package/dist/powersync-sdk/errors/utils.js +0 -18
  34. package/dist/powersync-sdk/fetch-clients/codecs.d.ts +0 -4
  35. package/dist/powersync-sdk/fetch-clients/codecs.js +0 -34
  36. package/dist/powersync-sdk/fetch-clients/decoders.d.ts +0 -3
  37. package/dist/powersync-sdk/fetch-clients/decoders.js +0 -35
  38. package/dist/powersync-sdk/fetch-clients/definitions.d.ts +0 -97
  39. package/dist/powersync-sdk/fetch-clients/definitions.js +0 -22
  40. package/dist/powersync-sdk/fetch-clients/errors.d.ts +0 -7
  41. package/dist/powersync-sdk/fetch-clients/errors.js +0 -19
  42. package/dist/powersync-sdk/fetch-clients/index.d.ts +0 -5
  43. package/dist/powersync-sdk/fetch-clients/index.js +0 -5
  44. package/dist/powersync-sdk/fetch-clients/node-client.d.ts +0 -9
  45. package/dist/powersync-sdk/fetch-clients/node-client.js +0 -107
  46. package/dist/powersync-sdk/fetch-clients/request.d.ts +0 -11
  47. package/dist/powersync-sdk/fetch-clients/request.js +0 -138
  48. package/dist/powersync-sdk/fetch-clients/streaming.d.ts +0 -65
  49. package/dist/powersync-sdk/fetch-clients/streaming.js +0 -99
  50. package/dist/powersync-sdk/fetch-clients/utils.d.ts +0 -13
  51. package/dist/powersync-sdk/fetch-clients/utils.js +0 -43
  52. package/dist/powersync-sdk/fetch-clients/web-client.d.ts +0 -14
  53. package/dist/powersync-sdk/fetch-clients/web-client.js +0 -56
  54. package/dist/powersync-sdk/json/index.d.ts +0 -4
  55. package/dist/powersync-sdk/json/index.js +0 -3
  56. package/dist/powersync-sdk/json/json.d.ts +0 -5
  57. package/dist/powersync-sdk/json/json.js +0 -35
  58. package/dist/powersync-sdk/json/json_container.d.ts +0 -18
  59. package/dist/powersync-sdk/json/json_container.js +0 -28
  60. package/dist/powersync-sdk/json/json_stringify.d.ts +0 -36
  61. package/dist/powersync-sdk/json/json_stringify.js +0 -162
  62. package/dist/powersync-sdk/powersync/client.d.ts +0 -449
  63. package/dist/powersync-sdk/powersync/client.js +0 -53
  64. package/dist/powersync-sdk/powersync/config/HostedConfig.d.ts +0 -327
  65. package/dist/powersync-sdk/powersync/config/HostedConfig.js +0 -92
  66. package/dist/powersync-sdk/powersync/config/PowerSyncConfig.d.ts +0 -211
  67. package/dist/powersync-sdk/powersync/config/PowerSyncConfig.js +0 -97
  68. package/dist/powersync-sdk/powersync/config/normalize.d.ts +0 -32
  69. package/dist/powersync-sdk/powersync/config/normalize.js +0 -85
  70. package/dist/powersync-sdk/powersync/definitions.d.ts +0 -149
  71. package/dist/powersync-sdk/powersync/definitions.js +0 -97
  72. package/dist/powersync-sdk/powersync/index.d.ts +0 -8
  73. package/dist/powersync-sdk/powersync/index.js +0 -8
  74. package/dist/powersync-sdk/powersync/internal_routes.d.ts +0 -247
  75. package/dist/powersync-sdk/powersync/internal_routes.js +0 -69
  76. package/dist/powersync-sdk/powersync/routes.d.ts +0 -779
  77. package/dist/powersync-sdk/powersync/routes.js +0 -191
  78. package/dist/powersync-sdk/sdk-clients/endpoints.d.ts +0 -31
  79. package/dist/powersync-sdk/sdk-clients/endpoints.js +0 -48
  80. package/dist/powersync-sdk/sdk-clients/index.d.ts +0 -3
  81. package/dist/powersync-sdk/sdk-clients/index.js +0 -3
  82. package/dist/powersync-sdk/sdk-clients/pagination.d.ts +0 -14
  83. package/dist/powersync-sdk/sdk-clients/pagination.js +0 -21
  84. package/dist/powersync-sdk/sdk-clients/sdk-client.d.ts +0 -17
  85. package/dist/powersync-sdk/sdk-clients/sdk-client.js +0 -30
  86. package/dist/powersync-sdk/streaming/bson/buffer-array.d.ts +0 -31
  87. package/dist/powersync-sdk/streaming/bson/buffer-array.js +0 -79
  88. package/dist/powersync-sdk/streaming/bson/constants.d.ts +0 -2
  89. package/dist/powersync-sdk/streaming/bson/constants.js +0 -1
  90. package/dist/powersync-sdk/streaming/bson/decoder.d.ts +0 -10
  91. package/dist/powersync-sdk/streaming/bson/decoder.js +0 -62
  92. package/dist/powersync-sdk/streaming/bson/encoder.d.ts +0 -10
  93. package/dist/powersync-sdk/streaming/bson/encoder.js +0 -23
  94. package/dist/powersync-sdk/streaming/bson/header.d.ts +0 -11
  95. package/dist/powersync-sdk/streaming/bson/header.js +0 -52
  96. package/dist/powersync-sdk/streaming/bson/index.d.ts +0 -5
  97. package/dist/powersync-sdk/streaming/bson/index.js +0 -5
  98. package/dist/powersync-sdk/streaming/core/backpressure.d.ts +0 -15
  99. package/dist/powersync-sdk/streaming/core/backpressure.js +0 -34
  100. package/dist/powersync-sdk/streaming/core/cross-stream.d.ts +0 -6
  101. package/dist/powersync-sdk/streaming/core/cross-stream.js +0 -17
  102. package/dist/powersync-sdk/streaming/core/index.d.ts +0 -4
  103. package/dist/powersync-sdk/streaming/core/index.js +0 -4
  104. package/dist/powersync-sdk/streaming/core/node-utils.d.ts +0 -7
  105. package/dist/powersync-sdk/streaming/core/node-utils.js +0 -11
  106. package/dist/powersync-sdk/streaming/core/transformers.d.ts +0 -41
  107. package/dist/powersync-sdk/streaming/core/transformers.js +0 -70
  108. package/dist/powersync-sdk/streaming/core/utils.d.ts +0 -36
  109. package/dist/powersync-sdk/streaming/core/utils.js +0 -66
  110. package/dist/powersync-sdk/streaming/index.d.ts +0 -3
  111. package/dist/powersync-sdk/streaming/index.js +0 -3
  112. package/dist/powersync-sdk/streaming/web.d.ts +0 -4
  113. package/dist/powersync-sdk/streaming/web.js +0 -4
  114. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.d.ts +0 -2
  115. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binaryreader.js +0 -36
  116. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.d.ts +0 -2
  117. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/binarywriter.js +0 -58
  118. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.d.ts +0 -18
  119. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometry.js +0 -348
  120. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.d.ts +0 -11
  121. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/geometrycollection.js +0 -118
  122. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.d.ts +0 -11
  123. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/linestring.js +0 -131
  124. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.d.ts +0 -11
  125. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multilinestring.js +0 -134
  126. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.d.ts +0 -11
  127. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipoint.js +0 -122
  128. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.d.ts +0 -11
  129. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/multipolygon.js +0 -158
  130. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.d.ts +0 -14
  131. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/point.js +0 -165
  132. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.d.ts +0 -11
  133. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/polygon.js +0 -207
  134. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.d.ts +0 -30
  135. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/types.js +0 -29
  136. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.d.ts +0 -2
  137. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wktparser.js +0 -111
  138. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.d.ts +0 -9
  139. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/wkx.js +0 -9
  140. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.d.ts +0 -5
  141. package/dist/powersync-sdk/sync-rules/@syncpoint/wkx/zigzag.js +0 -9
  142. package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.d.ts +0 -12
  143. package/dist/powersync-sdk/sync-rules/DartSchemaGenerator.js +0 -35
  144. package/dist/powersync-sdk/sync-rules/ExpressionType.d.ts +0 -33
  145. package/dist/powersync-sdk/sync-rules/ExpressionType.js +0 -62
  146. package/dist/powersync-sdk/sync-rules/IdSequence.d.ts +0 -4
  147. package/dist/powersync-sdk/sync-rules/IdSequence.js +0 -7
  148. package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.d.ts +0 -12
  149. package/dist/powersync-sdk/sync-rules/JsSchemaGenerator.js +0 -38
  150. package/dist/powersync-sdk/sync-rules/SchemaGenerator.d.ts +0 -14
  151. package/dist/powersync-sdk/sync-rules/SchemaGenerator.js +0 -24
  152. package/dist/powersync-sdk/sync-rules/SourceTableInterface.d.ts +0 -5
  153. package/dist/powersync-sdk/sync-rules/SourceTableInterface.js +0 -1
  154. package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.d.ts +0 -37
  155. package/dist/powersync-sdk/sync-rules/SqlBucketDescriptor.js +0 -116
  156. package/dist/powersync-sdk/sync-rules/SqlDataQuery.d.ts +0 -39
  157. package/dist/powersync-sdk/sync-rules/SqlDataQuery.js +0 -245
  158. package/dist/powersync-sdk/sync-rules/SqlParameterQuery.d.ts +0 -43
  159. package/dist/powersync-sdk/sync-rules/SqlParameterQuery.js +0 -254
  160. package/dist/powersync-sdk/sync-rules/SqlSyncRules.d.ts +0 -52
  161. package/dist/powersync-sdk/sync-rules/SqlSyncRules.js +0 -252
  162. package/dist/powersync-sdk/sync-rules/StaticSchema.d.ts +0 -26
  163. package/dist/powersync-sdk/sync-rules/StaticSchema.js +0 -71
  164. package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.d.ts +0 -24
  165. package/dist/powersync-sdk/sync-rules/StaticSqlParameterQuery.js +0 -71
  166. package/dist/powersync-sdk/sync-rules/TablePattern.d.ts +0 -17
  167. package/dist/powersync-sdk/sync-rules/TablePattern.js +0 -56
  168. package/dist/powersync-sdk/sync-rules/TableQuerySchema.d.ts +0 -9
  169. package/dist/powersync-sdk/sync-rules/TableQuerySchema.js +0 -41
  170. package/dist/powersync-sdk/sync-rules/errors.d.ts +0 -22
  171. package/dist/powersync-sdk/sync-rules/errors.js +0 -60
  172. package/dist/powersync-sdk/sync-rules/generators.d.ts +0 -6
  173. package/dist/powersync-sdk/sync-rules/generators.js +0 -6
  174. package/dist/powersync-sdk/sync-rules/index.d.ts +0 -17
  175. package/dist/powersync-sdk/sync-rules/index.js +0 -17
  176. package/dist/powersync-sdk/sync-rules/json_schema.d.ts +0 -3
  177. package/dist/powersync-sdk/sync-rules/json_schema.js +0 -50
  178. package/dist/powersync-sdk/sync-rules/sql_filters.d.ts +0 -91
  179. package/dist/powersync-sdk/sync-rules/sql_filters.js +0 -491
  180. package/dist/powersync-sdk/sync-rules/sql_functions.d.ts +0 -53
  181. package/dist/powersync-sdk/sync-rules/sql_functions.js +0 -755
  182. package/dist/powersync-sdk/sync-rules/sql_support.d.ts +0 -22
  183. package/dist/powersync-sdk/sync-rules/sql_support.js +0 -208
  184. package/dist/powersync-sdk/sync-rules/types.d.ts +0 -174
  185. package/dist/powersync-sdk/sync-rules/types.js +0 -9
  186. package/dist/powersync-sdk/sync-rules/utils.d.ts +0 -45
  187. package/dist/powersync-sdk/sync-rules/utils.js +0 -163
  188. package/dist/service/accounts.client.d.ts +0 -26
  189. package/dist/service/powersync.client.d.ts +0 -269
  190. package/dist/service/types.d.ts +0 -69
  191. package/dist/service/utils.d.ts +0 -5
@@ -1,755 +0,0 @@
1
- /* eslint-disable unicorn/filename-case */
2
- /* eslint-disable @typescript-eslint/ban-ts-comment */
3
- /* eslint-disable @typescript-eslint/no-explicit-any */
4
- /* eslint-disable no-empty */
5
- /* eslint-disable camelcase */
6
- /* eslint-disable @typescript-eslint/no-unused-vars */
7
- /* eslint-disable no-eq-null */
8
- /* eslint-disable eqeqeq */
9
- import { JSONBig } from '../json/index.js';
10
- import Geometry from './@syncpoint/wkx/geometry.js';
11
- import Point from './@syncpoint/wkx/point.js';
12
- import { ExpressionType, TYPE_INTEGER } from './ExpressionType.js';
13
- import { SQLITE_FALSE, SQLITE_TRUE, sqliteBool } from './sql_support.js';
14
- import { jsonValueToSqlite } from './utils.js';
15
- export const BASIC_OPERATORS = new Set([
16
- '=',
17
- '!=',
18
- '<',
19
- '>',
20
- '<=',
21
- '>=',
22
- '+',
23
- '-',
24
- '*',
25
- '/',
26
- '||',
27
- 'AND',
28
- 'OR',
29
- 'IS',
30
- 'IS NOT'
31
- ]);
32
- const upper = {
33
- call(value) {
34
- const text = castAsText(value);
35
- return text?.toUpperCase() ?? null;
36
- },
37
- getReturnType(args) {
38
- return ExpressionType.TEXT;
39
- },
40
- parameters: [{ name: 'value', optional: false, type: ExpressionType.ANY }]
41
- };
42
- const lower = {
43
- call(value) {
44
- const text = castAsText(value);
45
- return text?.toLowerCase() ?? null;
46
- },
47
- getReturnType(args) {
48
- return ExpressionType.TEXT;
49
- },
50
- parameters: [{ name: 'value', optional: false, type: ExpressionType.ANY }]
51
- };
52
- const hex = {
53
- call(value) {
54
- const binary = castAsBlob(value);
55
- if (binary == null) {
56
- return '';
57
- }
58
- return Buffer.from(binary).toString('hex').toUpperCase();
59
- },
60
- getReturnType(args) {
61
- return ExpressionType.TEXT;
62
- },
63
- parameters: [{ name: 'value', optional: false, type: ExpressionType.ANY }]
64
- };
65
- const length = {
66
- call(value) {
67
- if (value == null) {
68
- return null;
69
- }
70
- if (value instanceof Uint8Array) {
71
- return BigInt(value.byteLength);
72
- }
73
- value = castAsText(value);
74
- return BigInt(value.length);
75
- },
76
- getReturnType(args) {
77
- return ExpressionType.INTEGER;
78
- },
79
- parameters: [{ name: 'value', optional: false, type: ExpressionType.ANY }]
80
- };
81
- const base64 = {
82
- call(value) {
83
- const binary = castAsBlob(value);
84
- if (binary == null) {
85
- return '';
86
- }
87
- return Buffer.from(binary).toString('base64');
88
- },
89
- getReturnType(args) {
90
- return ExpressionType.TEXT;
91
- },
92
- parameters: [{ name: 'value', optional: false, type: ExpressionType.ANY }]
93
- };
94
- const fn_typeof = {
95
- call(value) {
96
- return sqliteTypeOf(value);
97
- },
98
- getReturnType(args) {
99
- return ExpressionType.TEXT;
100
- },
101
- parameters: [{ name: 'value', optional: false, type: ExpressionType.ANY }]
102
- };
103
- const ifnull = {
104
- call(x, y) {
105
- if (x == null) {
106
- return y;
107
- }
108
- return x;
109
- },
110
- getReturnType(args) {
111
- if (args.length === 0) {
112
- return ExpressionType.NONE;
113
- }
114
- if (args.length == 1) {
115
- return args[0];
116
- }
117
- return args[0].or(args[1]);
118
- },
119
- parameters: [
120
- { name: 'x', optional: false, type: ExpressionType.ANY },
121
- { name: 'y', optional: false, type: ExpressionType.ANY }
122
- ]
123
- };
124
- const json_extract = {
125
- call(json, path) {
126
- return jsonExtract(json, path, 'json_extract');
127
- },
128
- getReturnType(args) {
129
- return ExpressionType.ANY_JSON;
130
- },
131
- parameters: [
132
- { name: 'json', optional: false, type: ExpressionType.ANY },
133
- { name: 'path', optional: false, type: ExpressionType.ANY }
134
- ]
135
- };
136
- const json_array_length = {
137
- call(json, path) {
138
- if (path != null) {
139
- json = json_extract.call(json, path);
140
- }
141
- const jsonString = castAsText(json);
142
- if (jsonString == null) {
143
- return null;
144
- }
145
- const jsonParsed = JSONBig.parse(jsonString);
146
- if (!Array.isArray(jsonParsed)) {
147
- return 0n;
148
- }
149
- return BigInt(jsonParsed.length);
150
- },
151
- getReturnType(args) {
152
- return ExpressionType.INTEGER;
153
- },
154
- parameters: [
155
- { name: 'json', optional: false, type: ExpressionType.ANY },
156
- { name: 'path', optional: true, type: ExpressionType.ANY }
157
- ]
158
- };
159
- const json_valid = {
160
- call(json) {
161
- const jsonString = castAsText(json);
162
- if (jsonString == null) {
163
- return SQLITE_FALSE;
164
- }
165
- try {
166
- JSONBig.parse(jsonString);
167
- return SQLITE_TRUE;
168
- }
169
- catch {
170
- return SQLITE_FALSE;
171
- }
172
- },
173
- getReturnType(args) {
174
- return ExpressionType.INTEGER;
175
- },
176
- parameters: [{ name: 'json', optional: false, type: ExpressionType.ANY }]
177
- };
178
- const unixepoch = {
179
- call(value, specifier, specifier2) {
180
- if (value == null) {
181
- return null;
182
- }
183
- const flags = {
184
- subsecond: false,
185
- unixepoch: false
186
- };
187
- if (specifier == null) {
188
- }
189
- else if (specifier == 'unixepoch') {
190
- flags.unixepoch = true;
191
- if (specifier2 == null) {
192
- }
193
- else if (specifier2 == 'subsec' || specifier2 == 'subsecond') {
194
- flags.subsecond = true;
195
- }
196
- else {
197
- return null;
198
- }
199
- }
200
- else if (specifier == 'subsec' || specifier == 'subsecond') {
201
- flags.subsecond = true;
202
- }
203
- else {
204
- return null;
205
- }
206
- const epoch = convertToDate(value, flags)?.getTime();
207
- if (epoch == null || !Number.isFinite(epoch)) {
208
- return null;
209
- }
210
- if (flags.subsecond) {
211
- return epoch / 1000;
212
- }
213
- return BigInt(Math.floor(epoch / 1000));
214
- },
215
- getReturnType(args) {
216
- return ExpressionType.INTEGER.or(ExpressionType.REAL);
217
- },
218
- parameters: [
219
- { name: 'value', optional: false, type: ExpressionType.ANY },
220
- { name: 'specifier', optional: true, type: ExpressionType.ANY },
221
- { name: 'specifier2', optional: true, type: ExpressionType.ANY }
222
- ]
223
- };
224
- const datetime = {
225
- call(value, specifier, specifier2) {
226
- if (value == null) {
227
- return null;
228
- }
229
- const flags = {
230
- subsecond: false,
231
- unixepoch: false
232
- };
233
- if (specifier == null) {
234
- }
235
- else if (specifier == 'unixepoch') {
236
- flags.unixepoch = true;
237
- if (specifier2 == null) {
238
- }
239
- else if (specifier2 == 'subsec' || specifier2 == 'subsecond') {
240
- flags.subsecond = true;
241
- }
242
- else {
243
- return null;
244
- }
245
- }
246
- else if (specifier == 'subsec' || specifier == 'subsecond') {
247
- flags.subsecond = true;
248
- }
249
- else {
250
- return null;
251
- }
252
- const epoch = convertToDate(value, flags);
253
- if (epoch == null || !Number.isFinite(epoch.getTime())) {
254
- return null;
255
- }
256
- const baseString = epoch.toISOString().replace(/T/, ' ').replace(/^-0+/, '-');
257
- if (flags.subsecond) {
258
- return baseString.replace(/Z$/, '');
259
- }
260
- return baseString.replace(/(\.\d+)?Z$/, '');
261
- },
262
- getReturnType(args) {
263
- return ExpressionType.TEXT;
264
- },
265
- parameters: [
266
- { name: 'value', optional: false, type: ExpressionType.ANY },
267
- { name: 'specifier', optional: true, type: ExpressionType.ANY },
268
- { name: 'specifier2', optional: true, type: ExpressionType.ANY }
269
- ]
270
- };
271
- const st_asgeojson = {
272
- call(geometry) {
273
- const geo = parseGeometry(geometry);
274
- if (geo == null) {
275
- return null;
276
- }
277
- return JSONBig.stringify(geo.toGeoJSON());
278
- },
279
- getReturnType(args) {
280
- return ExpressionType.TEXT;
281
- },
282
- parameters: [{ name: 'geometry', optional: false, type: ExpressionType.ANY }]
283
- };
284
- const st_astext = {
285
- call(geometry) {
286
- const geo = parseGeometry(geometry);
287
- if (geo == null) {
288
- return null;
289
- }
290
- return geo.toWkt();
291
- },
292
- getReturnType(args) {
293
- return ExpressionType.TEXT;
294
- },
295
- parameters: [{ name: 'geometry', optional: false, type: ExpressionType.ANY }]
296
- };
297
- const st_x = {
298
- call(geometry) {
299
- const geo = parseGeometry(geometry);
300
- if (geo == null) {
301
- return null;
302
- }
303
- if (geo instanceof Point) {
304
- // @ts-ignore
305
- return geo.x;
306
- }
307
- return null;
308
- },
309
- getReturnType(args) {
310
- return ExpressionType.REAL;
311
- },
312
- parameters: [{ name: 'geometry', optional: false, type: ExpressionType.ANY }]
313
- };
314
- const st_y = {
315
- call(geometry) {
316
- const geo = parseGeometry(geometry);
317
- if (geo == null) {
318
- return null;
319
- }
320
- if (geo instanceof Point) {
321
- // @ts-ignore
322
- return geo.y;
323
- }
324
- return null;
325
- },
326
- getReturnType(args) {
327
- return ExpressionType.REAL;
328
- },
329
- parameters: [{ name: 'geometry', optional: false, type: ExpressionType.ANY }]
330
- };
331
- export const SQL_FUNCTIONS_NAMED = {
332
- base64,
333
- datetime,
334
- hex,
335
- ifnull,
336
- json_array_length,
337
- json_extract,
338
- json_valid,
339
- length,
340
- lower,
341
- st_asgeojson,
342
- st_astext,
343
- st_x,
344
- st_y,
345
- typeof: fn_typeof,
346
- unixepoch,
347
- upper
348
- };
349
- export const SQL_FUNCTIONS_CALL = Object.fromEntries(Object.entries(SQL_FUNCTIONS_NAMED).map(([name, fn]) => [name, fn.call]));
350
- export const SQL_FUNCTIONS = SQL_FUNCTIONS_NAMED;
351
- export const CAST_TYPES = new Set(['text', 'numeric', 'integer', 'real', 'blob']);
352
- const textEncoder = new TextEncoder();
353
- const textDecoder = new TextDecoder();
354
- export function castAsText(value) {
355
- if (value == null) {
356
- return null;
357
- }
358
- if (value instanceof Uint8Array) {
359
- return textDecoder.decode(value);
360
- }
361
- return value.toString();
362
- }
363
- export function castAsBlob(value) {
364
- if (value == null) {
365
- return null;
366
- }
367
- if (value instanceof Uint8Array) {
368
- return value;
369
- }
370
- if (typeof value !== 'string') {
371
- value = value.toString();
372
- }
373
- return textEncoder.encode(value);
374
- }
375
- export function cast(value, to) {
376
- if (value == null) {
377
- return null;
378
- }
379
- if (to == 'text') {
380
- return castAsText(value);
381
- }
382
- if (to == 'numeric') {
383
- if (value instanceof Uint8Array) {
384
- value = textDecoder.decode(value);
385
- }
386
- if (typeof value === 'string') {
387
- return parseNumeric(value);
388
- }
389
- if (typeof value === 'number' || typeof value === 'bigint') {
390
- return value;
391
- }
392
- return 0n;
393
- }
394
- if (to == 'real') {
395
- if (value instanceof Uint8Array) {
396
- value = textDecoder.decode(value);
397
- }
398
- if (typeof value === 'string') {
399
- const nr = Number.parseFloat(value);
400
- if (Number.isNaN(nr)) {
401
- return 0;
402
- }
403
- return nr;
404
- }
405
- if (typeof value === 'number') {
406
- return value;
407
- }
408
- if (typeof value === 'bigint') {
409
- return Number(value);
410
- }
411
- return 0;
412
- }
413
- if (to == 'integer') {
414
- if (value instanceof Uint8Array) {
415
- value = textDecoder.decode(value);
416
- }
417
- if (typeof value === 'string') {
418
- return parseBigInt(value);
419
- }
420
- if (typeof value === 'number') {
421
- return Number.isInteger(value) ? BigInt(value) : BigInt(Math.floor(value));
422
- }
423
- if (typeof value === 'bigint') {
424
- return value;
425
- }
426
- return 0n;
427
- }
428
- if (to == 'blob') {
429
- return castAsBlob(value);
430
- }
431
- throw new Error(`Type not supported for cast: '${to}'`);
432
- }
433
- export function sqliteTypeOf(arg) {
434
- if (arg == null) {
435
- return 'null';
436
- }
437
- if (typeof arg === 'string') {
438
- return 'text';
439
- }
440
- if (typeof arg === 'bigint') {
441
- return 'integer';
442
- }
443
- if (typeof arg === 'number') {
444
- return 'real';
445
- }
446
- if (arg instanceof Uint8Array) {
447
- return 'blob';
448
- }
449
- // Should not happen
450
- throw new Error(`Unknown type: ${arg}`);
451
- }
452
- export function parseGeometry(value) {
453
- let blob;
454
- if (value == null) {
455
- return null;
456
- }
457
- if (value instanceof Uint8Array) {
458
- blob = Buffer.from(value);
459
- }
460
- else if (typeof value === 'string') {
461
- blob = Buffer.from(value, 'hex');
462
- }
463
- else {
464
- return null;
465
- }
466
- const geo = Geometry.parse(blob);
467
- return geo;
468
- }
469
- function parseNumeric(text) {
470
- const match = /^\s*(\d+)(\.\d*)?(e[+-]?\d+)?/i.exec(text);
471
- if (!match) {
472
- return 0n;
473
- }
474
- if (match[2] != null || match[3] != null) {
475
- const v = Number.parseFloat(match[0]);
476
- return Number.isNaN(v) ? 0n : v;
477
- }
478
- return BigInt(match[1]);
479
- }
480
- function parseBigInt(text) {
481
- const match = /^\s*(\d+)/.exec(text);
482
- if (!match) {
483
- return 0n;
484
- }
485
- return BigInt(match[1]);
486
- }
487
- function isNumeric(a) {
488
- return typeof a === 'number' || typeof a === 'bigint';
489
- }
490
- export function evaluateOperator(op, a, b) {
491
- switch (op) {
492
- case '=':
493
- case '!=':
494
- case '>':
495
- case '<':
496
- case '>=':
497
- case '<=': {
498
- if (a == null || b == null) {
499
- return null;
500
- }
501
- const diff = compare(a, b);
502
- if (op == '=') {
503
- return sqliteBool(diff === 0);
504
- }
505
- if (op == '!=') {
506
- return sqliteBool(diff !== 0);
507
- }
508
- if (op == '>') {
509
- return sqliteBool(diff > 0);
510
- }
511
- if (op == '<') {
512
- return sqliteBool(diff < 0);
513
- }
514
- if (op == '>=') {
515
- return sqliteBool(diff >= 0);
516
- }
517
- if (op == '<=') {
518
- return sqliteBool(diff <= 0);
519
- }
520
- throw new Error('unreachable');
521
- }
522
- // Not currently supported by the parser, but used with IS NULL
523
- case 'IS': {
524
- const diff = compare(a, b);
525
- return sqliteBool(diff === 0);
526
- }
527
- // Not currently supported by the parser, but used with IS NOT NULL
528
- case 'IS NOT': {
529
- const diff = compare(a, b);
530
- return sqliteBool(diff !== 0);
531
- }
532
- case '+':
533
- case '-':
534
- case '*':
535
- case '/': {
536
- return doMath(op, a, b);
537
- }
538
- case '||': {
539
- return concat(a, b);
540
- }
541
- case 'AND': {
542
- return sqliteBool(sqliteBool(a) && sqliteBool(b));
543
- }
544
- case 'OR': {
545
- return sqliteBool(sqliteBool(a) || sqliteBool(b));
546
- }
547
- case 'IN': {
548
- if (a == null || b == null) {
549
- return null;
550
- }
551
- if (typeof b !== 'string') {
552
- throw new TypeError('IN is only supported on JSON arrays');
553
- }
554
- const bParsed = JSON.parse(b);
555
- if (!Array.isArray(bParsed)) {
556
- throw new TypeError('IN is only supported on JSON arrays');
557
- }
558
- return sqliteBool(bParsed.includes(a));
559
- }
560
- default: {
561
- throw new Error(`Operator not supported: ${op}`);
562
- }
563
- }
564
- }
565
- export function getOperatorReturnType(op, left, right) {
566
- switch (op) {
567
- case '=':
568
- case '!=':
569
- case '>':
570
- case '<':
571
- case '>=':
572
- case '<=': {
573
- return ExpressionType.INTEGER;
574
- }
575
- // Not currently supported by the parser, but used with IS NULL
576
- case 'IS': {
577
- return ExpressionType.INTEGER;
578
- }
579
- // Not currently supported by the parser, but used with IS NOT NULL
580
- case 'IS NOT': {
581
- return ExpressionType.INTEGER;
582
- }
583
- case '+':
584
- case '-':
585
- case '*':
586
- case '/': {
587
- if (left.typeFlags == TYPE_INTEGER && right.typeFlags == TYPE_INTEGER) {
588
- // INT, INT stays INT
589
- return ExpressionType.INTEGER;
590
- }
591
- if (left.isNumericOnly() && right.isNumericOnly()) {
592
- // INT, REAL or REAL, INT or REAL, REAL => always REAL
593
- return ExpressionType.REAL;
594
- }
595
- // Unknown - could be REAL or INT
596
- return ExpressionType.NUMERIC;
597
- }
598
- case '||': {
599
- return ExpressionType.TEXT;
600
- }
601
- case 'AND': {
602
- return ExpressionType.INTEGER;
603
- }
604
- case 'OR': {
605
- return ExpressionType.INTEGER;
606
- }
607
- case 'IN': {
608
- return ExpressionType.INTEGER;
609
- }
610
- default: {
611
- return ExpressionType.NONE;
612
- }
613
- }
614
- }
615
- function doMath(op, a, b) {
616
- if (a == null || b == null) {
617
- return null;
618
- }
619
- let na = cast(a, 'numeric');
620
- let nb = cast(b, 'numeric');
621
- if (typeof na === 'bigint' && typeof nb !== 'bigint') {
622
- // bigint, real
623
- na = Number(na);
624
- }
625
- else if (typeof na !== 'bigint' && typeof nb === 'bigint') {
626
- // real, bigint
627
- nb = Number(nb);
628
- }
629
- switch (op) {
630
- case '+': {
631
- return na + nb;
632
- }
633
- case '-': {
634
- return na - nb;
635
- }
636
- case '*': {
637
- return na * nb;
638
- }
639
- case '/': {
640
- return na / nb;
641
- }
642
- default: {
643
- throw new Error(`Operator not supported: ${op}`);
644
- }
645
- }
646
- }
647
- function concat(a, b) {
648
- const aText = castAsText(a);
649
- const bText = castAsText(b);
650
- if (aText == null || bText == null) {
651
- return null;
652
- }
653
- return aText + bText;
654
- }
655
- export function jsonExtract(sourceValue, path, operator) {
656
- const valueText = castAsText(sourceValue);
657
- const pathText = castAsText(path);
658
- if (valueText == null || pathText == null) {
659
- return null;
660
- }
661
- const components = pathText.split('.');
662
- if (components[0] == '$') {
663
- components.shift();
664
- }
665
- else if (operator == 'json_extract') {
666
- throw new Error(`JSON path must start with $.`);
667
- }
668
- let value = JSONBig.parse(valueText);
669
- for (const c of components) {
670
- if (value == null) {
671
- break;
672
- }
673
- value = value[c];
674
- }
675
- if (operator == '->') {
676
- // -> must always stringify
677
- return JSONBig.stringify(value);
678
- }
679
- if (typeof value === 'object' || Array.isArray(value)) {
680
- // Objects and arrays must be stringified
681
- return JSONBig.stringify(value);
682
- }
683
- // Plain scalar value - simple conversion.
684
- return jsonValueToSqlite(value);
685
- }
686
- export function convertToDate(dateTime, flags) {
687
- if (typeof dateTime === 'string') {
688
- return parseUTCDate(dateTime);
689
- }
690
- if (typeof dateTime === 'bigint') {
691
- if (flags.unixepoch) {
692
- return new Date(Number(dateTime) * 1000);
693
- }
694
- return julianToJSDate(Number(dateTime));
695
- }
696
- if (typeof dateTime === 'number') {
697
- if (flags.unixepoch) {
698
- return new Date(dateTime * 1000);
699
- }
700
- return julianToJSDate(dateTime);
701
- }
702
- return null;
703
- }
704
- function parseUTCDate(isoDateString) {
705
- const hasTimezone = /[Zz]|[+-]\d{2}:\d{2}$/;
706
- const isJulienDay = /^\d+(\.\d*)?$/;
707
- if (hasTimezone.test(isoDateString)) {
708
- // If the string already has a timezone, parse it directly
709
- return new Date(isoDateString);
710
- }
711
- if (isJulienDay.test(isoDateString)) {
712
- return julianToJSDate(Number.parseFloat(isoDateString));
713
- }
714
- // If the string has no timezone, append "Z" to the end to interpret it as UTC
715
- return new Date(isoDateString + 'Z');
716
- }
717
- function julianToJSDate(julianDay) {
718
- // The Julian date for the Unix Epoch is 2440587.5
719
- const julianAtEpoch = 2440587.5;
720
- // Calculate the difference between the Julian date and the Unix Epoch in days
721
- const daysSinceEpoch = julianDay - julianAtEpoch;
722
- // Convert this to milliseconds
723
- const msSinceEpoch = daysSinceEpoch * 24 * 60 * 60 * 1000;
724
- // Create a new Date object with this number of milliseconds since the Unix Epoch
725
- return new Date(msSinceEpoch);
726
- }
727
- const TYPE_ORDERING = {
728
- blob: 3,
729
- integer: 1,
730
- null: 0,
731
- real: 1,
732
- text: 2
733
- };
734
- function compare(a, b) {
735
- // https://www.sqlite.org/datatype3.html#comparisons
736
- if (a == null && b == null) {
737
- // Only for IS / IS NOT
738
- return 0;
739
- }
740
- if ((isNumeric(a) && isNumeric(b)) || (typeof a === 'string' && typeof b === 'string')) {
741
- if (a == b) {
742
- return 0;
743
- }
744
- if (a > b) {
745
- return 1;
746
- }
747
- return -1;
748
- }
749
- if (a instanceof Uint8Array && b instanceof Uint8Array) {
750
- throw new TypeError('Comparing blobs is not supported currently');
751
- }
752
- const typeA = sqliteTypeOf(a);
753
- const typeB = sqliteTypeOf(b);
754
- return TYPE_ORDERING[typeA] - TYPE_ORDERING[typeB];
755
- }