From 254ccf5d5fae2fc4f50a47a7a4016490fbab773b Mon Sep 17 00:00:00 2001 From: jian Date: Wed, 13 Nov 2019 16:48:49 -0500 Subject: [PATCH 1/3] Fix `editGeography`. --- packages/mds-db/geographies.ts | 17 +++++++++++++++-- packages/mds-db/tests/mds-db.spec.ts | 3 ++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/mds-db/geographies.ts b/packages/mds-db/geographies.ts index be0d90288..aa6546966 100644 --- a/packages/mds-db/geographies.ts +++ b/packages/mds-db/geographies.ts @@ -118,8 +118,21 @@ 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 = [] + // conditions.push(`publish_date = ${vals.add(publish_date)}`) + // conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`) + // conditions.push() + // eslint-disable-next-line @typescript-eslint/no-explicit-any + Object.keys(geography).forEach(key as keyof Geography => { + if (key === 'geography_json') { + conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`) + } else { + conditions.push(`${key} = ${vals.add(geography[key])}`) + } + }) + 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) }) From 01dab099bb178a5b1c1f310446207db9c338bb6e Mon Sep 17 00:00:00 2001 From: Max Johansen Date: Wed, 13 Nov 2019 15:44:59 -0700 Subject: [PATCH 2/3] ts assist --- packages/mds-db/geographies.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/mds-db/geographies.ts b/packages/mds-db/geographies.ts index aa6546966..2e1241d99 100644 --- a/packages/mds-db/geographies.ts +++ b/packages/mds-db/geographies.ts @@ -119,16 +119,15 @@ export async function editGeography(geography: Geography) { const client = await getWriteableClient() const vals = new SqlVals() - const conditions = [] + const conditions: string[] = [] // conditions.push(`publish_date = ${vals.add(publish_date)}`) // conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`) // conditions.push() - // eslint-disable-next-line @typescript-eslint/no-explicit-any - Object.keys(geography).forEach(key as keyof Geography => { + 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(geography[key])}`) + 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` From 5a301dbac9162786cce3fc54fc665dd85114b37b Mon Sep 17 00:00:00 2001 From: jian Date: Wed, 13 Nov 2019 18:03:18 -0500 Subject: [PATCH 3/3] Fixed `editGeography` with TS assist. --- packages/mds-db/geographies.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/mds-db/geographies.ts b/packages/mds-db/geographies.ts index 2e1241d99..86f49387c 100644 --- a/packages/mds-db/geographies.ts +++ b/packages/mds-db/geographies.ts @@ -120,9 +120,6 @@ export async function editGeography(geography: Geography) { const client = await getWriteableClient() const vals = new SqlVals() const conditions: string[] = [] - // conditions.push(`publish_date = ${vals.add(publish_date)}`) - // conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`) - // conditions.push() Object.entries(geography).forEach(([key, value]) => { if (key === 'geography_json') { conditions.push(`geography_json = ${vals.add(JSON.stringify(geography.geography_json))}`)