(db: Pool, table: string, objectProvided: Record<string, any>)
| 93 | } |
| 94 | |
| 95 | export async function insertRow(db: Pool, table: string, objectProvided: Record<string, any>) { |
| 96 | // Handling of related fields |
| 97 | const { source__plugin_json, source__index_ts, source__frontend_tsx, source__site_ts, ...object } = objectProvided |
| 98 | |
| 99 | const keys = Object.keys(object) |
| 100 | .map((key) => `"${key}"`) |
| 101 | .join(',') |
| 102 | const params = Object.keys(object) |
| 103 | .map((_, i) => `\$${i + 1}`) |
| 104 | .join(',') |
| 105 | const values = Object.values(object).map((value) => { |
| 106 | if (Array.isArray(value) && value.length > 0) { |
| 107 | return JSON.stringify(value) |
| 108 | } |
| 109 | return value |
| 110 | }) |
| 111 | |
| 112 | try { |
| 113 | const { |
| 114 | rows: [rowSaved], |
| 115 | } = await db.query(`INSERT INTO ${table} (${keys}) VALUES (${params}) RETURNING *`, values) |
| 116 | const dependentQueries: Promise<void>[] = [] |
| 117 | if (source__plugin_json) { |
| 118 | dependentQueries.push( |
| 119 | insertRow(db, 'posthog_pluginsourcefile', { |
| 120 | id: new UUIDT().toString(), |
| 121 | filename: 'plugin.json', |
| 122 | source: source__plugin_json, |
| 123 | plugin_id: rowSaved.id, |
| 124 | error: null, |
| 125 | transpiled: null, |
| 126 | }) |
| 127 | ) |
| 128 | } |
| 129 | if (source__index_ts) { |
| 130 | dependentQueries.push( |
| 131 | insertRow(db, 'posthog_pluginsourcefile', { |
| 132 | id: new UUIDT().toString(), |
| 133 | filename: 'index.ts', |
| 134 | source: source__index_ts, |
| 135 | plugin_id: rowSaved.id, |
| 136 | error: null, |
| 137 | transpiled: null, |
| 138 | }) |
| 139 | ) |
| 140 | } |
| 141 | if (source__frontend_tsx) { |
| 142 | dependentQueries.push( |
| 143 | insertRow(db, 'posthog_pluginsourcefile', { |
| 144 | id: new UUIDT().toString(), |
| 145 | filename: 'frontend.tsx', |
| 146 | source: source__frontend_tsx, |
| 147 | plugin_id: rowSaved.id, |
| 148 | error: null, |
| 149 | transpiled: null, |
| 150 | }) |
| 151 | ) |
| 152 | } |
searching dependent graphs…