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

Use tmpVar in generated queries #2261

Merged
merged 1 commit into from
Jan 20, 2020
Merged
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
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