@looker/wholly-artifact
Version:
Looker Artifact API as a Table SDK
268 lines (267 loc) • 8.73 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stringer = exports.rowPosition = exports.noDate = exports.nilCell = exports.addMinutes = exports.RowModel = exports.RowAction = exports.APP_JSON = void 0;
var _uuid = require("uuid");
var _sdkRtl = require("@looker/sdk-rtl");
var _omit = _interopRequireDefault(require("lodash/omit"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var noDate = exports.noDate = new Date(-8640000000000000);
var APP_JSON = exports.APP_JSON = 'application/json';
var nilCell = exports.nilCell = '\0';
var addMinutes = (date, minutes) => {
return new Date(date.getTime() + minutes * 60000);
};
exports.addMinutes = addMinutes;
var stringer = value => {
if (value === undefined || value === null) return nilCell;
if (value instanceof Date) {
if (value === noDate) return nilCell;
return value.toISOString();
}
return value.toString();
};
exports.stringer = stringer;
var rowPosition = exports.rowPosition = '_row';
var RowAction = exports.RowAction = function (RowAction) {
RowAction[RowAction["None"] = 0] = "None";
RowAction[RowAction["Create"] = 1] = "Create";
RowAction[RowAction["Update"] = 2] = "Update";
RowAction[RowAction["Delete"] = 3] = "Delete";
return RowAction;
}({});
class RowModel {
constructor(values) {
_defineProperty(this, "_row", 0);
_defineProperty(this, "_id", '');
_defineProperty(this, "_updated", noDate);
_defineProperty(this, "$artifact", {
version: 0
});
_defineProperty(this, "$_action", RowAction.None);
this.initValues(values);
if (!this.key) {
this.key = this.makey();
this.$_action = RowAction.Create;
}
}
isNew() {
return this.$artifact.version < 1;
}
isStored() {
return !this.isNew();
}
initFromArray(values) {
if (Array.isArray(values)) {
var _keys = this.header();
if (Array.isArray(values)) {
values.forEach((val, index) => {
if (val !== undefined && val !== null && index < _keys.length) {
var _key = _keys[index];
this[_key] = this.typeCast(_key, val);
}
});
}
return true;
}
return false;
}
initValues(values) {
if (this.initFromArray(values)) return;
if (typeof values === 'object' && values !== null && values !== undefined) {
var nested = ('value' in values);
var _value = nested ? values.value : values;
if (nested) {
this.$artifact = values;
}
if (typeof _value === 'string') {
try {
_value = JSON.parse(_value);
} catch (e) {
var _values$$artifact, _values$$artifact$con;
if (((_values$$artifact = values.$artifact) === null || _values$$artifact === void 0 ? void 0 : (_values$$artifact$con = _values$$artifact.content_type) === null || _values$$artifact$con === void 0 ? void 0 : _values$$artifact$con.localeCompare(APP_JSON, 'en', {
sensitivity: 'base'
})) === 0) {
throw new _sdkRtl.LookerSDKError("Expected ".concat(APP_JSON, " but got ").concat(e));
} else {
throw new _sdkRtl.LookerSDKError(e);
}
}
}
Object.keys(_value).forEach(k => {
var v = this.typeCast(k, _value[k]);
this[k] = typeof v === 'string' ? decodeURI(v) : v;
});
if (values.key) this.key = values.key;
return true;
}
return false;
}
tableName() {
return this.constructor.name;
}
makey() {
return "".concat(this.tableName(), ":").concat((0, _uuid.v4)());
}
get key() {
return this._id;
}
set key(value) {
this._id = value;
this.$artifact.key = value;
}
oops(message) {
throw new _sdkRtl.LookerSDKError("".concat(this.key, ": ").concat(message));
}
get $action() {
if (this.$_action === RowAction.None && this.isNew()) return RowAction.Create;
return this.$_action;
}
set $action(value) {
switch (value) {
case RowAction.Create:
{
if (this.isStored()) this.oops("can't create an existing item");
break;
}
case RowAction.Delete:
{
if (this.isNew()) {
this.oops("can't delete a new item");
}
break;
}
case RowAction.Update:
{
if (this.isNew()) {
this.oops("can't update a new item");
}
break;
}
case RowAction.None:
{
if (this.isNew()) {
this.oops("Action must be assigned for a new item");
}
break;
}
}
this.$_action = value;
}
setCreate() {
this.$action = RowAction.Create;
return true;
}
setUpdate() {
this.$action = RowAction.Update;
return true;
}
setDelete() {
this.$action = RowAction.Delete;
return true;
}
keys() {
return Object.keys(this);
}
header() {
var keys = this.keys();
var result = keys.filter(v => !(v.startsWith('$') || RowModel.hide.has(v)));
return result;
}
displayHeader() {
return this.header().filter(v => !(v.startsWith('_') || RowModel.hide.has(v)));
}
prepare() {
if (!this.key) {
this.key = this.makey();
this.$action = RowAction.Create;
}
this._updated = new Date();
return this;
}
values() {
var result = [];
var keys = this.header();
keys.forEach(key => {
result.push(stringer(this[key]));
});
return result;
}
typeCast(key, value) {
if (value === undefined || value === null) value = '';
var type = typeof this[key];
var fromType = typeof value;
if (type === fromType) {
return value;
}
if (type === 'string') {
return value.toString();
}
if (type === 'number') {
if (value === '') return 0;
var isInt = /^([+-]?[1-9]\d*|0)$/;
if (value.toString().match(isInt)) {
return parseInt(value, 10);
}
return parseFloat(value);
}
if (type === 'boolean') {
return (0, _sdkRtl.boolDefault)(value, false);
}
if (this[key] instanceof Date) {
if (value) return new Date(value);
return noDate;
}
if (Array.isArray(this[key])) {
if (!value) return [];
return value.toString().split(',');
}
return value;
}
assign(values) {
if (values) {
this.initValues(values);
}
return this;
}
validate() {
return undefined;
}
toObject() {
return (0, _omit.default)(_objectSpread({}, this), ['$_action']);
}
fromObject(obj) {
return this.assign(obj);
}
fromArtifact(obj) {
return this.assign(obj);
}
storageValues() {
var result = {};
var keys = this.header();
for (var _key2 of keys) {
var v = this[_key2];
var val = typeof v === 'string' ? encodeURI(v) : v;
result[_key2] = val;
}
return result;
}
toArtifact() {
var _this$$artifact$versi;
return {
key: this.key,
value: JSON.stringify(this.storageValues()),
version: (_this$$artifact$versi = this.$artifact.version) !== null && _this$$artifact$versi !== void 0 ? _this$$artifact$versi : 0,
content_type: APP_JSON
};
}
}
exports.RowModel = RowModel;
_defineProperty(RowModel, "hide", new Set(['_row']));
//# sourceMappingURL=RowModel.js.map