|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -/* |
4 |
| - * QuickBlox JavaScript SDK |
5 |
| - * |
6 |
| - * Custom Objects module |
7 |
| - * |
8 |
| - */ |
9 |
| - |
10 |
| -var config = require('../qbConfig'), |
11 |
| - Utils = require('../qbUtils'); |
12 |
| - |
13 |
| -// For server-side applications through using npm package 'quickblox' you should include the following lines |
14 |
| -var isBrowser = typeof window !== 'undefined'; |
| 3 | +const config = require('../qbConfig'); |
| 4 | +const Utils = require('../qbUtils'); |
15 | 5 |
|
16 | 6 | /**
|
17 |
| - * Custom Objects |
| 7 | + * Custom Objects Module |
18 | 8 | * @namespace QB.data
|
19 | 9 | **/
|
20 | 10 | function DataProxy(service){
|
21 | 11 | this.service = service;
|
22 | 12 | }
|
23 | 13 |
|
24 | 14 | DataProxy.prototype = {
|
25 |
| - |
26 | 15 | /**
|
27 | 16 | * Create new custom object ({@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Create_object read more})
|
| 17 | + * |
28 | 18 | * @memberof QB.data
|
| 19 | + * |
29 | 20 | * @param {string} className - A class name to which a new object belongs
|
30 | 21 | * @param {object} data - Object of parameters (custom fields' names and their values)
|
31 | 22 | * @param {createDataCallback} callback - The createDataCallback function
|
| 23 | + * |
32 | 24 | * @example
|
33 |
| - * QB.data.create('GameOfThrones', { |
| 25 | + * const data = { |
34 | 26 | * 'name': 'John',
|
35 | 27 | * 'age':'20',
|
36 | 28 | * 'family': [
|
37 | 29 | * 'Stark',
|
38 | 30 | * 'Targaryen'
|
39 | 31 | * ]
|
40 |
| - * }, function(error, response) { |
| 32 | + * }; |
| 33 | + * |
| 34 | + * function createdDataCallback(error, response) { |
41 | 35 | * if (error) {
|
42 | 36 | * console.log(error);
|
43 | 37 | * } else {
|
44 | 38 | * console.log(response);
|
45 | 39 | * }
|
46 |
| - * }); |
| 40 | + * } |
| 41 | + * |
| 42 | + * QB.data.create('GameOfThrones', data, createdDataCallback); |
47 | 43 | */
|
48 | 44 | create: function(className, data, callback) {
|
49 | 45 | /**
|
@@ -71,7 +67,9 @@ DataProxy.prototype = {
|
71 | 67 |
|
72 | 68 | /**
|
73 | 69 | * Search for records of particular class ({@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Retrieve_objects read more})
|
| 70 | + * |
74 | 71 | * @memberof QB.data
|
| 72 | + * |
75 | 73 | * @param {string} className - A class name to which a new record belongs
|
76 | 74 | * @param {(object|string[])} filters - Search records with field which contains exactly specified value or by array of records' ids to retrieve
|
77 | 75 | * @param {number} [filters.skip=0] - Skip N records in search results. Useful for pagination. Default (if not specified) - 0
|
@@ -122,7 +120,7 @@ DataProxy.prototype = {
|
122 | 120 | },
|
123 | 121 |
|
124 | 122 | /**
|
125 |
| - * Update record by ID of particular class ({@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Retrieve_objects read more}) |
| 123 | + * Update record by ID of particular class. ({@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Retrieve_objects Read more}) |
126 | 124 | * @memberof QB.data
|
127 | 125 | * @param {string} className - A class name of record
|
128 | 126 | * @param {object} data - Object of parameters
|
@@ -165,27 +163,114 @@ DataProxy.prototype = {
|
165 | 163 | },
|
166 | 164 |
|
167 | 165 | /**
|
168 |
| - * Delete record by ID ({@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Delete_objects_by_IDs or records by ids}) of particular class ({@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Delete_object read more}) |
| 166 | + * Delete record / records by ID, IDs or criteria (filters) of particular class. <br /> |
| 167 | + * Check out {@link https://docsdev.quickblox.com/rest_api/CustomObjects_API.html#Delete_object official documentaion} to get detailed information. |
| 168 | + * |
169 | 169 | * @memberof QB.data
|
170 |
| - * @param {string} className - A class name of record (records) |
171 |
| - * @param {(string|array)} id - An ID of record (or an array of records' ids) to delete |
172 |
| - * @param {deleteDataByIdsCallback} callback - The deleteDataByIdsCallback function |
| 170 | + * |
| 171 | + * @param {string} className - A class name of record |
| 172 | + * @param {(string|array|object)} requestedData - An ID of record or an array of record's ids or object of criteria rules to delete |
| 173 | + * @param {deletedDataCallback} callback - The deletedDataCallback function |
| 174 | + * |
| 175 | + * @example |
| 176 | + * const className = 'Movie'; |
| 177 | + * |
| 178 | + * function deletedMovie(error, responce) { |
| 179 | + * if(error) { |
| 180 | + * throw new Error(error.toString()); |
| 181 | + * } else { |
| 182 | + * console.log(`Deleted movies with ids: ${responce.deleted.toString()}`); |
| 183 | + * } |
| 184 | + * } |
| 185 | + * |
| 186 | + * // By ID, must be string |
| 187 | + * const id = '502f7c4036c9ae2163000002'; |
| 188 | + * QB.data.delete(className, id, deletedMovie); |
| 189 | + * |
| 190 | + * // By IDs, must be array |
| 191 | + * const ids = ['502f7c4036c9ae2163000002', '502f7c4036c9ae2163000003']; |
| 192 | + * QB.data.delete(className, ids, deletedMovie); |
| 193 | + * |
| 194 | + * const criteria = { 'price': { 'gt': 100 }; |
| 195 | + * function deletedMoviesByCriteria(error, responce) { |
| 196 | + * if(error) { |
| 197 | + * throw new Error(error.toString()); |
| 198 | + * } else { |
| 199 | + * // Note! Deleted by creteria doesn't return ids of deleted objects |
| 200 | + * console.log(`Deleted ${responce.deletedCount} movies`); |
| 201 | + * } |
| 202 | + * } |
| 203 | + * QB.data.delete(className, criteria, deletedMoviesByCriteria); |
| 204 | + * |
| 205 | + * |
173 | 206 | */
|
174 |
| - delete: function(className, id, callback) { |
| 207 | + delete: function(className, requestedData, callback) { |
175 | 208 | /**
|
176 |
| - * Callback for QB.data.delete(className, id, callback) |
177 |
| - * @callback deleteDataByIdsCallback |
| 209 | + * Callback for QB.data.delete(className, requestedData, callback) |
| 210 | + * @callback deletedDataCallback |
178 | 211 | * @param {object} error - The error object
|
179 |
| - * @param {object} response - Empty body |
| 212 | + * @param {object|null} response |
| 213 | + * @param {array} response.deleted - Array of ids of deleted records. If you delete BY CRITERIA this property will be null. |
| 214 | + * @param {number} response.deletedCount - count of deleted records. |
180 | 215 | */
|
181 |
| - this.service.ajax({url: Utils.getUrl(config.urls.data, className + '/' + id), type: 'DELETE', dataType: 'text'}, |
182 |
| - function(err, result) { |
183 |
| - if (err) { |
184 |
| - callback(err, null); |
185 |
| - } else { |
186 |
| - callback (err, true); |
| 216 | + const typesData = { |
| 217 | + id: 1, |
| 218 | + ids: 2, |
| 219 | + criteria: 3 |
| 220 | + }; |
| 221 | + |
| 222 | + let requestedTypeOf; |
| 223 | + |
| 224 | + const responceNormalized = { |
| 225 | + deleted: [], |
| 226 | + deletedCount: 0 |
| 227 | + }; |
| 228 | + |
| 229 | + const ajaxParams = { |
| 230 | + type: 'DELETE', |
| 231 | + dataType: 'text' |
| 232 | + }; |
| 233 | + |
| 234 | + /** Define what type of data passed by client */ |
| 235 | + if(typeof requestedData === 'string') { |
| 236 | + requestedTypeOf = typesData.id; |
| 237 | + } else if(Utils.isArray(requestedData)) { |
| 238 | + requestedTypeOf = typesData.ids; |
| 239 | + } else if(Utils.isObject(requestedData)) { |
| 240 | + requestedTypeOf = typesData.criteria; |
| 241 | + } |
| 242 | + |
| 243 | + if(requestedTypeOf === typesData.id) { |
| 244 | + ajaxParams.url = Utils.getUrl(config.urls.data, className + '/' + requestedData); |
| 245 | + } else if(requestedTypeOf === typesData.ids) { |
| 246 | + ajaxParams.url = Utils.getUrl(config.urls.data, className + '/' + requestedData.toString()); |
| 247 | + } else if(requestedTypeOf === typesData.criteria) { |
| 248 | + ajaxParams.url = Utils.getUrl(config.urls.data, className + '/by_criteria'); |
| 249 | + ajaxParams.data = requestedData; |
| 250 | + } |
| 251 | + |
| 252 | + function handleDeleteCO(error, result) { |
| 253 | + if (error) { |
| 254 | + callback(error, null); |
| 255 | + } else { |
| 256 | + if(requestedTypeOf === typesData.id) { |
| 257 | + responceNormalized.deleted.push(requestedData); |
| 258 | + responceNormalized.deletedCount = responceNormalized.deleted.length; |
| 259 | + } else if(requestedTypeOf === typesData.ids) { |
| 260 | + const response = JSON.parse(result); |
| 261 | + responceNormalized.deleted = response.SuccessfullyDeleted.ids.slice(0); |
| 262 | + responceNormalized.deletedCount = responceNormalized.deleted.length; |
| 263 | + } else if(requestedTypeOf === typesData.criteria) { |
| 264 | + const response = JSON.parse(result); |
| 265 | + responceNormalized.deleted = null; |
| 266 | + responceNormalized.deletedCount = response.total_deleted; |
187 | 267 | }
|
188 |
| - }); |
| 268 | + |
| 269 | + callback (error, responceNormalized); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + this.service.ajax(ajaxParams, handleDeleteCO); |
189 | 274 | },
|
190 | 275 |
|
191 | 276 | /**
|
|
0 commit comments