|
| 1 | +"use strict"; |
| 2 | +var __extends = (this && this.__extends) || (function () { |
| 3 | + var extendStatics = function (d, b) { |
| 4 | + extendStatics = Object.setPrototypeOf || |
| 5 | + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || |
| 6 | + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; |
| 7 | + return extendStatics(d, b); |
| 8 | + } |
| 9 | + return function (d, b) { |
| 10 | + extendStatics(d, b); |
| 11 | + function __() { this.constructor = d; } |
| 12 | + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); |
| 13 | + }; |
| 14 | +})(); |
| 15 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 16 | +var constants_1 = require("./constants"); |
| 17 | +var errors_1 = require("./errors"); |
| 18 | +var RecordFieldModel_1 = require("./models/RecordFieldModel"); |
| 19 | +var RecordModel_1 = require("./models/RecordModel"); |
| 20 | +var RecordSetModel_1 = require("./models/RecordSetModel"); |
| 21 | +var TableModel_1 = require("./models/TableModel"); |
| 22 | +var TableSchemaModel_1 = require("./models/TableSchemaModel"); |
| 23 | +var createRecordModelClass = function (Base) { |
| 24 | + return /** @class */ (function (_super) { |
| 25 | + __extends(ExtendedRecordModel, _super); |
| 26 | + function ExtendedRecordModel(id, table) { |
| 27 | + var _this = _super.call(this, id, table) || this; |
| 28 | + _this._fields = {}; |
| 29 | + return _this; |
| 30 | + } |
| 31 | + return ExtendedRecordModel; |
| 32 | + }(Base)); |
| 33 | +}; |
| 34 | +var DefaultModelFactory = /** @class */ (function () { |
| 35 | + function DefaultModelFactory() { |
| 36 | + this._recordClass = {}; |
| 37 | + this._defineProperty = function (model, name, field, factory, cache) { |
| 38 | + if (cache === void 0) { cache = true; } |
| 39 | + if (constants_1.RESERVED_PROPERTIES.indexOf(name) >= 0) |
| 40 | + throw new Error(errors_1.default.reservedProperty(field.table.name, name)); |
| 41 | + Object.defineProperty(model.prototype, name, { |
| 42 | + get: function () { |
| 43 | + // TODO: Improve the instance cache mechanism. Invalidate when the field value changes.. |
| 44 | + return cache |
| 45 | + ? (this._fields[name] || (this._fields[name] = factory(field, this))) |
| 46 | + : factory(field, this); |
| 47 | + } |
| 48 | + }); |
| 49 | + }; |
| 50 | + } |
| 51 | + DefaultModelFactory.prototype.newTableSchema = function (db, name, schema) { |
| 52 | + return new TableSchemaModel_1.default(db, name, schema); |
| 53 | + }; |
| 54 | + DefaultModelFactory.prototype.newTableModel = function (session, schema, state) { |
| 55 | + return new TableModel_1.default(session, schema, state); |
| 56 | + }; |
| 57 | + DefaultModelFactory.prototype.newRecordModel = function (id, table) { |
| 58 | + var model = this.createRecordModel(table.schema); |
| 59 | + return new model(id, table); |
| 60 | + }; |
| 61 | + DefaultModelFactory.prototype.newRecordSetModel = function (table, schema, owner) { |
| 62 | + return new RecordSetModel_1.default(table, schema, owner); |
| 63 | + }; |
| 64 | + DefaultModelFactory.prototype.getRecordBaseClass = function (schema) { |
| 65 | + return RecordModel_1.default; |
| 66 | + }; |
| 67 | + DefaultModelFactory.prototype.createRecordModel = function (schema) { |
| 68 | + var _this = this; |
| 69 | + if (this._recordClass[schema.name]) |
| 70 | + return this._recordClass[schema.name]; |
| 71 | + else { |
| 72 | + var model_1 = createRecordModelClass(this.getRecordBaseClass(schema)); |
| 73 | + schema.fields.forEach(function (f) { |
| 74 | + return (f.isForeignKey || !f.isPrimaryKey) |
| 75 | + && _this._defineProperty(model_1, f.propName, f, _this._newRecordField.bind(_this), false); |
| 76 | + }); |
| 77 | + schema.relations.forEach(function (f) { |
| 78 | + return f.relationName && _this._defineProperty(model_1, f.relationName, f, f.unique |
| 79 | + ? _this._newRecordRelation.bind(_this) |
| 80 | + : _this._newRecordSet.bind(_this), !f.unique); |
| 81 | + }); |
| 82 | + return this._recordClass[schema.name] = model_1; |
| 83 | + } |
| 84 | + }; |
| 85 | + DefaultModelFactory.prototype._newRecordField = function (schema, record) { |
| 86 | + if (!schema.isForeignKey) |
| 87 | + return new RecordFieldModel_1.default(schema, record); |
| 88 | + if (!schema.references) |
| 89 | + throw new Error(errors_1.default.fkInvalidReference(schema.name)); |
| 90 | + var refTable = schema.references && record.table.session.tables[schema.references]; |
| 91 | + if (!refTable) |
| 92 | + throw new Error(errors_1.default.fkReferenceNotInSession(schema.name, schema.references)); |
| 93 | + var recordId = schema.getRecordValue(record); |
| 94 | + if (recordId === undefined) |
| 95 | + return null; |
| 96 | + return refTable.getOrDefault(recordId); |
| 97 | + }; |
| 98 | + DefaultModelFactory.prototype._newRecordSet = function (schema, record) { |
| 99 | + var refTable = record.table.session.tables[schema.table.name]; |
| 100 | + if (!refTable) |
| 101 | + throw new Error(errors_1.default.tableNotInSession(schema.table.name)); |
| 102 | + return this.newRecordSetModel(refTable, schema, record); |
| 103 | + }; |
| 104 | + DefaultModelFactory.prototype._newRecordRelation = function (schema, record) { |
| 105 | + var refTable = record.table.session.tables[schema.table.name]; |
| 106 | + if (!refTable) |
| 107 | + throw new Error(errors_1.default.tableNotInSession(schema.table.name)); |
| 108 | + var id = refTable.getIndex(schema.name, record.id)[0]; |
| 109 | + if (id === undefined) |
| 110 | + return null; |
| 111 | + return this.newRecordModel(id, refTable); |
| 112 | + }; |
| 113 | + return DefaultModelFactory; |
| 114 | +}()); |
| 115 | +exports.default = DefaultModelFactory; |
0 commit comments