convert schema objects to model with nodejs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
4.5 KiB

2 weeks ago
const fs = require('fs');
const modelName = "user"
fs.writeFileSync("./result.js","");
// خواندن فایل متنی
fs.readFile('schema.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
// پردازش و تبدیل متن به آبجکت
const parsedObject = parseTextToObject(data);
console.log(parsedObject);
});
const sleep = (ms)=>{
return new Promise(resolve=>{
setTimeout(resolve,ms);
})
}
// تابع برای پردازش و تبدیل متن
function parseTextToObject(data) {
const cleanedData = data
.replace(/\/\/.*$/gm, '') // حذف کامنتها
.replace(/(this\.DataTypes\.[A-Z]+)((\([0-9]{1,4}\))?)/g, '"$1$2"'); // سادهسازی نام DataTypes
try {
// return fs.(`(${cleanedData})`);
fs.writeFileSync("./schema.js",`module.exports = { schemaObject: ${cleanedData} }`,{encoding:"utf-8"})
console.log("done")
getObject();
} catch (error) {
console.error('Error parsing text to object:', error);
return null;
}
}
async function getObject(){
console.log('in getobjects and wait 3 seconds')
await sleep(3000);
const {schemaObject} = require('./schema');
console.log("schemaObject",schemaObject);
for(let keys in schemaObject){
let dynamicContent= "";
let dynamicContent2=""
let takeProperties = {}
let theType="string";
for(let property in schemaObject[keys]){
switch(property){
case "type":
if(schemaObject[keys][property]?.includes("string".toUpperCase())){
takeProperties[property] = schemaObject[keys][property].replace(/\([0-9]{1,4}\)/g,"").replace("this.DataTypes","DataType").replace(/\"/g,"");
theType="string";
console.log(schemaObject[keys][property].replace(/\([0-9]{1-4}\)/g,"").replace(/\([0-9]{1-4}\)/g,"").replace("this.DataTypes","DataType"))
}else if(schemaObject[keys][property]?.includes("date".toUpperCase())){
takeProperties[property] = schemaObject[keys][property].replace("this.DataTypes","DataType");
theType="Date";
}else if(schemaObject[keys][property]?.includes("int".toUpperCase())){
takeProperties[property] = schemaObject[keys][property].replace("this.DataTypes","DataType");
theType="number";
}else if(schemaObject[keys][property]?.includes("char".toUpperCase())){
takeProperties[property] = schemaObject[keys][property].replace("this.DataTypes","DataType");
theType="string";
}else if(schemaObject[keys][property]?.includes("text".toUpperCase())){
takeProperties[property] = schemaObject[keys][property].replace("this.DataTypes","DataType");
theType="string";
}else if(schemaObject[keys][property]?.includes("bool".toUpperCase())){
takeProperties[property] = schemaObject[keys][property].replace("this.DataTypes","DataType");
theType="boolean";
}else{
console.log('oh what was it !!!!');
console.log(property,schemaObject[keys][property]);
console.log("--------------------------------------------")
takeProperties[property] = schemaObject[keys][property].replace("this.DataTypes","DataType");
theType="string";
}
break;
case "regex":
const regexContent = schemaObject[keys][property];
takeProperties[property] = schemaObject[keys][property];
takeProperties['validate'] = `{is:/${regexContent}}/}`
break;
default:
takeProperties[property] = schemaObject[keys][property];
break;
}
}
for(let i in takeProperties){
dynamicContent2+=`\n\t${i}:${JSON.stringify(takeProperties[i])},`
}
dynamicContent2=dynamicContent2.replace(/\"(DataType\.[a-zA-Z]{1,10}(\([0-9]{1,5}\))?)\"/g,"$1").replace(/\"(\{is\:\/.{1,300}\})\"/gi,"$1")
dynamicContent+=`@Column({${dynamicContent2}
})
${keys}:${theType};\n\n`
fs.appendFileSync("./result.js",dynamicContent);
console.log('append')
}
console.log("finish")
}