@arianaprojects/prisma-nestjs-crud-generator
Version:
code generator from Prisma schema
143 lines • 5.42 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDescription = exports.isMoney = exports.isZipcode = exports.isCountry = exports.isStreet = exports.isCity = exports.isName = exports.isEmail = exports.isUrl = exports.defaultGenerator = exports.tsTypes = exports.prettierFormatFormat = exports.prettierFormat = exports.writeTSFile = exports.toArray = exports.parseBoolean = exports.log = void 0;
const sdk_1 = require("@prisma/sdk");
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const generator_1 = require("./generator");
const error_handler_1 = require("./error-handler");
const prettier_1 = require("prettier");
const log = (src) => {
sdk_1.logger.info(`[${generator_1.GENERATOR_NAME}]:${src}`);
};
exports.log = log;
const parseBoolean = (value) => {
if (['true', 'false'].includes(value.toString()) === false) {
throw new error_handler_1.GeneratorFormatNotValidError('parseBoolean failed');
}
return value.toString() === 'true';
};
exports.parseBoolean = parseBoolean;
const toArray = (value) => {
return Array.isArray(value) ? value : [value];
};
exports.toArray = toArray;
const writeTSFile = (fullPath, content, dryRun = true) => {
(0, exports.log)(`${dryRun ? '[dryRun] ' : ''}Generate ${fullPath}`);
if (dryRun) {
console.log(content);
return;
}
const dirname = path.dirname(fullPath);
if (fs.existsSync(dirname) === false) {
fs.mkdirSync(dirname, { recursive: true });
}
fs.writeFileSync(fullPath, (0, exports.prettierFormatFormat)(content));
};
exports.writeTSFile = writeTSFile;
const prettierFormat = (content) => {
return (0, prettier_1.format)(content, {
parser: 'typescript',
singleQuote: true,
trailingComma: 'all',
semi: true,
printWidth: 250,
arrowParens: 'always',
bracketSpacing: true,
});
};
exports.prettierFormat = prettierFormat;
const prettierFormatFormat = (content, options = {}) => {
return (0, prettier_1.format)(content, Object.assign(Object.assign({}, options), { parser: 'typescript' }));
};
exports.prettierFormatFormat = prettierFormatFormat;
function tsTypes(type, entity = false) {
if (type == 'Int' || type == 'BigInt')
return 'number';
else if (type == 'String' || type == 'Text')
return 'string';
else if (type == 'DateTime')
return 'Date';
else if (type == 'Boolean')
return 'boolean';
else if (type == 'Decimal' || type == 'Float') {
if (entity)
return 'Decimal | number';
else
return 'number';
}
else if (type == 'Json')
return 'Prisma.JsonValue';
else
return type;
}
exports.tsTypes = tsTypes;
function defaultGenerator(name, type) {
return '';
}
exports.defaultGenerator = defaultGenerator;
function isUrl(f) {
const sl = f.name.toLocaleLowerCase();
return (tsTypes(f.type) == 'string' && sl.includes('url')) || sl.includes('link');
}
exports.isUrl = isUrl;
function isEmail(f) {
const sl = f.name.toLocaleLowerCase();
return tsTypes(f.type) == 'string' && sl.includes('email');
}
exports.isEmail = isEmail;
function isName(f) {
const sl = f.name.toLocaleLowerCase();
return tsTypes(f.type) == 'string' && sl.includes('name');
}
exports.isName = isName;
function isCity(f) {
const sl = f.name.toLocaleLowerCase();
return tsTypes(f.type) == 'string' && sl.includes('city');
}
exports.isCity = isCity;
function isStreet(f) {
const sl = f.name.toLocaleLowerCase();
return (tsTypes(f.type) == 'string' && sl.includes('street')) || sl.includes('line');
}
exports.isStreet = isStreet;
function isCountry(f) {
const sl = f.name.toLocaleLowerCase();
return tsTypes(f.type) == 'string' && sl.includes('country');
}
exports.isCountry = isCountry;
function isZipcode(f) {
const sl = f.name.toLocaleLowerCase();
return (tsTypes(f.type) == 'string' || tsTypes(f.type) == 'number') && (sl.includes('zipcode') || sl.includes('plz'));
}
exports.isZipcode = isZipcode;
function isMoney(f) {
const sl = f.name.toLocaleLowerCase();
return tsTypes(f.type) == 'number' || (tsTypes(f.type) == 'Prisma.Decimal' && sl.includes('cost')) || sl.includes('amount');
}
exports.isMoney = isMoney;
function isDescription(f) {
const sl = f.name.toLocaleLowerCase();
return (tsTypes(f.type) == 'string' && sl.includes('description')) || sl.includes('info');
}
exports.isDescription = isDescription;
//# sourceMappingURL=util.js.map