/
index.ts
325 lines (275 loc) · 11.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { faker } from '@faker-js/faker';
import { UniqueEnforcer } from 'enforce-unique';
import { Argon2id } from 'oslo/password';
import { config } from 'config';
import { db } from '../src/db/db';
import { type InsertLabelModel, labelsTable } from '../src/db/schema/labels';
import { type InsertMembershipModel, membershipsTable } from '../src/db/schema/memberships';
import { type InsertOrganizationModel, organizationsTable } from '../src/db/schema/organizations';
import { type InsertProjectModel, projectsTable } from '../src/db/schema/projects';
import { type InsertTaskModel, tasksTable } from '../src/db/schema/tasks';
import { type InsertUserModel, usersTable } from '../src/db/schema/users';
import { type InsertWorkspaceModel, workspacesTable } from '../src/db/schema/workspaces';
import { nanoid } from '../src/lib/nanoid';
import type { Stage, Status } from './data';
// Seed an admin user to access app first time
export const userSeed = async () => {
const usersInTable = await db.select().from(usersTable).limit(1);
if (usersInTable.length > 0) {
console.info('Users table is not empty, skipping seed');
return;
}
const password = '12345678';
const hashedPassword = await new Argon2id().hash(password);
const email = '[email protected]';
await db
.insert(usersTable)
.values({
id: nanoid(),
email,
emailVerified: true,
name: 'Admin User',
language: config.defaultLanguage,
slug: 'admin-user',
role: 'ADMIN',
hashedPassword,
})
.onConflictDoNothing();
console.info(`Created admin user with verified email ${email} and password ${password}.`);
};
// Seed 100 organizations with 100 members each
export const dataSeed = async (progressCallback?: (stage: Stage, count: number, status: Status) => void) => {
const organizationsInTable = await db.select().from(organizationsTable).limit(1);
if (organizationsInTable.length > 0) {
console.info('Organizations table is not empty, skipping seed');
return;
}
const organizationsUniqueEnforcer = new UniqueEnforcer();
const organizations: (InsertOrganizationModel & {
id: string;
})[] = Array.from({
length: 100,
}).map(() => {
const name = organizationsUniqueEnforcer.enforce(() => faker.company.name());
return {
id: nanoid(),
name,
slug: faker.helpers.slugify(name).toLowerCase(),
bannerUrl: faker.image.url(),
brandColor: faker.internet.color(),
chatSupport: faker.datatype.boolean(),
country: faker.location.country(),
createdAt: faker.date.past(),
logoUrl: faker.image.url(),
thumbnailUrl: faker.image.url(),
};
});
await db.insert(organizationsTable).values(organizations).onConflictDoNothing();
const hashedPassword = await new Argon2id().hash('12345678');
const usersSlugUniqueEnforcer = new UniqueEnforcer();
const usersEmailUniqueEnforcer = new UniqueEnforcer();
let usersCount = 0;
let organizationsCount = 0;
let workspacesCount = 0;
let projectsCount = 0;
let tasksCount = 0;
let labelsCount = 0;
let relationsCount = 0;
// Create 100 users for each organization
for (const organization of organizations) {
organizationsCount++;
if (progressCallback) {
progressCallback('organizations', organizationsCount, 'inserting');
}
const insertUsers: InsertUserModel[] = Array.from({ length: 100 }).map(() => {
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const firstAndLastName = { firstName, lastName };
const name = faker.person.fullName(firstAndLastName);
const email = usersEmailUniqueEnforcer.enforce(() => faker.internet.email(firstAndLastName).toLocaleLowerCase());
const slug = usersSlugUniqueEnforcer.enforce(() =>
faker.internet
.userName(firstAndLastName)
.toLowerCase()
.replace(/[^a-z0-9]/g, '-'),
);
return {
id: nanoid(),
firstName,
lastName,
thumbnailUrl: faker.image.avatar(),
language: config.defaultLanguage,
name,
email,
hashedPassword,
slug,
avatarUrl: faker.image.avatar(),
createdAt: faker.date.past(),
};
});
usersCount += insertUsers.length;
if (progressCallback) {
progressCallback('users', usersCount, 'inserting');
}
const users = await db.insert(usersTable).values(insertUsers).returning().onConflictDoNothing();
// Create 100 memberships for each organization
const memberships: InsertMembershipModel[] = users.map((user) => {
return {
id: nanoid(),
userId: user.id,
organizationId: organization.id,
role: faker.helpers.arrayElement(['ADMIN', 'MEMBER']),
createdAt: faker.date.past(),
};
});
relationsCount += memberships.length;
if (progressCallback) {
progressCallback('relations', relationsCount, 'inserting');
}
await db.insert(membershipsTable).values(memberships).onConflictDoNothing();
const insertWorkspaces: InsertWorkspaceModel[] = Array.from({ length: 5 }).map(() => {
const name = organizationsUniqueEnforcer.enforce(() => faker.company.name());
return {
id: nanoid(),
organizationId: organization.id,
name: faker.company.name(),
slug: faker.helpers.slugify(name).toLowerCase(),
bannerUrl: faker.image.url(),
thumbnailUrl: faker.image.url(),
createdAt: faker.date.past(),
createdBy: users[Math.floor(Math.random() * users.length)].id,
modifiedAt: faker.date.past(),
modifiedBy: users[Math.floor(Math.random() * users.length)].id,
};
});
const workspaces = await db.insert(workspacesTable).values(insertWorkspaces).returning().onConflictDoNothing();
const groupSize = 10;
let groupIndex = 0;
for (const workspace of workspaces) {
workspacesCount++;
if (progressCallback) {
progressCallback('workspaces', workspacesCount, 'inserting');
}
// slice users to 10 members for each workspace
const usersGroup = users.slice(groupIndex * groupSize, (groupIndex + 1) * groupSize);
groupIndex++;
const workspaceMemberships: InsertMembershipModel[] = usersGroup.map((user) => {
return {
id: nanoid(),
userId: user.id,
workspaceId: workspace.id,
role: faker.helpers.arrayElement(['ADMIN', 'MEMBER']),
createdAt: faker.date.past(),
};
});
relationsCount += workspaceMemberships.length;
if (progressCallback) {
progressCallback('relations', relationsCount, 'inserting');
}
await db.insert(membershipsTable).values(workspaceMemberships).onConflictDoNothing();
const insertProjects: InsertProjectModel[] = Array.from({ length: 3 }).map(() => {
const name = organizationsUniqueEnforcer.enforce(() => faker.company.name());
return {
id: nanoid(),
workspaceId: workspace.id,
name,
color: faker.internet.color(),
slug: faker.helpers.slugify(name).toLowerCase(),
createdAt: faker.date.past(),
createdBy: usersGroup[Math.floor(Math.random() * usersGroup.length)].id,
modifiedAt: faker.date.past(),
modifiedBy: usersGroup[Math.floor(Math.random() * usersGroup.length)].id,
};
});
const projects = await db.insert(projectsTable).values(insertProjects).returning().onConflictDoNothing();
for (const project of projects) {
projectsCount++;
if (progressCallback) {
progressCallback('projects', projectsCount, 'inserting');
}
const projectMemberships: InsertMembershipModel[] = usersGroup.map((user) => {
return {
id: nanoid(),
userId: user.id,
projectId: project.id,
role: faker.helpers.arrayElement(['ADMIN', 'MEMBER']),
createdAt: faker.date.past(),
};
});
relationsCount += projectMemberships.length;
if (progressCallback) {
progressCallback('relations', relationsCount, 'inserting');
}
await db.insert(membershipsTable).values(projectMemberships).onConflictDoNothing();
const insertTasks: InsertTaskModel[] = Array.from({ length: 25 }).map(() => {
const name = organizationsUniqueEnforcer.enforce(() => faker.company.name());
return {
id: nanoid(),
projectId: project.id,
summary: name,
slug: faker.helpers.slugify(name).toLowerCase(),
// random integer between 0 and 6
status: Math.floor(Math.random() * 7),
type: faker.helpers.arrayElement(['bug', 'feature', 'chore']),
// random integer between 0 and 3
impact: Math.floor(Math.random() * 4),
markdown: faker.lorem.paragraphs(),
createdAt: faker.date.past(),
createdBy: usersGroup[Math.floor(Math.random() * usersGroup.length)].id,
modifiedAt: faker.date.past(),
modifiedBy: usersGroup[Math.floor(Math.random() * usersGroup.length)].id,
assignedAt: faker.date.past(),
assignedBy: usersGroup[Math.floor(Math.random() * usersGroup.length)].id,
};
});
const tasks = await db.insert(tasksTable).values(insertTasks).returning().onConflictDoNothing();
const insertLabels: InsertLabelModel[] = Array.from({ length: 5 }).map(() => {
const name = organizationsUniqueEnforcer.enforce(() => faker.company.name());
return {
id: nanoid(),
projectId: project.id,
name,
color: faker.internet.color(),
};
});
labelsCount += insertLabels.length;
if (progressCallback) {
progressCallback('labels', labelsCount, 'inserting');
}
const labels = await db.insert(labelsTable).values(insertLabels).returning().onConflictDoNothing();
for (const task of tasks) {
tasksCount++;
if (progressCallback) {
progressCallback('tasks', tasksCount, 'inserting');
}
const taskLabels = labels.filter(() => faker.datatype.boolean());
if (taskLabels.length === 0) {
continue;
}
const insertTaskLabels: InsertMembershipModel[] = taskLabels.map((label) => {
return {
id: nanoid(),
taskId: task.id,
labelId: label.id,
createdAt: faker.date.past(),
};
});
relationsCount += insertTaskLabels.length;
if (progressCallback) {
progressCallback('relations', relationsCount, 'inserting');
}
await db.insert(membershipsTable).values(insertTaskLabels).onConflictDoNothing();
}
}
}
}
if (progressCallback) {
progressCallback('relations', relationsCount, 'done');
progressCallback('labels', labelsCount, 'done');
progressCallback('tasks', tasksCount, 'done');
progressCallback('projects', projectsCount, 'done');
progressCallback('workspaces', workspacesCount, 'done');
progressCallback('users', usersCount, 'done');
progressCallback('organizations', organizationsCount, 'done');
}
};