-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathseed.mts
More file actions
314 lines (279 loc) · 9.55 KB
/
Copy pathseed.mts
File metadata and controls
314 lines (279 loc) · 9.55 KB
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
import { prisma } from "./app/db.server";
import { createOrganization } from "./app/models/organization.server";
import { createProject } from "./app/models/project.server";
import { AuthenticationMethod, Organization, Prisma, User } from "@trigger.dev/database";
async function seed() {
console.log("🌱 Starting seed...");
// Create or find the local user
let user = await prisma.user.findUnique({
where: { email: "local@trigger.dev" },
});
if (!user) {
console.log("Creating local user...");
user = await prisma.user.create({
data: {
email: "local@trigger.dev",
authenticationMethod: AuthenticationMethod.MAGIC_LINK,
name: "Local Developer",
displayName: "Local Developer",
admin: true,
confirmedBasicDetails: true,
},
});
console.log(`✅ Created user: ${user.email} (${user.id})`);
} else {
console.log(`✅ User already exists: ${user.email} (${user.id})`);
}
// Create or find the references organization
// Look for an organization where the user is a member and the title is "References"
let organization = await prisma.organization.findFirst({
where: {
title: "References",
members: {
some: {
userId: user.id,
},
},
},
});
if (!organization) {
console.log("Creating references organization...");
organization = await createOrganization({
title: "References",
userId: user.id,
companySize: "1-10",
});
console.log(`✅ Created organization: ${organization.title} (${organization.slug})`);
} else {
console.log(`✅ Organization already exists: ${organization.title} (${organization.slug})`);
}
// Reference projects with their specific project refs. These refs MUST stay in
// sync with the corresponding projects in the standalone references repo
// (github.com/triggerdotdev/references): hello-world hardcodes its ref in
// trigger.config.ts; d3-chat and realtime-streams read TRIGGER_PROJECT_REF.
const referenceProjects = [
{
name: "hello-world",
externalRef: "proj_rrkpdguyagvsoktglnod",
},
{
name: "d3-chat",
externalRef: "proj_cdmymsrobxmcgjqzhdkq",
},
{
name: "realtime-streams",
externalRef: "proj_klxlzjnzxmbgiwuuwhvb",
},
];
// Create or find each project
for (const projectConfig of referenceProjects) {
await findOrCreateProject(projectConfig.name, organization, user.id, projectConfig.externalRef);
}
await createBatchLimitOrgs(user);
await ensureDefaultWorkerGroup();
console.log("\n🎉 Seed complete!\n");
console.log("Summary:");
console.log(`User: ${user.email}`);
console.log(`Organization: ${organization.title} (${organization.slug})`);
console.log(`Projects: ${referenceProjects.map((p) => p.name).join(", ")}`);
console.log("\n⚠️ Note: in your triggerdotdev/references clone, set TRIGGER_PROJECT_REF in:");
console.log(` - projects/d3-chat/.env: TRIGGER_PROJECT_REF=proj_cdmymsrobxmcgjqzhdkq`);
console.log(` - projects/realtime-streams/.env: TRIGGER_PROJECT_REF=proj_klxlzjnzxmbgiwuuwhvb`);
}
async function createBatchLimitOrgs(user: User) {
const org1 = await findOrCreateOrganization("batch-limit-org-1", user, {
batchQueueConcurrencyConfig: { processingConcurrency: 1 },
});
const org2 = await findOrCreateOrganization("batch-limit-org-2", user, {
batchQueueConcurrencyConfig: { processingConcurrency: 5 },
});
const org3 = await findOrCreateOrganization("batch-limit-org-3", user, {
batchQueueConcurrencyConfig: { processingConcurrency: 10 },
});
// Create 3 projects in each organization
const org1Project1 = await findOrCreateProject("batch-limit-project-1", org1, user.id);
const org1Project2 = await findOrCreateProject("batch-limit-project-2", org1, user.id);
const org1Project3 = await findOrCreateProject("batch-limit-project-3", org1, user.id);
const org2Project1 = await findOrCreateProject("batch-limit-project-1", org2, user.id);
const org2Project2 = await findOrCreateProject("batch-limit-project-2", org2, user.id);
const org2Project3 = await findOrCreateProject("batch-limit-project-3", org2, user.id);
const org3Project1 = await findOrCreateProject("batch-limit-project-1", org3, user.id);
const org3Project2 = await findOrCreateProject("batch-limit-project-2", org3, user.id);
const org3Project3 = await findOrCreateProject("batch-limit-project-3", org3, user.id);
console.log("tenants.json");
console.log(
JSON.stringify({
apiUrl: "http://localhost:3030",
tenants: [
{
id: org1Project1.project.externalRef,
secretKey: org1Project1.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org1Project2.project.externalRef,
secretKey: org1Project2.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org1Project3.project.externalRef,
secretKey: org1Project3.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org2Project1.project.externalRef,
secretKey: org2Project1.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org2Project2.project.externalRef,
secretKey: org2Project2.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org2Project3.project.externalRef,
secretKey: org2Project3.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org3Project1.project.externalRef,
secretKey: org3Project1.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org3Project2.project.externalRef,
secretKey: org3Project2.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
{
id: org3Project3.project.externalRef,
secretKey: org3Project3.environments.find((e) => e.type === "DEVELOPMENT")?.apiKey,
},
],
})
);
}
seed()
.catch((e) => {
console.error("❌ Seed failed:");
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
async function findOrCreateOrganization(
title: string,
user: User,
updates?: Prisma.OrganizationUpdateInput
) {
let organization = await prisma.organization.findFirst({
where: {
title: title,
members: {
some: {
userId: user.id,
},
},
},
});
if (!organization) {
console.log(`Creating organization: ${title}...`);
organization = await createOrganization({
title: title,
userId: user.id,
companySize: "1-10",
});
}
if (updates) {
organization = await prisma.organization.update({
where: { id: organization.id },
data: updates,
});
}
return organization;
}
async function findOrCreateProject(
name: string,
organization: Organization,
userId: string,
externalRef?: string
) {
let project = await prisma.project.findFirst({
where: {
name,
organizationId: organization.id,
},
});
if (!project) {
console.log(`Creating project: ${name}...`);
project = await createProject({
organizationSlug: organization.slug,
name,
userId,
version: "v3",
});
if (externalRef) {
project = await prisma.project.update({
where: { id: project.id },
data: { externalRef },
});
}
}
console.log(`✅ Project ready: ${project.name} (${project.externalRef})`);
// list environments for this project
const environments = await prisma.runtimeEnvironment.findMany({
where: { projectId: project.id },
select: {
slug: true,
type: true,
apiKey: true,
},
});
console.log(` Environments for ${project.name}:`);
for (const env of environments) {
console.log(` - ${env.type.toLowerCase()} (${env.slug}): ${env.apiKey}`);
}
return { project, environments };
}
async function ensureDefaultWorkerGroup() {
// Check if the feature flag already exists
const existingFlag = await prisma.featureFlag.findUnique({
where: { key: "defaultWorkerInstanceGroupId" },
});
if (existingFlag) {
console.log(`✅ Default worker instance group already configured`);
return;
}
// Check if a managed worker group already exists
let workerGroup = await prisma.workerInstanceGroup.findFirst({
where: { type: "MANAGED" },
});
if (!workerGroup) {
console.log("Creating default worker instance group...");
const { createHash, randomBytes } = await import("crypto");
const tokenValue = `tr_wgt_${randomBytes(20).toString("hex")}`;
const tokenHash = createHash("sha256").update(tokenValue).digest("hex");
const token = await prisma.workerGroupToken.create({
data: { tokenHash },
});
workerGroup = await prisma.workerInstanceGroup.create({
data: {
type: "MANAGED",
name: "local-dev",
masterQueue: "local-dev",
description: "Local development worker group",
tokenId: token.id,
},
});
console.log(`✅ Created worker instance group: ${workerGroup.name} (${workerGroup.id})`);
} else {
console.log(
`✅ Worker instance group already exists: ${workerGroup.name} (${workerGroup.id})`
);
}
// Set the feature flag
await prisma.featureFlag.upsert({
where: { key: "defaultWorkerInstanceGroupId" },
create: {
key: "defaultWorkerInstanceGroupId",
value: workerGroup.id,
},
update: {
value: workerGroup.id,
},
});
console.log(`✅ Set defaultWorkerInstanceGroupId feature flag`);
}