Created
May 16, 2023 23:21
-
-
Save ahmedrowaihi/a63e48df2a222a75008d095a19064c28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mysqlInsertToEloquent(model, raw, excludeKey = [], excludeValue = []) { | |
const tableName = raw.match(/(?<=INSERT INTO `)(.*?)(?=`)/)[0]; | |
const columns = raw.match(/(?<=\()(.+?)(?=\))/)[0].split(",").map(c => c.trim().replace(/`/g, '')); | |
const values = raw.match(/(?<=VALUES \()(.*?)(?=\);?$)/)[0].split("),("); | |
let record = ''; | |
values.forEach(val => { | |
const modelData = {}; | |
const vals = val.split(","); | |
columns.forEach((col, i) => { | |
let value = vals[i].trim(); | |
if (value.toLowerCase() === 'null') value = null; | |
else if (/^(?:'|"|`)(.*)(?:'|"|`)$/.test(value)) { | |
value = value.slice(1, -1); | |
} else if (!isNaN(value)) { | |
value = parseInt(value); | |
} else if (!isNaN(Date.parse(value))) { | |
value = new Date(value).toISOString().slice(0, 19).replace('T', ' '); | |
} | |
modelData[col] = value; | |
}); | |
record += `${model}::create([`; | |
for (const [key, value] of Object.entries(modelData)) { | |
if (value === null || excludeKey.includes(key) || excludeValue.includes(value)) continue; | |
if (typeof value === 'string') record += `'${key}' => '${value}', `; | |
else record += `'${key}' => ${value}, `; | |
} | |
record = record.slice(0, -2); | |
record += "]);\n"; | |
}); | |
return record; | |
} | |
// example usage: | |
const RAW_INSERT = "INSERT INTO `ages` (`id`, `from`, `to`, `is_active`, `created_by`, `created_at`, `updated_by`, `updated_at`, `deleted_at`) VALUES (1,4,5,1,NULL,NULL,1,'2022-07-16 12:30:08',NULL),(3,3,4,1,1,'2021-11-11 23:54:18',1,'2022-07-16 12:29:58',NULL),(6,5,6,1,1,'2022-07-16 12:30:16',1,'2022-07-16 12:30:16',NULL),(7,6,7,1,1,'2022-07-16 12:30:55',1,'2022-07-16 12:30:55',NULL),(8,8,9,1,1,'2022-07-16 12:31:01',1,'2022-07-18 09:55:49',NULL),(9,10,11,1,1,'2022-07-16 12:31:10',1,'2022-07-18 09:56:23',NULL),(10,3,5,1,1,'2022-07-16 13:43:03',1,'2022-07-16 13:43:03',NULL),(11,3,11,1,1,'2022-07-18 11:12:30',1,'2022-07-18 11:12:30',NULL),(12,3,7,1,1,'2022-07-18 11:21:05',1,'2022-07-18 11:21:05',NULL),(13,9,11,1,1,'2022-08-23 18:43:12',1,'2022-08-23 18:43:12',NULL),(14,3,8,1,1,'2022-08-23 18:54:05',1,'2022-08-23 18:54:05',NULL),(15,8,11,1,1,'2022-08-23 19:18:46',1,'2022-08-23 19:18:46',NULL),(16,7,8,1,1,'2022-08-24 13:15:20',1,'2022-08-24 13:15:20',NULL);"; | |
const result = mysqlInsertToEloquent('Age', RAW_INSERT); | |
// you can exlude keys, i.e mysqlInsertToEloquent('Age', RAW_INSERT, ['id']); // this will output without id field | |
console.log(result); | |
// Age::create(['id' => 1, 'from' => 4, 'to' => 5, 'is_active' => 1, 'updated_by' => 1, 'updated_at' => '2022-07-16 12:30:08']); | |
// Age::create(['id' => 3, 'from' => 3, 'to' => 4, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2021-11-11 23:54:18', 'updated_by' => 1, 'updated_at' => '2022-07-16 12:29:58']); | |
// Age::create(['id' => 6, 'from' => 5, 'to' => 6, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-16 12:30:16', 'updated_by' => 1, 'updated_at' => '2022-07-16 12:30:16']); | |
// Age::create(['id' => 7, 'from' => 6, 'to' => 7, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-16 12:30:55', 'updated_by' => 1, 'updated_at' => '2022-07-16 12:30:55']); | |
// Age::create(['id' => 8, 'from' => 8, 'to' => 9, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-16 12:31:01', 'updated_by' => 1, 'updated_at' => '2022-07-18 09:55:49']); | |
// Age::create(['id' => 9, 'from' => 10, 'to' => 11, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-16 12:31:10', 'updated_by' => 1, 'updated_at' => '2022-07-18 09:56:23']); | |
// Age::create(['id' => 10, 'from' => 3, 'to' => 5, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-16 13:43:03', 'updated_by' => 1, 'updated_at' => '2022-07-16 13:43:03']); | |
// Age::create(['id' => 11, 'from' => 3, 'to' => 11, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-18 11:12:30', 'updated_by' => 1, 'updated_at' => '2022-07-18 11:12:30']); | |
// Age::create(['id' => 12, 'from' => 3, 'to' => 7, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-07-18 11:21:05', 'updated_by' => 1, 'updated_at' => '2022-07-18 11:21:05']); | |
// Age::create(['id' => 13, 'from' => 9, 'to' => 11, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-08-23 18:43:12', 'updated_by' => 1, 'updated_at' => '2022-08-23 18:43:12']); | |
// Age::create(['id' => 14, 'from' => 3, 'to' => 8, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-08-23 18:54:05', 'updated_by' => 1, 'updated_at' => '2022-08-23 18:54:05']); | |
// Age::create(['id' => 15, 'from' => 8, 'to' => 11, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-08-23 19:18:46', 'updated_by' => 1, 'updated_at' => '2022-08-23 19:18:46']); | |
// Age::create(['id' => 16, 'from' => 7, 'to' => 8, 'is_active' => 1, 'created_by' => 1, 'created_at' => '2022-08-24 13:15:20', 'updated_by' => 1, 'updated_at' => '2022-08-24 13:15:20']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment