@tsdi/typeorm-adapter
Version:
@tsdi/typeorm-adapter is typeorm adapter orm for boot application, mvc frameworks on server.
122 lines (120 loc) • 4.25 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { ModelParser, DefaultModelParserToken } from '@tsdi/boot';
import { Singleton, Autorun, isFunction, isString, tokenId, Inject } from '@tsdi/ioc';
import { getMetadataArgsStorage } from 'typeorm';
export const ObjectIDToken = tokenId('ObjectID');
const numbExp = /(int|float|double|dec|numeric|number)/;
const intExp = /int/;
const strExp = /(char|var|string|text)/;
const boolExp = /(bool|bit)/;
const timeExp = /(time|date)/;
const arrayExp = /array/;
const bytesExp = /(bytes|bytea)/;
let TypeOrmModelParser = class TypeOrmModelParser extends ModelParser {
isObjectId(type) {
return this._ObjectID && this._ObjectID === type;
}
setup() {
let ObjectID = this._ObjectID;
if (ObjectID) {
this.getTypeMap()
.register(ObjectID, (id) => new ObjectID(id));
}
}
getPropertyMeta(type) {
let metas = {};
getMetadataArgsStorage().columns.filter(col => col.target === type)
.forEach(col => {
metas[col.propertyName] = metas[col.propertyName] || [];
metas[col.propertyName].push({
propertyKey: col.propertyName,
dbtype: isString(col.options.type) ? col.options.type : '',
type: this.getModeType(col)
});
});
getMetadataArgsStorage().relations.filter(col => col.target === type)
.forEach(col => {
metas[col.propertyName] = metas[col.propertyName] || [];
let relaModel = isFunction(col.type) ? col.type() : undefined;
metas[col.propertyName].push({
propertyKey: col.propertyName,
provider: relaModel,
type: (col.relationType === 'one-to-many' || col.relationType === 'many-to-many') ? Array : relaModel
});
});
return metas;
}
isExtendBaseType(type, propmeta) {
if (propmeta.dbtype) {
if (intExp.test(propmeta.dbtype)) {
return true;
}
}
if (this.isObjectId(type)) {
return true;
}
return super.isExtendBaseType(type, propmeta);
}
resolveExtendType(type, value, propmeta) {
if (propmeta.dbtype) {
if (intExp.test(propmeta.dbtype)) {
return parseInt(value);
}
}
if (this.isObjectId(type)) {
return new this._ObjectID(value);
}
return super.resolveExtendType(type, propmeta);
}
getModeType(col) {
let type = col.options.type;
if (type) {
if (isString(type)) {
if (type === 'uuid') {
return String;
}
else if (numbExp.test(type)) {
return Number;
}
else if (boolExp.test(type)) {
return Boolean;
}
else if (strExp.test(type)) {
return String;
}
else if (timeExp.test(type)) {
return Date;
}
else if (arrayExp.test(type)) {
return Array;
}
else if (bytesExp.test(type)) {
return Buffer;
}
else {
return Object;
}
}
return type;
}
switch (col.mode) {
case 'objectId':
type = this._ObjectID;
break;
}
return type;
}
static ρAnn() {
return { "name": "TypeOrmModelParser", "params": { "isObjectId": ["type"], "getPropertyMeta": ["type"], "isExtendBaseType": ["type", "propmeta"], "resolveExtendType": ["type", "value", "propmeta"], "getModeType": ["col"] } };
}
};
__decorate([
Inject(ObjectIDToken),
__metadata("design:type", Object)
], TypeOrmModelParser.prototype, "_ObjectID", void 0);
TypeOrmModelParser = __decorate([
Singleton(DefaultModelParserToken),
Autorun('setup')
], TypeOrmModelParser);
export { TypeOrmModelParser };
//# sourceMappingURL=sourcemaps/TypeOrmModelParser.js.map