@looker/wholly-artifact
Version:
Looker Artifact API as a Table SDK
320 lines (319 loc) • 11.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.compareDates = exports.WhollyArtifact = exports.TypedRows = void 0;
var _sdkRtl = require("@looker/sdk-rtl");
var _sdk = require("@looker/sdk");
var _RowModel = require("./RowModel");
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
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 compareDates = (a, b) => a.getTime() - b.getTime();
exports.compareDates = compareDates;
class TypedRows {
constructor(rows, Maker) {
_defineProperty(this, "rows", []);
if (Maker) {
this.rows = rows.map(v => new Maker(v));
} else {
this.rows = rows;
}
}
add(value) {
this.rows.push(value);
}
where(predicate) {
return TypedRows.from(this.rows.filter(predicate));
}
select(selector) {
return TypedRows.from(this.rows.map(selector));
}
toArray() {
return this.rows;
}
static from(values) {
return new TypedRows(values);
}
static create(values) {
return new TypedRows(values !== null && values !== void 0 ? values : []);
}
}
exports.TypedRows = TypedRows;
class WhollyArtifact extends TypedRows {
constructor(sdk, table) {
var keyColumn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '_id';
super([]);
this.sdk = sdk;
this.table = table;
this.keyColumn = keyColumn;
_defineProperty(this, "index", {});
_defineProperty(this, "namespace", '');
_defineProperty(this, "tableName", '');
_defineProperty(this, "_displayHeader", []);
this.loadRows(table.rows);
var meta = this.typeRow();
this.namespace = meta.namespace();
this.tableName = meta.tableName();
}
loadRows(rows) {
this.rows = this.typeRows(rows);
this.checkHeader();
this.createIndex();
return this.rows;
}
refresh(values) {
var _this = this;
return _asyncToGenerator(function* () {
if (!values) {
values = yield _this.sdk.ok((0, _sdk.search_artifacts)(_this.sdk, {
namespace: _this.namespace,
key: "".concat(_this.tableName, ":%")
}));
}
_this.rows = _this.typeRows(values);
_this.createIndex();
return _this.rows;
})();
}
typeRows(rows) {
var result = [];
var pos = 1;
rows.forEach(r => {
var row = this.typeRow(r);
pos++;
if (!row[_RowModel.rowPosition]) row[_RowModel.rowPosition] = pos;
result.push(row);
});
return result;
}
static toAT(value) {
if (value) return value;
return undefined;
}
checkHeader() {
if (this.rows.length === 0) return true;
var row = this.rows[0];
var rowHeader = row.header().join();
var tableHeader = this.table.header.join();
if (tableHeader !== rowHeader) throw new _sdkRtl.LookerSDKError("Expected ".concat(this.tableName, " header to be ").concat(rowHeader, " not ").concat(tableHeader));
return true;
}
get header() {
return this.table.header;
}
get nextRow() {
return this.rows.length + 2;
}
createIndex() {
this.index = {};
this.rows.forEach(r => {
this.index[r[this.keyColumn]] = r;
});
}
values(model) {
var result = [];
this.header.forEach(h => {
result.push((0, _RowModel.stringer)(model[h]));
});
return result;
}
allValues() {
var values = [this.header];
values.push(...this.rows.map(r => this.values(r)));
return values;
}
save(model) {
var _arguments = arguments,
_this2 = this;
return _asyncToGenerator(function* () {
var force = _arguments.length > 1 && _arguments[1] !== undefined ? _arguments[1] : false;
switch (model.$action) {
case _RowModel.RowAction.Delete:
{
yield _this2.sdk.ok((0, _sdk.delete_artifact)(_this2.sdk, _this2.namespace, model[_this2.keyColumn]));
return model;
}
case _RowModel.RowAction.Update:
{
return yield _this2.update(model, force);
}
case _RowModel.RowAction.None:
{
return model;
}
}
return yield _this2.create(model);
})();
}
checkId(model) {
if (!model[this.keyColumn]) throw new _sdkRtl.LookerSDKError("\"".concat(this.keyColumn, "\" must be assigned for ").concat(this.tableName, " row ").concat(model._row));
}
create(model) {
var _this3 = this;
return _asyncToGenerator(function* () {
if (model._row > 0) throw new _sdkRtl.LookerSDKError("create needs ".concat(_this3.tableName, " \"").concat(model[_this3.keyColumn], "\" row to be < 1, not ").concat(model._row));
model.prepare();
_this3.checkId(model);
yield _this3.createUpdateBatch([model]);
return _this3.index[model[_this3.keyColumn]];
})();
}
update(model) {
var _arguments2 = arguments,
_this4 = this;
return _asyncToGenerator(function* () {
var _force = _arguments2.length > 1 && _arguments2[1] !== undefined ? _arguments2[1] : false;
if (!model._row) throw new _sdkRtl.LookerSDKError("".concat(_this4.tableName, " \"").concat(model[_this4.keyColumn], "\" row must be > 0 to update"));
model.prepare();
_this4.checkId(model);
yield _this4.createUpdateBatch([model]);
return _this4.index[model[_this4.keyColumn]];
})();
}
find(value, columnName) {
if (!value) return undefined;
var key = columnName || this.keyColumn;
if (typeof value === 'number') {
if (!columnName) key = '_row';
}
if (columnName === this.keyColumn) {
return WhollyArtifact.toAT(this.index[value.toString()]);
}
return WhollyArtifact.toAT(this.rows.find(r => r[key] === value));
}
get displayHeader() {
if (this._displayHeader.length === 0) {
this._displayHeader = this.header.filter(colName => this.displayable(colName));
}
return this._displayHeader;
}
delete(model) {
var _arguments3 = arguments,
_this5 = this;
return _asyncToGenerator(function* () {
var force = _arguments3.length > 1 && _arguments3[1] !== undefined ? _arguments3[1] : false;
if (!force) yield _this5.checkOutdated(model);
yield _this5.deleteBatch([model]);
return true;
})();
}
displayable(columnName) {
return !(columnName.startsWith('_') || columnName.startsWith('$'));
}
checkOutdated(model, source) {
var _this6 = this;
return _asyncToGenerator(function* () {
if (model[_RowModel.rowPosition] < 1) return false;
var errors = [];
if (!source) {
var art = yield _this6.sdk.ok((0, _sdk.artifact)(_this6.sdk, {
namespace: _this6.namespace,
key: model[_this6.keyColumn]
}));
if (art.length === 1) source = _this6.typeRow(art[0]);
}
if (!source) {
errors.push("Row not found");
} else {
if (source.$artifact.version !== model.$artifact.version) {
errors.push("version ".concat(source.$artifact.version, " != ").concat(model.$artifact.version));
}
}
if (errors.length > 0) throw new _sdkRtl.LookerSDKError("".concat(_this6.tableName, " row ").concat(model[_this6.keyColumn], " is outdated: ").concat(errors.join()));
return false;
})();
}
fromObject(obj) {
this.loadRows(obj);
return this.rows;
}
toObject() {
return this.rows.map(r => r.toObject());
}
batchUpdate() {
var _arguments4 = arguments,
_this7 = this;
return _asyncToGenerator(function* () {
var _force = _arguments4.length > 0 && _arguments4[0] !== undefined ? _arguments4[0] : false;
var updates = _this7.rows.filter(r => r.$action === _RowModel.RowAction.Update || r.$action === _RowModel.RowAction.Create);
var deletes = _this7.rows.filter(r => r.$action === _RowModel.RowAction.Delete);
yield _this7.createUpdateBatch(updates);
yield _this7.deleteBatch(deletes);
return _this7.rows;
})();
}
getDelta() {
var updates = this.rows.filter(r => r.$action === _RowModel.RowAction.Update);
var deletes = this.rows.filter(r => r.$action === _RowModel.RowAction.Delete).sort((a, b) => b._row - a._row);
var creates = this.rows.filter(r => r.$action === _RowModel.RowAction.Create);
return {
updates: updates,
deletes: deletes,
creates: creates
};
}
mergePurge(values, delta) {
delta.updates.forEach(u => values[u._row - 1] = this.values(u));
delta.deletes.forEach(d => values.splice(d._row - 1, 1));
delta.creates.forEach(c => values.push(this.values(c)));
return values;
}
prepareBatch(values, delta, force) {
if (!force) {
var errors = [];
try {
delta.updates.forEach(u => this.checkOutdated(u, this.typeRow(values[u._row - 1])));
delta.deletes.forEach(d => this.checkOutdated(d, this.typeRow(values[d._row - 1])));
} catch (e) {
errors.push(e.message);
}
if (errors.length > 0) throw new _sdkRtl.LookerSDKError(errors.join('\n'));
}
delta.updates.forEach(u => u.prepare());
delta.creates.forEach(c => c.prepare());
return true;
}
createUpdateBatch(items) {
var _this8 = this;
return _asyncToGenerator(function* () {
if (items.length < 1) return Promise.resolve(_this8.rows);
var namespace = items[0].namespace();
var arts = items.map(i => i.toArtifact());
var result = yield _this8.sdk.ok((0, _sdk.update_artifacts)(_this8.sdk, namespace, arts));
result.forEach(r => {
var row = _this8.typeRow(r);
var key = row[_this8.keyColumn];
_this8.index[key] = row;
});
_this8.updateRowsFromIndex();
return Promise.resolve(_this8.rows);
})();
}
updateRowsFromIndex() {
var result = [];
Object.entries(this.index).forEach((_ref, index) => {
var [_key, row] = _ref;
row[_RowModel.rowPosition] = index + 1;
result.push(row);
});
this.rows = result;
return this.rows;
}
deleteBatch(items) {
var _this9 = this;
return _asyncToGenerator(function* () {
if (items.length < 1) return Promise.resolve(_this9.rows);
var namespace = items[0].namespace();
var keys = items.map(r => r[_this9.keyColumn]);
yield _this9.sdk.ok((0, _sdk.delete_artifact)(_this9.sdk, namespace, keys.join()));
keys.forEach(k => delete _this9.index[k]);
_this9.updateRowsFromIndex();
return Promise.resolve(_this9.rows);
})();
}
}
exports.WhollyArtifact = WhollyArtifact;
//# sourceMappingURL=WhollyArtifact.js.map