@awhere/api
Version:
The awesome aWhere API for JavaScript.
258 lines • 9.68 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var interfaces_1 = require("@awhere/interfaces");
var DbAdapter_1 = require("./DbAdapter");
var DbWatcher_1 = require("../DbWatcher");
var FieldDefinition_1 = __importDefault(require("../FieldDefinition"));
var utils_1 = require("../utils");
var StructuredCollection = /** @class */ (function (_super) {
__extends(StructuredCollection, _super);
function StructuredCollection() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.nameIndexes = {};
return _this;
}
Object.defineProperty(StructuredCollection.prototype, "type", {
get: function () {
return interfaces_1.ItemType.databaseItem;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StructuredCollection.prototype, "subtype", {
get: function () {
return this._subtype;
},
enumerable: false,
configurable: true
});
Object.defineProperty(StructuredCollection.prototype, "props", {
get: function () {
return this._props;
},
set: function (value) {
var _this = this;
this.nameIndexes = {};
if (value === null || value === void 0 ? void 0 : value.schemaDefinition) {
Object.keys(value === null || value === void 0 ? void 0 : value.schemaDefinition).forEach(function (key) {
var _a;
if ((_a = value === null || value === void 0 ? void 0 : value.schemaDefinition[key]) === null || _a === void 0 ? void 0 : _a.name) {
_this.nameIndexes[value.schemaDefinition[key].name] = key;
}
});
}
this._props = value;
},
enumerable: false,
configurable: true
});
StructuredCollection.prototype.getFieldByName = function (fieldName) {
var id = this.nameIndexes[fieldName];
if (!id) {
return;
}
var schemaTypeOpts = this.props.schemaDefinition[id];
return new FieldDefinition_1.default({
_id: id,
type: schemaTypeOpts.type,
name: schemaTypeOpts.name,
props: schemaTypeOpts,
});
};
StructuredCollection.prototype.getField = function (id) {
var schemaTypeOpts = this.props.schemaDefinition[id];
return new FieldDefinition_1.default({
_id: id,
type: schemaTypeOpts.type,
name: schemaTypeOpts.name,
props: schemaTypeOpts,
});
};
StructuredCollection.prototype.moveField = function (source, target) {
var _this = this;
var schemaDefinition = this.props.schemaDefinition;
var keys = Object.keys(schemaDefinition);
var sourceIndex = keys.findIndex(function (key) { return key === source._id; });
if (sourceIndex === -1) {
throw new Error("Invalid 'source' parameter.");
}
var sourceFieldId = keys.splice(sourceIndex, 1)[0];
var targetIndex = keys.findIndex(function (key) { return key === target._id; });
if (targetIndex === -1) {
throw new Error("Invalid 'target' parameter.");
}
keys.splice(targetIndex, 0, sourceFieldId);
this.props.schemaDefinition = {};
keys.forEach(function (key) {
if (schemaDefinition.hasOwnProperty(key)) {
_this.props.schemaDefinition[key] = schemaDefinition[key];
}
});
return this.props.schemaDefinition;
};
StructuredCollection.prototype.addField = function (field) {
var schemaDefinition = {};
if (field instanceof FieldDefinition_1.default) {
schemaDefinition[field._id] = __assign(__assign({}, field.props), { name: field.name, type: field.type, alias: field.name });
}
else {
var fieldDef = new FieldDefinition_1.default(field);
schemaDefinition[fieldDef._id] = __assign(__assign({}, fieldDef.props), { name: fieldDef.name, type: fieldDef.type, alias: fieldDef.name });
}
this.props.schemaDefinition = __assign(__assign({}, this.props.schemaDefinition), schemaDefinition);
return this.props.schemaDefinition;
};
StructuredCollection.prototype.updateField = function (field) {
var schemaDefinition = {};
if (field instanceof FieldDefinition_1.default) {
this.props.schemaDefinition[field._id] = __assign(__assign({}, field.props), { name: field.name, type: field.type, alias: field.name });
}
else {
var fieldDef = new FieldDefinition_1.default(field);
this.props.schemaDefinition[fieldDef._id] = __assign(__assign({}, fieldDef.props), { name: fieldDef.name, type: fieldDef.type, alias: fieldDef.name });
}
return this.props.schemaDefinition;
};
StructuredCollection.prototype.deleteField = function (field) {
if (field instanceof FieldDefinition_1.default) {
delete this.props.schemaDefinition[field._id];
}
else if (typeof field === 'string') {
delete this.props.schemaDefinition[field];
}
return this.props.schemaDefinition;
};
StructuredCollection.prototype.watchDb = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.socket.open();
_this.socket.emit('call', "sub:/item/database", _this._id, function (err) {
if (err) {
return reject(err);
}
resolve(new DbWatcher_1.DbWatcher(_this.socket, _this._id));
});
});
};
StructuredCollection.systemSchema = {
_id: {
name: 'id',
type: 'String',
default: 'uuid',
deletable: false,
readonly: true,
editable: false,
},
seq: {
name: 'seq',
type: 'Number',
hidden: true,
deletable: false,
readonly: true,
editable: false,
},
createdAt: {
name: 'createdAt',
type: 'Date',
hidden: true,
default: 'Date.now',
deletable: false,
readonly: true,
editable: false,
},
updatedAt: {
name: 'updatedAt',
type: 'Date',
hidden: true,
default: 'Date.now',
deletable: false,
readonly: true,
editable: false,
},
lastModified: {
name: 'lastModified',
type: 'Date',
hidden: true,
default: 'Date.now',
deletable: false,
readonly: true,
editable: false,
},
createdBy: {
name: 'createdBy',
type: 'String',
hidden: true,
deletable: false,
readonly: true,
editable: false,
},
updatedBy: {
name: 'updatedBy',
type: 'String',
hidden: true,
deletable: false,
readonly: true,
editable: false,
},
deleted: {
name: 'deleted',
type: 'Boolean',
hidden: true,
default: 'false',
deletable: false,
readonly: true,
editable: false,
},
deletedAt: {
name: 'deletedAt',
type: 'Date',
hidden: true,
deletable: false,
readonly: true,
editable: false,
},
deletedBy: {
name: 'deletedBy',
type: 'String',
hidden: true,
deletable: false,
readonly: true,
editable: false,
},
};
StructuredCollection.generateDocumentId = utils_1.generateDocumentId;
StructuredCollection.generateHistoryDocumentId = utils_1.generateHistoryDocumentId;
StructuredCollection.generateAttachmentId = utils_1.generateAttachmentId;
return StructuredCollection;
}(DbAdapter_1.ReadWriteAdapter));
exports.default = StructuredCollection;
//# sourceMappingURL=StructuredCollection.js.map