UNPKG

@xmobitea/gn-typescript-client

Version:

GearN Typescript Client SDK by XmobiTea (Pro)

209 lines (208 loc) 7.92 kB
import "reflect-metadata"; import { FieldDataType } from "./GNMetadata"; // Thanks special for athongintel @clairejs/core //@internal export const GN_METADATA = "__GN_METADATA__"; const getGNReflectedDataType = (prototype, propertyKey) => { let t = Reflect.getMetadata("design:type", prototype, propertyKey); if (!t) { throw Error(`Cannot get gn reflected data type for ${prototype.constructor.name}:${propertyKey}`); } switch (t.name) { case "Boolean": return FieldDataType.Boolean; case "Number": return FieldDataType.Number; case "String": return FieldDataType.String; case "Array": return FieldDataType.Array; default: return FieldDataType.Object; } }; const getGNReflectedMetadata = (prototype, propertyKey) => { let t = Reflect.getMetadata("design:type", prototype, propertyKey); if (!t) { throw Error(`Cannot get gn reflected data type for ${prototype.constructor.name}:${propertyKey}`); } let answer = { fieldDataType: null, prototype: t.prototype }; if (t.name == "Boolean") answer.fieldDataType = FieldDataType.Boolean; else if (t.name == "Number") answer.fieldDataType = FieldDataType.Number; else if (t.name == "String") answer.fieldDataType = FieldDataType.String; else if (t.name == "Array") answer.fieldDataType = FieldDataType.Array; else answer.fieldDataType = FieldDataType.Object; return answer; }; export const initGNObjectMetadata = (prototype) => { if (prototype === Object.prototype) { return {}; } if (!prototype.hasOwnProperty(GN_METADATA)) { const superPrototype = initGNObjectMetadata(Object.getPrototypeOf(prototype)); prototype[GN_METADATA] = Object.assign(Object.assign({}, superPrototype), { id: getTableName(prototype.constructor), fields: superPrototype.fields ? superPrototype.fields.map((f) => (Object.assign({}, f))) : [] }); } return prototype[GN_METADATA]; }; export const initGNObjectFieldMetadata = (prototype, propertyKey) => { let meta = initGNObjectMetadata(prototype); let fieldIndex = meta.fields.findIndex((f) => f.name === propertyKey); if (fieldIndex < 0) { let gnReflectedMetadata = getGNReflectedMetadata(prototype, propertyKey); const field = { name: propertyKey, // fieldType: getGNReflectedDataType(prototype, propertyKey), fieldType: gnReflectedMetadata.fieldDataType, cls: gnReflectedMetadata.prototype.constructor }; meta.fields.push(field); return field; } else { //-- shadow clone the field const field = Object.assign({}, meta.fields[fieldIndex]); meta.fields[fieldIndex] = field; return field; } }; export const getGNObjectMetadata = (obj) => { return obj.prototype[GN_METADATA]; }; export const setGNObjectMetadata = (obj, metadata) => { obj.prototype[GN_METADATA] = metadata; }; export const getTableName = (model) => model === null || model === void 0 ? void 0 : model.name.toLowerCase(); ; export var GNFieldDataType; (function (GNFieldDataType) { GNFieldDataType[GNFieldDataType["Other"] = 0] = "Other"; GNFieldDataType[GNFieldDataType["Number"] = 1] = "Number"; GNFieldDataType[GNFieldDataType["String"] = 2] = "String"; GNFieldDataType[GNFieldDataType["Boolean"] = 3] = "Boolean"; GNFieldDataType[GNFieldDataType["GNHashtable"] = 4] = "GNHashtable"; GNFieldDataType[GNFieldDataType["GNArray"] = 5] = "GNArray"; })(GNFieldDataType || (GNFieldDataType = {})); ; ; ; ; ; ; ; ; ; ; export const DataMember = (args) => (prototype, propertyKey) => { let field = initGNObjectFieldMetadata(prototype, propertyKey); field.code = args.code; field.isOptional = args.isOptional == null ? false : args.isOptional; field.defaultValue = args.defaultValue; field.gnFieldType = args.gnFieldType == null ? GNFieldDataType.Other : args.gnFieldType; field.activeConditionValid = false; }; export const StringDataMember = (args) => (prototype, propertyKey) => { let field = initGNObjectFieldMetadata(prototype, propertyKey); field.code = args.code; field.isOptional = args.isOptional == null ? false : args.isOptional; field.defaultValue = args.defaultValue; field.gnFieldType = GNFieldDataType.String; if (args.mustNonNull != null || args.minLength != null || args.maxLength != null) { field.activeConditionValid = true; if (args.mustNonNull == null) { args.mustNonNull = false; } if (args.minLength == null) { args.minLength = 0; } if (args.maxLength == null) { args.maxLength = 256; } } field.mustNonNull = args.mustNonNull; field.minLength = args.minLength; field.maxLength = args.maxLength; }; export const BooleanDataMember = (args) => (prototype, propertyKey) => { let field = initGNObjectFieldMetadata(prototype, propertyKey); field.code = args.code; field.isOptional = args.isOptional == null ? false : args.isOptional; field.defaultValue = args.defaultValue; field.gnFieldType = GNFieldDataType.Boolean; field.activeConditionValid = false; }; export const GNHashtableDataMember = (args) => (prototype, propertyKey) => { let field = initGNObjectFieldMetadata(prototype, propertyKey); field.code = args.code; field.isOptional = args.isOptional == null ? false : args.isOptional; field.defaultValue = args.defaultValue; field.gnFieldType = GNFieldDataType.GNHashtable; if (args.mustNonNull != null || args.minLength != null || args.maxLength != null) { field.activeConditionValid = true; if (args.mustNonNull == null) { args.mustNonNull = false; } if (args.minLength == null) { args.minLength = 0; } if (args.maxLength == null) { args.maxLength = 256; } } field.mustNonNull = args.mustNonNull; field.minLength = args.minLength; field.maxLength = args.maxLength; }; export const GNArrayDataMember = (args) => (prototype, propertyKey) => { let field = initGNObjectFieldMetadata(prototype, propertyKey); if (args.elementCls != null) field.cls = args.elementCls; field.code = args.code; field.isOptional = args.isOptional == null ? false : args.isOptional; field.defaultValue = args.defaultValue; field.gnFieldType = GNFieldDataType.GNArray; if (args.mustNonNull != null || args.minLength != null || args.maxLength != null) { field.activeConditionValid = true; if (args.mustNonNull == null) { args.mustNonNull = false; } if (args.minLength == null) { args.minLength = 0; } if (args.maxLength == null) { args.maxLength = 256; } } field.mustNonNull = args.mustNonNull; field.minLength = args.minLength; field.maxLength = args.maxLength; }; export const NumberDataMember = (args) => (prototype, propertyKey) => { let field = initGNObjectFieldMetadata(prototype, propertyKey); field.code = args.code; field.isOptional = args.isOptional == null ? false : args.isOptional; field.defaultValue = args.defaultValue; field.gnFieldType = GNFieldDataType.Number; if (args.mustInt != null || args.minValue != null || args.maxValue != null) { field.activeConditionValid = true; if (args.mustInt == null) { args.mustInt = false; } if (args.minValue == null) { args.minValue = -Infinity; } if (args.maxValue == null) { args.maxValue = Infinity; } } field.minValue = args.minValue; field.maxValue = args.maxValue; field.mustInt = args.mustInt; };