vue-service-model
Version:
Vue.js library for handling REST service requests with caching, aggregation and model definitions
170 lines (169 loc) • 5.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseModel = void 0;
var tslib_1 = require("tslib");
var vue_1 = tslib_1.__importDefault(require("vue"));
var common_1 = require("../utils/common");
var BaseClass_1 = require("../utils/BaseClass");
var ModelExceptions_1 = require("../exceptions/ModelExceptions");
/**
* BaseModel class
*/
var BaseModel = /** @class */ (function (_super) {
tslib_1.__extends(BaseModel, _super);
/**
* Constructor
* @param data Model data
*/
function BaseModel(data) {
if (data === void 0) { data = {}; }
var _this = _super.call(this) || this;
/**
* Bound field objects of model
*/
_this._fields = {};
_this._data = data;
vue_1.default.observable(_this._data);
_this.cls.register();
_this._bindFields();
return _this;
}
Object.defineProperty(BaseModel, "_modelRegistered", {
/**
* Getter to simulate static class property with fixed inheritance
*/
get: function () {
// Check whether model has property __modelRegistered and not inherited from super class
return Object.prototype.hasOwnProperty.call(this, '__modelRegistered') ? this.__modelRegistered : false;
},
/**
* Setter to simulate static class property with fixed inheritance
*/
set: function (v) {
this.__modelRegistered = v;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseModel.prototype, "data", {
/**
* Data containing values
*/
get: function () {
return this._data;
},
set: function (value) {
this._data = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseModel.prototype, "fields", {
/**
* Bound dictionary of fields by field name
*/
get: function () {
return this._fields;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseModel.prototype, "val", {
/**
* Getter with values to return data of model
* Can be accessed as object (e.g. for field name 'description': val.description)
*/
get: function () {
return new Proxy(this, {
get: function (target, name) {
var field = target.getField(name);
return Promise.resolve(field.value);
},
set: function (target, name, value) {
var field = target.getField(name);
field.value = value;
return true;
}
});
},
enumerable: false,
configurable: true
});
Object.defineProperty(BaseModel.prototype, "pk", {
/**
* Return primary key of model instance or null if not set
*/
get: function () {
var primaryKeyField = this.getPrimaryKeyField();
if (!primaryKeyField)
return null;
var pk = primaryKeyField.valueGetter(this.data);
return !common_1.isNull(pk) ? pk : null;
},
enumerable: false,
configurable: true
});
/**
* Return unbound static field by name.
* Throws NotDeclaredFieldException if field name is not in fields
*/
BaseModel.getField = function (fieldName) {
if (Object.keys(this.fieldsDef).indexOf(fieldName) === -1) {
throw new ModelExceptions_1.NotDeclaredFieldException(this, fieldName);
}
return this.fieldsDef[fieldName];
};
/**
* Return field by name.
* Throws NotDeclaredFieldException if field name is not in fields
*/
BaseModel.prototype.getField = function (fieldName) {
if (Object.keys(this.fields).indexOf(fieldName) === -1) {
throw new ModelExceptions_1.NotDeclaredFieldException(this, fieldName);
}
return this.fields[fieldName];
};
/**
* Return primary key field or null of no primary key field exists
*/
BaseModel.prototype.getPrimaryKeyField = function () {
var pkFields = Object.values(this.fields).filter(function (field) { return field.isPrimaryKey; });
if (pkFields.length > 1) {
console.error('[vue-service-model] Multiple primary key fields found in model', this.constructor.name);
}
return pkFields.length ? pkFields[0] : null;
};
/**
* Bind fields from fieldsDef to _fields
*/
BaseModel.prototype._bindFields = function () {
this._fields = {};
var fields = this.cls.fieldsDef;
for (var _i = 0, _a = Object.keys(fields); _i < _a.length; _i++) {
var fieldName = _a[_i];
var field = fields[fieldName];
this._fields[fieldName] = field.bind({ name: fieldName, model: this });
}
};
/**
* Register model to perform unique actions
*/
BaseModel.register = function () {
if (this._modelRegistered)
return false;
this._modelRegistered = true;
return true;
};
/**
* Field definitions for current model
* e.g:
* {id: new UUIDField(), name: new CharField()}
*/
BaseModel.fieldsDef = {};
/**
* Flag whether model has be registered or not
*/
BaseModel.__modelRegistered = false;
return BaseModel;
}(BaseClass_1.BaseClass));
exports.BaseModel = BaseModel;