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 | 22x 22x 22x 132x 132x 1x 8x 8x 8x 2x 2x 8x 6x 1x 5x 8x 1x 13x 13x 13x 5x 5x 13x 11x 2x 9x 8x 7x 7x 1x 1x 1x 7x 7x 13x 1x 3x 3x 1x 1x 1x 1x | /** * 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 */ /** * When creating a new data collection in the persistent data storage layer, Kuzzle uses a default mapping. * It means that, by default, you won’t be able to exploit the full capabilities of our persistent data storage layer * (currently handled by ElasticSearch), and your searches may suffer from below-average performances, depending on * the amount of data you stored in a collection and the complexity of your database. * * The CollectionMapping object allow to get the current mapping of a data collection and to modify it if needed. * * @param {object} collection - Instance of the inherited Collection object * @param {object} [mapping] - mappings * @constructor */ function CollectionMapping(collection, mapping) { Object.defineProperties(this, { //read-only properties collection: { value: collection, enumerable: true }, kuzzle: { value: collection.kuzzle, enumerable: true }, // writable properties headers: { value: JSON.parse(JSON.stringify(collection.headers)), enumerable: true, writable: true }, mapping: { value: mapping || {}, enumerable: true, writable: true } }); Eif (this.kuzzle.bluebird) { return this.kuzzle.bluebird.promisifyAll(this, { suffix: 'Promise', filter: function (name, func, target, passes) { var blacklist = ['set', 'setHeaders']; return passes && blacklist.indexOf(name) === -1; } }); } return this; } /** * Applies the new mapping to the data collection. * * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - Handles the query response */ CollectionMapping.prototype.apply = function (options, cb) { var self = this, data = this.kuzzle.addHeaders({body: {properties: this.mapping}}, this.headers); if (!cb && typeof options === 'function') { cb = options; options = null; } self.kuzzle.query(this.collection.buildQueryArgs('collection', 'updateMapping'), data, options, function (err) { if (err) { return cb && cb(err); } self.refresh(options, cb); }); return this; }; /** * Replaces the current content with the mapping stored in Kuzzle * * Calling this function will discard any uncommited changes. You can commit changes by calling the “apply” function * * @param {object} [options] - Optional parameters * @param {responseCallback} [cb] - Handles the query response * @returns {*} this */ CollectionMapping.prototype.refresh = function (options, cb) { var self = this, data = this.kuzzle.addHeaders({}, this.headers); if (!cb && typeof options === 'function') { cb = options; options = null; } this.kuzzle.query(this.collection.buildQueryArgs('collection', 'getMapping'), data, options, function (err, res) { if (err) { return cb ? cb(err) : false; } if (res.result[self.collection.index]) { if (res.result[self.collection.index].mappings[self.collection.collection]) { self.mapping = res.result[self.collection.index].mappings[self.collection.collection].properties; // Mappings can be empty. The mapping property should never be "undefined" if (self.mapping === undefined) { self.mapping = {}; } } else { return cb && cb(new Error('No mapping found for collection ' + self.collection.collection)); } } else { return cb && cb(new Error('No mapping found for index ' + self.collection.index)); } Eif (cb) { cb(null, self); } }); return this; }; /** * Adds or updates a field mapping. * * Changes made by this function won’t be applied until you call the apply method * * @param {string} field - Name of the field from which the mapping is to be added or updated * @param {object} mapping - corresponding field mapping * @returns {CollectionMapping} */ CollectionMapping.prototype.set = function (field, mapping) { this.mapping[field] = mapping; return this; }; /** * 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 */ CollectionMapping.prototype.setHeaders = function (content, replace) { this.kuzzle.setHeaders.call(this, content, replace); return this; }; module.exports = CollectionMapping; |