Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ebase into nathan815-master
  • Loading branch information
benwinding committed Feb 27, 2022
2 parents dad04f6 + 2839e23 commit 46539f1
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 604 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ const options = {
firestoreCostsLogger: {
enabled: false,
localStoragePrefix // optional
}
},
// Function to transform documents before they are written to Firestore
transformToDb: (resourceName, document, documentId) => document
}

const dataProvider = FirebaseDataProvider(config, options);
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"rxjs": "^6.5.x"
},
"devDependencies": {
"@firebase/testing": "^0.20.11",
"@types/firebase": "^3.2.1",
"@firebase/rules-unit-testing": "^2.0.2",
"@types/jest": "^24.0.13",
"@types/lodash": "^4.14.150",
"@types/node": "^10.9.4",
Expand All @@ -25,13 +24,13 @@
"@types/rx": "^4.1.1",
"cpx": "^1.5.0",
"del": "^3.0.0",
"ra-core": "3.10.0",
"firebase": "^9.6.4",
"firebase-tools": "^8.1.1",
"gulp": "^3.9.1",
"gulp-sequence": "^1.0.0",
"jest": "^23.6.0",
"microbundle": "^0.12.4",
"ra-core": "3.10.0",
"ts-jest": "^25",
"tslint": "^5.16.0",
"typescript": "4.5.5"
Expand Down
10 changes: 6 additions & 4 deletions src/providers/commands/Create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export async function Create<T extends ra.Record>(
client.checkRemoveIdField(docObj, overridenId);
await client.addCreatedByFields(docObj);
await client.addUpdatedByFields(docObj);
const docObjTransformed = client.transformToDb(resourceName, docObj, overridenId);
log("Create", { docObj });
await r.collection.doc(overridenId).set(docObj, { merge: false });
await r.collection.doc(overridenId).set(docObjTransformed, { merge: false });
return {
data: {
...data,
...docObjTransformed,
id: overridenId,
},
};
Expand All @@ -43,10 +44,11 @@ export async function Create<T extends ra.Record>(
client.checkRemoveIdField(docObj, newId);
await client.addCreatedByFields(docObj);
await client.addUpdatedByFields(docObj);
await r.collection.doc(newId).set(docObj, { merge: false });
const docObjTransformed = client.transformToDb(resourceName, docObj, newId);
await r.collection.doc(newId).set(docObjTransformed, { merge: false });
return {
data: {
...data,
...docObjTransformed,
id: newId,
},
};
Expand Down
3 changes: 2 additions & 1 deletion src/providers/commands/Update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export async function Update<T extends ra.Record>(
const docObj = { ...data };
client.checkRemoveIdField(docObj, id);
await client.addUpdatedByFields(docObj);
await r.collection.doc(id).update(docObj);
const docObjTransformed = client.transformToDb(resourceName, docObj, id);
await r.collection.doc(id).update(docObjTransformed);
return {
data: {
...data,
Expand Down
3 changes: 2 additions & 1 deletion src/providers/commands/UpdateMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export async function UpdateMany(
const docObj = { ...data };
client.checkRemoveIdField(docObj, idStr);
await client.addUpdatedByFields(docObj);
const docObjTransformed = client.transformToDb(resourceName, docObj, idStr);
await r.collection
.doc(idStr)
.update(docObj);
.update(docObjTransformed);
return {
...data,
id: idStr
Expand Down
7 changes: 7 additions & 0 deletions src/providers/database/FireClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export class FireClient {
}
}

public transformToDb(resourceName: string, document: any, docId: string): any {
if (typeof this.options.transformToDb === 'function') {
return this.options.transformToDb(resourceName, document, docId);
}
return document;
}

public async parseDataAndUpload(r: IResource, id: string, data: any) {
if (!data) {
return data;
Expand Down
2 changes: 2 additions & 0 deletions src/providers/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export interface RAFirebaseOptions {
enabled: boolean;
persistCount?: boolean;
};
// Function to transform documents before they are written to Firestore
transformToDb?: (resourceName: string, document: any, documentId: string) => any;
/* OLD FLAGS */
/**
* @deprecated The method should not be used
Expand Down
40 changes: 38 additions & 2 deletions tests/integration-tests/ApiCreate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Create } from "../../src/providers/commands";

describe("ApiCreate", () => {
test("FireClient create doc", async () => {
const client = MakeMockClient({
const client = await MakeMockClient({
logging: true,
disableMeta: true,
});
Expand All @@ -15,7 +15,7 @@ describe("ApiCreate", () => {
expect(first.name).toBe("John");
}, 100000);
test("FireClient create doc with custom meta", async () => {
const client = MakeMockClient({
const client = await MakeMockClient({
logging: true,
renameMetaFields: {
updated_by: 'MY_CREATED_BY',
Expand All @@ -28,4 +28,40 @@ describe("ApiCreate", () => {

expect(first.hasOwnProperty('MY_CREATED_BY')).toBeTruthy();
}, 100000);
test("FireClient create doc with transformToDb function provided", async () => {
const client = await MakeMockClient({
logging: true,
transformToDb: (resourceName, document, id) => {
if (resourceName === "users") {
return {
...document,
firstName: document.firstName.toUpperCase(),
picture: document.picture.src || document.picture,
};
}
return document;
}
});

const newUser = {
firstName: "John",
lastName: "Last",
age: 20,
picture: {
src: "http://example.com/pic.png"
},
};

await Create("users", { data: newUser }, client);
const users = (await client.fireWrapper.dbGetCollection("users").get()).docs;

expect(users.length).toBe(1);
expect(users[0].data()).toMatchObject({
firstName: "JOHN",
lastName: "Last",
age: 20,
picture: "http://example.com/pic.png",
});

}, 100000);
});
2 changes: 1 addition & 1 deletion tests/integration-tests/ApiDelete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Delete } from "../../src/providers/commands";

describe("api methods", () => {
test("FireClient delete doc", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();

const docId = "test123";
const docObj = { id: docId, name: "Jim" };
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/ApiDeleteMany.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeleteMany } from "../../src/providers/commands";

describe("api methods", () => {
test("FireClient delete doc", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const docIds = ["test123", "test22222", "asdads"];
const collName = "deleteables";
const collection = client.fireWrapper.dbGetCollection(collName);
Expand Down
8 changes: 4 additions & 4 deletions tests/integration-tests/ApiGetList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FireStoreCollectionRef } from "../../src/misc/firebase-models";

describe("api methods", () => {
test("FireClient list docs", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const docIds = ["test123", "test22222", "asdads"];
const collName = "list-mes";
const collection = client.fireWrapper.dbGetCollection(collName);
Expand All @@ -31,7 +31,7 @@ describe("api methods", () => {
}, 100000);

test("FireClient list docs with boolean filter", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const testDocs = [
{
title: "A",
Expand Down Expand Up @@ -71,7 +71,7 @@ describe("api methods", () => {
}, 100000);

test("FireClient list docs with dotpath sort", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const testDocs = [
{
obj: {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("api methods", () => {
}, 100000);

test("FireClient with filter gte", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const testDocs = [
{
title: "A",
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/ApiGetMany.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GetMany } from "../../src/providers/queries";

describe("api methods", () => {
test("FireClient list docs", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const docIds = ["test123", "test22222", "asdads"];
const collName = "list-mes";
const collection = client.fireWrapper.dbGetCollection(collName);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration-tests/ApiGetOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GetOne } from "../../src/providers/queries";

describe("api methods", () => {
test("FireClient apiGetOne", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const docIds = ["test123", "test22222", "asdads"];
const collName = "list-mes";
const collection = client.fireWrapper.dbGetCollection(collName);
Expand All @@ -18,7 +18,7 @@ describe("api methods", () => {
}, 100000);

test("FireClient apiGetOne, with nested Dates", async () => {
const client = MakeMockClient();
const client = await MakeMockClient();
const collName = "list-mes";
const docId = "1234";
const collection = client.fireWrapper.dbGetCollection(collName);
Expand Down
4 changes: 2 additions & 2 deletions tests/integration-tests/ApiRootRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MakeMockClient } from "./utils/test-helpers";

describe("ApiRootRef", () => {
test("rootref1", async () => {
const client = MakeMockClient({ rootRef: "root-ref1/ok" });
const client = await MakeMockClient({ rootRef: "root-ref1/ok" });
const rm = client.rm;
const docRef = await client
.fireWrapper
Expand All @@ -15,7 +15,7 @@ describe("ApiRootRef", () => {
}, 10000);

test("rootreffunction1", async () => {
const client = MakeMockClient({ rootRef: "root-ref-function1/ok" });
const client = await MakeMockClient({ rootRef: "root-ref-function1/ok" });
const rm = client.rm;
const docRef = await client
.fireWrapper
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/ApiSoftDelete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeleteSoft } from "../../src/providers/commands";

describe("api methods", () => {
test("FireClient delete doc", async () => {
const client = MakeMockClient({ softDelete: true, disableMeta: true });
const client = await MakeMockClient({ softDelete: true, disableMeta: true });
const id = "test123";
const collName = "t2";
const docRef = client.fireWrapper.dbGetCollection(collName).doc(id);
Expand Down
2 changes: 1 addition & 1 deletion tests/integration-tests/ApiSoftDeleteMany.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DeleteManySoft } from "../../src/providers/commands";

describe("api methods", () => {
test("FireClient delete doc", async () => {
const client = MakeMockClient({ softDelete: true, disableMeta: true });
const client = await MakeMockClient({ softDelete: true, disableMeta: true });
const docIds = ["test123", "test22222", "asdads"];
const collName = "deleteables";
const collection = client.fireWrapper.dbGetCollection(collName);
Expand Down
47 changes: 46 additions & 1 deletion tests/integration-tests/ApiUpdate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Update } from "../../src/providers/commands";

describe("api methods", () => {
test("FireClient update doc", async () => {
const client = MakeMockClient({ disableMeta: true });
const client = await MakeMockClient({ disableMeta: true });
const id = "testsss123";
const collName = "t2";
const docRef = client.fireWrapper.dbGetCollection(collName).doc(id);
Expand All @@ -23,4 +23,49 @@ describe("api methods", () => {
expect(res.exists).toBeTruthy();
expect(res.get("title")).toBe("asd");
}, 100000);

test("FireClient update doc with transformToDb function provided", async () => {
const client = await MakeMockClient({
transformToDb: (resourceName, document, id) => {
if (resourceName === "users") {
return {
...document,
firstName: document.firstName.toUpperCase(),
};
}
return document;
}
});

const id = "user123";
const docRef = client.fireWrapper.dbGetCollection("users").doc(id);
await docRef.set({ name: "Jim" });

const previousUser = {
id,
firstName: "Bob",
lastName: "Last",
};
const user = {
...previousUser,
firstName: "John",
};

await Update(
"users",
{
id: id,
data: user,
previousData: previousUser,
},
client
);

const res = await docRef.get();
expect(res.exists).toBeTruthy();
expect(res.data()).toMatchObject({
firstName: "JOHN",
lastName: "Last",
});
}, 100000);
});
Loading

0 comments on commit 46539f1

Please sign in to comment.