UNPKG

type-dexie

Version:

class oriented schema building tool for dexie.js

116 lines 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileEntity = void 0; var column_1 = require("../decorator/column"); var compound_1 = require("../decorator/compound"); var entity_1 = require("../decorator/entity"); var join_column_1 = require("../decorator/join-column"); var multi_valued_1 = require("../decorator/multi-valued"); var primary_1 = require("../decorator/primary"); var unique_1 = require("../decorator/unique"); var naming_1 = require("./naming"); var primary_key_not_associated_error_1 = require("../error/primary-key-not-associated.error"); var compound_not_associated_error_1 = require("../error/compound-not-associated.error"); var EntityCompiler = /** @class */ (function () { function EntityCompiler(target, parent, propertyKey) { this.target = target; this.parent = parent; this.propertyKey = propertyKey; } EntityCompiler.compile = function (target, parent, propertyKey) { return new EntityCompiler(target, parent, propertyKey).compile(); }; EntityCompiler.prototype.compile = function () { return this.primaryKeyDefinition() .concat(this.columnDefinitions(), this.compoundDefinitions(), this.childrenDefinition(), this.parentDefinition()) .map(function (_a) { var keyPath = _a.keyPath, unique = _a.unique, multiValued = _a.multiValued, autoIncrement = _a.autoIncrement; return ((autoIncrement ? '++' : '') + (unique ? '&' : '') + (multiValued ? '*' : '') + keyPath); }) .join(', '); }; EntityCompiler.prototype.primaryKeyDefinition = function () { var primary = primary_1.Primary.getMetadata(this.target); if (null == primary.propertyKey) { return [ { keyPath: '', autoIncrement: primary.autoIncrement, }, ]; } var column = column_1.Column.findMetadata(this.target, primary.propertyKey); if (null == column) { throw new primary_key_not_associated_error_1.PrimaryKeyNotAssociatedError(); } return [ { keyPath: column.keyPath, autoIncrement: primary.autoIncrement, }, ]; }; EntityCompiler.prototype.columnDefinitions = function () { var _this = this; return column_1.Column.getMetadata(this.target) .filter(function (_a) { var type = _a.type, keyPath = _a.keyPath, propertyKey = _a.propertyKey; return !(primary_1.Primary.has(_this.target, propertyKey) || compound_1.Compound.has(_this.target, keyPath) || entity_1.Entity.isEntity(type) || join_column_1.JoinColumn.has(_this.target, propertyKey)); }) .map(function (_a) { var keyPath = _a.keyPath, propertyKey = _a.propertyKey; return { keyPath: keyPath, unique: unique_1.Unique.has(_this.target, propertyKey), multiValued: multi_valued_1.MultiValued.has(_this.target, propertyKey), }; }); }; EntityCompiler.prototype.compoundDefinitions = function () { var _this = this; return compound_1.Compound.getMetadata(this.target).map(function (compound) { var column = column_1.Column.getMetadata(_this.target); var keyPaths = compound.keyPaths.map(function (keyPath) { if (column.some(function (_) { return keyPath === _.keyPath; })) { return keyPath; } throw new compound_not_associated_error_1.CompoundNotAssociatedError(); }); return { unique: compound.unique, keyPath: '[' + keyPaths.join('+') + ']', }; }); }; EntityCompiler.prototype.childrenDefinition = function () { var _this = this; return column_1.Column.getMetadata(this.target) .filter(function (_a) { var type = _a.type, propertyKey = _a.propertyKey; return (entity_1.Entity.isEntity(type) && join_column_1.JoinColumn.has(_this.target, propertyKey)); }) .map(function (_a) { var keyPath = _a.keyPath, propertyKey = _a.propertyKey; return { keyPath: keyPath, unique: unique_1.Unique.has(_this.target, propertyKey), multiValued: multi_valued_1.MultiValued.has(_this.target, propertyKey), }; }); }; EntityCompiler.prototype.parentDefinition = function () { if (!this.parent || join_column_1.JoinColumn.has(this.parent, this.propertyKey)) { return []; } return { keyPath: naming_1.default.camelCase(this.parent) }; }; return EntityCompiler; }()); exports.compileEntity = EntityCompiler.compile; //# sourceMappingURL=entity-compiler.js.map