syncano-server
Advanced tools
Comparing version 0.8.2-4 to 0.8.2-5
@@ -39,2 +39,4 @@ 'use strict'; | ||
var _utils = require('./utils'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -50,2 +52,4 @@ | ||
var MAX_BATCH_SIZE = 50; | ||
/** | ||
@@ -55,2 +59,3 @@ * Syncano server | ||
*/ | ||
var Data = function (_QueryBuilder) { | ||
@@ -177,5 +182,6 @@ _inherits(Data, _QueryBuilder); | ||
request('' + baseUrl + response.next); | ||
} else { | ||
return true; | ||
return false; | ||
} | ||
return true; | ||
} | ||
@@ -185,3 +191,3 @@ | ||
if (shouldResolve === false) { | ||
return; | ||
return false; | ||
} | ||
@@ -264,3 +270,3 @@ | ||
if (shouldResolve === false) { | ||
return; | ||
return false; | ||
} | ||
@@ -289,3 +295,3 @@ | ||
if (shouldResolve === false) { | ||
return; | ||
return false; | ||
} | ||
@@ -674,3 +680,46 @@ | ||
} | ||
}, { | ||
key: '_chunk', | ||
value: function _chunk(items, size) { | ||
var chunks = []; | ||
while (items.length > 0) { | ||
chunks.push(items.splice(0, size)); | ||
} | ||
return chunks; | ||
} | ||
}, { | ||
key: '_batch', | ||
value: function _batch(body, headers) { | ||
var _this8 = this; | ||
var requests = this._chunk(body, MAX_BATCH_SIZE).map(function (chunk) { | ||
return function () { | ||
var fetchObject = _this8._batchFetchObject(chunk); | ||
return _this8.fetch(fetchObject.url, fetchObject, headers); | ||
}; | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
var resolves = []; | ||
var i = 0;(function next() { | ||
var request = requests[i++]; | ||
if (request) { | ||
request().then(function (data) { | ||
resolves.push(data); | ||
next(); // eslint-disable-line promise/no-callback-in-promise | ||
}).catch(function (err) { | ||
reject(err); | ||
}); | ||
} else { | ||
resolve(resolves); | ||
} | ||
})(); | ||
}); | ||
} | ||
/** | ||
@@ -706,3 +755,3 @@ * Create new object. | ||
} else if (Array.isArray(body)) { | ||
fetchObject = this._batchFetchObject(body); | ||
return this._batch(body, headers); | ||
} | ||
@@ -734,4 +783,5 @@ | ||
value: function update(id, body) { | ||
var _this8 = this; | ||
var _this9 = this; | ||
var headers = null; | ||
var isQueryUpdate = (typeof id === 'undefined' ? 'undefined' : _typeof(id)) === 'object' && id !== null && !Array.isArray(id); | ||
@@ -750,13 +800,14 @@ var fetchObject = { | ||
fetchObject = _this8._batchFetchObject(ids); | ||
return _this8.fetch(fetchObject.url, fetchObject); | ||
return _this9._batch(ids); | ||
}); | ||
} | ||
if (Array.isArray(id)) { | ||
fetchObject = this._batchFetchObject(id); | ||
if (body instanceof _formData2.default) { | ||
fetchObject.body = body; | ||
headers = body.getHeaders(); | ||
} else if (Array.isArray(id)) { | ||
return this._batch(id); | ||
} | ||
return this.fetch(fetchObject.url, fetchObject); | ||
return this.fetch(fetchObject.url, fetchObject, headers); | ||
} | ||
@@ -779,3 +830,3 @@ | ||
value: function _delete(id) { | ||
var _this9 = this; | ||
var _this10 = this; | ||
@@ -794,5 +845,3 @@ var isQueryDelete = id === undefined; | ||
fetchObject = _this9._batchFetchObject(ids); | ||
return _this9.fetch(fetchObject.url, fetchObject); | ||
return _this10._batch(ids); | ||
}); | ||
@@ -802,3 +851,3 @@ } | ||
if (Array.isArray(id)) { | ||
fetchObject = this._batchFetchObject(id); | ||
return this._batch(id); | ||
} | ||
@@ -805,0 +854,0 @@ |
{ | ||
"name": "syncano-server", | ||
"version": "0.8.2-4", | ||
"version": "0.8.2-5", | ||
"description": "A library to intereact with the Syncano API on a server side", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
112
src/data.js
@@ -8,3 +8,6 @@ import querystring from 'querystring' | ||
import {NotFoundError} from './errors' | ||
import {buildInstanceURL} from './utils' | ||
const MAX_BATCH_SIZE = 50 | ||
/** | ||
@@ -113,5 +116,6 @@ * Syncano server | ||
request(`${baseUrl}${response.next}`) | ||
} else { | ||
return true | ||
return false | ||
} | ||
return true | ||
} | ||
@@ -121,3 +125,3 @@ | ||
if (shouldResolve === false) { | ||
return | ||
return false | ||
} | ||
@@ -200,3 +204,3 @@ | ||
if (shouldResolve === false) { | ||
return | ||
return false | ||
} | ||
@@ -225,3 +229,3 @@ | ||
if (shouldResolve === false) { | ||
return | ||
return false | ||
} | ||
@@ -246,6 +250,6 @@ | ||
_mapFields (items, fields) { | ||
return fields.length === 0 | ||
? items | ||
: items.map(item => | ||
_mapFields(items, fields) { | ||
return fields.length === 0 ? | ||
items : | ||
items.map(item => | ||
Object.keys(fields).reduce( | ||
@@ -370,5 +374,5 @@ (all, key) => set(all, fields[key] || key, get(item, key)), | ||
.then(response => { | ||
const shouldThrow = Array.isArray(ids) | ||
? response.length !== ids.length | ||
: response === null | ||
const shouldThrow = Array.isArray(ids) ? | ||
response.length !== ids.length : | ||
response === null | ||
@@ -445,9 +449,9 @@ return shouldThrow ? reject(new NotFoundError()) : resolve(response) | ||
(child, item) => ({ | ||
[item]: child === null | ||
? { | ||
[whereOperator]: whereValue | ||
} | ||
: { | ||
_is: child | ||
} | ||
[item]: child === null ? | ||
{ | ||
[whereOperator]: whereValue | ||
} : | ||
{ | ||
_is: child | ||
} | ||
}), | ||
@@ -540,2 +544,42 @@ null | ||
_chunk(items, size) { | ||
const chunks = [] | ||
while (items.length > 0) { | ||
chunks.push(items.splice(0, size)) | ||
} | ||
return chunks | ||
} | ||
_batch(body, headers) { | ||
const requests = this._chunk(body, MAX_BATCH_SIZE).map(chunk => () => { | ||
const fetchObject = this._batchFetchObject(chunk) | ||
return this.fetch(fetchObject.url, fetchObject, headers) | ||
}) | ||
return new Promise((resolve, reject) => { | ||
const resolves = [] | ||
let i = 0 | ||
;(function next() { | ||
const request = requests[i++] | ||
if (request) { | ||
request() | ||
.then(data => { | ||
resolves.push(data) | ||
next() // eslint-disable-line promise/no-callback-in-promise | ||
}) | ||
.catch(err => { | ||
reject(err) | ||
}) | ||
} else { | ||
resolve(resolves) | ||
} | ||
})() | ||
}) | ||
} | ||
/** | ||
@@ -558,3 +602,3 @@ * Create new object. | ||
let headers = null | ||
let fetchObject = { | ||
const fetchObject = { | ||
url: this.url(), | ||
@@ -569,3 +613,3 @@ method: 'POST', | ||
} else if (Array.isArray(body)) { | ||
fetchObject = this._batchFetchObject(body) | ||
return this._batch(body, headers) | ||
} | ||
@@ -593,6 +637,7 @@ | ||
*/ | ||
update (id, body) { | ||
update(id, body) { | ||
let headers = null | ||
const isQueryUpdate = | ||
typeof id === 'object' && id !== null && !Array.isArray(id) | ||
let fetchObject = { | ||
const fetchObject = { | ||
url: this.url(id), | ||
@@ -607,13 +652,14 @@ method: 'PATCH', | ||
fetchObject = this._batchFetchObject(ids) | ||
return this.fetch(fetchObject.url, fetchObject) | ||
return this._batch(ids) | ||
}) | ||
} | ||
if (Array.isArray(id)) { | ||
fetchObject = this._batchFetchObject(id) | ||
if (body instanceof FormData) { | ||
fetchObject.body = body | ||
headers = body.getHeaders() | ||
} else if (Array.isArray(id)) { | ||
return this._batch(id) | ||
} | ||
return this.fetch(fetchObject.url, fetchObject) | ||
return this.fetch(fetchObject.url, fetchObject, headers) | ||
} | ||
@@ -634,3 +680,3 @@ | ||
const isQueryDelete = id === undefined | ||
let fetchObject = { | ||
const fetchObject = { | ||
url: this.url(id), | ||
@@ -644,5 +690,3 @@ method: 'DELETE' | ||
fetchObject = this._batchFetchObject(ids) | ||
return this.fetch(fetchObject.url, fetchObject) | ||
return this._batch(ids) | ||
}) | ||
@@ -652,3 +696,3 @@ } | ||
if (Array.isArray(id)) { | ||
fetchObject = this._batchFetchObject(id) | ||
return this._batch(id) | ||
} | ||
@@ -655,0 +699,0 @@ |
Sorry, the diff of this file is not supported yet
899359
3820