Skip to content

Commit

Permalink
Merge pull request #391 from TradeWing/partial-filter
Browse files Browse the repository at this point in the history
For Many links support partial filter index to allow multiple documents
  • Loading branch information
theodorDiaconu authored Nov 4, 2019
2 parents 727094a + e9a006a commit 41405a0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/links/linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,23 @@ export default class Linker {
);
}

let options = { unique: true };

if (this.isSingle()) {
options = {...options, sparse: true};
} else {
options = {...options,
partialFilterExpression: {
[field]: { $type: 'string' }
}
}
}

this.mainCollection._ensureIndex(
{
[field]: 1,
},
{ unique: true, sparse: true }
options
);
}
}
Expand Down
23 changes: 23 additions & 0 deletions lib/links/tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ PostCollection.addLinks({
field: "commentIds",
index: true
},
commentsUnique: {
type: "*",
collection: CommentCollection,
field: "commentUniqueIds",
unique: true,
},
autoRemoveComments: {
type: "*",
collection: CommentCollection,
Expand Down Expand Up @@ -55,6 +61,10 @@ CommentCollection.addLinks({
collection: PostCollection,
inversedBy: "comments"
},
postUnique: {
collection: PostCollection,
inversedBy: "commentsUnique"
},
inversedPost: {
collection: PostCollection,
field: "postId"
Expand Down Expand Up @@ -246,13 +256,17 @@ describe("Collection Links", function() {
let post = PostCollection.findOne(postId);
let comment = CommentCollection.findOne(commentId);
let commentsLink = PostCollection.getLink(post, "comments");
let commentsUniqueLink = PostCollection.getLink(post, "commentsUnique");
let metaCommentsLink = PostCollection.getLink(post, "metaComments");
let postLink = CommentCollection.getLink(comment, "post");
let postUniqueLink = CommentCollection.getLink(comment, "postUnique");
let postMetaLink = CommentCollection.getLink(comment, "metaPost");

commentsLink.add(comment);
commentsUniqueLink.add(comment);
metaCommentsLink.add(comment);
assert.lengthOf(postLink.find().fetch(), 1);
assert.isObject(postUniqueLink.fetch());
assert.lengthOf(postMetaLink.find().fetch(), 1);

CommentCollection.remove(comment._id);
Expand Down Expand Up @@ -433,4 +447,13 @@ describe("Collection Links", function() {

assert.equal('someData', result.oneMeta.$metadata.data);
});

it("Should not result in duplicate key error on Many Unique links", function() {
let postIdA = PostCollection.insert({ text: "abc" });
let postIdB = PostCollection.insert({ text: "abc" });

PostCollection.remove(postIdA);
PostCollection.remove(postIdB);
});

});

0 comments on commit 41405a0

Please sign in to comment.