typeorm-translatable
Version:
Translation classes, utils and decorators for i18n in TypeORM
3 lines (2 loc) • 9.11 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("typeorm");function n(){return(n=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function r(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,a(t,e)}function o(t){return(o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function a(t,e){return(a=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t})(t,e)}function i(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function s(t,e,n){return(s=i()?Reflect.construct.bind():function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&a(o,n.prototype),o}).apply(null,arguments)}function u(t){var e="function"==typeof Map?new Map:void 0;return(u=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return s(t,arguments,o(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),a(n,t)})(t)}function l(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function c(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}var f=function(t){function e(n){var r;return r=t.call(this,n)||this,Object.setPrototypeOf(l(r),e.prototype),r}return r(e,t),e}(u(Error)),p=function(t){function e(n){var r;return r=t.call(this,n)||this,Object.setPrototypeOf(l(r),e.prototype),r}return r(e,t),e}(u(Error)),y=function(t){function e(n){var r;return r=t.call(this,n)||this,Object.setPrototypeOf(l(r),e.prototype),r}return r(e,t),e}(u(Error)),d=function(t){function e(n){var r;return r=t.call(this,n)||this,Object.setPrototypeOf(l(r),e.prototype),r}return r(e,t),e}(u(Error)),g=function(t){return t.constructor.translatableFields},h=function(t){return"object"==typeof t&&null!==t&&"locale"in t},v=function(t){if(void 0===t.translations||0===t.translations.length)return!1;for(var e,n=function(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return c(t,void 0);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?c(t,void 0):void 0}}(t))){n&&(t=n);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(t.translations);!(e=n()).done;)if(!h(e.value))return!1;return!0},b=function(t,e,n){var r;void 0===e&&(e=M.getInstance().getLocale());var o,a=void 0===n?{}:n,i=a.shouldDelete,s=void 0===i?M.getInstance().shouldDelete:i,u=a.shouldMutate,l=void 0===u?M.getInstance().shouldMutate:u;if(!v(t))throw new f("Entity is not translatable.");var c=null==(r=(o=l?t:Object.create(Object.getPrototypeOf(t),Object.getOwnPropertyDescriptors(t))).translations)?void 0:r.find((function(t){return t.locale===e}));return s&&(l&&delete t.translations,delete o.translations),void 0===c||Object.keys(o).forEach((function(t){var e=g(o);if(void 0===e)throw new f("Entity has to translatable fields.");e.has(t)&&(o[t]=c[t])})),o},_=function(){function t(){}return t.prototype.translate=function(t,e){return b(this,t,e)},t}(),O=function(){};t.__decorate([e.PrimaryGeneratedColumn({type:"bigint",unsigned:!0}),t.__metadata("design:type",String)],O.prototype,"id",void 0),t.__decorate([e.Column("varchar",{length:6}),t.__metadata("design:type",String)],O.prototype,"locale",void 0);var M=function(){function o(){this.getLocale=function(){},this._shouldDelete=!0,this._shouldMutate=!1,this._entitySuffix="Translation",this._translationMetadataArgsStorage={translatableTables:[],translatableColumns:[],translationsOneToManyRelations:[]},this._translationEntitiesMap=new Map,this.getEntityManager=function(){},this.hasGenerated=!1}var a,i,s=o.prototype;return s.getTranslationMetadataArgsStorage=function(){return this._translationMetadataArgsStorage},s.getTranslationEntities=function(){return Array.from(this._translationEntitiesMap.values())},o.use=function(t){var e=t.getLocale,n=t.shouldDelete,r=t.shouldMutate,o=t.entitySuffix,a=t.getEntityManager;if(void 0!==e&&(this._instance.getLocale=e),void 0!==n&&(this._instance._shouldDelete=n),void 0!==r&&(this._instance._shouldMutate=r),void 0!==o){if(this._instance.hasGenerated)throw new p("Cannot change entity suffix after translation entities are generated.");this._instance._entitySuffix=o}void 0!==a&&(this._instance.getEntityManager=a)},o.getInstance=function(){return this._instance},o.generate=function(){var o=this;if(this._instance.hasGenerated)throw new p("Translation entities are already generated.");var a=this._instance._translationMetadataArgsStorage,i=a.translatableColumns,s=a.translationsOneToManyRelations;return a.translatableTables.forEach((function(a){var u=a.target,l=a.options,c=i.filter((function(t){return t.target===u})),f="string"==typeof u?_:u,p=function(t){function e(){return t.apply(this,arguments)||this}return r(e,t),e}(O);t.__decorate([e.ManyToOne((function(){return f}),(function(t){return t.translations})),t.__metadata("design:type",Object)],p.prototype,"source",void 0),Object.defineProperty(p,"name",{value:""+("string"==typeof u?u:u.name)+o._instance._entitySuffix,writable:!1}),e.Entity(l)(p),c.forEach((function(t){var r=t.target,o=t.propertyName,a=t.options,i=e.getMetadataArgsStorage().columns.find((function(t){return t.target===r&&t.propertyName===o}));r.translatableFields.add(o),e.Column(n({},a,{type:(null==a?void 0:a.type)||(null==i?void 0:i.options.type)}))(new p,o)})),s.filter((function(t){return t.target.constructor===u})).forEach((function(t){var n=t.target,r=t.propertyName;e.OneToMany((function(){return p}),(function(t){return t.source}),t.options)(n,r)})),o._instance._translationEntitiesMap.set(u,p)})),this._instance.hasGenerated=!0,this._instance.getTranslationEntities()},o.getTranslationRepository=function(t,e){return(e=this.getResolvedManager(e)).getRepository(this.getTranslationEntity(t))},o.getTranslationEntity=function(t,e){e=this.getResolvedManager(e);var n=this._instance._translationEntitiesMap.get(e.connection.getMetadata(t).target);if(!n)throw new d("Could not find translation entity for entity"+t);return n},o.getResolvedManager=function(t){if(!(t=t||this._instance.getEntityManager()))throw new y("Could not resolve entity manager. Provide in the function call or set default in config.");return t},a=o,(i=[{key:"shouldDelete",get:function(){return this._shouldDelete}},{key:"shouldMutate",get:function(){return this._shouldMutate}},{key:"entitySuffix",get:function(){return this._entitySuffix}}])&&function(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}(a.prototype,i),Object.defineProperty(a,"prototype",{writable:!1}),o}();M._instance=new M,exports.AlreadyGeneratedException=p,exports.EntityManagerNotFoundException=y,exports.NotTranslatableException=f,exports.Translatable=function(t){return function(e){M.getInstance().getTranslationMetadataArgsStorage().translatableTables.push({target:e,options:t})}},exports.TranslatableColumn=function(t){return function(e,n){M.getInstance().getTranslationMetadataArgsStorage().translatableColumns.push({target:e.constructor,propertyName:n.toString(),options:t})}},exports.TranslatableEntity=_,exports.TranslatableRepository=function(t,e){return t.createQueryBuilder=function(t,n,r){var o=null!=e?e:M.getInstance().getLocale();if(void 0===o)throw new f("No locale specified");return n?this.connection.createQueryBuilder(t,n,r||this.queryRunner).leftJoinAndSelect(n+".translations","translation","translation.locale = :locale",{locale:o}):this.connection.createQueryBuilder(t||r||this.queryRunner)},{}},exports.TranslationConfig=M,exports.TranslationEntity=O,exports.TranslationEntityNotFoundException=d,exports.Translations=function(t){return function(e,n){M.getInstance().getTranslationMetadataArgsStorage().translationsOneToManyRelations.push({target:e,propertyName:n.toString(),options:t})}},exports.getTranslatableFields=g,exports.isTranslatable=v,exports.isTranslation=h,exports.translateEntity=b;
//# sourceMappingURL=typeorm-translatable.cjs.production.min.js.map