MCPcopy Create free account
hub / github.com/TanStack/db / initialFetch

Function initialFetch

packages/rxdb-db-collection/src/rxdb.ts:130–196  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

128
129 let ready = false
130 async function initialFetch() {
131 /**
132 * RxDB stores a last-write-time
133 * which can be used to "sort" document writes,
134 * so for initial sync we iterate over that.
135 */
136 let cursor: RxDocumentData<Row> | undefined = undefined
137 const syncBatchSize = config.syncBatchSize ? config.syncBatchSize : 1000
138 begin()
139
140 while (!ready) {
141 let query: FilledMangoQuery<Row>
142 if (cursor) {
143 query = {
144 selector: {
145 $or: [
146 { '_meta.lwt': { $gt: cursor._meta.lwt } },
147 {
148 '_meta.lwt': cursor._meta.lwt,
149 [primaryPath]: {
150 $gt: getKey(cursor),
151 },
152 },
153 ],
154 _deleted: false,
155 },
156 sort: [{ '_meta.lwt': `asc` }, { [primaryPath]: `asc` }],
157 limit: syncBatchSize,
158 skip: 0,
159 }
160 } else {
161 query = {
162 selector: { _deleted: false },
163 sort: [{ '_meta.lwt': `asc` }, { [primaryPath]: `asc` }],
164 limit: syncBatchSize,
165 skip: 0,
166 }
167 }
168
169 /**
170 * Instead of doing a RxCollection.query(),
171 * we directly query the storage engine of the RxCollection so we do not use the
172 * RxCollection document cache because it likely wont be used anyway
173 * since most queries will run directly on the tanstack-db side.
174 */
175 const preparedQuery = prepareQuery<Row>(
176 rxCollection.storageInstance.schema,
177 query,
178 )
179 const result = await rxCollection.storageInstance.query(preparedQuery)
180 const docs = result.documents
181
182 cursor = lastOfArray(docs)
183 if (docs.length === 0) {
184 ready = true
185 break
186 }
187

Callers 1

startFunction · 0.85

Calls 4

stripRxdbFieldsFunction · 0.90
getKeyFunction · 0.70
queryMethod · 0.45
forEachMethod · 0.45

Tested by

no test coverage detected