Skip to content

Commit

Permalink
Use tmpVar in generated queries
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Jan 20, 2020
1 parent 5a3d475 commit 3e49b94
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-feet-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/mongo-join-builder': patch
---

Query generator now uses `tmpVar` rather than a generated variable name in lookup queries.
6 changes: 2 additions & 4 deletions packages/mongo-join-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ db.orders.aggregate([
$lookup: {
from: 'items',
as: 'abc123_items',
let: {
abc123_items_items: '$items',
},
let: { tmpVar: '$items' },
pipeline: [
{
$match: {
$and: [{ name: { $regex: /a/ } }, { $expr: { $in: ['$_id', '$$abc123_items_items'] } }],
$and: [{ name: { $regex: /a/ } }, { $expr: { $in: ['$_id', '$$tmpVar'] } }],
},
},
{
Expand Down
5 changes: 2 additions & 3 deletions packages/mongo-join-builder/lib/join-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,18 @@ const { flatten, defaultObj } = require('@keystonejs/utils');

function relationshipPipeline(relationship) {
const { field, many, from, uniqueField } = relationship.relationshipInfo;
const idsName = `${uniqueField}_id${many ? 's' : ''}`;
return [
{
$lookup: {
from,
as: uniqueField,
// We use `ifNull` here to handle the case unique to mongo where a record may be
// entirely missing a field (or have the value set to `null`).
let: { [idsName]: many ? { $ifNull: [`$${field}`, []] } : `$${field}` },
let: { tmpVar: many ? { $ifNull: [`$${field}`, []] } : `$${field}` },
pipeline: [
// The ID / list of IDs we're joining by. Do this very first so it limits any work
// required in subsequent steps / $and's.
{ $match: { $expr: { [many ? '$in' : '$eq']: ['$_id', `$$${idsName}`] } } },
{ $match: { $expr: { [many ? '$in' : '$eq']: ['$_id', `$$tmpVar`] } } },
...pipelineBuilder(relationship),
],
},
Expand Down
8 changes: 4 additions & 4 deletions packages/mongo-join-builder/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ describe('Test main export', () => {
$lookup: {
from: 'posts',
as: 'posts_every_posts',
let: { posts_every_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$posts_every_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'tags',
as: 'tags_some_tags',
let: { tags_some_tags_ids: { $ifNull: ['$tags', []] } },
let: { tmpVar: { $ifNull: ['$tags', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$tags_some_tags_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{ $match: { name: { $eq: 'foo' } } },
{ $addFields: { id: '$_id' } },
],
Expand Down
56 changes: 28 additions & 28 deletions packages/mongo-join-builder/tests/join-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ describe('join builder', () => {
$lookup: {
from: 'user-collection',
as: 'abc123_author',
let: { abc123_author_id: '$author' },
let: { tmpVar: '$author' },
pipeline: [
{ $match: { $expr: { $eq: ['$_id', '$$abc123_author_id'] } } },
{ $match: { $expr: { $eq: ['$_id', '$$tmpVar'] } } },
{ $match: { name: { $eq: 'Alice' } } },
{ $addFields: { id: '$_id' } },
],
Expand Down Expand Up @@ -131,9 +131,9 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'abc123_posts',
let: { abc123_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$abc123_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{ $addFields: { id: '$_id' } },
],
},
Expand Down Expand Up @@ -193,9 +193,9 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'abc123_posts',
let: { abc123_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$abc123_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{ $addFields: { id: '$_id' } },
{ $orderBy: 'title' },
],
Expand Down Expand Up @@ -305,23 +305,23 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'abc123_posts',
let: { abc123_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$abc123_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'tags-collection',
as: 'def456_tags',
let: { def456_tags_ids: { $ifNull: ['$tags', []] } },
let: { tmpVar: { $ifNull: ['$tags', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$def456_tags_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'posts-collection',
as: 'xyz890_posts',
let: { xyz890_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$xyz890_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{ $match: { published: { $eq: true } } },
{ $addFields: { id: '$_id' } },
],
Expand Down Expand Up @@ -445,16 +445,16 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'zip567_posts',
let: { zip567_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$zip567_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'labels-collection',
as: 'quux987_labels',
let: { quux987_labels_ids: { $ifNull: ['$labels', []] } },
let: { tmpVar: { $ifNull: ['$labels', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$quux987_labels_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{ $match: { name: { $eq: 'foo' } } },
{ $addFields: { id: '$_id' } },
],
Expand Down Expand Up @@ -561,16 +561,16 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'zip567_posts',
let: { zip567_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$zip567_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'labels-collection',
as: 'quux987_labels',
let: { quux987_labels_ids: { $ifNull: ['$labels', []] } },
let: { tmpVar: { $ifNull: ['$labels', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$quux987_labels_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$match: { name: { $eq: 'foo' } },
},
Expand Down Expand Up @@ -680,16 +680,16 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'zip567_posts',
let: { zip567_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$zip567_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'labels-collection',
as: 'quux987_labels',
let: { quux987_labels_ids: { $ifNull: ['$labels', []] } },
let: { tmpVar: { $ifNull: ['$labels', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$quux987_labels_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$match: { name: { $eq: 'foo' } },
},
Expand Down Expand Up @@ -798,16 +798,16 @@ describe('join builder', () => {
$lookup: {
from: 'posts-collection',
as: 'zip567_posts',
let: { zip567_posts_ids: { $ifNull: ['$posts', []] } },
let: { tmpVar: { $ifNull: ['$posts', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$zip567_posts_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{
$lookup: {
from: 'labels-collection',
as: 'quux987_labels',
let: { quux987_labels_ids: { $ifNull: ['$labels', []] } },
let: { tmpVar: { $ifNull: ['$labels', []] } },
pipeline: [
{ $match: { $expr: { $in: ['$_id', '$$quux987_labels_ids'] } } },
{ $match: { $expr: { $in: ['$_id', '$$tmpVar'] } } },
{ $match: { name: { $eq: 'foo' } } },
{ $addFields: { id: '$_id' } },
],
Expand Down

0 comments on commit 3e49b94

Please sign in to comment.