fakeit-facet
Version:
Command-line utility that generates fake data which can be output as JSON, YAML, CSON, or CSV formats based on models defined in YAML.
324 lines (252 loc) • 12.7 kB
JavaScript
"use strict";
var _typeof = require("@babel/runtime-corejs3/helpers/typeof");
var _Reflect$construct = require("@babel/runtime-corejs3/core-js-stable/reflect/construct");
var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
_Object$defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs3/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/getPrototypeOf"));
var _lodash = require("lodash");
var couchbase = _interopRequireWildcard(require("couchbase"));
var _defaultOptions = _interopRequireDefault(require("./default-options"));
var _base = _interopRequireDefault(require("../base"));
function _getRequireWildcardCache(nodeInterop) { if (typeof _WeakMap !== "function") return null; var cacheBabelInterop = new _WeakMap(); var cacheNodeInterop = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = _Object$defineProperty && _Object$getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? _Object$getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { _Object$defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = _Reflect$construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !_Reflect$construct) return false; if (_Reflect$construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(_Reflect$construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
/// @name Couchbase
/// @page api
/// @description This is used to output data to the Couchbase
var Couchbase = /*#__PURE__*/function (_Base) {
(0, _inherits2["default"])(Couchbase, _Base);
var _super = _createSuper(Couchbase);
/// # @name constructor
/// # @arg {object} options - The base options
/// # @arg {object} output_options - The output options
function Couchbase() {
var _this;
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var output_options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
(0, _classCallCheck2["default"])(this, Couchbase);
_this = _super.call(this, options);
_this.couchbaseOptions = {};
_this.output_options = (0, _lodash.extend)({}, _defaultOptions["default"], output_options);
var _this$output_options = _this.output_options,
username = _this$output_options.username,
password = _this$output_options.password;
if (username && password) {
_this.couchbaseOptions = {
username: username,
password: password
};
}
_this.collection = null;
_this.prepared = false;
return _this;
} /// # @name connect
/// # @description
/// # Establishes a connection to Couchbase
/// # @returns {promise} - The setup function that was called
/// # @async
(0, _createClass2["default"])(Couchbase, [{
key: "connect",
value: function () {
var _connect = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return couchbase.connect(this.output_options.server, this.couchbaseOptions);
case 2:
this.cluster = _context.sent;
case 3:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
function connect() {
return _connect.apply(this, arguments);
}
return connect;
}() /// # @name prepare
/// # @description
/// # This is used to prepare the saving functionality that is determined by the
/// # options that were passed to the constructor.
/// # It sets a variable of `this.preparing` that ultimately calls `this.setup` that returns a promise.
/// # This way when you go to save data it, that function will know if the setup is complete or not and
/// # wait for it to be done before it starts saving data.
/// # @returns {promise} - The setup function that was called
/// # @async
}, {
key: "prepare",
value: function () {
var _prepare = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
return _regenerator["default"].wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
this.preparing = true;
_context2.next = 3;
return this.connect();
case 3:
_context2.next = 5;
return this.setup();
case 5:
this.preparing = _context2.sent;
return _context2.abrupt("return", this.preparing);
case 7:
case "end":
return _context2.stop();
}
}
}, _callee2, this);
}));
function prepare() {
return _prepare.apply(this, arguments);
}
return prepare;
}() /// # @name setup
/// # @description
/// # This is used to setup the saving function that will be used.
}, {
key: "setup",
value: function () {
var _setup = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
var _this2 = this;
var _this$output_options2, server, bucket;
return _regenerator["default"].wrap(function _callee3$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!(this.preparing == null)) {
_context4.next = 2;
break;
}
return _context4.abrupt("return", this.prepare());
case 2:
_this$output_options2 = this.output_options, server = _this$output_options2.server, bucket = _this$output_options2.bucket;
return _context4.abrupt("return", new _promise["default"](function (resolve, reject) {
var _context3;
try {
_this2.bucket = _this2.cluster.bucket(bucket);
} catch (err) {
console.log(err);
reject(err);
}
_this2.log('verbose', (0, _concat["default"])(_context3 = "Connection to '".concat(bucket, "' bucket at '")).call(_context3, server, "' was successful"));
_this2.prepared = true;
_this2.bucket.connected = true;
resolve();
}));
case 4:
case "end":
return _context4.stop();
}
}
}, _callee3, this);
}));
function setup() {
return _setup.apply(this, arguments);
}
return setup;
}() /// # @name output
/// # @description
/// # This is used to output the data that's passed to it
/// # @arg {string} id - The id to use for this data
/// # @arg {object, array, string} data - The data that you want to be saved
/// # @arg {object} options - Options from the original YAML file definition to be used by the output logic
/// # @async
}, {
key: "output",
value: function () {
var _output = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(id, data) {
var options,
collection,
_args4 = arguments;
return _regenerator["default"].wrap(function _callee4$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
options = _args4.length > 2 && _args4[2] !== undefined ? _args4[2] : {};
if (!(this.prepared !== true)) {
_context5.next = 5;
break;
}
if (this.preparing == null) {
this.prepare();
}
_context5.next = 5;
return this.preparing;
case 5:
collection = this.bucket.defaultCollection(); // Check if the user configured a collection and/or a scope. If so,
// use them, otherwise use the default scope and default collection.
if (options.scope && options.collection) {
collection = this.bucket.scope(options.scope).collection(options.collection);
} else if (options.collection) {
collection = this.bucket.collection(options.collection);
} // upserts a document into couchbase
return _context5.abrupt("return", collection.upsert(id, data, {
timeout: 5000
}));
case 8:
case "end":
return _context5.stop();
}
}
}, _callee4, this);
}));
function output(_x, _x2) {
return _output.apply(this, arguments);
}
return output;
}() /// # @name finalize
/// # @description
/// # This disconnect from couchbase if it is connected
/// # @async
}, {
key: "finalize",
value: function () {
var _finalize = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
return _regenerator["default"].wrap(function _callee5$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
if (!(this.bucket && this.bucket.connected)) {
_context6.next = 4;
break;
}
_context6.next = 3;
return this.cluster.close();
case 3:
this.bucket.connected = false;
case 4:
case "end":
return _context6.stop();
}
}
}, _callee5, this);
}));
function finalize() {
return _finalize.apply(this, arguments);
}
return finalize;
}()
}]);
return Couchbase;
}(_base["default"]);
exports["default"] = Couchbase;