-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NERT-373: Create seed file for local development and testing (#87)
- Loading branch information
Showing
4 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// import { faker } from '@faker-js/faker'; | ||
import { faker } from '@faker-js/faker'; | ||
import { Knex } from 'knex'; | ||
|
||
const DB_SCHEMA = process.env.DB_SCHEMA; | ||
const DB_SCHEMA_DAPI_V1 = process.env.DB_SCHEMA_DAPI_V1; | ||
|
||
const NUM_SEED_PROJECTS = Number(process.env.NUM_SEED_PROJECTS ?? 2); | ||
const NUM_SEED_PLANS = Number(process.env.NUM_SEED_PLANS ?? 2); | ||
|
||
/** | ||
* Add spatial transform | ||
* | ||
* @export | ||
* @param {Knex} knex | ||
* @return {*} {Promise<void>} | ||
*/ | ||
export async function seed(knex: Knex): Promise<void> { | ||
await knex.raw(` | ||
SET SCHEMA '${DB_SCHEMA}'; | ||
SET SEARCH_PATH=${DB_SCHEMA},${DB_SCHEMA_DAPI_V1}; | ||
`); | ||
|
||
// Check if at least 1 project already exists | ||
const checkProjectsResponse = await knex.raw(checkAnyProjectExists()); | ||
|
||
if (!checkProjectsResponse.rows.length) { | ||
for (let i = 0; i < NUM_SEED_PROJECTS; i++) { | ||
// Insert project data | ||
const createProjectResponse = await knex.raw(insertProjectData(`Seed Project ${i + 1}`, true)); | ||
const projectId = createProjectResponse.rows[0].project_id; | ||
|
||
await knex.raw(` | ||
${insertProjectContactData(projectId)} | ||
${insertProjectSpatialData(projectId)} | ||
${insertProjectNRMRegionData(projectId)} | ||
`); | ||
} | ||
} | ||
|
||
//check if at least 1 plan already exists | ||
const checkPlansResponse = await knex.raw(checkAnyPlanExists()); | ||
|
||
if (!checkPlansResponse.rows.length) { | ||
for (let i = 0; i < NUM_SEED_PLANS; i++) { | ||
// // Insert plan data | ||
const createPlanResponse = await knex.raw(insertProjectData(`Seed Plan ${i + 1}`, false)); | ||
const planId = createPlanResponse.rows[0].project_id; | ||
|
||
await knex.raw(` | ||
${insertProjectContactData(planId)} | ||
${insertProjectSpatialData(planId)} | ||
${insertProjectNRMRegionData(planId)} | ||
`); | ||
} | ||
} | ||
} | ||
|
||
const checkAnyPlanExists = () => ` | ||
SELECT | ||
project_id | ||
FROM | ||
project | ||
WHERE | ||
is_project = false | ||
; | ||
`; | ||
|
||
const checkAnyProjectExists = () => ` | ||
SELECT | ||
project_id | ||
FROM | ||
project; | ||
`; | ||
|
||
const insertProjectNRMRegionData = (projectId: number) => ` | ||
INSERT INTO nrm_region ( | ||
project_id, | ||
objectid, | ||
name | ||
) VALUES ( | ||
${projectId}, | ||
1, | ||
1 | ||
) | ||
RETURNING | ||
nrm_region_id; | ||
`; | ||
|
||
const insertProjectSpatialData = (projectId: number) => ` | ||
INSERT INTO project_spatial_component ( | ||
project_id, | ||
project_spatial_component_type_id, | ||
name, | ||
is_within_overlapping, | ||
number_sites, | ||
size_ha, | ||
geojson, | ||
geography | ||
) VALUES ( | ||
${projectId}, | ||
(SELECT project_spatial_component_type_id from project_spatial_component_type WHERE name = 'Boundary'), | ||
'Boundary', | ||
'N', | ||
1, | ||
100, | ||
'{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[-121, 51], | ||
[-121, 51.7], | ||
[-120.5, 51.7], | ||
[-120.5, 51], | ||
[-121, 51] | ||
] | ||
] | ||
}, | ||
"properties": {} | ||
}', | ||
public.geography( | ||
public.ST_Force2D( | ||
public.ST_SetSRID( | ||
public.ST_Force2D(public.ST_GeomFromGeoJSON(' | ||
{ | ||
"type": "Polygon", | ||
"coordinates": [ | ||
[ | ||
[-121, 51], | ||
[-121, 51.7], | ||
[-120.5, 51.7], | ||
[-120.5, 51], | ||
[-121, 51] | ||
] | ||
] | ||
} | ||
') | ||
), 4326 | ||
) | ||
) | ||
) | ||
) | ||
RETURNING | ||
project_spatial_component_id | ||
;`; | ||
|
||
const insertProjectContactData = (projectId: number) => ` | ||
INSERT INTO project_contact ( | ||
project_id, contact_type_id, first_name, last_name, agency, email_address, is_public, is_primary | ||
) VALUES ( | ||
${projectId}, 1, 'John', 'Doe', 'Ministry of Forests', '[email protected]', 'Y', 'Y' | ||
); | ||
`; | ||
|
||
const insertProjectData = (projectName: string, isProject: boolean) => ` | ||
INSERT INTO project ( | ||
name, | ||
brief_desc, | ||
is_project, | ||
state_code, | ||
start_date, | ||
end_date, | ||
actual_start_date, | ||
actual_end_date, | ||
is_healing_land, | ||
is_healing_people, | ||
is_land_initiative, | ||
is_cultural_initiative, | ||
people_involved | ||
) VALUES ( | ||
'${projectName}', | ||
$$${faker.lorem.sentences(2)}$$, | ||
${isProject}, | ||
1, | ||
$$${faker.date.between({ from: '2000-01-01T00:00:00-08:00', to: '2005-01-01T00:00:00-08:00' }).toISOString()}$$, | ||
$$${faker.date.between({ from: '2025-01-01T00:00:00-08:00', to: '2030-01-01T00:00:00-08:00' }).toISOString()}$$, | ||
$$${faker.date.between({ from: '2000-01-01T00:00:00-08:00', to: '2005-01-01T00:00:00-08:00' }).toISOString()}$$, | ||
$$${faker.date.between({ from: '2025-01-01T00:00:00-08:00', to: '2030-01-01T00:00:00-08:00' }).toISOString()}$$, | ||
true, | ||
true, | ||
true, | ||
true, | ||
10 | ||
) | ||
RETURNING | ||
project_id; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters