@livestore/livestore 0.2.0-dev.2 → 0.3.0-dev.10

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 (100) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/SynchronousDatabaseWrapper.d.ts +6 -1
  3. package/dist/SynchronousDatabaseWrapper.d.ts.map +1 -1
  4. package/dist/SynchronousDatabaseWrapper.js +14 -2
  5. package/dist/SynchronousDatabaseWrapper.js.map +1 -1
  6. package/dist/__tests__/fixture.d.ts +252 -0
  7. package/dist/__tests__/fixture.d.ts.map +1 -0
  8. package/dist/__tests__/fixture.js +18 -0
  9. package/dist/__tests__/fixture.js.map +1 -0
  10. package/dist/effect/LiveStore.d.ts +6 -6
  11. package/dist/effect/LiveStore.d.ts.map +1 -1
  12. package/dist/effect/LiveStore.js +5 -12
  13. package/dist/effect/LiveStore.js.map +1 -1
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/live-queries/db.d.ts.map +1 -1
  18. package/dist/live-queries/db.js +28 -23
  19. package/dist/live-queries/db.js.map +1 -1
  20. package/dist/live-queries/db.test.js +2 -1
  21. package/dist/live-queries/db.test.js.map +1 -1
  22. package/dist/row-query-utils.js +1 -1
  23. package/dist/row-query-utils.js.map +1 -1
  24. package/dist/store/create-store.d.ts +12 -10
  25. package/dist/store/create-store.d.ts.map +1 -1
  26. package/dist/store/create-store.js +22 -28
  27. package/dist/store/create-store.js.map +1 -1
  28. package/dist/store/devtools.d.ts +1 -1
  29. package/dist/store/devtools.d.ts.map +1 -1
  30. package/dist/store/devtools.js +41 -19
  31. package/dist/store/devtools.js.map +1 -1
  32. package/dist/store/store-types.d.ts +9 -14
  33. package/dist/store/store-types.d.ts.map +1 -1
  34. package/dist/store/store.d.ts +29 -28
  35. package/dist/store/store.d.ts.map +1 -1
  36. package/dist/store/store.js +147 -160
  37. package/dist/store/store.js.map +1 -1
  38. package/dist/store/store.test.d.ts +2 -0
  39. package/dist/store/store.test.d.ts.map +1 -0
  40. package/dist/store/store.test.js +27 -0
  41. package/dist/store/store.test.js.map +1 -0
  42. package/dist/utils/dev.d.ts.map +1 -1
  43. package/dist/utils/dev.js +3 -2
  44. package/dist/utils/dev.js.map +1 -1
  45. package/dist/utils/tests/fixture.d.ts +1 -1
  46. package/dist/utils/tests/fixture.d.ts.map +1 -1
  47. package/dist/utils/tests/fixture.js +4 -8
  48. package/dist/utils/tests/fixture.js.map +1 -1
  49. package/dist/utils/tests/otel.d.ts +60 -1
  50. package/dist/utils/tests/otel.d.ts.map +1 -1
  51. package/dist/utils/tests/otel.js +65 -4
  52. package/dist/utils/tests/otel.js.map +1 -1
  53. package/package.json +12 -12
  54. package/src/SynchronousDatabaseWrapper.ts +18 -2
  55. package/src/ambient.d.ts +1 -1
  56. package/src/effect/LiveStore.ts +11 -20
  57. package/src/index.ts +1 -1
  58. package/src/live-queries/__snapshots__/db.test.ts.snap +42 -45
  59. package/src/live-queries/db.test.ts +2 -1
  60. package/src/live-queries/db.ts +28 -23
  61. package/src/row-query-utils.ts +1 -1
  62. package/src/store/create-store.ts +115 -119
  63. package/src/store/devtools.ts +48 -22
  64. package/src/store/store-types.ts +14 -14
  65. package/src/store/store.ts +188 -224
  66. package/src/utils/dev.ts +4 -2
  67. package/src/utils/tests/fixture.ts +4 -9
  68. package/src/utils/tests/otel.ts +71 -5
  69. package/dist/live-queries/sql.d.ts +0 -62
  70. package/dist/live-queries/sql.d.ts.map +0 -1
  71. package/dist/live-queries/sql.js +0 -175
  72. package/dist/live-queries/sql.js.map +0 -1
  73. package/dist/live-queries/sql.test.d.ts +0 -2
  74. package/dist/live-queries/sql.test.d.ts.map +0 -1
  75. package/dist/live-queries/sql.test.js +0 -285
  76. package/dist/live-queries/sql.test.js.map +0 -1
  77. package/dist/reactiveQueries/base-class.d.ts +0 -64
  78. package/dist/reactiveQueries/base-class.d.ts.map +0 -1
  79. package/dist/reactiveQueries/base-class.js +0 -31
  80. package/dist/reactiveQueries/base-class.js.map +0 -1
  81. package/dist/reactiveQueries/computed.d.ts +0 -26
  82. package/dist/reactiveQueries/computed.d.ts.map +0 -1
  83. package/dist/reactiveQueries/computed.js +0 -38
  84. package/dist/reactiveQueries/computed.js.map +0 -1
  85. package/dist/reactiveQueries/graphql.d.ts +0 -49
  86. package/dist/reactiveQueries/graphql.d.ts.map +0 -1
  87. package/dist/reactiveQueries/graphql.js +0 -122
  88. package/dist/reactiveQueries/graphql.js.map +0 -1
  89. package/dist/reactiveQueries/sql.d.ts +0 -62
  90. package/dist/reactiveQueries/sql.d.ts.map +0 -1
  91. package/dist/reactiveQueries/sql.js +0 -175
  92. package/dist/reactiveQueries/sql.js.map +0 -1
  93. package/dist/reactiveQueries/sql.test.d.ts +0 -2
  94. package/dist/reactiveQueries/sql.test.d.ts.map +0 -1
  95. package/dist/reactiveQueries/sql.test.js +0 -285
  96. package/dist/reactiveQueries/sql.test.js.map +0 -1
  97. package/dist/row-query.d.ts +0 -16
  98. package/dist/row-query.d.ts.map +0 -1
  99. package/dist/row-query.js +0 -30
  100. package/dist/row-query.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"sql.test.js","sourceRoot":"","sources":["../../src/live-queries/sql.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AAC9G,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AAE7C,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,2BAA2B,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A;AACJ,CAAC,CAAC,CAAA"}
@@ -1,64 +0,0 @@
1
- import type { QueryInfo } from '@livestore/common';
2
- import type * as otel from '@opentelemetry/api';
3
- import { type Atom, type GetAtom, ReactiveGraph, type Thunk } from '../reactive.js';
4
- import type { Store } from '../store/store.js';
5
- import type { QueryDebugInfo, RefreshReason } from '../store/store-types.js';
6
- import type { StackInfo } from '../utils/stack-info.js';
7
- export type ReactivityGraph = ReactiveGraph<RefreshReason, QueryDebugInfo, QueryContext>;
8
- export declare const makeReactivityGraph: () => ReactivityGraph;
9
- export type QueryContext = {
10
- store: Store;
11
- otelTracer: otel.Tracer;
12
- rootOtelContext: otel.Context;
13
- effectsWrapper: (run: () => void) => void;
14
- };
15
- export type UnsubscribeQuery = () => void;
16
- export type GetResult<TQuery extends LiveQueryAny> = TQuery extends LiveQuery<infer TResult, infer _1> ? TResult : unknown;
17
- export type LiveQueryAny = LiveQuery<any, QueryInfo>;
18
- export declare const TypeId: unique symbol;
19
- export type TypeId = typeof TypeId;
20
- export interface LiveQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo.None> {
21
- id: number;
22
- _tag: 'computed' | 'sql' | 'graphql';
23
- [TypeId]: TypeId;
24
- /** This should only be used on a type-level and doesn't hold any value during runtime */
25
- '__result!': TResult;
26
- /** A reactive thunk representing the query results */
27
- results$: Thunk<TResult, QueryContext, RefreshReason>;
28
- label: string;
29
- run: (otelContext?: otel.Context, debugRefreshReason?: RefreshReason) => TResult;
30
- runAndDestroy: (otelContext?: otel.Context, debugRefreshReason?: RefreshReason) => TResult;
31
- destroy(): void;
32
- subscribe(onNewValue: (value: TResult) => void, onUnsubsubscribe?: () => void, options?: {
33
- label?: string;
34
- otelContext?: otel.Context;
35
- }): () => void;
36
- activeSubscriptions: Set<StackInfo>;
37
- queryInfo: TQueryInfo;
38
- runs: number;
39
- executionTimes: number[];
40
- }
41
- export declare abstract class LiveStoreQueryBase<TResult, TQueryInfo extends QueryInfo> implements LiveQuery<TResult, TQueryInfo> {
42
- '__result!': TResult;
43
- id: number;
44
- [TypeId]: TypeId;
45
- abstract _tag: 'computed' | 'sql' | 'graphql';
46
- /** Human-readable label for the query for debugging */
47
- abstract label: string;
48
- abstract results$: Thunk<TResult, QueryContext, RefreshReason>;
49
- activeSubscriptions: Set<StackInfo>;
50
- protected abstract reactivityGraph: ReactivityGraph;
51
- abstract queryInfo: TQueryInfo;
52
- get runs(): number;
53
- executionTimes: number[];
54
- abstract destroy: () => void;
55
- run: (otelContext?: otel.Context, debugRefreshReason?: RefreshReason) => TResult;
56
- runAndDestroy: (otelContext?: otel.Context, debugRefreshReason?: RefreshReason) => TResult;
57
- subscribe: (onNewValue: (value: TResult) => void, onUnsubsubscribe?: () => void, options?: {
58
- label?: string;
59
- otelContext?: otel.Context;
60
- } | undefined) => (() => void);
61
- }
62
- export type GetAtomResult = <T>(atom: Atom<T, any, RefreshReason> | LiveQuery<T, any>) => T;
63
- export declare const makeGetAtomResult: (get: GetAtom, otelContext: otel.Context) => GetAtomResult;
64
- //# sourceMappingURL=base-class.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"base-class.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/base-class.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAClD,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAE/C,OAAO,EAAE,KAAK,IAAI,EAAE,KAAK,OAAO,EAAE,aAAa,EAA2B,KAAK,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC5G,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAA;AAEvD,MAAM,MAAM,eAAe,GAAG,aAAa,CAAC,aAAa,EAAE,cAAc,EAAE,YAAY,CAAC,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,KAAG,OAAO,CAIxF;IAED,SAAS,eACK,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,qBACjB,MAAM,IAAI,YACnB;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,IAAI,CAAC,OAAO,CAAA;KAAE,GAAG,SAAS,KACnE,CAAC,MAAM,IAAI,CAAC,CAEgC;CAChD;AAED,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,EAAE,aAAa,CAAC,GAAG,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;AAE3F,eAAO,MAAM,iBAAiB,QAAS,OAAO,eAAe,IAAI,CAAC,OAAO,kBAOxE,CAAA"}
@@ -1,31 +0,0 @@
1
- import { ReactiveGraph, throwContextNotSetError } from '../reactive.js';
2
- export const makeReactivityGraph = () => new ReactiveGraph();
3
- let queryIdCounter = 0;
4
- export const TypeId = Symbol.for('LiveQuery');
5
- export class LiveStoreQueryBase {
6
- '__result!';
7
- id = queryIdCounter++;
8
- [TypeId] = TypeId;
9
- activeSubscriptions = new Set();
10
- get runs() {
11
- return this.results$.recomputations;
12
- }
13
- executionTimes = [];
14
- run = (otelContext, debugRefreshReason) => this.results$.computeResult(otelContext, debugRefreshReason);
15
- runAndDestroy = (otelContext, debugRefreshReason) => {
16
- const result = this.run(otelContext, debugRefreshReason);
17
- this.destroy();
18
- return result;
19
- };
20
- subscribe = (onNewValue, onUnsubsubscribe, options) => this.reactivityGraph.context?.store.subscribe(this, onNewValue, onUnsubsubscribe, options) ??
21
- throwContextNotSetError(this.reactivityGraph);
22
- }
23
- export const makeGetAtomResult = (get, otelContext) => {
24
- const getAtom = (atom) => {
25
- if (atom._tag === 'thunk' || atom._tag === 'ref')
26
- return get(atom, otelContext);
27
- return get(atom.results$, otelContext);
28
- };
29
- return getAtom;
30
- };
31
- //# sourceMappingURL=base-class.js.map
@@ -1 +0,0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M,CAAC,MAAM,iBAAiB,GAAG,CAAC,GAAY,EAAE,WAAyB,EAAE,EAAE;IAC3E,MAAM,OAAO,GAAkB,CAAC,IAAI,EAAE,EAAE;QACtC,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK;YAAE,OAAO,GAAG,CAAC,IAAI,EAAE,WAAW,CAAC,CAAA;QAC/E,OAAO,GAAG,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAA;IACxC,CAAC,CAAA;IAED,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
@@ -1,26 +0,0 @@
1
- import type { QueryInfo } from '@livestore/common';
2
- import type { Thunk } from '../reactive.js';
3
- import type { RefreshReason } from '../store/store-types.js';
4
- import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js';
5
- import { LiveStoreQueryBase } from './base-class.js';
6
- export declare const computed: <TResult, TQueryInfo extends QueryInfo = QueryInfo.None>(fn: (get: GetAtomResult) => TResult, options?: {
7
- label: string;
8
- reactivityGraph?: ReactivityGraph;
9
- queryInfo?: TQueryInfo;
10
- }) => LiveQuery<TResult, TQueryInfo>;
11
- export declare class LiveStoreJSQuery<TResult, TQueryInfo extends QueryInfo = QueryInfo.None> extends LiveStoreQueryBase<TResult, TQueryInfo> {
12
- _tag: 'computed';
13
- /** A reactive thunk representing the query results */
14
- results$: Thunk<TResult, QueryContext, RefreshReason>;
15
- label: string;
16
- protected reactivityGraph: ReactivityGraph;
17
- queryInfo: TQueryInfo;
18
- constructor({ fn, label, reactivityGraph, queryInfo, }: {
19
- label: string;
20
- fn: (get: GetAtomResult) => TResult;
21
- reactivityGraph?: ReactivityGraph;
22
- queryInfo?: TQueryInfo;
23
- });
24
- destroy: () => void;
25
- }
26
- //# sourceMappingURL=computed.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/computed.ts"],"names":[],"mappings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elemetry/api';
2
- import { globalReactivityGraph } from '../global-state.js';
3
- import { getDurationMsFromSpan } from '../utils/otel.js';
4
- import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js';
5
- export const computed = (fn, options) => new LiveStoreJSQuery({
6
- fn,
7
- label: options?.label ?? fn.toString(),
8
- reactivityGraph: options?.reactivityGraph,
9
- queryInfo: options?.queryInfo,
10
- });
11
- export class LiveStoreJSQuery extends LiveStoreQueryBase {
12
- _tag = 'computed';
13
- /** A reactive thunk representing the query results */
14
- results$;
15
- label;
16
- reactivityGraph;
17
- queryInfo;
18
- constructor({ fn, label, reactivityGraph, queryInfo, }) {
19
- super();
20
- this.label = label;
21
- this.reactivityGraph = reactivityGraph ?? globalReactivityGraph;
22
- this.queryInfo = queryInfo ?? { _tag: 'None' };
23
- const queryLabel = `${label}:results`;
24
- this.results$ = this.reactivityGraph.makeThunk((get, setDebugInfo, { otelTracer, rootOtelContext }, otelContext) => otelTracer.startActiveSpan(`js:${label}`, {}, otelContext ?? rootOtelContext, (span) => {
25
- const otelContext = otel.trace.setSpan(otel.context.active(), span);
26
- const res = fn(makeGetAtomResult(get, otelContext));
27
- span.end();
28
- const durationMs = getDurationMsFromSpan(span);
29
- this.executionTimes.push(durationMs);
30
- setDebugInfo({ _tag: 'computed', label, query: fn.toString(), durationMs });
31
- return res;
32
- }), { label: queryLabel, meta: { liveStoreThunkType: 'computedResults' } });
33
- }
34
- destroy = () => {
35
- this.reactivityGraph.destroyNode(this.results$);
36
- };
37
- }
38
- //# sourceMappingURL=computed.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"computed.js","sourceRoot":"","sources":["../../src/reactiveQueries/computed.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAE1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAG1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAA;AAExD,OAAO,EAAE,kBAAkB,EAAE,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EAAE,WAAW,CAAC,CAAC,CAAA;YAEnD,IAAI,CAAC,GAAG,EAAE,CAAA;YAEV,MAAM,UAAU,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAA;YAE9C,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;YAEpC,YAAY,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,EAAE,EAAE,UAAU,EAAE,CAAC,CAAA;YAE3E,OAAO,GAAG,CAAA;QACZ,CAAC,CAAC,EACJ,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,EAAE,CACvE,CAAA;IACH,CAAC;IAED,OAAO,GAAG,GAAG,EAAE;QACb,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACjD,CAAC,CAAA;CACF"}
@@ -1,49 +0,0 @@
1
- import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
2
- import type { QueryInfo } from '@livestore/common';
3
- import { Schema } from '@livestore/utils/effect';
4
- import * as otel from '@opentelemetry/api';
5
- import * as graphql from 'graphql';
6
- import { type Thunk } from '../reactive.js';
7
- import type { Store } from '../store/store.js';
8
- import type { BaseGraphQLContext, RefreshReason } from '../store/store-types.js';
9
- import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js';
10
- import { LiveStoreQueryBase } from './base-class.js';
11
- export type MapResult<To, From> = ((res: From, get: GetAtomResult) => To) | Schema.Schema<To, From>;
12
- export declare const queryGraphQL: <TResult extends Record<string, any>, TVariableValues extends Record<string, any>, TResultMapped extends Record<string, any> = TResult>(document: DocumentNode<TResult, TVariableValues>, genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues), { label, reactivityGraph, map, }?: {
13
- label?: string;
14
- reactivityGraph?: ReactivityGraph;
15
- map?: MapResult<TResultMapped, TResult>;
16
- }) => LiveQuery<TResultMapped, QueryInfo.None>;
17
- export declare class LiveStoreGraphQLQuery<TResult extends Record<string, any>, TVariableValues extends Record<string, any>, TContext extends BaseGraphQLContext, TResultMapped extends Record<string, any> = TResult> extends LiveStoreQueryBase<TResultMapped, QueryInfo.None> {
18
- _tag: 'graphql';
19
- /** The abstract GraphQL query */
20
- document: DocumentNode<TResult, TVariableValues>;
21
- /** A reactive thunk representing the query results */
22
- results$: Thunk<TResultMapped, QueryContext, RefreshReason>;
23
- variableValues$: Thunk<TVariableValues, QueryContext, RefreshReason> | undefined;
24
- label: string;
25
- protected reactivityGraph: ReactivityGraph;
26
- queryInfo: QueryInfo.None;
27
- private mapResult;
28
- constructor({ document, label, genVariableValues, reactivityGraph, map, }: {
29
- document: DocumentNode<TResult, TVariableValues>;
30
- genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues);
31
- label?: string;
32
- reactivityGraph?: ReactivityGraph;
33
- map?: MapResult<TResultMapped, TResult>;
34
- });
35
- queryOnce: ({ document, otelContext, otelTracer, variableValues, store, get, }: {
36
- document: graphql.DocumentNode;
37
- otelContext: otel.Context;
38
- otelTracer: otel.Tracer;
39
- variableValues: TVariableValues;
40
- store: Store<TContext>;
41
- get: GetAtomResult;
42
- }) => {
43
- result: TResultMapped;
44
- queriedTables: string[];
45
- durationMs: number;
46
- };
47
- destroy: () => void;
48
- }
49
- //# sourceMappingURL=graphql.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,mCAAmC,CAAA;AAC1F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAElD,OAAO,EAAE,MAAM,EAAiB,MAAM,yBAAyB,CAAA;AAC/D,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,O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
1
- import { shouldNeverHappen } from '@livestore/utils';
2
- import { Schema, TreeFormatter } from '@livestore/utils/effect';
3
- import * as otel from '@opentelemetry/api';
4
- import * as graphql from 'graphql';
5
- import { globalReactivityGraph } from '../global-state.js';
6
- import { isThunk } from '../reactive.js';
7
- import { getDurationMsFromSpan } from '../utils/otel.js';
8
- import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js';
9
- export const queryGraphQL = (document, genVariableValues, { label, reactivityGraph, map, } = {}) => new LiveStoreGraphQLQuery({ document, genVariableValues, label, reactivityGraph, map });
10
- export class LiveStoreGraphQLQuery extends LiveStoreQueryBase {
11
- _tag = 'graphql';
12
- /** The abstract GraphQL query */
13
- document;
14
- /** A reactive thunk representing the query results */
15
- results$;
16
- variableValues$;
17
- label;
18
- reactivityGraph;
19
- queryInfo = { _tag: 'None' };
20
- mapResult;
21
- constructor({ document, label, genVariableValues, reactivityGraph, map, }) {
22
- super();
23
- const labelWithDefault = label ?? graphql.getOperationAST(document)?.name?.value ?? 'graphql';
24
- this.label = labelWithDefault;
25
- this.document = document;
26
- this.reactivityGraph = reactivityGraph ?? globalReactivityGraph;
27
- this.mapResult =
28
- map === undefined
29
- ? (res) => res
30
- : Schema.isSchema(map)
31
- ? (res) => {
32
- const parseResult = Schema.decodeEither(map)(res);
33
- if (parseResult._tag === 'Left') {
34
- console.error(`Error parsing GraphQL query result: ${TreeFormatter.formatErrorSync(parseResult.left)}`);
35
- return shouldNeverHappen(`Error parsing SQL query result: ${parseResult.left}`);
36
- }
37
- else {
38
- return parseResult.right;
39
- }
40
- }
41
- : typeof map === 'function'
42
- ? map
43
- : shouldNeverHappen(`Invalid map function ${map}`);
44
- // TODO don't even create a thunk if variables are static
45
- let variableValues$OrvariableValues;
46
- if (typeof genVariableValues === 'function') {
47
- variableValues$OrvariableValues = this.reactivityGraph.makeThunk((get, _setDebugInfo, { rootOtelContext }, otelContext) => {
48
- return genVariableValues(makeGetAtomResult(get, otelContext ?? rootOtelContext));
49
- }, { label: `${labelWithDefault}:variableValues`, meta: { liveStoreThunkType: 'graphqlQuery' } });
50
- this.variableValues$ = variableValues$OrvariableValues;
51
- }
52
- else {
53
- variableValues$OrvariableValues = genVariableValues;
54
- }
55
- const resultsLabel = `${labelWithDefault}:results`;
56
- this.results$ = this.reactivityGraph.makeThunk((get, setDebugInfo, { store, otelTracer, rootOtelContext }, otelContext) => {
57
- const variableValues = isThunk(variableValues$OrvariableValues)
58
- ? get(variableValues$OrvariableValues)
59
- : variableValues$OrvariableValues;
60
- const { result, queriedTables, durationMs } = this.queryOnce({
61
- document,
62
- variableValues,
63
- otelContext: otelContext ?? rootOtelContext,
64
- otelTracer,
65
- store: store,
66
- get: makeGetAtomResult(get, otelContext ?? rootOtelContext),
67
- });
68
- // Add dependencies on any tables that were used
69
- for (const tableName of queriedTables) {
70
- const tableRef = store.tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`);
71
- get(tableRef);
72
- }
73
- setDebugInfo({ _tag: 'graphql', label: resultsLabel, query: graphql.print(document), durationMs });
74
- return result;
75
- }, { label: resultsLabel, meta: { liveStoreThunkType: 'graphqlResults' } });
76
- }
77
- queryOnce = ({ document, otelContext, otelTracer, variableValues, store, get, }) => {
78
- const schema = store.graphQLSchema ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL schema");
79
- const context = store.graphQLContext ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL context");
80
- const operationName = graphql.getOperationAST(document)?.name?.value;
81
- return otelTracer.startActiveSpan(`executeGraphQLQuery: ${operationName}`, {}, otelContext, (span) => {
82
- span.setAttribute('graphql.variables', JSON.stringify(variableValues));
83
- span.setAttribute('graphql.query', graphql.print(document));
84
- context.queriedTables.clear();
85
- context.otelContext = otel.trace.setSpan(otel.context.active(), span);
86
- const res = graphql.executeSync({
87
- document,
88
- contextValue: context,
89
- schema: schema,
90
- variableValues,
91
- });
92
- // TODO track number of nested SQL queries via Otel + debug info
93
- if (res.errors) {
94
- span.setStatus({ code: otel.SpanStatusCode.ERROR, message: 'GraphQL error' });
95
- span.setAttribute('graphql.error', res.errors.join('\n'));
96
- span.setAttribute('graphql.error-detail', JSON.stringify(res.errors));
97
- console.error(`graphql error (${operationName}) - ${res.errors.length} errors`);
98
- for (const error of res.errors) {
99
- console.error(error);
100
- }
101
- debugger;
102
- shouldNeverHappen(`GraphQL error: ${res.errors.join('\n')}`);
103
- }
104
- span.end();
105
- const result = this.mapResult(res.data, get);
106
- const durationMs = getDurationMsFromSpan(span);
107
- this.executionTimes.push(durationMs);
108
- return {
109
- result,
110
- queriedTables: Array.from(context.queriedTables.values()),
111
- durationMs,
112
- };
113
- });
114
- };
115
- destroy = () => {
116
- if (this.variableValues$ !== undefined) {
117
- this.reactivityGraph.destroyNode(this.variableValues$);
118
- }
119
- this.reactivityGraph.destroyNode(this.results$);
120
- };
121
- }
122
- //# sourceMappingURL=graphql.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graphql.js","sourceRoot":"","sources":["../../src/reactiveQueries/graphql.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAElC,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,EAAE,OAAO,EAAc,MAAM,gBAAgB,CAAA;AAGpD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAA;AAExD,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAIvE,MAAM,CAAC,MAAM,YAAY,GAAG,CAK1B,QAAgD,EAChD,iBAA8E,EAC9E,EACE,KAAK,EACL,eAAe,EACf,GAAG,MAKD,EAAE,EACoC,EAAE,CAC5C,IAAI,qBAAq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,GAAG,EAAE,CAAA;YAEV,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAA0B,EAAE,GAAG,CAAC,CAAA;YAElE,MAAM,UAAU,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAA;YAE9C,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;YAEpC,OAAO;gBACL,MAAM;gBACN,aAAa,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,EAAE,CAAC;gBACzD,UAAU;aACX,CAAA;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;IAED,OAAO,GAAG,GAAG,EAAE;QACb,IAAI,IAAI,CAAC,eAAe,KAAK,SAAS,EAAE,CAAC;YACvC,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;QACxD,CAAC;QAED,IAAI,CAAC,eAAe,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACjD,CAAC,CAAA;CACF"}
@@ -1,62 +0,0 @@
1
- import type { Bindable, QueryBuilder, QueryInfo } from '@livestore/common';
2
- import { Schema } from '@livestore/utils/effect';
3
- import type { Thunk } from '../reactive.js';
4
- import type { RefreshReason } from '../store/store-types.js';
5
- import type { GetAtomResult, LiveQuery, QueryContext, ReactivityGraph } from './base-class.js';
6
- import { LiveStoreQueryBase } from './base-class.js';
7
- export type QueryInputRaw<TDecoded, TEncoded, TQueryInfo extends QueryInfo> = {
8
- query: string;
9
- schema: Schema.Schema<TDecoded, TEncoded>;
10
- bindValues?: Bindable;
11
- /**
12
- * Can be provided explicitly to slightly speed up initial query performance
13
- *
14
- * NOTE In the future we want to do this automatically at build time
15
- */
16
- queriedTables?: Set<string>;
17
- queryInfo?: TQueryInfo;
18
- execBeforeFirstRun?: (ctx: QueryContext) => void;
19
- };
20
- export type QueryInput<TDecoded, TEncoded, TQueryInfo extends QueryInfo> = QueryInputRaw<TDecoded, TEncoded, TQueryInfo> | QueryBuilder<TDecoded, any, any, TQueryInfo>;
21
- /**
22
- * NOTE `query` is only supposed to read data. Don't use it to insert/update/delete data but use mutations instead.
23
- */
24
- export declare const queryDb: {
25
- <TResultSchema, TResult = TResultSchema, TQueryInfo extends QueryInfo = QueryInfo.None>(queryInput: QueryInputRaw<TResultSchema, ReadonlyArray<any>, TQueryInfo> | QueryBuilder<TResultSchema, any, any, TQueryInfo>, options?: {
26
- map?: (rows: TResultSchema) => TResult;
27
- /**
28
- * Used for debugging / devtools
29
- */
30
- label?: string;
31
- reactivityGraph?: ReactivityGraph;
32
- }): LiveQuery<TResult, TQueryInfo>;
33
- <TResultSchema, TResult = TResultSchema, TQueryInfo extends QueryInfo = QueryInfo.None>(queryInput: ((get: GetAtomResult) => QueryInputRaw<TResultSchema, ReadonlyArray<any>, TQueryInfo>) | ((get: GetAtomResult) => QueryBuilder<TResultSchema, any, any, TQueryInfo>), options?: {
34
- map?: (rows: TResultSchema) => TResult;
35
- /**
36
- * Used for debugging / devtools
37
- */
38
- label?: string;
39
- reactivityGraph?: ReactivityGraph;
40
- queryInfo?: TQueryInfo;
41
- }): LiveQuery<TResult, TQueryInfo>;
42
- };
43
- export declare class LiveStoreSQLQuery<TResultSchema, TResult = TResultSchema, TQueryInfo extends QueryInfo = QueryInfo.None> extends LiveStoreQueryBase<TResult, TQueryInfo> {
44
- _tag: 'sql';
45
- /** A reactive thunk representing the query text */
46
- queryInput$: Thunk<QueryInput<TResultSchema, ReadonlyArray<any>, TQueryInfo>, QueryContext, RefreshReason> | undefined;
47
- /** A reactive thunk representing the query results */
48
- results$: Thunk<TResult, QueryContext, RefreshReason>;
49
- label: string;
50
- queryInfo: TQueryInfo;
51
- protected reactivityGraph: ReactivityGraph;
52
- private mapResult;
53
- constructor({ queryInput, label: inputLabel, reactivityGraph, map, queryInfo: queryInfo_, }: {
54
- label?: string;
55
- queryInput: QueryInput<TResultSchema, ReadonlyArray<any>, TQueryInfo> | ((get: GetAtomResult, ctx: QueryContext) => QueryInput<TResultSchema, ReadonlyArray<any>, TQueryInfo>);
56
- reactivityGraph?: ReactivityGraph;
57
- map?: (rows: TResultSchema) => TResult;
58
- queryInfo?: TQueryInfo;
59
- });
60
- destroy: () => void;
61
- }
62
- //# sourceMappingURL=sql.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sql.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/sql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAS1E,OAAO,EAAa,MAAM,EAAiB,MAAM,yBAAyB,CAAA;AAI1E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAG3C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAA;AAE5D,OAAO,KAAK,EAAE,aAAa,EAAE,SAAS,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAC9F,OAAO,EAAE,kBAAkB,EAAqB,MAAM,iBAAiB,CAAA;AAEvE,MAAM,MAAM,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,SAAS,SAAS,IAAI;IAC5E,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;IACzC,UAAU,CAAC,EAAE,QAAQ,CAAA;IACrB;;;;OAIG;IACH,aAAa,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC3B,SAAS,CAAC,EAAE,UAAU,CAAA;IACtB,kBAAkB,CAAC,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,IAAI,CAAA;CACjD,CAAA;AAED,MAAM,MAAM,UAAU,CAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,SAAS,SAAS,IACnE,aAAa,CAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,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AAO,GAAG,aAAa,EACvB,UAAU,SAAS,SAAS,GAAG,SAAS,CAAC,IAAI,CAC7C,SAAQ,kBAAkB,CAAC,OAAO,EAAE,UAAU,CAAC;IAC/C,IAAI,EAAE,KAAK,CAAQ;IAEnB,mDAAmD;IACnD,WAAW,EAAE,KAAK,CAAC,UAAU,CAAC,aAAa,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,EAAE,YAAY,EAAE,aAAa,CAAC,GAAG,SAAS,CAAA;IAEtH,sDAAsD;IACtD,QAAQ,EAAE,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,aAAa,CAAC,CAAA;IAErD,KAAK,EAAE,MAAM,CAAA;IAGb,SAAS,EAAE,UAAU,CAAA;IAErB,SAAS,CAAC,eAAe,kBAAA;IAEzB,OAAO,CAAC,SAAS,CAAkC;gBAEvC,EACV,UAAU,EACV,KAAK,EAAE,UAAU,EACjB,eAAe,EACf,GAAG,EACH,SAAS,EAAE,UAAU,GACtB,EAAE;QACD,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,UAAU,EACN,UAAU,CAAC,aAAa,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,GACzD,CAAC,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,EAAE,YAAY,KAAK,UAAU,CAAC,aAAa,EAAE,aAAa,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC,CAAC,CAAA;QAC1G,eAAe,CAAC,EAAE,eAAe,CAAA;QACjC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK,OAAO,CAAA;QACtC,SAAS,CAAC,EAAE,UAAU,CAAA;KACvB;IAmMD,OAAO,aAMN;CACF"}
@@ -1,175 +0,0 @@
1
- import { getResultSchema, isQueryBuilder, prepareBindValues, QueryBuilderAstSymbol, replaceSessionIdSymbol, } from '@livestore/common';
2
- import { deepEqual, shouldNeverHappen } from '@livestore/utils';
3
- import { Predicate, Schema, TreeFormatter } from '@livestore/utils/effect';
4
- import * as otel from '@opentelemetry/api';
5
- import { globalReactivityGraph } from '../global-state.js';
6
- import { isThunk, NOT_REFRESHED_YET } from '../reactive.js';
7
- import { makeExecBeforeFirstRun, rowQueryLabel } from '../row-query.js';
8
- import { getDurationMsFromSpan } from '../utils/otel.js';
9
- import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js';
10
- /**
11
- * NOTE `query` is only supposed to read data. Don't use it to insert/update/delete data but use mutations instead.
12
- */
13
- export const queryDb = (queryInput, options) => new LiveStoreSQLQuery({
14
- queryInput,
15
- label: options?.label,
16
- reactivityGraph: options?.reactivityGraph,
17
- map: options?.map,
18
- queryInfo: Predicate.hasProperty(options, 'queryInfo') ? options.queryInfo : undefined,
19
- });
20
- /* An object encapsulating a reactive SQL query */
21
- export class LiveStoreSQLQuery extends LiveStoreQueryBase {
22
- _tag = 'sql';
23
- /** A reactive thunk representing the query text */
24
- queryInput$;
25
- /** A reactive thunk representing the query results */
26
- results$;
27
- label;
28
- // TODO
29
- queryInfo;
30
- reactivityGraph;
31
- mapResult;
32
- constructor({ queryInput, label: inputLabel, reactivityGraph, map, queryInfo: queryInfo_, }) {
33
- super();
34
- // TODO implement a proper toString method
35
- this.label = inputLabel ? `sql(${inputLabel})` : `sql(${queryInput.toString()})`;
36
- this.reactivityGraph = reactivityGraph ?? globalReactivityGraph;
37
- this.mapResult = map === undefined ? (rows) => rows : map;
38
- const schemaRef = {
39
- current: typeof queryInput === 'function' ? undefined : isQueryBuilder(queryInput) ? undefined : queryInput.schema,
40
- };
41
- const execBeforeFirstRunRef = {
42
- current: undefined,
43
- };
44
- let queryInputRaw$OrQueryInputRaw;
45
- const queryBuilderToQueryInputRaw = (qb) => {
46
- const qbRes = qb.asSql();
47
- const schema = getResultSchema(qb);
48
- const ast = qb[QueryBuilderAstSymbol];
49
- if (ast._tag === 'RowQuery') {
50
- execBeforeFirstRunRef.current = makeExecBeforeFirstRun({
51
- table: ast.tableDef,
52
- insertValues: ast.insertValues,
53
- id: ast.id,
54
- });
55
- this.label = rowQueryLabel(ast.tableDef, ast.id);
56
- }
57
- return {
58
- query: qbRes.query,
59
- schema,
60
- bindValues: qbRes.bindValues,
61
- queriedTables: new Set([ast.tableDef.sqliteDef.name]),
62
- queryInfo: ast._tag === 'RowQuery' ? { _tag: 'Row', table: ast.tableDef, id: ast.id } : { _tag: 'None' },
63
- };
64
- };
65
- if (typeof queryInput === 'function') {
66
- queryInputRaw$OrQueryInputRaw = this.reactivityGraph.makeThunk((get, setDebugInfo, ctx, otelContext) => {
67
- const startMs = performance.now();
68
- const queryInputResult = queryInput(makeGetAtomResult(get, otelContext ?? ctx.rootOtelContext), ctx);
69
- const durationMs = performance.now() - startMs;
70
- const queryInputRaw = isQueryBuilder(queryInputResult)
71
- ? queryBuilderToQueryInputRaw(queryInputResult)
72
- : queryInputResult;
73
- setDebugInfo({ _tag: 'computed', label: `${this.label}:queryInput`, query: queryInputRaw.query, durationMs });
74
- schemaRef.current = queryInputRaw.schema;
75
- this.queryInfo = queryInputRaw.queryInfo;
76
- return queryInputRaw;
77
- }, {
78
- label: `${this.label}:query`,
79
- // TODO adjust to `sqlQueryInput` + adjust in devtools
80
- meta: { liveStoreThunkType: 'sqlQuery' },
81
- // NOTE we're not checking the schema here as we assume the query string to always change when the schema might change
82
- equal: (a, b) => a.query === b.query && deepEqual(a.bindValues, b.bindValues),
83
- });
84
- // TODO come up with a better way to handle this
85
- this.queryInfo = { _tag: 'None' };
86
- }
87
- else {
88
- const queryInputRaw = isQueryBuilder(queryInput) ? queryBuilderToQueryInputRaw(queryInput) : queryInput;
89
- schemaRef.current = queryInputRaw.schema;
90
- queryInputRaw$OrQueryInputRaw = queryInputRaw;
91
- // this.label = inputLabel ? this.label : `sql(${})`
92
- if (inputLabel === undefined && isQueryBuilder(queryInput)) {
93
- const ast = queryInput[QueryBuilderAstSymbol];
94
- if (ast._tag === 'RowQuery') {
95
- this.label = `sql(${rowQueryLabel(ast.tableDef, ast.id)})`;
96
- }
97
- }
98
- this.queryInfo = queryInputRaw.queryInfo;
99
- }
100
- // TODO
101
- const label = this.label;
102
- const queriedTablesRef = { current: undefined };
103
- // const schemaEqual = Schema.equivalence(schema)
104
- // TODO also support derived equality for `map` (probably will depend on having an easy way to transform a schema without an `encode` step)
105
- // This would mean dropping the `map` option
106
- const resultsEqual = map === undefined
107
- ? (a, b) => a === NOT_REFRESHED_YET || b === NOT_REFRESHED_YET ? false : Schema.equivalence(schemaRef.current)(a, b)
108
- : undefined;
109
- const results$ = this.reactivityGraph.makeThunk((get, setDebugInfo, queryContext, otelContext) => queryContext.otelTracer.startActiveSpan('sql:...', // NOTE span name will be overridden further down
110
- {}, otelContext ?? queryContext.rootOtelContext, (span) => {
111
- const otelContext = otel.trace.setSpan(otel.context.active(), span);
112
- const { store } = queryContext;
113
- if (execBeforeFirstRunRef.current !== undefined) {
114
- execBeforeFirstRunRef.current(queryContext, otelContext);
115
- execBeforeFirstRunRef.current = undefined;
116
- }
117
- const queryInputResult = isThunk(queryInputRaw$OrQueryInputRaw)
118
- ? get(queryInputRaw$OrQueryInputRaw, otelContext)
119
- : queryInputRaw$OrQueryInputRaw;
120
- const sqlString = queryInputResult.query;
121
- const bindValues = queryInputResult.bindValues;
122
- if (queriedTablesRef.current === undefined) {
123
- queriedTablesRef.current = store.syncDbWrapper.getTablesUsed(sqlString);
124
- }
125
- if (bindValues !== undefined) {
126
- replaceSessionIdSymbol(bindValues, store.clientSession.coordinator.sessionId);
127
- }
128
- // Establish a reactive dependency on the tables used in the query
129
- for (const tableName of queriedTablesRef.current) {
130
- const tableRef = store.tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`);
131
- get(tableRef, otelContext);
132
- }
133
- span.setAttribute('sql.query', sqlString);
134
- span.updateName(`sql:${sqlString.slice(0, 50)}`);
135
- const rawDbResults = store.syncDbWrapper.select(sqlString, {
136
- queriedTables: queriedTablesRef.current,
137
- bindValues: bindValues ? prepareBindValues(bindValues, sqlString) : undefined,
138
- otelContext,
139
- });
140
- span.setAttribute('sql.rowsCount', rawDbResults.length);
141
- const parsedResult = Schema.decodeEither(schemaRef.current)(rawDbResults);
142
- if (parsedResult._tag === 'Left') {
143
- const parseErrorStr = TreeFormatter.formatErrorSync(parsedResult.left);
144
- const expectedSchemaStr = String(schemaRef.current.ast);
145
- const bindValuesStr = bindValues === undefined ? '' : `\nBind values: ${JSON.stringify(bindValues)}`;
146
- console.error(`\
147
- Error parsing SQL query result.
148
-
149
- Query: ${sqlString}\
150
- ${bindValuesStr}
151
-
152
- Expected schema: ${expectedSchemaStr}
153
-
154
- Error: ${parseErrorStr}
155
-
156
- Result:`, rawDbResults);
157
- return shouldNeverHappen(`Error parsing SQL query result: ${parsedResult.left}`);
158
- }
159
- const result = this.mapResult(parsedResult.right);
160
- span.end();
161
- const durationMs = getDurationMsFromSpan(span);
162
- this.executionTimes.push(durationMs);
163
- setDebugInfo({ _tag: 'sql', label: `${label}:results`, query: sqlString, durationMs });
164
- return result;
165
- }), { label: `${label}:results`, meta: { liveStoreThunkType: 'sqlResult' }, equal: resultsEqual });
166
- this.results$ = results$;
167
- }
168
- destroy = () => {
169
- if (this.queryInput$ !== undefined) {
170
- this.reactivityGraph.destroyNode(this.queryInput$);
171
- }
172
- this.reactivityGraph.destroyNode(this.results$);
173
- };
174
- }
175
- //# sourceMappingURL=sql.js.map