globalstorage
Version:
Global Storage is a Global Distributed Data Warehouse
293 lines (247 loc) • 12.3 kB
JavaScript
;
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
var jstp = require('@metarhia/jstp');
var _require = require('./provider'),
StorageProvider = _require.StorageProvider;
var _require2 = require('./remote.cursor'),
RemoteCursor = _require2.RemoteCursor;
var RemoteProvider =
/*#__PURE__*/
function (_StorageProvider) {
_inherits(RemoteProvider, _StorageProvider);
function RemoteProvider() {
var _this;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, RemoteProvider);
_this = _possibleConstructorReturn(this, _getPrototypeOf(RemoteProvider).call(this, options));
_this.connection = null;
return _this;
} // Open RemoteProvider
// options <Object> options for jstp connection
// transport <string> jstp transport name
// connectionArgs <Array> arguments to be passed to corresponding
// transport's connect method
// callback <Function>
// error <Error> | <null>
// provider <StorageProvider>
_createClass(RemoteProvider, [{
key: "open",
value: function open(options, callback) {
var _this2 = this;
_get(_getPrototypeOf(RemoteProvider.prototype), "open", this).call(this, options, function (error) {
var _jstp$options$transpo;
if (error) {
callback(error, _this2);
return;
}
(_jstp$options$transpo = jstp[options.transport]).connect.apply(_jstp$options$transpo, _toConsumableArray(options.connectionArgs).concat([function (error, connection) {
if (error) {
callback(error);
return;
}
_this2.connection = connection;
_this2.active = true;
callback(null, _this2);
}]));
});
} // Close RemoteProvider
// callback <Function>
// err <Error> | <null>
}, {
key: "close",
value: function close(callback) {
var _this3 = this;
if (!this.connection) {
process.nextTick(callback);
return;
}
this.connection.once('close', function () {
_this3.active = false;
callback();
});
this.connection.close();
this.connection = null;
} // Get record from GlobalStorage
// id <string> globally unique record id
// callback <Function>
// error <Error> | <null>
// record <Object>
}, {
key: "get",
value: function get(id, callback) {
this.connection.callMethod('provider', 'get', [id], callback);
} // Get details for many-to-many link from GlobalStorage
// category - <string>, category to get details in
// id - <string>, object id
// fieldName - <string>, field with the Many decorator
// callback - <Function>
// err - <Error> | <null>
// details - <Object[]>
}, {
key: "getDetails",
value: function getDetails(category, id, fieldName, callback) {
this.connection.callMethod('provider', 'getDetails', [category, id, fieldName], callback);
} // Set record in GlobalStorage
// record <Object> record to be stored
// callback <Function>
// error <Error> | <null>
}, {
key: "set",
value: function set(record, callback) {
if (!record.Id) {
throw new TypeError('Id is not provided');
}
this.connection.callMethod('provider', 'set', [record], callback);
} // Create record in GlobalStorage
// category <string> category of record
// record <Object> record to be stored
// callback <Function>
// error <Error> | <null>
// id <string>
}, {
key: "create",
value: function create(category, record, callback) {
this.connection.callMethod('provider', 'create', [category, record], callback);
} // Update record in GlobalStorage
// category <string> category of record
// query <Object> record, example: { Id }
// patch <Object> record, fields to update
// callback <Function>
// error <Error> | <null>
// count <number>
}, {
key: "update",
value: function update(category, query, patch, callback) {
this.connection.callMethod('provider', 'update', [category, query, patch], callback);
} // Delete record in GlobalStorage
// category <string> category of record
// query <Object> record, example: { Id }
// callback <Function>
// error <Error> | <null>
// count <number>
}, {
key: "delete",
value: function _delete(category, query, callback) {
this.connection.callMethod('provider', 'delete', [category, query], callback);
} // Unlink records with Many relation between them
// category - <string>, category with field having the Many decorator
// field - <string>, field with the Many decorator
// fromId - <Uint64>, Id of the record in category specified in the first
// argument
// toIds - <Uint64> | <Uint64[]>, Id(s) of the record(s) in category
// specified in the Many decorator of the specified field
// callback - <Function>
// err - <Error> | <null>
}, {
key: "unlinkDetails",
value: function unlinkDetails(category, field, fromId, toIds, callback) {
this.connection.callMethod('provider', 'unlinkDetails', [category, field, fromId, toIds], callback);
} // Link records with Many relation between them
// category - <string>, category with field having the Many decorator
// field - <string>, field with the Many decorator
// fromId - <Uint64>, Id of the record in category specified in the first
// argument
// toIds - <Uint64> | <Uint64[]>, Id(s) of the record(s) in category
// specified in the Many decorator of the specified field
// callback - <Function>
// err - <Error> | <null>
}, {
key: "linkDetails",
value: function linkDetails(category, field, fromId, toIds, callback) {
this.connection.callMethod('provider', 'linkDetails', [category, field, fromId, toIds], callback);
} // Select record from GlobalStorage
// category <string> category of record
// query <Object> fields conditions
// Returns: <gs.Cursor> cursor
}, {
key: "select",
value: function select(category, query) {
return new RemoteCursor(this.connection, {
category: category
}).select(query);
} // Execute an action
// category <string> | <null> category name or null to execute public action
// action <string> action name
// args <Object>
// callback <Function>
// error <Error> | <null>
// result <any>
}, {
key: "execute",
value: function execute(category, action, args, callback) {
this.connection.callMethod('provider', 'execute', [category, action, args], callback);
}
}, {
key: "getSchemaSources",
value: function getSchemaSources(callback) {
this.connection.callMethod('provider', 'getSchemaSources', [], callback);
}
}, {
key: "listCategories",
value: function listCategories(callback) {
this.connection.callMethod('provider', 'listCategories', [], callback);
} // List categories permission flags
// callback <Function>
// error <Error> | <null>
// result <Object>
// [categoryName] <string> permission flags
}, {
key: "listCategoriesPermissions",
value: function listCategoriesPermissions(callback) {
this.connection.callMethod('provider', 'listCategoriesPermissions', [], callback);
}
}, {
key: "listActions",
value: function listActions(callback) {
this.connection.callMethod('provider', 'listActions', [], callback);
}
}, {
key: "listApplications",
value: function listApplications(callback) {
this.connection.callMethod('provider', 'listApplications', [], callback);
}
}, {
key: "getCategoryL10n",
value: function getCategoryL10n(langTag, category, callback) {
this.connection.callMethod('l10n', 'getCategory', [langTag, category], callback);
}
}, {
key: "getDomainsL10n",
value: function getDomainsL10n(langTag, callback) {
this.connection.callMethod('l10n', 'getDomains', [langTag], callback);
}
}, {
key: "getCommonL10n",
value: function getCommonL10n(langTag, callback) {
this.connection.callMethod('l10n', 'getCommon', [langTag], callback);
}
}, {
key: "getFormL10n",
value: function getFormL10n(langTag, category, form, callback) {
this.connection.callMethod('l10n', 'getForm', [langTag, category, form], callback);
}
}, {
key: "getActionL10n",
value: function getActionL10n(langTag, category, action, callback) {
this.connection.callMethod('l10n', 'getAction', [langTag, category, action], callback);
}
}]);
return RemoteProvider;
}(StorageProvider);
module.exports = {
RemoteProvider: RemoteProvider
};