MCPcopy
hub / github.com/aspen-cloud/triplit / connectQuery

Method connectQuery

packages/client/src/sync-engine.ts:672–757  ·  view source on GitHub ↗
(queryId: string)

Source from the content-addressed store, hash-verified

670 }
671
672 private async connectQuery(queryId: string) {
673 if (this.client.awaitReady) await this.client.awaitReady;
674 if (!this.queries.has(queryId)) return;
675
676 const queryMetadata = this.queries.get(queryId);
677 /**
678 * Do not send CONNECT_QUERY message if:
679 * - query no longer exists
680 * - query has already been sent in this session
681 * - query has been aborted
682 * - server is not ready
683 */
684 if (
685 !queryMetadata ||
686 queryMetadata.hasSent ||
687 queryMetadata.abortController.signal.aborted ||
688 !this.serverReady
689 ) {
690 return;
691 }
692
693 const latestServerTimestamp = (await this.client.db.getMetadata([
694 'latest_server_timestamp',
695 ])) as SyncTimestamp | undefined;
696
697 let queryState: QueryState | undefined = undefined;
698
699 if (latestServerTimestamp) {
700 const queryWithRelationalInclusions =
701 createQueryWithRelationalOrderAddedToIncludes(
702 createQueryWithExistsAddedToIncludes(
703 prepareQuery(
704 queryMetadata.params,
705 this.client.db.schema?.['collections'],
706 {},
707 undefined,
708 {
709 applyPermission: undefined,
710 }
711 )
712 )
713 );
714 // We should only consider your cache data for checkpointed fetch
715 // We dont have a great API for this right now, so using the DB's query engine directly
716 const queryEngine = new EntityStoreQueryEngine(
717 this.client.db.kv,
718 this.client.db.entityStore.dataStore
719 );
720 const syncedResults = await queryEngine.fetch(
721 queryWithRelationalInclusions
722 );
723 const changesFromResults = queryResultsToChanges(
724 syncedResults,
725 queryWithRelationalInclusions
726 );
727
728 const entityIds = changesToEntityIds(changesFromResults);
729

Callers 2

subscribeMethod · 0.95
initializeSyncMethod · 0.95

Calls 10

fetchMethod · 0.95
sendMessageMethod · 0.95
prepareQueryFunction · 0.90
queryResultsToChangesFunction · 0.90
changesToEntityIdsFunction · 0.85
hasMethod · 0.80
getMetadataMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected