From e6b260613ee548f8da42dd55e70bbef6b661ad9d Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 24 Feb 2022 23:16:00 +0000 Subject: [PATCH] Migrate existing relationships over to new lookup structure --- utility/db/dbTestData.js | 216 +++++++++++++++++++++------------------ 1 file changed, 115 insertions(+), 101 deletions(-) diff --git a/utility/db/dbTestData.js b/utility/db/dbTestData.js index 9365785..1d959f0 100644 --- a/utility/db/dbTestData.js +++ b/utility/db/dbTestData.js @@ -38,6 +38,15 @@ const tableDetails = { hashPassword: true, id: 'uuid' }, + studentParentLink: { + link: true + }, + studentClassLink: { + link: true + }, + accountClassLink: { + link: true + } }; // Object to store all of the test data to be inserted @@ -186,112 +195,164 @@ const data = { const relationships = { studentParentLink: [ { - studentId: 1, - parentId: 1 + lookups: { + studentId: 1, + parentId: 1 + } }, { - studentId: 2, - parentId: 2 + lookups: { + studentId: 2, + parentId: 2 + } }, { - studentId: 2, - parentId: 3 + lookups: { + studentId: 2, + parentId: 3 + } }, { - studentId: 3, - parentId: 4 + lookups: { + studentId: 3, + parentId: 4 + } }, { - studentId: 4, - parentId: 4 + lookups: { + studentId: 4, + parentId: 4 + } }, { - studentId: 4, - parentId: 5 + lookups: { + studentId: 4, + parentId: 5 + } }, { - studentId: 5, - parentId: 5 + lookups: { + studentId: 5, + parentId: 5 + } } ], studentClassLink: [ { - studentId: 1, - classId: 1 + lookups: { + studentId: 1, + classId: 1 + } }, { - studentId: 2, - classId: 1 + lookups: { + studentId: 2, + classId: 1 + } }, { - studentId: 3, - classId: 1 + lookups: { + studentId: 3, + classId: 1 + } }, { - studentId: 4, - classId: 1 + lookups: { + studentId: 4, + classId: 1 + } }, { - studentId: 5, - classId: 1 + lookups: { + studentId: 5, + classId: 1 + } }, { - studentId: 1, - classId: 2 + lookups: { + studentId: 1, + classId: 2 + } }, { - studentId: 2, - classId: 2 + lookups: { + studentId: 2, + classId: 2 + } }, { - studentId: 1, - classId: 3 + lookups: { + studentId: 1, + classId: 3 + } }, { - studentId: 2, - classId: 3 + lookups: { + studentId: 2, + classId: 3 + } }, { - studentId: 3, - classId: 3 + lookups: { + studentId: 3, + classId: 3 + } }, { - studentId: 4, - classId: 4 + lookups: { + studentId: 4, + classId: 4 + } }, { - studentId: 5, - classId: 4 + lookups: { + studentId: 5, + classId: 4 + } }, ], accountClassLink: [ { - accountId: 1, - classId: 1, + lookups: { + accountId: 1, + classId: 1, + } }, { - accountId: 1, - classId: 2, + lookups: { + accountId: 1, + classId: 2, + } }, { - accountId: 2, - classId: 2, + lookups: { + accountId: 2, + classId: 2, + } }, { - accountId: 3, - classId: 3, + lookups: { + accountId: 3, + classId: 3, + } }, { - accountId: 1, - classId: 4, + lookups: { + accountId: 1, + classId: 4, + } }, { - accountId: 2, - classId: 4, + lookups: { + accountId: 2, + classId: 4, + } }, { - accountId: 3, - classId: 4, + lookups: { + accountId: 3, + classId: 4, + } }, ] }; @@ -356,6 +417,8 @@ async function insertData(dbOptions) { 10); } + if (tableDetails?.[table]?.['link']) + delete dataToInsert[`${table}Id`]; if (record?.lookups) { delete dataToInsert.lookups; @@ -394,55 +457,6 @@ async function insertData(dbOptions) { } } - console.log('\n'); - - // Iterate over all of the link tables in the relationship object - for (const tableName of Object.keys(relationships)) { - // Array containing all of the relationships for the table - const table = relationships[tableName]; - - // Iterate over the relationships - for (const relationship of table) { - // Object that will contain a record to insert - const dataToInsert = {}; - - // Iterate over the individual relationships - // to easily parse them into records to insert - for (const [ k, v ] of Object.entries(relationship)) { - // The name of the table to lookup the given - // ID in - const resolveTable = k.split('Id')[0]; - - // Array containing the enumerated records - // from the data object relating to the lookup - // table - const d = - Object.values(data[resolveTable])[v-1]; - - // Add the id of the record that needs to be - // linked, to the record object - dataToInsert[k] = d[k]; - } - - const qs = '?, '.repeat(Object.keys( - dataToInsert).length).slice(0, -2); - - const sql = `INSERT INTO ${tableName} ` + - `(${Object.keys(dataToInsert)}) ` + - `VALUES ` + - `(${qs});`; - - console.log(sql); - - try { - await dbConnectionPool.runQuery(sql, - Object.values(dataToInsert)); - } catch (e) { - console.error(e); - } - } - } - await dbConnectionPool.close(); }