diff --git a/packages/mds-db/geographies.ts b/packages/mds-db/geographies.ts index be0d90288..86f49387c 100644 --- a/packages/mds-db/geographies.ts +++ b/packages/mds-db/geographies.ts @@ -118,8 +118,17 @@ export async function editGeography(geography: Geography) { } const client = await getWriteableClient() - const sql = `UPDATE ${schema.TABLE.geographies} SET geography_json=$1 WHERE geography_id='${geography.geography_id}' AND publish_date IS NULL` - await client.query(sql, [geography.geography_json]) + const vals = new SqlVals() + const conditions: string[] = [] + Object.entries(geography).forEach(([key, value]) => { + if (key === 'geography_json') { + conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`) + } else { + conditions.push(`${key} = ${vals.add(value)}`) + } + }) + const sql = `UPDATE ${schema.TABLE.geographies} SET ${conditions} WHERE geography_id='${geography.geography_id}' AND publish_date IS NULL` + await client.query(sql, vals.values()) return geography } diff --git a/packages/mds-db/tests/mds-db.spec.ts b/packages/mds-db/tests/mds-db.spec.ts index 1530a0582..220a117ad 100644 --- a/packages/mds-db/tests/mds-db.spec.ts +++ b/packages/mds-db/tests/mds-db.spec.ts @@ -401,12 +401,13 @@ if (pg_info.database) { const numFeatures = geography_json.features.length geography_json.features = [] await MDSDBPostgres.editGeography({ - name: 'District Seven', + name: 'District Seven Updated Name', geography_id: DistrictSeven.geography_id, geography_json }) const result = await MDSDBPostgres.readSingleGeography(GEOGRAPHY2_UUID) assert.notEqual(result.geography_json.features.length, numFeatures) + assert.equal(result.name, 'District Seven Updated Name') assert.equal(result.geography_json.features.length, 0) })