Example #1
0
        public void CanUseRecurse()
        {
            using (var store = GetDocumentStore())
            {
                new Posts_Recurse().Execute(store);

                using (var session = store.OpenSession())
                {
                    var post1 = new Post {
                        Title = "Post1", Desc = "Post1 desc"
                    };
                    var post2 = new Post {
                        Title = "Post2", Desc = "Post2 desc", Comments = new Post[] { post1 }
                    };
                    var post3 = new Post {
                        Title = "Post3", Desc = "Post3 desc", Comments = new Post[] { post2 }
                    };
                    var post4 = new Post {
                        Title = "Post4", Desc = "Post4 desc", Comments = new Post[] { post3 }
                    };
                    session.Store(post4);
                    session.SaveChanges();
                    Indexes.WaitForIndexing(store);

                    var posts = session.Query <Post, Posts_Recurse>()
                                .ToArray();
                    Assert.Equal(1, posts.Length);
                    Assert.Equal("Post4", posts[0].Title);
                    Assert.Equal("Post3", posts[0].Comments[0].Title);
                    Assert.Equal("Post2", posts[0].Comments[0].Comments[0].Title);
                    Assert.Equal("Post1", posts[0].Comments[0].Comments[0].Comments[0].Title);
                }
            }
        }
Example #2
0
        public void CanDoScriptedPatching()
        {
            using (var store = GetDocumentStore())
            {
                using (var commands = store.Commands())
                {
                    commands.Put("posts/1", null, new Post
                    {
                        Title    = "Post 1",
                        Comments = new Post[] { }
                    }, null);

                    var comment = new Post
                    {
                        Title = "comment 1"
                    };

                    store.Operations.Send(new PatchOperation("posts/1", null,
                                                             new PatchRequest
                    {
                        Script = @"this.Comments.push(args.comment1)",
                        Values = { { "comment1", comment } }
                    }));

                    dynamic result   = commands.Get("posts/1");
                    dynamic comments = result.Comments;
                    Assert.Equal(1, comments.Length);
                    Assert.Equal("comment 1", comments[0].Title.ToString());

                    commands.Put("posts/2", null, new Post
                    {
                        Title         = "Post 2",
                        AttachmentIds = new[] { "id1", "id2" }
                    }, null);

                    store.Operations.Send(new PatchOperation("posts/2", null,
                                                             new PatchRequest
                    {
                        Script = @"
this.AttachmentIds = this.AttachmentIds.filter(function (t) { 
    return t != args.tagToRemove
})",
                        Values = { { "tagToRemove", "id2" } }
                    }));

                    result = commands.Get("posts/2");
                    var attachmentIds = result.AttachmentIds;
                    Assert.Equal(1, attachmentIds.Length);
                    Assert.Equal("id1", attachmentIds[0]);

                    store.Operations.Send(new PatchOperation("posts/1", null,
                                                             new PatchRequest
                    {
                        Script = @"
this.Comments = this.Comments.filter(function (c) { 
    return c.Title !== 'comment 1'
});",
                    }));

                    result   = commands.Get("posts/1");
                    comments = result.Comments;
                    Assert.Equal(0, comments.Length);

                    var comment1 = new Post
                    {
                        Title = "Comment 1",
                        Desc  = "Some post without searched phrase inside."
                    };
                    var comment2 = new Post
                    {
                        Title = "Comment 2",
                        Desc  = "Some post with Raven phrase inside."
                    };

                    commands.Put("posts/3", null, new Post
                    {
                        Title    = "Post 3",
                        Comments = new[] { comment1, comment2 }
                    }, null);

                    store.Operations.Send(new PatchOperation("posts/3", null,
                                                             new PatchRequest
                    {
                        Script = @"
                            this.Comments.map(function(comment) {  
                                if(comment.Desc.indexOf(""Raven"") != -1)
                                {
                                    comment.Title = ""[Raven] "" + comment.Title;
                                }
                                return comment;
                            });
                        "
                    }));

                    result   = commands.Get("posts/3");
                    comments = result.Comments;
                    Assert.Equal(2, comments.Length);
                    Assert.Equal("Comment 1", comments[0].Title.ToString());
                    Assert.Equal("[Raven] Comment 2", comments[1].Title.ToString());

                    commands.Put("posts/4", null, new Post
                    {
                        Title         = "Post 4",
                        AttachmentIds = new[] { "posts/5" }
                    }, null);

                    commands.Put("posts/5", null, new Post
                    {
                        Title = "Post 5"
                    }, null);

                    store.Operations.Send(new PatchOperation("posts/4", null,
                                                             new PatchRequest
                    {
                        Script = @"
                            var loaded = load(this.AttachmentIds[0]);
                            this.Title = loaded.Title;
                        "
                    }));

                    result = commands.Get("posts/4");;
                    Assert.Equal("Post 5", result.Title.ToString());

                    var command = new PatchOperation.PatchCommand(
                        store.Conventions,
                        commands.Context,
                        "posts/4",
                        null,
                        new PatchRequest
                    {
                        Script = @"
                            var loaded = load(this.AttachmentIds[0]);
                            this.Title = loaded.Title;
                            output(this.Title); 
                        "
                    },
                        patchIfMissing: null,
                        skipPatchIfChangeVectorMismatch: false,
                        returnDebugInformation: true,
                        test: false);

                    commands.RequestExecutor.Execute(command, commands.Context);

                    var commandResult = command.Result;
                    var array         = (BlittableJsonReaderArray)commandResult.Debug["Info"];
                    var id            = array[0].ToString();
                    Assert.Equal("Post 5", id);

                    store.Operations.Send(new PatchOperation("posts/3", null,
                                                             new PatchRequest
                    {
                        Script = @"
                            put('posts/4',
                                { 'Title' : 'new title' }
                            );"
                    }));

                    result = commands.Get("posts/4");
                    Assert.NotNull(result);
                    Assert.Equal("new title", result.Title.ToString());
                }
            }
        }
Example #3
0
        public void CanDoScriptedPatching()
        {
            using (var store = GetDocumentStore())
            {
                store.DatabaseCommands.Put(
                    "posts/1",
                    null,
                    RavenJObject.FromObject(new Post
                {
                    Title    = "Post 1",
                    Comments = new Post[] { }
                }),
                    new RavenJObject());

                var comment = new Post
                {
                    Title = "comment 1"
                };

                store.DatabaseCommands.Patch(
                    "posts/1",
                    new PatchRequest
                {
                    Script = @"this.Comments.push(comment1)",
                    Values = { { "comment1", comment } }
                });
                var result   = store.DatabaseCommands.Get("posts/1");
                var comments = result.DataAsJson.Value <RavenJArray>("Comments");
                Assert.Equal(1, comments.Length);
                Assert.Equal("comment 1", comments[0].Value <string>("Title"));


                store.DatabaseCommands.Put(
                    "posts/2",
                    null,
                    RavenJObject.FromObject(new Post
                {
                    Title         = "Post 2",
                    AttachmentIds = new[] { "id1", "id2" }
                }),
                    new RavenJObject());

                store.DatabaseCommands.Patch(
                    "posts/2",
                    new PatchRequest
                {
                    Script = @"this.AttachmentIds.Remove(tagToRemove)",
                    Values = { { "tagToRemove", "id2" } }
                });
                result = store.DatabaseCommands.Get("posts/2");
                Assert.Equal(1, result.DataAsJson.Value <RavenJArray>("AttachmentIds").Length);
                Assert.Equal("id1", result.DataAsJson.Value <RavenJArray>("AttachmentIds")[0]);


                store.DatabaseCommands.Patch(
                    "posts/1",
                    new PatchRequest
                {
                    Script = @"
                            this.Comments.RemoveWhere(function(comment) {
                                return comment.Title === 'comment 1';
                            });",
                });
                result   = store.DatabaseCommands.Get("posts/1");
                comments = result.DataAsJson.Value <RavenJArray>("Comments");
                Assert.Equal(0, comments.Length);


                var comment1 = new Post
                {
                    Title = "Comment 1",
                    Desc  = "Some post without searched phrase inside."
                };
                var comment2 = new Post
                {
                    Title = "Comment 2",
                    Desc  = "Some post with Raven phrase inside."
                };

                store.DatabaseCommands.Put(
                    "posts/3",
                    null,
                    RavenJObject.FromObject(new Post
                {
                    Title    = "Post 3",
                    Comments = new[] { comment1, comment2 }
                }),
                    new RavenJObject());
                store.DatabaseCommands.Patch(
                    "posts/3",
                    new PatchRequest
                {
                    Script = @"
                            this.Comments.Map(function(comment) {  
                                if(comment.Desc.indexOf(""Raven"") != -1)
                                {
                                    comment.Title = ""[Raven] "" + comment.Title;
                                }
                                return comment;
                            });
                        "
                });
                result   = store.DatabaseCommands.Get("posts/3");
                comments = result.DataAsJson.Value <RavenJArray>("Comments");
                Assert.Equal(2, comments.Length);
                Assert.Equal("Comment 1", comments[0].Value <string>("Title"));
                Assert.Equal("[Raven] Comment 2", comments[1].Value <string>("Title"));


                store.DatabaseCommands.Put(
                    "posts/4",
                    null,
                    RavenJObject.FromObject(new Post
                {
                    Title         = "Post 4",
                    AttachmentIds = new[] { "posts/5" }
                }),
                    new RavenJObject());
                store.DatabaseCommands.Put(
                    "posts/5",
                    null,
                    RavenJObject.FromObject(new Post
                {
                    Title = "Post 5"
                }),
                    new RavenJObject());
                store.DatabaseCommands.Patch(
                    "posts/4",
                    new PatchRequest
                {
                    Script = @"
                            var loaded = LoadDocument(this.AttachmentIds[0]);
                            this.Title = loaded.Title;
                        "
                });
                result = store.DatabaseCommands.Get("posts/4");
                Assert.Equal("Post 5", result.DataAsJson.Value <string>("Title"));


                var output = store.DatabaseCommands.Patch(
                    "posts/4",
                    new PatchRequest
                {
                    Script = @"
                            var loaded = LoadDocument(this.AttachmentIds[0]);
                            this.Title = loaded.Title;
                            output(this.Title); 
                        "
                });
                var debugInfo = output.Value <RavenJArray>("Debug");
                Assert.Equal("Post 5", debugInfo[0]);

                store.DatabaseCommands.Patch(
                    "posts/4",
                    new PatchRequest
                {
                    Script = @"
                            PutDocument('posts/4',
                                { 'Title' : 'new title' }
                            );"
                });
                var post = store.DatabaseCommands.Get("posts/4");
                Assert.NotNull(post);
                Assert.Equal("new title", post.DataAsJson.Value <string>("Title"));
            }
        }