diff --git a/src/get/index.ts b/src/get/index.ts index 4eb69f4b..bf2fc255 100644 --- a/src/get/index.ts +++ b/src/get/index.ts @@ -1,4 +1,4 @@ -import adaptor from '../adaptor' +import adaptor, { Adaptor } from '../adaptor' import type { Collection } from '../collection' import { wrapData } from '../data' import { AnyDoc, doc, Doc } from '../doc' @@ -76,6 +76,25 @@ export async function get< ServerTimestamps > | null> { const a = await adaptor() + return getCommon(collectionOrRef, maybeIdOrOptions, maybeOptions, {a, t: undefined}) +} + + +export async function getCommon< + Model, + Environment extends RuntimeEnvironment | undefined, + ServerTimestamps extends ServerTimestampsStrategy +>( + collectionOrRef: Collection | Ref, + maybeIdOrOptions: string | GetOptions | undefined, + maybeOptions: GetOptions | undefined, + {a, t}: {a: Adaptor, t: FirebaseFirestore.Transaction | undefined} +): Promise | null> { let collection: Collection let id: string let options: GetOptions | undefined @@ -93,10 +112,11 @@ export async function get< | undefined } - assertEnvironment(a, options?.assertEnvironment) + if (!t) + assertEnvironment(a, options?.assertEnvironment) const firestoreDoc = a.firestore.collection(collection.path).doc(id) - const firestoreSnap = await firestoreDoc.get() + const firestoreSnap = await (t ? t.get(firestoreDoc) : firestoreDoc.get()) const firestoreData = a.getDocData(firestoreSnap, options) const data = firestoreData && (wrapData(a, firestoreData) as Model) return data diff --git a/src/query/index.ts b/src/query/index.ts index a11db310..595e0bda 100644 --- a/src/query/index.ts +++ b/src/query/index.ts @@ -1,4 +1,4 @@ -import adaptor from '../adaptor' +import adaptor, { Adaptor } from '../adaptor' import type { Collection } from '../collection' import type { Cursor, CursorMethod } from '../cursor' import { unwrapData, wrapData } from '../data' @@ -60,8 +60,23 @@ export async function query< options?: QueryOptions ): Promise[]> { const a = await adaptor() + return queryCommon(collection, queries, options, {a, t: undefined}) +} - assertEnvironment(a, options?.assertEnvironment) + +export async function queryCommon< + Model, + Environment extends RuntimeEnvironment | undefined, + ServerTimestamps extends ServerTimestampsStrategy +>( + collection: Collection | CollectionGroup, + queries: Query[], + options: QueryOptions | undefined, + {a, t}: {a: Adaptor, t: FirebaseFirestore.Transaction | undefined} +): Promise[]> { + + if (!t) + assertEnvironment(a, options?.assertEnvironment) const { firestoreQuery, cursors } = queries.reduce( (acc, q) => { @@ -144,7 +159,7 @@ export async function query< }, firestoreQuery) : firestoreQuery - const firebaseSnap = await paginatedFirestoreQuery.get() + const firebaseSnap = await (t ? t.get(paginatedFirestoreQuery) : paginatedFirestoreQuery.get()) return firebaseSnap.docs.map((snap) => doc( @@ -160,3 +175,4 @@ export async function query< ) ) } + diff --git a/src/transaction/index.ts b/src/transaction/index.ts index e40d739f..21350239 100644 --- a/src/transaction/index.ts +++ b/src/transaction/index.ts @@ -1,7 +1,8 @@ +import { get, getCommon, GetOptions, query, queryCommon } from '..'; import adaptor from '../adaptor' import type { Collection } from '../collection' import { unwrapData, wrapData } from '../data' -import { AnyDoc, doc } from '../doc' +import { AnyDoc, doc, Doc } from '../doc' import type { Field } from '../field' import { Ref, ref } from '../ref' import type { @@ -15,6 +16,8 @@ import type { UpdateModel } from '../update' import type { UpsetModel } from '../upset' import { assertEnvironment } from '../_lib/assertEnvironment' + + /** * The transaction read API object. It contains {@link TransactionRead.get|get} * the function that allows reading documents from the database. @@ -54,6 +57,7 @@ export interface TransactionRead { id: string, options?: DocOptions ): Promise | null> + query: typeof query } /** @@ -281,44 +285,6 @@ export async function transaction< assertEnvironment(a, options?.assertEnvironment) return a.firestore.runTransaction((t) => { - async function get< - Model, - ServerTimestamps extends ServerTimestampsStrategy - >( - collectionOrRef: Collection | Ref, - maybeIdOrOptions?: string | DocOptions, - maybeOptions?: DocOptions - ): Promise | null> { - let collection: Collection - let id: string - let options: DocOptions | undefined - - if (collectionOrRef.__type__ === 'collection') { - collection = collectionOrRef as Collection - id = maybeIdOrOptions as string - options = maybeOptions as DocOptions - } else { - const ref = collectionOrRef as Ref - collection = ref.collection - id = ref.id - options = maybeIdOrOptions as DocOptions | undefined - } - - const firestoreDoc = a.firestore.collection(collection.path).doc(id) - // ^ above - // TODO: Refactor code above and below because is all the same as in the regular get function - const firestoreSnap = await t.get(firestoreDoc) - // v below - const firestoreData = a.getDocData(firestoreSnap, options) - const data = firestoreData && (wrapData(a, firestoreData) as Model) - return data - ? doc(ref(collection, id), data, { - environment: a.environment as Environment, - serverTimestamps: options?.serverTimestamps, - ...a.getDocMeta(firestoreSnap) - }) - : null - } function set( collectionOrRef: Collection | Ref, @@ -426,7 +392,10 @@ export async function transaction< t.delete(firebaseDoc) } - return readFunction({ get }).then((data) => + return readFunction({ + get: (...props) => (getCommon as any)(props[0], props[1], props[2], {a, t}), + query: (...props) => queryCommon(props[0], props[1], props[2], {a, t}) + }).then((data) => writeFunction({ data, set, upset, update, remove }) ) })