Skip to content

Commit

Permalink
move terraform into firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Oct 2, 2023
1 parent 719e51a commit aa01d4a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/test-changed-firestore-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ jobs:
- name: Setup Terraform
uses: hashicorp/setup-terraform@v2
- name: Terraform Init
run: terraform init
run: |
cd packages/firestore
terraform init
continue-on-error: true
- name: Terraform Apply
if: github.event_name == 'pull_request'
run: |
cp config/ci.config.json config/project.json
terraform apply -var-file=config/project.json -auto-approve
cp ../../config/ci.config.json config/project.json
terraform apply -var-file=../../config/project.json -auto-approve
cd ../..
continue-on-error: true
- name: Set up Node (16)
uses: actions/setup-node@v3
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { and } from '../../../src/lite-api/query';
import { AutoId } from '../../../src/util/misc';
import { field } from '../../util/helpers';

import {
query as internalQuery,
Expand All @@ -32,7 +33,13 @@ import {
setDoc as setDocument,
QueryCompositeFilterConstraint,
QueryNonFilterConstraint,
Timestamp
Timestamp,
DocumentSnapshot,
getDoc as getDocument,
updateDoc as updateDocument,
UpdateData,
getDocs as getDocuments,
QuerySnapshot
} from './firebase_export';
import {
batchCommitDocsToCollection,
Expand All @@ -55,6 +62,7 @@ import { COMPOSITE_INDEX_TEST_COLLECTION, DEFAULT_SETTINGS } from './settings';
export class CompositeIndexTestHelper {
private readonly testId: string;
private readonly TEST_ID_FIELD: string = 'testId';
private readonly TTL_FIELD: string = 'expireAt';

// Creates a new instance of the CompositeIndexTestHelper class, with a unique test
// identifier for data isolation.
Expand Down Expand Up @@ -91,13 +99,20 @@ export class CompositeIndexTestHelper {
return {
...doc,
[this.TEST_ID_FIELD]: this.testId,
expireAt: new Timestamp( // Expire test data after 24 hours
[this.TTL_FIELD]: new Timestamp( // Expire test data after 24 hours
Timestamp.now().seconds + 24 * 60 * 60,
Timestamp.now().nanoseconds
)
};
}

// Remove test-specific fields from a document, including the testId and expiration date.
removeTestSpecificFieldsFromDoc(doc: DocumentData): DocumentData {
doc._document?.data?.delete(field(this.TTL_FIELD));
doc._document?.data?.delete(field(this.TEST_ID_FIELD));
return doc;
}

// Helper method to hash document keys and add test-specific fields for the provided documents.
private prepareTestDocuments(docs: { [key: string]: DocumentData }): {
[key: string]: DocumentData;
Expand Down Expand Up @@ -172,4 +187,34 @@ export class CompositeIndexTestHelper {
) as WithFieldValue<T>;
return setDocument(reference, processedData);
}

// This is is the same as making the update on the doc directly with merge=true.
updateDoc<T, DbModelType extends DocumentData>(
reference: DocumentReference<T, DbModelType>,
data: UpdateData<DbModelType>
): Promise<void> {
const processedData = this.addTestSpecificFieldsToDoc(
data
) as UpdateData<DbModelType>;
return updateDocument(reference, processedData);
}


async getDoc<T, DbModelType extends DocumentData>(
reference: DocumentReference<T, DbModelType>
): Promise<DocumentSnapshot<T, DbModelType>> {
const docSnapshot = await getDocument<T, DbModelType>(reference);
this.removeTestSpecificFieldsFromDoc(docSnapshot);
return docSnapshot;
}

async getDocs<T, DbModelType extends DocumentData>(
query_: Query<T, DbModelType>
): Promise<QuerySnapshot<T, DbModelType>> {
const querySnapshot = await getDocuments(this.query(query_));
querySnapshot.forEach(doc => {
this.removeTestSpecificFieldsFromDoc(doc);
});
return querySnapshot;
}
}

0 comments on commit aa01d4a

Please sign in to comment.