@livestore/livestore 0.0.16 → 0.0.21
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/README.md +18 -21
- package/dist/.tsbuildinfo +1 -1
- package/dist/QueryCache.d.ts +1 -1
- package/dist/QueryCache.d.ts.map +1 -1
- package/dist/QueryCache.js.map +1 -1
- package/dist/__tests__/react/fixture.d.ts +5 -4
- package/dist/__tests__/react/fixture.d.ts.map +1 -1
- package/dist/__tests__/react/fixture.js +5 -5
- package/dist/__tests__/react/fixture.js.map +1 -1
- package/dist/__tests__/react/useComponentState.test.d.ts +2 -0
- package/dist/__tests__/react/useComponentState.test.d.ts.map +1 -0
- package/dist/__tests__/react/useComponentState.test.js +68 -0
- package/dist/__tests__/react/useComponentState.test.js.map +1 -0
- package/dist/__tests__/react/useLQuery.test.d.ts +2 -0
- package/dist/__tests__/react/useLQuery.test.d.ts.map +1 -0
- package/dist/__tests__/react/useLQuery.test.js +38 -0
- package/dist/__tests__/react/useLQuery.test.js.map +1 -0
- package/dist/__tests__/react/useLiveStoreComponent.test.js +4 -9
- package/dist/__tests__/react/useLiveStoreComponent.test.js.map +1 -1
- package/dist/__tests__/react/useQuery.test.d.ts +2 -0
- package/dist/__tests__/react/useQuery.test.d.ts.map +1 -0
- package/dist/__tests__/react/useQuery.test.js +33 -0
- package/dist/__tests__/react/useQuery.test.js.map +1 -0
- package/dist/__tests__/react/utils/extractStackInfoFromStackTrace.test.d.ts +2 -0
- package/dist/__tests__/react/utils/extractStackInfoFromStackTrace.test.d.ts.map +1 -0
- package/dist/__tests__/react/utils/extractStackInfoFromStackTrace.test.js +38 -0
- package/dist/__tests__/react/utils/extractStackInfoFromStackTrace.test.js.map +1 -0
- package/dist/__tests__/reactive.test.js +167 -93
- package/dist/__tests__/reactive.test.js.map +1 -1
- package/dist/__tests__/reactiveQueries/sql.test.d.ts +2 -0
- package/dist/__tests__/reactiveQueries/sql.test.d.ts.map +1 -0
- package/dist/__tests__/reactiveQueries/sql.test.js +337 -0
- package/dist/__tests__/reactiveQueries/sql.test.js.map +1 -0
- package/dist/inMemoryDatabase.d.ts +2 -2
- package/dist/inMemoryDatabase.d.ts.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/react/index.d.ts +3 -3
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +2 -2
- package/dist/react/index.js.map +1 -1
- package/dist/react/useComponentState.d.ts +50 -0
- package/dist/react/useComponentState.d.ts.map +1 -0
- package/dist/react/useComponentState.js +248 -0
- package/dist/react/useComponentState.js.map +1 -0
- package/dist/react/useGlobalQuery.d.ts +3 -0
- package/dist/react/useGlobalQuery.d.ts.map +1 -0
- package/dist/react/useGlobalQuery.js +26 -0
- package/dist/react/useGlobalQuery.js.map +1 -0
- package/dist/react/useGraphQL.d.ts +3 -3
- package/dist/react/useGraphQL.d.ts.map +1 -1
- package/dist/react/useGraphQL.js +10 -8
- package/dist/react/useGraphQL.js.map +1 -1
- package/dist/react/useLiveStoreComponent.d.ts +6 -6
- package/dist/react/useLiveStoreComponent.d.ts.map +1 -1
- package/dist/react/useLiveStoreComponent.js +143 -99
- package/dist/react/useLiveStoreComponent.js.map +1 -1
- package/dist/react/useQuery.d.ts +2 -2
- package/dist/react/useQuery.d.ts.map +1 -1
- package/dist/react/useQuery.js +26 -22
- package/dist/react/useQuery.js.map +1 -1
- package/dist/react/useTemporaryQuery.d.ts +8 -0
- package/dist/react/useTemporaryQuery.d.ts.map +1 -0
- package/dist/react/useTemporaryQuery.js +17 -0
- package/dist/react/useTemporaryQuery.js.map +1 -0
- package/dist/react/utils/extractNamesFromStackTrace.d.ts +3 -0
- package/dist/react/utils/extractNamesFromStackTrace.d.ts.map +1 -0
- package/dist/react/utils/extractNamesFromStackTrace.js +40 -0
- package/dist/react/utils/extractNamesFromStackTrace.js.map +1 -0
- package/dist/react/utils/extractStackInfoFromStackTrace.d.ts +7 -0
- package/dist/react/utils/extractStackInfoFromStackTrace.d.ts.map +1 -0
- package/dist/react/utils/extractStackInfoFromStackTrace.js +40 -0
- package/dist/react/utils/extractStackInfoFromStackTrace.js.map +1 -0
- package/dist/reactive.d.ts +42 -48
- package/dist/reactive.d.ts.map +1 -1
- package/dist/reactive.js +293 -186
- package/dist/reactive.js.map +1 -1
- package/dist/reactiveQueries/base-class.d.ts +28 -23
- package/dist/reactiveQueries/base-class.d.ts.map +1 -1
- package/dist/reactiveQueries/base-class.js +25 -18
- package/dist/reactiveQueries/base-class.js.map +1 -1
- package/dist/reactiveQueries/graph.d.ts +10 -0
- package/dist/reactiveQueries/graph.d.ts.map +1 -0
- package/dist/reactiveQueries/graph.js +6 -0
- package/dist/reactiveQueries/graph.js.map +1 -0
- package/dist/reactiveQueries/graphql.d.ts +34 -17
- package/dist/reactiveQueries/graphql.d.ts.map +1 -1
- package/dist/reactiveQueries/graphql.js +91 -10
- package/dist/reactiveQueries/graphql.js.map +1 -1
- package/dist/reactiveQueries/js.d.ts +16 -12
- package/dist/reactiveQueries/js.d.ts.map +1 -1
- package/dist/reactiveQueries/js.js +31 -8
- package/dist/reactiveQueries/js.js.map +1 -1
- package/dist/reactiveQueries/sql.d.ts +22 -18
- package/dist/reactiveQueries/sql.d.ts.map +1 -1
- package/dist/reactiveQueries/sql.js +82 -16
- package/dist/reactiveQueries/sql.js.map +1 -1
- package/dist/store.d.ts +12 -52
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +283 -264
- package/dist/store.js.map +1 -1
- package/package.json +10 -9
- package/src/QueryCache.ts +1 -1
- package/src/__tests__/react/fixture.tsx +12 -7
- package/src/__tests__/react/{useLiveStoreComponent.test.tsx → useComponentState.test.tsx} +9 -20
- package/src/__tests__/react/useQuery.test.tsx +48 -0
- package/src/__tests__/react/utils/extractStackInfoFromStackTrace.test.ts +40 -0
- package/src/__tests__/reactive.test.ts +193 -140
- package/src/__tests__/reactiveQueries/sql.test.ts +372 -0
- package/src/inMemoryDatabase.ts +2 -2
- package/src/index.ts +7 -11
- package/src/react/index.ts +3 -7
- package/src/react/{useLiveStoreComponent.ts → useComponentState.ts} +89 -247
- package/src/react/useQuery.ts +29 -27
- package/src/react/useTemporaryQuery.ts +21 -0
- package/src/react/utils/extractStackInfoFromStackTrace.ts +47 -0
- package/src/reactive.ts +385 -268
- package/src/reactiveQueries/base-class.ts +60 -44
- package/src/reactiveQueries/graph.ts +15 -0
- package/src/reactiveQueries/graphql.ts +145 -29
- package/src/reactiveQueries/js.ts +53 -20
- package/src/reactiveQueries/sql.ts +129 -36
- package/src/store.ts +338 -408
- package/src/react/useGraphQL.ts +0 -138
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
import * as otel from '@opentelemetry/api';
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
1
|
+
import type * as otel from '@opentelemetry/api';
|
|
2
|
+
import type { StackInfo } from '../react/utils/extractStackInfoFromStackTrace.js';
|
|
3
|
+
import type { Atom, GetAtom, Thunk } from '../reactive.js';
|
|
4
|
+
import { type DbContext } from './graph.js';
|
|
5
|
+
import type { LiveStoreJSQuery } from './js.js';
|
|
4
6
|
export type UnsubscribeQuery = () => void;
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
export interface ILiveStoreQuery<TResult> {
|
|
8
|
+
id: number;
|
|
9
|
+
/** A reactive thunk representing the query results */
|
|
10
|
+
results$: Thunk<TResult, DbContext>;
|
|
9
11
|
label: string;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
run: (otelContext?: otel.Context) => TResult;
|
|
13
|
+
destroy(): void;
|
|
14
|
+
activeSubscriptions: Set<SubscriberInfo>;
|
|
15
|
+
}
|
|
16
|
+
export type SubscriberInfo = {
|
|
17
|
+
stack: StackInfo[];
|
|
18
|
+
};
|
|
19
|
+
export declare abstract class LiveStoreQueryBase<TResult> implements ILiveStoreQuery<TResult> {
|
|
20
|
+
id: number;
|
|
21
|
+
/** Human-readable label for the query for debugging */
|
|
22
|
+
abstract label: string;
|
|
23
|
+
abstract results$: Thunk<TResult, DbContext>;
|
|
24
|
+
activeSubscriptions: Set<SubscriberInfo>;
|
|
25
|
+
get runs(): number;
|
|
26
|
+
abstract destroy: () => void;
|
|
27
|
+
run: (otelContext?: otel.Context) => TResult;
|
|
28
|
+
runAndDestroy: (otelContext?: otel.Context) => TResult;
|
|
26
29
|
}
|
|
30
|
+
export type GetAtomResult = <T>(atom: Atom<T, any> | LiveStoreJSQuery<T>) => T;
|
|
31
|
+
export declare const makeGetAtomResult: (get: GetAtom, otelContext: otel.Context) => GetAtomResult;
|
|
27
32
|
//# sourceMappingURL=base-class.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-class.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/base-class.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"base-class.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/base-class.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAE/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kDAAkD,CAAA;AACjF,OAAO,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE/C,MAAM,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAA;AAIzC,MAAM,WAAW,eAA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|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
constructor() {
|
|
4
|
+
this.id = queryIdCounter++;
|
|
5
|
+
this.activeSubscriptions = new Set();
|
|
6
|
+
// subscribe = (
|
|
7
|
+
// onNewValue: (value: TResult) => void,
|
|
8
|
+
// onSubsubscribe?: () => void,
|
|
9
|
+
// options?: { label?: string } | undefined,
|
|
10
|
+
// ): (() => void) => this.store.subscribe(this as any, onNewValue as any, onSubsubscribe, options)
|
|
11
|
+
this.run = (otelContext) => this.results$.computeResult(otelContext);
|
|
12
|
+
this.runAndDestroy = (otelContext) => {
|
|
13
|
+
const result = this.run(otelContext);
|
|
14
|
+
this.destroy();
|
|
15
|
+
return result;
|
|
15
16
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
this.store = store;
|
|
20
|
-
this.otelContext = otelContext;
|
|
17
|
+
}
|
|
18
|
+
get runs() {
|
|
19
|
+
return this.results$.recomputations;
|
|
21
20
|
}
|
|
22
21
|
}
|
|
22
|
+
export const makeGetAtomResult = (get, otelContext) => {
|
|
23
|
+
const getAtom = (atom) => {
|
|
24
|
+
if (atom._tag === 'thunk' || atom._tag === 'ref')
|
|
25
|
+
return get(atom, otelContext);
|
|
26
|
+
return get(atom.results$, otelContext);
|
|
27
|
+
};
|
|
28
|
+
return getAtom;
|
|
29
|
+
};
|
|
23
30
|
//# sourceMappingURL=base-class.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-class.js","sourceRoot":"","sources":["../../src/reactiveQueries/base-class.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base-class.js","sourceRoot":"","sources":["../../src/reactiveQueries/base-class.ts"],"names":[],"mappings":"AASA,IAAI,cAAc,GAAG,CAAC,CAAA;AAqBtB,MAAM,OAAgB,kBAAkB;IAAxC;QACE,OAAE,GAAG,cAAc,EAAE,CAAA;QAOrB,wBAAmB,GAAwB,IAAI,GAAG,EAAE,CAAA;QAQpD,gBAAgB;QAChB,0CAA0C;QAC1C,iCAAiC;QACjC,8CAA8C;QAC9C,mGAAmG;QAEnG,QAAG,GAAG,CAAC,WAA0B,EAAW,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,aAAa,CAAC,WAAW,CAAC,CAAA;QAEvF,kBAAa,GAAG,CAAC,WAA0B,EAAW,EAAE;YACtD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;YACpC,IAAI,CAAC,OAAO,EAAE,CAAA;YACd,OAAO,MAAM,CAAA;QACf,CAAC,CAAA;IACH,CAAC;IAnBC,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAA;IACrC,CAAC;CAiBF;AAID,MAAM,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"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type * as otel from '@opentelemetry/api';
|
|
2
|
+
import { ReactiveGraph } from '../reactive.js';
|
|
3
|
+
import type { QueryDebugInfo, RefreshReason, Store } from '../store.js';
|
|
4
|
+
export type DbContext = {
|
|
5
|
+
store: Store;
|
|
6
|
+
otelTracer: otel.Tracer;
|
|
7
|
+
rootOtelContext: otel.Context;
|
|
8
|
+
};
|
|
9
|
+
export declare const dbGraph: ReactiveGraph<RefreshReason, QueryDebugInfo, DbContext>;
|
|
10
|
+
//# sourceMappingURL=graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAG/C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAEvE,MAAM,MAAM,SAAS,GAAG;IACtB,KAAK,EAAE,KAAK,CAAA;IACZ,UAAU,EAAE,IAAI,CAAC,MAAM,CAAA;IACvB,eAAe,EAAE,IAAI,CAAC,OAAO,CAAA;CAC9B,CAAA;AAED,eAAO,MAAM,OAAO,yDAElB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph.js","sourceRoot":"","sources":["../../src/reactiveQueries/graph.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,WAAW,CAAA;AAEhC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAS9C,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,aAAa,CAA2C;IACjF,cAAc,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,uBAAuB,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;CACvE,CAAC,CAAA"}
|
|
@@ -1,25 +1,42 @@
|
|
|
1
1
|
import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import * as otel from '@opentelemetry/api';
|
|
3
|
+
import * as graphql from 'graphql';
|
|
4
4
|
import type { Thunk } from '../reactive.js';
|
|
5
|
-
import type
|
|
6
|
-
import { LiveStoreQueryBase } from './base-class.js';
|
|
7
|
-
import type
|
|
8
|
-
|
|
5
|
+
import { type BaseGraphQLContext, type Store } from '../store.js';
|
|
6
|
+
import { type GetAtomResult, LiveStoreQueryBase } from './base-class.js';
|
|
7
|
+
import { type DbContext } from './graph.js';
|
|
8
|
+
import { LiveStoreJSQuery } from './js.js';
|
|
9
|
+
export declare const queryGraphQL: <TResult extends Record<string, any>, TVariableValues extends Record<string, any>>(document: DocumentNode<TResult, TVariableValues>, genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues), { label }?: {
|
|
10
|
+
label?: string | undefined;
|
|
11
|
+
}) => LiveStoreGraphQLQuery<TResult, TVariableValues, BaseGraphQLContext>;
|
|
12
|
+
export declare class LiveStoreGraphQLQuery<TResult extends Record<string, any>, TVariableValues extends Record<string, any>, TContext extends BaseGraphQLContext> extends LiveStoreQueryBase<TResult> {
|
|
9
13
|
_tag: 'graphql';
|
|
10
14
|
/** The abstract GraphQL query */
|
|
11
|
-
document: DocumentNode<TResult,
|
|
15
|
+
document: DocumentNode<TResult, TVariableValues>;
|
|
12
16
|
/** A reactive thunk representing the query results */
|
|
13
|
-
results$: Thunk<TResult>;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
label
|
|
20
|
-
store: Store<TContext>;
|
|
21
|
-
otelContext: otel.Context;
|
|
17
|
+
results$: Thunk<TResult, DbContext>;
|
|
18
|
+
variableValues$: Thunk<TVariableValues, DbContext>;
|
|
19
|
+
label: string;
|
|
20
|
+
constructor({ document, label, genVariableValues, }: {
|
|
21
|
+
document: DocumentNode<TResult, TVariableValues>;
|
|
22
|
+
genVariableValues: TVariableValues | ((get: GetAtomResult) => TVariableValues);
|
|
23
|
+
label?: string;
|
|
22
24
|
});
|
|
23
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Returns a new reactive query that contains the result of
|
|
27
|
+
* running an arbitrary JS computation on the results of this SQL query.
|
|
28
|
+
*/
|
|
29
|
+
pipe: <U>(fn: (result: TResult, get: GetAtomResult) => U) => LiveStoreJSQuery<U>;
|
|
30
|
+
queryOnce: ({ document, otelContext, otelTracer, variableValues, store, }: {
|
|
31
|
+
document: graphql.DocumentNode;
|
|
32
|
+
otelContext: otel.Context;
|
|
33
|
+
otelTracer: otel.Tracer;
|
|
34
|
+
variableValues: TVariableValues;
|
|
35
|
+
store: Store<TContext>;
|
|
36
|
+
}) => {
|
|
37
|
+
result: TResult;
|
|
38
|
+
queriedTables: string[];
|
|
39
|
+
};
|
|
40
|
+
destroy: () => void;
|
|
24
41
|
}
|
|
25
42
|
//# sourceMappingURL=graphql.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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;AAE1F,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAElC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,KAAK,EAAE,MAAM,aAAa,CAAA;AACjE,OAAO,EAAE,KAAK,aAAa,EAAE,kBAAkB,EAAqB,MAAM,iBAAiB,CAAA;AAC3F,OAAO,EAAE,KAAK,SAAS,EAAW,MAAM,YAAY,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE1C,eAAO,MAAM,YAAY,mLAEqB,aAAa;;yEAEW,CAAA;AAEtE,qBAAa,qBAAqB,CAChC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACnC,eAAe,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3C,QAAQ,SAAS,kBAAkB,CACnC,SAAQ,kBAAkB,CAAC,OAAO,CAAC;IACnC,IAAI,EAAE,SAAS,CAAY;IAE3B,iCAAiC;IACjC,QAAQ,EAAE,YAAY,CAAC,OAAO,EAAE,eAAe,CAAC,CAAA;IAEhD,sDAAsD;IACtD,QAAQ,EAAE,KAAK,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;IAEnC,eAAe,EAAE,KAAK,CAAC,eAAe,EAAE,SAAS,CAAC,CAAA;IAElD,KAAK,EAAE,MAAM,CAAA;gBAED,EACV,QAAQ,EACR,KAAK,EACL,iBAAiB,GAClB,EAAE;QACD,QAAQ,EAAE,YAAY,CAAC,OAAO,EAAE,eAAe,CAAC,CAAA;QAChD,iBAAiB,EAAE,eAAe,GAAG,CAAC,CAAC,GAAG,EAAE,aAAa,KAAK,eAAe,CAAC,CAAA;QAC9E,KAAK,CAAC,EAAE,MAAM,CAAA;KACf;IAuDD;;;OAGG;IACH,IAAI,mBAAoB,OAAO,OAAO,aAAa,+BAQ/C;IAEJ,SAAS;kBAOG,oBAAoB;qBACjB,KAAK,OAAO;oBACb,KAAK,MAAM;wBACP,eAAe;eACxB,MAAM,QAAQ,CAAC;;;;MAyCvB;IAED,OAAO,aAGN;CACF"}
|
|
@@ -1,18 +1,99 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertNever, shouldNeverHappen } from '@livestore/utils';
|
|
2
|
+
import * as otel from '@opentelemetry/api';
|
|
3
|
+
import * as graphql from 'graphql';
|
|
4
|
+
import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js';
|
|
5
|
+
import { dbGraph } from './graph.js';
|
|
6
|
+
import { LiveStoreJSQuery } from './js.js';
|
|
7
|
+
export const queryGraphQL = (document, genVariableValues, { label } = {}) => new LiveStoreGraphQLQuery({ document, genVariableValues, label });
|
|
2
8
|
export class LiveStoreGraphQLQuery extends LiveStoreQueryBase {
|
|
3
|
-
constructor({ document,
|
|
4
|
-
|
|
9
|
+
constructor({ document, label, genVariableValues, // context,
|
|
10
|
+
}) {
|
|
11
|
+
super();
|
|
5
12
|
this._tag = 'graphql';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Returns a new reactive query that contains the result of
|
|
15
|
+
* running an arbitrary JS computation on the results of this SQL query.
|
|
16
|
+
*/
|
|
17
|
+
this.pipe = (fn) => new LiveStoreJSQuery({
|
|
18
|
+
fn: (get) => {
|
|
19
|
+
const results = get(this.results$);
|
|
20
|
+
return fn(results, get);
|
|
21
|
+
},
|
|
11
22
|
label: `${this.label}:js`,
|
|
12
|
-
|
|
23
|
+
onDestroy: () => this.destroy(),
|
|
13
24
|
});
|
|
25
|
+
this.queryOnce = ({ document, otelContext, otelTracer, variableValues, store, }) => {
|
|
26
|
+
// const schema = this.schema
|
|
27
|
+
// const context = this.context
|
|
28
|
+
const schema = store.graphQLSchema ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL schema");
|
|
29
|
+
const context = store.graphQLContext ?? shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL context");
|
|
30
|
+
const operationName = graphql.getOperationAST(document)?.name?.value;
|
|
31
|
+
return otelTracer.startActiveSpan(`executeGraphQLQuery: ${operationName}`, {}, otelContext, (span) => {
|
|
32
|
+
try {
|
|
33
|
+
span.setAttribute('graphql.variables', JSON.stringify(variableValues));
|
|
34
|
+
span.setAttribute('graphql.query', graphql.print(document));
|
|
35
|
+
context.queriedTables.clear();
|
|
36
|
+
context.otelContext = otel.trace.setSpan(otel.context.active(), span);
|
|
37
|
+
const res = graphql.executeSync({
|
|
38
|
+
document,
|
|
39
|
+
contextValue: context,
|
|
40
|
+
schema: schema,
|
|
41
|
+
variableValues,
|
|
42
|
+
});
|
|
43
|
+
// TODO track number of nested SQL queries via Otel + debug info
|
|
44
|
+
if (res.errors) {
|
|
45
|
+
span.setStatus({ code: otel.SpanStatusCode.ERROR, message: 'GraphQL error' });
|
|
46
|
+
span.setAttribute('graphql.error', res.errors.join('\n'));
|
|
47
|
+
span.setAttribute('graphql.error-detail', JSON.stringify(res.errors));
|
|
48
|
+
console.error(`graphql error (${operationName})`, res.errors);
|
|
49
|
+
}
|
|
50
|
+
return { result: res.data, queriedTables: Array.from(context.queriedTables.values()) };
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
span.end();
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
this.destroy = () => {
|
|
58
|
+
dbGraph.destroy(this.variableValues$);
|
|
59
|
+
dbGraph.destroy(this.results$);
|
|
60
|
+
};
|
|
61
|
+
const labelWithDefault = label ?? graphql.getOperationAST(document)?.name?.value ?? 'graphql';
|
|
62
|
+
this.label = labelWithDefault;
|
|
14
63
|
this.document = document;
|
|
15
|
-
|
|
64
|
+
// if (context === undefined) {
|
|
65
|
+
// return shouldNeverHappen("Can't run a GraphQL query on a store without GraphQL context")
|
|
66
|
+
// }
|
|
67
|
+
// TODO don't even create a thunk if variables are static
|
|
68
|
+
const variableValues$ = dbGraph.makeThunk((get, _addDebugInfo, { rootOtelContext }, otelContext) => {
|
|
69
|
+
if (typeof genVariableValues === 'function') {
|
|
70
|
+
return genVariableValues(makeGetAtomResult(get, otelContext ?? rootOtelContext));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return genVariableValues;
|
|
74
|
+
}
|
|
75
|
+
}, { label: `${labelWithDefault}:variableValues`, meta: { liveStoreThunkType: 'graphqlVariableValues' } });
|
|
76
|
+
this.variableValues$ = variableValues$;
|
|
77
|
+
// const resultsLabel = `${labelWithDefault}:results` + (this.temporaryQueries ? ':temp' : '')
|
|
78
|
+
const resultsLabel = `${labelWithDefault}:results`;
|
|
79
|
+
this.results$ = dbGraph.makeThunk((get, addDebugInfo, { store, otelTracer, rootOtelContext }, otelContext) => {
|
|
80
|
+
const variableValues = get(variableValues$);
|
|
81
|
+
const { result, queriedTables } = this.queryOnce({
|
|
82
|
+
document,
|
|
83
|
+
variableValues,
|
|
84
|
+
otelContext: otelContext ?? rootOtelContext,
|
|
85
|
+
otelTracer,
|
|
86
|
+
store: store,
|
|
87
|
+
});
|
|
88
|
+
// Add dependencies on any tables that were used
|
|
89
|
+
for (const tableName of queriedTables) {
|
|
90
|
+
const tableRef = store.tableRefs[tableName];
|
|
91
|
+
assertNever(tableRef !== undefined, `No table ref found for ${tableName}`);
|
|
92
|
+
get(tableRef);
|
|
93
|
+
}
|
|
94
|
+
addDebugInfo({ _tag: 'graphql', label: resultsLabel, query: graphql.print(document) });
|
|
95
|
+
return result;
|
|
96
|
+
}, { label: resultsLabel, meta: { liveStoreThunkType: 'graphqlResults' } });
|
|
16
97
|
}
|
|
17
98
|
}
|
|
18
99
|
//# sourceMappingURL=graphql.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../../src/reactiveQueries/graphql.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../../src/reactiveQueries/graphql.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACjE,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAC1C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AAIlC,OAAO,EAAsB,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAC3F,OAAO,EAAkB,OAAO,EAAE,MAAM,YAAY,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE1C,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,QAAgD,EAChD,iBAA8E,EAC9E,EAAE,KAAK,KAAyB,EAAE,EAClC,EAAE,CAAC,IAAI,qBAAqB,CAAC,EAAE,QAAQ,EAAE,iBAAiB,EAAE,KAA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,gDAAgD;YAChD,KAAK,MAAM,SAAS,IAAI,aAAa,EAAE;gBACrC,MAAM,QAAQ,GAAG,KAAK,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;gBAC3C,WAAW,CAAC,QAAQ,KAAK,SAAS,EAAE,0BAA0B,SAAS,EAAE,CAAC,CAAA;gBAC1E,GAAG,CAAC,QAAS,CAAC,CAAA;aACf;YAED,YAAY,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,YAAY,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAA;YAEtF,OAAO,MAAM,CAAA;QACf,CAAC,EACD,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,EAAE,CAExE,CAAA;IACH,CAAC;CA0EF"}
|
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { Thunk } from '../reactive.js';
|
|
2
|
+
import { type GetAtomResult, LiveStoreQueryBase } from './base-class.js';
|
|
3
|
+
import type { DbContext } from './graph.js';
|
|
4
|
+
export declare const queryJS: <TResult>(fn: (get: GetAtomResult) => TResult, options: {
|
|
5
|
+
label: string;
|
|
6
|
+
}) => LiveStoreJSQuery<TResult>;
|
|
6
7
|
export declare class LiveStoreJSQuery<TResult> extends LiveStoreQueryBase<TResult> {
|
|
7
8
|
_tag: 'js';
|
|
8
9
|
/** A reactive thunk representing the query results */
|
|
9
|
-
results$: Thunk<TResult>;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
results$: Thunk<TResult, DbContext>;
|
|
11
|
+
label: string;
|
|
12
|
+
/** Currently only used for "nested destruction" of piped queries */
|
|
13
|
+
private onDestroy;
|
|
14
|
+
constructor({ fn, label, onDestroy, }: {
|
|
13
15
|
label: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
fn: (get: GetAtomResult) => TResult;
|
|
17
|
+
/** Currently only used for "nested destruction" of piped queries */
|
|
18
|
+
onDestroy?: () => void;
|
|
16
19
|
});
|
|
17
|
-
pipe: <U>(
|
|
20
|
+
pipe: <U>(fn: (result: TResult, get: GetAtomResult) => U) => LiveStoreJSQuery<U>;
|
|
21
|
+
destroy: () => void;
|
|
18
22
|
}
|
|
19
23
|
//# sourceMappingURL=js.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/js.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"js.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/js.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,EAAE,KAAK,aAAa,EAAE,kBAAkB,EAAqB,MAAM,iBAAiB,CAAA;AAC3F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAG3C,eAAO,MAAM,OAAO,sBAAuB,aAAa,uBAAuB;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,8BACnC,CAAA;AAE7D,qBAAa,gBAAgB,CAAC,OAAO,CAAE,SAAQ,kBAAkB,CAAC,OAAO,CAAC;IACxE,IAAI,EAAE,IAAI,CAAO;IAEjB,sDAAsD;IACtD,QAAQ,EAAE,KAAK,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;IAEnC,KAAK,EAAE,MAAM,CAAA;IAEb,oEAAoE;IACpE,OAAO,CAAC,SAAS,CAA0B;gBAE/B,EACV,EAAE,EACF,KAAK,EACL,SAAS,GACV,EAAE;QACD,KAAK,EAAE,MAAM,CAAA;QACb,EAAE,EAAE,CAAC,GAAG,EAAE,aAAa,KAAK,OAAO,CAAA;QACnC,oEAAoE;QACpE,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;KACvB;IAwBD,IAAI,mBAAoB,OAAO,OAAO,aAAa,+BAQ/C;IAEJ,OAAO,aAGN;CACF"}
|
|
@@ -1,13 +1,36 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as otel from '@opentelemetry/api';
|
|
2
|
+
import { LiveStoreQueryBase, makeGetAtomResult } from './base-class.js';
|
|
3
|
+
import { dbGraph } from './graph.js';
|
|
4
|
+
export const queryJS = (fn, options) => new LiveStoreJSQuery({ fn, label: options.label });
|
|
2
5
|
export class LiveStoreJSQuery extends LiveStoreQueryBase {
|
|
3
|
-
constructor({
|
|
4
|
-
super(
|
|
6
|
+
constructor({ fn, label, onDestroy, }) {
|
|
7
|
+
super();
|
|
5
8
|
this._tag = 'js';
|
|
6
|
-
this.pipe = (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
this.pipe = (fn) => new LiveStoreJSQuery({
|
|
10
|
+
fn: (get) => {
|
|
11
|
+
const results = get(this.results$);
|
|
12
|
+
return fn(results, get);
|
|
13
|
+
},
|
|
14
|
+
label: `${this.label}:js`,
|
|
15
|
+
onDestroy: () => this.destroy(),
|
|
16
|
+
});
|
|
17
|
+
this.destroy = () => {
|
|
18
|
+
dbGraph.destroy(this.results$);
|
|
19
|
+
this.onDestroy?.();
|
|
20
|
+
};
|
|
21
|
+
this.onDestroy = onDestroy;
|
|
22
|
+
this.label = label;
|
|
23
|
+
const queryLabel = `${label}:results`;
|
|
24
|
+
this.results$ = dbGraph.makeThunk((get, addDebugInfo, { otelTracer, rootOtelContext }, otelContext) => otelTracer.startActiveSpan(`js:${label}`, {}, otelContext ?? rootOtelContext, (span) => {
|
|
25
|
+
try {
|
|
26
|
+
addDebugInfo({ _tag: 'js', label, query: fn.toString() });
|
|
27
|
+
const otelContext = otel.trace.setSpan(otel.context.active(), span);
|
|
28
|
+
return fn(makeGetAtomResult(get, otelContext));
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
span.end();
|
|
32
|
+
}
|
|
33
|
+
}), { label: queryLabel, meta: { liveStoreThunkType: 'jsResults' } });
|
|
11
34
|
}
|
|
12
35
|
}
|
|
13
36
|
//# sourceMappingURL=js.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"js.js","sourceRoot":"","sources":["../../src/reactiveQueries/js.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"js.js","sourceRoot":"","sources":["../../src/reactiveQueries/js.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAG1C,OAAO,EAAsB,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAE3F,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEpC,MAAM,CAAC,MAAM,OAAO,GAAG,CAAU,EAAmC,EAAE,OAA0B,EAAE,EAAE,CAClG,IAAI,gBAAgB,CAAU,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAA;AAE7D,MAAM,OAAO,gBAA0B,SAAQ,kBAA2B;IAWxE,YAAY,EACV,EAAE,EACF,KAAK,EACL,SAAS,GAMV;QACC,KAAK,EAAE,CAAA;QApBT,SAAI,GAAS,IAAI,CAAA;QA2CjB,SAAI,GAAG,CAAI,EAA8C,EAAuB,EAAE,CAChF,IAAI,gBAAgB,CAAC;YACnB,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE;gBACV,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;gBAClC,OAAO,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;YACzB,CAAC;YACD,KAAK,EAAE,GAAG,IAAI,CAAC,KAAK,KAAK;YACzB,SAAS,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,EAAE;SAChC,CAAC,CAAA;QAEJ,YAAO,GAAG,GAAG,EAAE;YACb,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YAC9B,IAAI,CAAC,SAAS,EAAE,EAAE,CAAA;QACpB,CAAC,CAAA;QAlCC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAElB,MAAM,UAAU,GAAG,GAAG,KAAK,UAAU,CAAA;QAErC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,SAAS,CAC/B,CAAC,GAAG,EAAE,YAAY,EAAE,EAAE,UAAU,EAAE,eAAe,EAAE,EAAE,WAAW,EAAE,EAAE,CAClE,UAAU,CAAC,eAAe,CAAC,MAAM,KAAK,EAAE,EAAE,EAAE,EAAE,WAAW,IAAI,eAAe,EAAE,CAAC,IAAI,EAAE,EAAE;YACrF,IAAI;gBACF,YAAY,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAA;gBAEzD,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,CAAA;gBACnE,OAAO,EAAE,CAAC,iBAAiB,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC,CAAA;aAC/C;oBAAS;gBACR,IAAI,CAAC,GAAG,EAAE,CAAA;aACX;QACH,CAAC,CAAC,EACJ,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,EAAE,kBAAkB,EAAE,WAAW,EAAE,EAAE,CACjE,CAAA;IACH,CAAC;CAgBF"}
|
|
@@ -1,31 +1,35 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
3
|
-
import type
|
|
4
|
-
import type {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import type { Thunk } from '../reactive.js';
|
|
2
|
+
import type { Bindable } from '../util.js';
|
|
3
|
+
import { type GetAtomResult, LiveStoreQueryBase } from './base-class.js';
|
|
4
|
+
import type { DbContext } from './graph.js';
|
|
5
|
+
import { LiveStoreJSQuery } from './js.js';
|
|
6
|
+
export declare const querySQL: <Row>(query: string | ((get: GetAtomResult) => string), options: {
|
|
7
|
+
queriedTables: ReadonlyArray<string>;
|
|
8
|
+
bindValues?: Bindable;
|
|
9
|
+
label?: string;
|
|
10
|
+
}) => LiveStoreSQLQuery<Row>;
|
|
11
|
+
export declare class LiveStoreSQLQuery<Row> extends LiveStoreQueryBase<ReadonlyArray<Row>> {
|
|
8
12
|
_tag: 'sql';
|
|
9
13
|
/** A reactive thunk representing the query text */
|
|
10
|
-
queryString$: Thunk<string>;
|
|
14
|
+
queryString$: Thunk<string, DbContext>;
|
|
11
15
|
/** A reactive thunk representing the query results */
|
|
12
|
-
results$: Thunk<ReadonlyArray<Row
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
otelContext: otel.Context;
|
|
16
|
+
results$: Thunk<ReadonlyArray<Row>, DbContext>;
|
|
17
|
+
label: string;
|
|
18
|
+
constructor({ genQueryString, queriedTables, bindValues, label: label_, }: {
|
|
19
|
+
label?: string;
|
|
20
|
+
genQueryString: string | ((get: GetAtomResult) => string);
|
|
21
|
+
queriedTables: ReadonlyArray<string>;
|
|
22
|
+
bindValues?: Bindable;
|
|
20
23
|
});
|
|
21
24
|
/**
|
|
22
25
|
* Returns a new reactive query that contains the result of
|
|
23
26
|
* running an arbitrary JS computation on the results of this SQL query.
|
|
24
27
|
*/
|
|
25
|
-
pipe: <U>(fn: (result: ReadonlyArray<Row>, get:
|
|
28
|
+
pipe: <U>(fn: (result: ReadonlyArray<Row>, get: GetAtomResult) => U) => LiveStoreJSQuery<U>;
|
|
26
29
|
/** Returns a reactive query */
|
|
27
30
|
getFirstRow: (args?: {
|
|
28
31
|
defaultValue?: Row;
|
|
29
|
-
}) => LiveStoreJSQuery<Row
|
|
32
|
+
}) => LiveStoreJSQuery<NonNullable<Row>>;
|
|
33
|
+
destroy: () => void;
|
|
30
34
|
}
|
|
31
35
|
//# sourceMappingURL=sql.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/sql.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sql.d.ts","sourceRoot":"","sources":["../../src/reactiveQueries/sql.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAE1C,OAAO,EAAE,KAAK,aAAa,EAAE,kBAAkB,EAAqB,MAAM,iBAAiB,CAAA;AAC3F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE1C,eAAO,MAAM,QAAQ,+BACI,aAAa,KAAK,MAAM,YACtC;IACP,aAAa,EAAE,cAAc,MAAM,CAAC,CAAA;IACpC,UAAU,CAAC,EAAE,QAAQ,CAAA;IACrB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf,2BAOC,CAAA;AAGJ,qBAAa,iBAAiB,CAAC,GAAG,CAAE,SAAQ,kBAAkB,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAChF,IAAI,EAAE,KAAK,CAAQ;IAEnB,mDAAmD;IACnD,YAAY,EAAE,KAAK,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IAEtC,sDAAsD;IACtD,QAAQ,EAAE,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC,CAAA;IAE9C,KAAK,EAAE,MAAM,CAAA;gBAED,EACV,cAAc,EACd,aAAa,EACb,UAAU,EACV,KAAK,EAAE,MAAM,GACd,EAAE;QACD,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,cAAc,EAAE,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,aAAa,KAA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