Skip to content

Commit

Permalink
NERT-373: Create seed file for local development and testing (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
KjartanE authored May 31, 2024
1 parent e442f0d commit 0e10f04
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 1 deletion.
22 changes: 22 additions & 0 deletions database/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"format-fix": "prettier --write \"./src/**/*.{js,jsx,ts,tsx,json,css,scss}\""
},
"dependencies": {
"@faker-js/faker": "^8.4.1",
"faker-js": "^1.0.0",
"knex": "^3.1.0",
"pg": "^8.11.3"
},
Expand Down
189 changes: 189 additions & 0 deletions database/src/seeds/02_basic_project_plan_setup.ts
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;
`;
8 changes: 7 additions & 1 deletion env_config/env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,10 @@ ELASTICSEARCH_TAXONOMY_INDEX=taxonomy_3.0.0
# ------------------------------------------------------------------------------
# MapTiler API Key
REACT_APP_MAPTILER_API_KEY=<Put your key here>
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Project Seeder Details
# ------------------------------------------------------------------------------
NUM_SEED_PROJECTS=2
NUM_SEED_PLANS=2

0 comments on commit 0e10f04

Please sign in to comment.