Skip to content

Commit

Permalink
DB: add temporary script to calculate length of existing subsections … (
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannaPeanut authored Jan 24, 2024
2 parents 8227a53 + dab5671 commit c05718e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
41 changes: 41 additions & 0 deletions db/calculate-subsection-subsubsection-lengthkm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require("dotenv").config({ path: "./.env.local" })
const { length, lineString } = require("@turf/turf")
const { PrismaClient } = require("@prisma/client")

const db = new PrismaClient()
/*
* This function is executed when you run `todo`.
*/
const calculateLengthKm = async () => {
const subsections = await db.subsection.findMany()
console.log("subsections successfully fetched")
subsections.forEach(async (subsection) => {
const calculatedLengthKm = Number(length(lineString(subsection.geometry)).toFixed(3))
const updatedSubsection = await db.subsection.update({
where: { id: subsection.id },
data: { lengthKm: calculatedLengthKm },
})
console.log(
`updated subsection ${updatedSubsection.id} successfuly`,
updatedSubsection.lengthKm,
)
})
const subsubsections = await db.subsubsection.findMany()
console.log("subsubsections successfully fetched")
subsubsections.forEach(async (subsubsection) => {
const calculatedLengthKm =
subsubsection.type === "AREA"
? 0.1 // AREAs are SF, they have no length so they get a default
: Number(length(lineString(subsubsection.geometry)).toFixed(3))
const updatedSubsubsection = await db.subsubsection.update({
where: { id: subsubsection.id },
data: { lengthKm: calculatedLengthKm },
})
console.log(
`updated subsubsection ${updatedSubsubsection.id} successfuly`,
updatedSubsubsection.lengthKm,
)
})
}

calculateLengthKm()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"seed": "npm run predev && npm run migrate && blitz prisma migrate reset && blitz db seed",
"updatePackages": "npx taze major --write",
"release": "gh pr create --base main --head develop --title \"Release $(date '+%Y-%m-%d')\" --body \"\"",
"prepare": "husky install"
"prepare": "husky install",
"calc-lengthkm": "node db/calculate-subsection-subsubsection-lengthkm.js"
},
"prisma": {
"schema": "db/schema.prisma"
Expand Down

0 comments on commit c05718e

Please sign in to comment.