Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | 2x 2x 2x 2x 2x 202x 2x 200x 200x 168x 200x 153x 6426x 6426x 47x 2x 7x 2x 2x 7x 4x 4x 4x 2x 9x 9x 9x 9x 9x 9x 16x 7x 1x 6x 9x 8x 5x 3x 2x 1x 1x 6x 4x 4x 2x 6x 6x 6x 6x 2x 18x 18x 18x 18x 8x 8x 8x 8x 18x 6x 6x 18x 1x 17x 18x 3x 1x 2x 1x 17x 4x 17x 17x 11x 1x 10x 10x 10x 17x 2x 6x 6x 5x 5x 1x 1x 6x 3x 3x 6x 6x 5x 1x 4x 6x 2x 6x 6x 6x 3x 3x 6x 6x 5x 6x 2x 7x 7x 7x 2x 2x 7x 4x 4x 2x 7x 7x 7x 2x 2x 7x 4x 4x 4x 1x 3x 3x 3x 2x 6x 1x 1x 6x 4x 4x 2x 4x 4x 4x 2x 2x 4x 1x 3x 3x 8x 3x 3x 2x 3x 2x 4x 4x 4x 2x 2x 4x 1x 3x 3x 8x 3x 3x 2x 3x 2x 4x 4x 4x 2x 2x 4x 1x 3x 3x 3x 2x 3x 2x 4x 4x 4x 2x 2x 4x 1x 3x 3x 3x 2x 2x 4x 4x 4x 2x 2x 4x 1x 3x 3x 8x 3x 3x 2x 3x 2x 4x 4x 4x 2x 2x 4x 1x 3x 3x 8x 3x 3x 2x 3x 2x 6x 6x 6x 1x 1x 6x 4x 4x 4x 2x 2x 2x 1x 1x 2x 2x 2x 2x 6x 6x 6x 2x 2x 6x 6x 4x 1x 3x 3x 3x 6x 2x 7x 7x 2x 2x 7x 4x 4x 4x 4x 1x 3x 3x 3x 3x 3x 3x 3x 2x 3x 3x 3x 1x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 3x 3x 1x 2x 2x 1x 1x 1x 2x 7x 7x 7x 2x 2x 7x 4x 4x 4x 2x 4x 1x 1x 4x 3x 3x 3x 3x 2x 5x 5x 1x 1x 5x 5x 5x 2x 6x 6x 6x 2x 2x 6x 1x 6x 6x 4x 1x 3x 6x 2x 6x 6x 6x 6x 6x 6x 2x 2x 6x 6x 4x 6x 2x 6x 6x 6x 6x 6x 6x 1x 1x 6x 4x 4x 4x 2x 1x 2x 1x 2x 1x 2x 2x 2x 2x | var KuzzleSearchResult = require('./SearchResult'), Document = require('./Document'), CollectionMapping = require('./CollectionMapping'), Room = require('./Room'), SubscribeResult = require('./SubscribeResult'); /** * This is a global callback pattern, called by all asynchronous functions of the Kuzzle object. * * @callback responseCallback * @param {Object} err - Error object, NULL if the query is successful * @param {Object} [data] - The content of the query response */ /** * A data collection is a set of data managed by Kuzzle. It acts like a data table for persistent documents, * or like a room for pub/sub messages. * * @property {string} collection * @property {string} index * @property {Kuzzle} kuzzle * @property {Array.<string>} collection * @param {object} kuzzle - Kuzzle instance to inherit from * @param {string} collection - name of the data collection to handle * @param {string} index - Index containing the data collection * @constructor */ function Collection(kuzzle, collection, index) { if (!index || !collection) { throw new Error('The Collection object constructor needs an index and a collection arguments'); } Object.defineProperties(this, { // read-only properties collection: { value: collection, enumerable: true }, index: { value: index, enumerable: true }, kuzzle: { value: kuzzle, enumerable: true }, // writable properties headers: { value: JSON.parse(JSON.stringify(kuzzle.headers)), enumerable: true, writable: true } }); Object.defineProperty(this, 'buildQueryArgs', { value: function (controller, action) { return { controller: controller, action: action, collection: this.collection, index: this.index }; } }); if (this.kuzzle.bluebird) { return this.kuzzle.bluebird.promisifyAll(this, { suffix: 'Promise', filter: function (name, func, target, passes) { var blacklist = ['publishMessage', 'setHeaders', 'subscribe']; return passes && blacklist.indexOf(name) === -1; } }); } return this; } /** * Returns the number of documents matching the provided set of filters. * * There is a small delay between documents creation and their existence in our advanced search layer, * usually a couple of seconds. * That means that a document that was just been created won’t be returned by this function * * @param {object} filters - Filters in Elasticsearch Query DSL format * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.count = function (filters, options, cb) { var query; if (!cb && typeof options === 'function') { cb = options; options = null; } this.kuzzle.callbackRequired('Collection.count', cb); query = this.kuzzle.addHeaders({body: filters}, this.headers); this.kuzzle.query(this.buildQueryArgs('document', 'count'), query, options, function (err, res) { cb(err, err ? undefined : res.result.count); }); }; /** * Create a new empty data collection, with no associated mapping. * Kuzzle automatically creates data collections when storing documents, but there are cases where we * want to create and prepare data collections before storing documents in it. * * @param {object} [mappings] - Optional collection mappings * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - returns Kuzzle's response * @returns {*} this */ Collection.prototype.create = function () { var i, data = {}, mapping = null, options = null, cb = null, self = this; for (i = 0; i < arguments.length; i++) { if (typeof arguments[i] === 'function') { if (i < arguments.length - 1) { throw new Error('Invalid argument: ' + arguments[i+1]); } cb = arguments[i]; } else if (typeof arguments[i] === 'object' && !Array.isArray(arguments[i])) { if (mapping === null) { mapping = arguments[i]; } else if (options === null) { options = arguments[i]; } else { throw new Error('Too many objects arguments'); } } else { throw new Error('Invalid argument: ' + arguments[i]); } } if (mapping === null || (options === null && typeof mapping.queuable === 'boolean')) { options = mapping; data = {}; } else { data = {body: mapping}; } data = this.kuzzle.addHeaders(data, this.headers); this.kuzzle.query(this.buildQueryArgs('collection', 'create'), data, options, function(err) { cb(err, err ? undefined : self); }); return this; }; /** * Create a new document in Kuzzle. * * Takes an optional argument object with the following properties: * - volatile (object, default: null): * Additional information passed to notifications to other users * - ifExist (string, allowed values: "error" (default), "replace"): * If the same document already exists: * - resolves with an error if set to "error". * - replaces the existing document if set to "replace" * * @param {string} [id] - (optional) document identifier * @param {object} document - either an instance of a Document object, or a document * @param {object} [options] - optional arguments * @param {responseCallback} [cb] - Handles the query response * @returns {Object} this */ Collection.prototype.createDocument = function (id, document, options, cb) { var self = this, data = {}, action = 'create'; if (id && typeof id !== 'string') { cb = options; options = document; document = id; id = null; } if (!cb && typeof options === 'function') { cb = options; options = null; } if (document instanceof Document) { data = document.serialize(); } else { data.body = document; } if (options && options.ifExist) { if (options.ifExist === 'replace') { action = 'createOrReplace'; } else if (options.ifExist !== 'error') { throw new Error('Invalid value for the "ifExist" option: ' + options.ifExist); } } if (id) { data._id = id; } data = self.kuzzle.addHeaders(data, self.headers); self.kuzzle.query(this.buildQueryArgs('document', action), data, options, cb && function (err, res) { var doc; if (err) { return cb(err); } doc = new Document(self, res.result._id, res.result._source, res.result._meta); doc.version = res.result._version; cb(null, doc); }); return this; }; /** * Delete persistent documents. * * There is a small delay between documents creation and their existence in our advanced search layer, * usually a couple of seconds. * That means that a document that was just been created won’t be returned by this function * * Takes an optional argument object with the following properties: * - volatile (object, default: null): * Additional information passed to notifications to other users * * @param {string|object} arg - Either a document ID (will delete only this particular document), or a set of filters * @param {object} [options] - optional arguments * @param {responseCallback} [cb] - Handles the query response * @returns {Collection} this */ Collection.prototype.deleteDocument = function (arg, options, cb) { var action, data = {}; if (typeof arg === 'string') { data._id = arg; action = 'delete'; } else { data.body = {query: arg}; action = 'deleteByQuery'; } if (!cb && typeof options === 'function') { cb = options; options = null; } data = this.kuzzle.addHeaders(data, this.headers); this.kuzzle.query(this.buildQueryArgs('document', action), data, options, cb && function (err, res) { if (err) { cb(err); } else { cb(null, (action === 'delete' ? [res.result._id] : res.result.ids)); } }); return this; }; /** * Deletes the current specifications of this collection * * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - Handles the query response * @return {object} this */ Collection.prototype.deleteSpecifications = function (options, cb) { var data = { index: this.index, collection: this.collection }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('collection', 'deleteSpecifications'), data, options, function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Returns a boolean indicating whether or not a document with provided ID exists. * * @param {string} documentId - Unique document identifier * @param {object} options [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.documentExists = function (documentId, options, cb) { var data = {_id: documentId}, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } self.kuzzle.callbackRequired('Collection.documentExists', cb); self.kuzzle.query(this.buildQueryArgs('document', 'exists'), data, options, function (err, res) { cb(err, err ? undefined : res.result); }); }; /** * Retrieve a single stored document using its unique document ID. * * @param {string} documentId - Unique document identifier * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.fetchDocument = function (documentId, options, cb) { var data = {_id: documentId}, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } self.kuzzle.callbackRequired('Collection.fetch', cb); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'get'), data, options, function (err, res) { var document; if (err) { return cb(err); } document = new Document(self, res.result._id, res.result._source, res.result._meta); document.version = res.result._version; cb(null, document); }); }; /** * Instantiates a CollectionMapping object containing the current mapping of this collection. * * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object */ Collection.prototype.getMapping = function (options, cb) { var kuzzleMapping; if (!cb && typeof options === 'function') { cb = options; options = null; } this.kuzzle.callbackRequired('Collection.getMapping', cb); kuzzleMapping = new CollectionMapping(this); kuzzleMapping.refresh(options, cb); }; /** * Create the provided documents * * @param {Array.<document>} documents - Array of documents to create * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object * @returns {object} this */ Collection.prototype.mCreateDocument = function (documents, options, cb) { var data = { body: {}, }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (!Array.isArray(documents)) { return cb(new Error('Collection.mCreateDocument: documents parameter format is invalid (should be an array of documents)')); } self.kuzzle.callbackRequired('Collection.mCreate', cb); data.body.documents = documents.map(function (doc) { return (doc instanceof Document) ? doc.serialize() : doc; }); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'mCreate'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Create or replace the provided documents * * @param {Array.<document>} documents - Array of documents to create or replace * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object * @returns {object} this */ Collection.prototype.mCreateOrReplaceDocument = function (documents, options, cb) { var data = { body: {}, }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (!Array.isArray(documents)) { return cb(new Error('Collection.mCreateOrReplaceDocument: documents parameter format is invalid (should be an array of documents)')); } self.kuzzle.callbackRequired('Collection.mCreateOrReplace', cb); data.body.documents = documents.map(function (doc) { return (doc instanceof Document) ? doc.serialize() : doc; }); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'mCreateOrReplace'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Delete specific documents according to given IDs * * @param {Array.<string>} documentIds - IDs of the documents to delete * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object * @returns {object} this */ Collection.prototype.mDeleteDocument = function (documentIds, options, cb) { var data = { body: { ids: documentIds } }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (!Array.isArray(documentIds)) { return cb(new Error('Collection.mDeleteDocument: documentIds parameter format is invalid (should be an array of IDs)')); } self.kuzzle.callbackRequired('Collection.mDelete', cb); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'mDelete'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Get specific documents according to given IDs * * @param {Array.<string>} documentIds - IDs of the documents to retrieve * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object */ Collection.prototype.mGetDocument = function (documentIds, options, cb) { var data = { body: { ids: documentIds } }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (!Array.isArray(documentIds)) { return cb(new Error('Collection.mGetDocument: documentIds parameter format is invalid (should be an array of IDs)')); } self.kuzzle.callbackRequired('Collection.mGet', cb); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'mGet'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); }; /** * Replace the provided documents * * @param {Array.<document>} documents - Array of documents to replace * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object * @returns {object} this */ Collection.prototype.mReplaceDocument = function (documents, options, cb) { var data = { body: {} }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (!Array.isArray(documents)) { return cb(new Error('Collection.mReplaceDocument: documents parameter format is invalid (should be an array of documents)')); } self.kuzzle.callbackRequired('Collection.mReplace', cb); data.body.documents = documents.map(function (doc) { return (doc instanceof Document) ? doc.serialize() : doc; }); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'mReplace'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Update the provided documents * * @param {Array.<document>} documents - Array of documents to update * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Returns an instantiated CollectionMapping object * @returns {object} this */ Collection.prototype.mUpdateDocument = function (documents, options, cb) { var data = { body: {} }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (!Array.isArray(documents)) { return cb(new Error('Collection.mUpdateDocument: documents parameter format is invalid (should be an array of documents)')); } self.kuzzle.callbackRequired('Collection.mUpdate', cb); data.body.documents = documents.map(function (doc) { return (doc instanceof Document) ? doc.serialize() : doc; }); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'mUpdate'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Retrieves the current specifications of this collection * * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.getSpecifications = function (options, cb) { var data = { index: this.index, collection: this.collection }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } self.kuzzle.callbackRequired('Collection.getSpecifications', cb); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('collection', 'getSpecifications'), data, options, function (err, res) { cb(err, err ? undefined : res.result); }); }; /** * Publish a realtime message * * Takes an optional argument object with the following properties: * - volatile (object, default: null): * Additional information passed to notifications to other users * * @param {object} document - either a Document instance or a JSON object * @param {object} [options] - optional arguments * @param {responseCallback} [cb] - Returns a raw Kuzzle response * @returns {*} this */ Collection.prototype.publishMessage = function (document, options, cb) { var data = {}; if (document instanceof Document) { data = document.serialize(); } else { data.body = document; } data = this.kuzzle.addHeaders(data, this.headers); this.kuzzle.query(this.buildQueryArgs('realtime', 'publish'), data, options, cb); return this; }; /** * Replace an existing document with a new one. * * Takes an optional argument object with the following properties: * - volatile (object, default: null): * Additional information passed to notifications to other users * * @param {string} documentId - Unique document identifier of the document to replace * @param {object} content - JSON object representing the new document version * @param {object} [options] - additional arguments * @param {responseCallback} [cb] - Returns an instantiated Document object * @return {object} this */ Collection.prototype.replaceDocument = function (documentId, content, options, cb) { var self = this, data = { _id: documentId, body: content }; if (!cb && typeof options === 'function') { cb = options; options = null; } data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'createOrReplace'), data, options, cb && function (err, res) { var document; if (err) { return cb(err); } document = new Document(self, res.result._id, res.result._source, res.result._meta); document.version = res.result._version; cb(null, document); }); return this; }; /** * Executes an advanced search on the data collection. * * /!\ There is a small delay between documents creation and their existence in our advanced search layer, * usually a couple of seconds. * That means that a document that was just been created won’t be returned by this function. * * @param {object} filters - Filters in Elasticsearch Query DSL format * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.search = function (filters, options, cb) { var query, self = this; if (!cb && typeof options === 'function') { cb = options; options = {}; } self.kuzzle.callbackRequired('Collection.search', cb); query = self.kuzzle.addHeaders({body: filters}, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'search'), query, options, function (error, result) { var documents = []; if (error) { return cb(error); } result.result.hits.forEach(function (doc) { var newDocument = new Document(self, doc._id, doc._source, doc._meta); newDocument.version = doc._version; documents.push(newDocument); }); Eif (result.result._scroll_id) { options.scrollId = result.result._scroll_id; } cb(null, new KuzzleSearchResult( self, result.result.total, documents, result.result.aggregations ? result.result.aggregations : {}, options, filters, options.previous || null )); }); }; /** * A "scroll" option can be passed to search queries, creating persistent * paginated results. * This method can be used to manually get the next page of a search result, * instead of using KuzzleSearchResult.next() * * @param {string} scrollId * @param {object} [options] * @param {object} [filters] * @param {responseCallback} cb */ Collection.prototype.scroll = function (scrollId, options, filters, cb) { var request = {}, self = this; if (!scrollId) { throw new Error('Collection.scroll: scrollId is required'); } if (!cb) { cb = filters; filters = null; } Iif (!cb && typeof options === 'function') { cb = options; options = {}; } this.kuzzle.callbackRequired('Collection.scroll', cb); request.scrollId = scrollId; Iif (options && options.scroll) { request.scroll = options.scroll; } this.kuzzle.query({controller: 'document', action: 'scroll'}, request, options, function (error, result) { var documents = []; Iif (error) { return cb(error); } result.result.hits.forEach(function (doc) { var newDocument = new Document(self, doc._id, doc._source, doc._meta); newDocument.version = doc._version; documents.push(newDocument); }); Eif (result.result._scroll_id) { options.scrollId = result.result._scroll_id; } cb(null, new KuzzleSearchResult( self, result.result.total, documents, {}, options, filters, options.previous || null )); }); return this; }; /** * Retrieves next result of a search with scroll query. * * @param {string} scrollId * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.scrollSpecifications = function (scrollId, options, cb) { var data = { scrollId: scrollId }; if (!scrollId) { throw new Error('Collection.scrollSpecifications: scrollId is required'); } Iif (!cb && typeof options === 'function') { cb = options; options = {}; } this.kuzzle.callbackRequired('Collection.scrollSpecifications', cb); Iif (options && options.scroll) { data.scroll = options.scroll; } this.kuzzle.query( { controller: 'collection', action: 'scrollSpecifications'}, this.kuzzle.addHeaders(data, this.headers), options, function (err, res) { cb (err, err ? undefined : res.result); } ); }; /** * Searches specifications across indexes/collections according to the provided filters * * @param {object} [filters] - Optional filters in ElasticSearch Query DSL format * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.searchSpecifications = function (filters, options, cb) { var data = { body: { query: filters } }, self = this; if (!cb && typeof options === 'function') { cb = options; options = {}; } self.kuzzle.callbackRequired('Collection.searchSpecifications', cb); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query({ controller: 'collection', action: 'searchSpecifications' }, data, options, function (err, res) { cb(err, err ? undefined : res.result); }); }; /** * Subscribes to this data collection with a set of filters. * To subscribe to the entire data collection, simply provide an empty filter. * * @param {object} filters - Filters in Kuzzle DSL format * @param {object} [options] - subscriptions options * @param {responseCallback} cb - called for each new notification * @returns {*} KuzzleSubscribeResult object */ Collection.prototype.subscribe = function (filters, options, cb) { var room, subscribeResult; if (!cb && typeof options === 'function') { cb = options; options = null; } this.kuzzle.callbackRequired('Collection.subscribe', cb); subscribeResult = new SubscribeResult(); room = new Room(this, options); room.renew(filters, cb, subscribeResult.done.bind(subscribeResult)); return subscribeResult; }; /** * Truncate the data collection, removing all stored documents but keeping all associated mappings. * This method is a lot faster than removing all documents using a query. * * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - returns Kuzzle's response * @returns {*} this */ Collection.prototype.truncate = function (options, cb) { var data = {}; if (!cb && typeof options === 'function') { cb = options; options = null; } data = this.kuzzle.addHeaders(data, this.headers); this.kuzzle.query(this.buildQueryArgs('collection', 'truncate'), data, options, cb); return this; }; /** * Update parts of a document * * Takes an optional argument object with the following properties: * - volatile (object, default: null): * Additional information passed to notifications to other users * * @param {string} documentId - Unique document identifier of the document to update * @param {object} content - JSON object containing changes to perform on the document * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - Returns an instantiated Document object * @return {object} this */ Collection.prototype.updateDocument = function (documentId, content, options, cb) { var data = { _id: documentId, body: content }, self = this; if (!cb && typeof options === 'function') { cb = options; options = null; } if (options && options.retryOnConflict) { data.retryOnConflict = options.retryOnConflict; } data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('document', 'update'), data, options, cb && function (err, res) { if (err) { return cb(err); } (new Document(self, res.result._id)).refresh(cb); }); return self; }; /** * Updates the current specifications of this collection * * @param {object} specifications - Specifications content * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - Handles the query response * @return {object} this */ Collection.prototype.updateSpecifications = function (specifications, options, cb) { var collection = {}, data = { body: {} }, self = this; collection[this.collection] = specifications; data.body[this.index] = collection; if (!cb && typeof options === 'function') { cb = options; options = null; } data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('collection', 'updateSpecifications'), data, options, cb && function (err, res) { cb(err, err ? undefined : res.result); }); return self; }; /** * Validates the provided specifications * * @param {object} specifications - Specifications content * @param {object} [options] - Optional parameters * @param {responseCallback} cb - Handles the query response */ Collection.prototype.validateSpecifications = function (specifications, options, cb) { var collection = {}, data = { body: {} }, self = this; collection[this.collection] = specifications; data.body[this.index] = collection; if (!cb && typeof options === 'function') { cb = options; options = null; } self.kuzzle.callbackRequired('Collection.validateSpecifications', cb); data = self.kuzzle.addHeaders(data, this.headers); self.kuzzle.query(this.buildQueryArgs('collection', 'validateSpecifications'), data, options, function (err, res) { cb(err, err ? undefined : res.result.valid); }); }; /** * Instantiate a new Document object. Workaround to the module.exports limitation, preventing multiple * constructors to be exposed without having to use a factory or a composed object. * * @param {string} id - document id * @param {object} content - document content * @constructor */ Collection.prototype.document = function (id, content) { return new Document(this, id, content); }; /** * Instantiate a new Room object. Workaround to the module.exports limitation, preventing multiple * constructors to be exposed without having to use a factory or a composed object. * * @param {object} [options] - subscription configuration * @constructor */ Collection.prototype.room = function (options) { return new Room(this, options); }; /** * Instantiate a new CollectionMapping object. Workaround to the module.exports limitation, preventing multiple * constructors to be exposed without having to use a factory or a composed object. * * @param {object} [mapping] - mapping to instantiate the CollectionMapping object with * @constructor */ Collection.prototype.collectionMapping = function (mapping) { return new CollectionMapping(this, mapping); }; /** * Helper function allowing to set headers while chaining calls. * * If the replace argument is set to true, replace the current headers with the provided content. * Otherwise, it appends the content to the current headers, only replacing already existing values * * @param content - new headers content * @param [replace] - default: false = append the content. If true: replace the current headers with tj */ Collection.prototype.setHeaders = function (content, replace) { this.kuzzle.setHeaders.call(this, content, replace); return this; }; module.exports = Collection; |