Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): support path select #203

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 58 additions & 21 deletions __tests__/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe('Queries', () => {
},
currentUser: { uid: 'homer-user' },
},
{ simulateQueryFilters: true },
{ simulateQueryFilters: true }
);

const firebase = require('firebase');
Expand Down Expand Up @@ -194,6 +194,58 @@ describe('Queries', () => {
expect(cow).toHaveProperty('id', 'cow');
});

test('it can select nested values', async () => {
const res = await db
.collection('animals')
.where('id', '==', 'cow')
.select('appearance.color')
.get();

expect(res).toHaveProperty('size', 1);
const cow = res.docs[0].data();
expect(cow).toBeDefined();
expect(cow).toHaveProperty('appearance', { color: 'brown' });
});

test('it can select and get nested values', async () => {
const res = await db
.collection('animals')
.where('id', '==', 'cow')
.select('appearance.color')
.get();

expect(res).toHaveProperty('size', 1);
const appearance = res.docs[0].get('appearance');
expect(appearance).toEqual({ color: 'brown' });
const color = res.docs[0].get('appearance.color');
expect(color).toEqual('brown');
});

test('it can handle missing nested values', async () => {
const res = await db
.collection('animals')
.where('id', '==', 'cow')
.select('size.height.shoulder')
.get();

expect(res).toHaveProperty('size', 1);
const data = res.docs[0].data();
expect(data).toHaveProperty('size', {});
});

test('it can select many nested values', async () => {
const res = await db
.collection('animals')
.where('id', '==', 'cow')
.select('appearance.color', 'appearance.size')
.get();

expect(res).toHaveProperty('size', 1);
const cow = res.docs[0].data();
expect(cow).toBeDefined();
expect(cow).toHaveProperty('appearance', {color: 'brown', size: 'large'});
});

test('it can query date values for equality', async () => {
const elephant = await db
.collection('animals')
Expand Down Expand Up @@ -443,10 +495,7 @@ describe('Queries', () => {
// eslint-disable-next-line quotes
"it performs '$comp' queries on number values ($count doc(s) where legCount $comp $value)",
async ({ comp, value, count }) => {
const results = await db
.collection('animals')
.where('legCount', comp, value)
.get();
const results = await db.collection('animals').where('legCount', comp, value).get();
expect(results.size).toBe(count);
},
);
Expand Down Expand Up @@ -475,10 +524,7 @@ describe('Queries', () => {
// eslint-disable-next-line quotes
"it performs '$comp' queries on number values that may be zero ($count doc(s) where foodCount $comp $value)",
async ({ comp, value, count }) => {
const results = await db
.collection('animals')
.where('foodCount', comp, value)
.get();
const results = await db.collection('animals').where('foodCount', comp, value).get();
expect(results.size).toBe(count);
},
);
Expand All @@ -505,10 +551,7 @@ describe('Queries', () => {
// eslint-disable-next-line quotes
"it performs '$comp' queries on string values ($count doc(s) where type $comp '$value')",
async ({ comp, value, count }) => {
const results = await db
.collection('animals')
.where('type', comp, value)
.get();
const results = await db.collection('animals').where('type', comp, value).get();
expect(results.size).toBe(count);
},
);
Expand All @@ -527,10 +570,7 @@ describe('Queries', () => {
// eslint-disable-next-line quotes
"it performs '$comp' queries on array values ($count doc(s) where food $comp '$value')",
async ({ comp, value, count }) => {
const results = await db
.collection('animals')
.where('food', comp, value)
.get();
const results = await db.collection('animals').where('food', comp, value).get();
expect(results.size).toBe(count);
},
);
Expand All @@ -550,10 +590,7 @@ describe('Queries', () => {
// eslint-disable-next-line quotes
"it performs '$comp' queries on array values that may be zero ($count doc(s) where foodEaten $comp '$value')",
async ({ comp, value, count }) => {
const results = await db
.collection('animals')
.where('foodEaten', comp, value)
.get();
const results = await db.collection('animals').where('foodEaten', comp, value).get();
expect(results.size).toBe(count);
},
);
Expand Down
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"jest": "^26.6.3",
"jest-watch-typeahead": "^0.3.1",
"lint-staged": "^10.0.2",
"lodash": "^4.17.21",
"prettier": "^3.1.1",
"ts-jest": "^26.5.1",
"typescript": "^4.9.5"
Expand Down
20 changes: 17 additions & 3 deletions src/mocks/helpers/buildDocFromHash.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const timestamp = require('../timestamp');
const {merge} = require('lodash');

module.exports = function buildDocFromHash(hash = {}, id = 'abc123', selectFields = undefined) {
const exists = !!hash || false;
Expand Down Expand Up @@ -29,9 +30,10 @@ module.exports = function buildDocFromHash(hash = {}, id = 'abc123', selectField
delete copy._updateTime;

if (selectFields !== undefined) {
copy = Object.keys(copy)
.filter(key => key === 'id' || selectFields.includes(key))
.reduce((res, key) => ((res[key] = copy[key]), res), {});
copy = selectFields.reduce((acc, field) => {
const path = field.split('.');
return merge(acc, buildDocFromPath(copy, path));
}, {});
}

return copy;
Expand All @@ -56,3 +58,15 @@ module.exports = function buildDocFromHash(hash = {}, id = 'abc123', selectField
},
};
};

function buildDocFromPath(data, path) {
if (data === undefined || data === null) {
return {};
}

const [root, ...subPath] = path;
const rootData = data[root];
return {
[root]: subPath.length ? buildDocFromPath(rootData, subPath) : rootData
};
}