prisma-markdown
Version:
Prisma Markdown documents generator including ERD diagrams and comment descriptions
93 lines • 3.09 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldUtil = void 0;
const PrismaUtil_1 = require("../utils/PrismaUtil");
/**
* FieldUtil
*
* @param {DMMF.Field} field
* @param {boolean} [isFK]
* @return {*} {IField}
*/
const FieldUtil = (field, isFK) => {
return new Field(field, isFK);
};
exports.FieldUtil = FieldUtil;
/**
* DMMF Field Paser
*
* @class Field
*/
class Field {
constructor(field, isFK) {
this.field = field;
this.isFK = isFK;
}
/**
* t: type
* s: size
* d: native type
* n: name
* k: constant
* r: nullable
*
* @param {string} [formatString]
* @memberof Field
*/
format(formatString) {
if (!formatString)
return this.field.type;
const data = this.data();
return formatString.replace(/[tsdnkr]/g, (match) => {
var _a, _b;
switch (match) {
case "t": // type
return this.field.type;
case "s": // size
return `${(_a = data.size) !== null && _a !== void 0 ? _a : ""}`;
case "d": // databaseType
return data.nativeType || this.field.type;
case "n": // name
return data.name;
case "k": // PK, FK, UK
return `${(_b = data.constraint) !== null && _b !== void 0 ? _b : ""}`;
case "r":
return data.nullable ? `"nullable"` : "";
default:
return "";
}
});
}
data() {
var _a, _b, _c, _d, _e, _f, _g, _h;
const spec = {
type: "",
name: "",
format: null,
nativeType: null,
size: null,
constraint: null,
nullable: false,
};
spec.type = this.field.type;
if (PrismaUtil_1.PrismaUtil.tagValues("format")(this.field).length > 0) {
spec["format"] = PrismaUtil_1.PrismaUtil.tagValues("format")(this.field)[0];
}
spec["nativeType"] = (_b = (_a = this.field.nativeType) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null;
spec["size"] = ((_d = (_c = this.field.nativeType) === null || _c === void 0 ? void 0 : _c[1]) === null || _d === void 0 ? void 0 : _d[0])
? parseInt((_g = (_f = (_e = this.field.nativeType) === null || _e === void 0 ? void 0 : _e[1]) === null || _f === void 0 ? void 0 : _f[0]) !== null && _g !== void 0 ? _g : "0")
: null;
const keys = [];
if (this.field.isId)
keys.push("PK");
if (this.isFK)
keys.push("FK");
if (this.field.isUnique)
keys.push("UK");
spec["constraint"] = keys.join(",");
spec["nullable"] = !this.field.isRequired;
spec["name"] = (_h = this.field.dbName) !== null && _h !== void 0 ? _h : this.field.name;
return spec;
}
}
//# sourceMappingURL=field-util.js.map