Skip to content

Commit d105b00

Browse files
🌱 Seed the database with some places.
NOTE: you need places to be able to create places as NTSMapSheet - required for creation of places - is actually a list of values taken from existing places.
1 parent 5c37889 commit d105b00

File tree

7 files changed

+153
-7
lines changed

7 files changed

+153
-7
lines changed

src/api/config.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ dotenv.config({ path: dotEnvPath });
1616

1717
export const API_PORT = parseInt(process.env.API_PORT || '3000');
1818
export const FRONTEND_URL = process.env.FRONTEND_URL || 'localhost:8080';
19-
export const AUTH_REDIRECT =
20-
process.env.AUTH_REDIRECT || process.env.FRONTEND_URL || '';
19+
export const AUTH_REDIRECT = process.env.AUTH_REDIRECT || process.env.FRONTEND_URL || '';
2120
export const NODE_ENV = process.env.NODE_ENV;
2221

2322
export const DB_NAME = process.env.DB_NAME || '';
@@ -40,15 +39,19 @@ export const DB_CONFIG = {
4039
port: parseInt(DB_PORT),
4140
},
4241
migrations: {
43-
directory: path.resolve(__dirname, "./data/migrations"),
44-
extension: "ts",
45-
stub: path.resolve(__dirname, "./data/templates/sample-migration.ts"),
42+
directory: path.resolve(__dirname, './data/migrations'),
43+
extension: 'ts',
44+
stub: path.resolve(__dirname, './data/templates/sample-migration.ts'),
45+
},
46+
seeds: {
47+
directory: path.resolve(__dirname, `./data/seeds/${NODE_ENV}`),
48+
extension: 'ts',
49+
stub: path.resolve(__dirname, './data/templates/sample-seed.ts'),
4650
},
4751
};
4852

4953
export const GIS_PORTAL_CLIENT_ID = process.env.GIS_PORTAL_CLIENT_ID || '';
50-
export const GIS_PORTAL_CLIENT_SECRET =
51-
process.env.GIS_PORTAL_CLIENT_SECRET || '';
54+
export const GIS_PORTAL_CLIENT_SECRET = process.env.GIS_PORTAL_CLIENT_SECRET || '';
5255
export const GIS_FEATURE_USERNAME = process.env.GIS_FEATURE_USERNAME || '';
5356
export const GIS_FEATURE_PASSWORD = process.env.GIS_FEATURE_PASSWORD || '';
5457

src/api/data/seeds/.gitkeep

Whitespace-only changes.

src/api/data/seeds/development/.gitkeep

Whitespace-only changes.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { Knex } from 'knex';
2+
import { isNil } from 'lodash';
3+
4+
export async function seed(knex: Knex): Promise<void> {
5+
const communityAttributes = { Name: 'Whitehorse' };
6+
let community = await knex('Community').where({ Name: communityAttributes.Name }).first();
7+
if (isNil(community)) {
8+
[community] = await knex('Community').insert(communityAttributes).returning('*');
9+
} else {
10+
await knex('Community').where('Id', community.Id).update(communityAttributes);
11+
}
12+
13+
const statuteAttributes = {
14+
RecognitionAuthority: 'Yukon Authority',
15+
RecognitionType: 'Recognition Type',
16+
Description: 'Default Statute',
17+
};
18+
let statute = await knex('Statute')
19+
.where({ RecognitionAuthority: statuteAttributes.RecognitionAuthority })
20+
.first();
21+
if (isNil(statute)) {
22+
[statute] = await knex('Statute').insert(statuteAttributes).returning('*');
23+
} else {
24+
await knex('Statute').where('Id', statute.Id).update(statuteAttributes);
25+
}
26+
27+
const placesAttributes = [
28+
{
29+
PrimaryName: 'Sample Place 1',
30+
YHSIId: 'YH0001',
31+
NTSMapSheet: '105A',
32+
Jurisdiction: 1,
33+
StatuteId: statute.Id,
34+
Statute2Id: statute.Id,
35+
OwnerConsent: 1,
36+
Category: 1,
37+
IsPubliclyAccessible: 1,
38+
CommunityId: community.Id,
39+
SiteStatus: 1,
40+
FloorCondition: 1,
41+
WallCondition: 1,
42+
DoorCondition: 1,
43+
RoofCondition: 1,
44+
CoordinateDetermination: 1,
45+
ShowInRegister: 1,
46+
},
47+
{
48+
PrimaryName: 'Sample Place 2',
49+
YHSIId: 'YH0002',
50+
NTSMapSheet: '105B',
51+
Jurisdiction: 1,
52+
StatuteId: statute.Id,
53+
Statute2Id: statute.Id,
54+
OwnerConsent: 1,
55+
Category: 1,
56+
IsPubliclyAccessible: 1,
57+
CommunityId: community.Id,
58+
SiteStatus: 1,
59+
FloorCondition: 1,
60+
WallCondition: 1,
61+
DoorCondition: 1,
62+
RoofCondition: 1,
63+
CoordinateDetermination: 1,
64+
ShowInRegister: 1,
65+
},
66+
{
67+
PrimaryName: 'Sample Place 3',
68+
YHSIId: 'YH0003',
69+
NTSMapSheet: '115A',
70+
Jurisdiction: 1,
71+
StatuteId: statute.Id,
72+
Statute2Id: statute.Id,
73+
OwnerConsent: 1,
74+
Category: 1,
75+
IsPubliclyAccessible: 1,
76+
CommunityId: community.Id,
77+
SiteStatus: 1,
78+
FloorCondition: 1,
79+
WallCondition: 1,
80+
DoorCondition: 1,
81+
RoofCondition: 1,
82+
CoordinateDetermination: 1,
83+
ShowInRegister: 1,
84+
},
85+
{
86+
PrimaryName: 'Sample Place 4',
87+
YHSIId: 'YH0004',
88+
NTSMapSheet: '115B',
89+
Jurisdiction: 1,
90+
StatuteId: statute.Id,
91+
Statute2Id: statute.Id,
92+
OwnerConsent: 1,
93+
Category: 1,
94+
IsPubliclyAccessible: 1,
95+
CommunityId: community.Id,
96+
SiteStatus: 1,
97+
FloorCondition: 1,
98+
WallCondition: 1,
99+
DoorCondition: 1,
100+
RoofCondition: 1,
101+
CoordinateDetermination: 1,
102+
ShowInRegister: 1,
103+
},
104+
];
105+
106+
for (const placeAttributes of placesAttributes) {
107+
let place = await knex('Place').where({ YHSIId: placeAttributes.YHSIId }).first();
108+
if (isNil(place)) {
109+
[place] = await knex('Place').insert(placeAttributes).returning('*');
110+
} else {
111+
await knex('Place').where('Id', place.Id).update(placeAttributes);
112+
}
113+
}
114+
}

src/api/data/seeds/production/.gitkeep

Whitespace-only changes.

src/api/data/seeds/test/.gitkeep

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Knex } from 'knex';
2+
import { isNil } from 'lodash';
3+
4+
export async function seed(knex: Knex): Promise<void> {
5+
const usersAttributes = [
6+
{
7+
email: 'admin@example.com',
8+
first_name: 'System',
9+
last_name: 'Administrator',
10+
create_date: new Date(),
11+
roles: 'Administrator',
12+
status: 'Active',
13+
},
14+
];
15+
16+
for (const userAttributes of usersAttributes) {
17+
let user = await knex
18+
.withSchema('Security')
19+
.from('User')
20+
.where({ email: userAttributes.email })
21+
.first();
22+
23+
if (isNil(user)) {
24+
[user] = await knex.withSchema('Security').insert(userAttributes).into('User').returning('*');
25+
} else {
26+
await knex.withSchema('Security').from('User').where('Id', user.Id).update(userAttributes);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)