Skip to content

Commit 8d02612

Browse files
committed
feat: add seed database migration for pets, categories and superuser seeding
1 parent c55e37b commit 8d02612

File tree

1 file changed

+197
-0
lines changed

1 file changed

+197
-0
lines changed
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/// <reference path="../pb_data/types.d.ts" />
2+
migrate(
3+
(app) => {
4+
// Add superuser
5+
let superusers = app.findCollectionByNameOrId("_superusers");
6+
let record = new Record(superusers, {
7+
email: $os.getenv("POCKETBASE_ADMIN_EMAIL") || "test@example.com",
8+
password: $os.getenv("POCKETBASE_ADMIN_PASSWORD") || "test",
9+
});
10+
app.save(record);
11+
12+
// Add categories
13+
let categoryIds = {};
14+
let categories = app.findCollectionByNameOrId("categories");
15+
for (let name of CATEGORIES) {
16+
let record = new Record(categories, {
17+
name: name,
18+
});
19+
app.save(record);
20+
categoryIds[name] = record.id;
21+
}
22+
23+
// Add Dogs
24+
let pets = app.findCollectionByNameOrId("pets");
25+
for (let dogName of DOG_NAMES) {
26+
let record = new Record(pets, {
27+
name: dogName,
28+
category: categoryIds["Dog"],
29+
status: "available",
30+
});
31+
app.save(record);
32+
}
33+
34+
// Add Cats
35+
for (let catName of CAT_NAMES) {
36+
let record = new Record(pets, {
37+
name: catName,
38+
category: categoryIds["Cat"],
39+
status: "available",
40+
});
41+
app.save(record);
42+
}
43+
},
44+
(app) => {
45+
// Remove cats
46+
let pets = app.findCollectionByNameOrId("pets");
47+
let catRecords = app.findRecordsByFilter(
48+
pets,
49+
CAT_NAMES.map((name) => `name = "${name}"`).join("||")
50+
);
51+
for (let record of catRecords) {
52+
app.delete(record);
53+
}
54+
55+
// Remove dogs
56+
let dogRecords = app.findRecordsByFilter(
57+
pets,
58+
DOG_NAMES.map((name) => `name = "${name}"`).join("||")
59+
);
60+
for (let record of dogRecords) {
61+
app.delete(record);
62+
}
63+
64+
// Remove categories
65+
let categories = app.findCollectionByNameOrId("categories");
66+
let categoryRecords = app.findRecordsByFilter(
67+
categories,
68+
CATEGORIES.map((name) => `name = "${name}"`).join("||")
69+
);
70+
for (let record of categoryRecords) {
71+
app.delete(record);
72+
}
73+
74+
// Remove superuser
75+
let record = app.findAuthRecordByEmail(
76+
"_superusers",
77+
$os.getenv("POCKETBASE_ADMIN_EMAIL") || "test@example.com"
78+
);
79+
app.delete(record);
80+
}
81+
);
82+
83+
const CATEGORIES = [
84+
"Cat",
85+
"Dog",
86+
"Bird",
87+
"Fish",
88+
"Reptile",
89+
"Small Mammal",
90+
"Farm Animal",
91+
];
92+
93+
const DOG_NAMES = [
94+
"Apollo",
95+
"Bandit",
96+
"Bear",
97+
"Bentley",
98+
"Buster",
99+
"Buddy",
100+
"Charlie",
101+
"Cody",
102+
"Cooper",
103+
"Diesel",
104+
"Duke",
105+
"Finn",
106+
"Gunner",
107+
"Gus",
108+
"Hunter",
109+
"Jake",
110+
"Leo",
111+
"Max",
112+
"Milo",
113+
"Murphy",
114+
"Oliver",
115+
"Oscar",
116+
"Rex",
117+
"Riley",
118+
"Rocky",
119+
"Samson",
120+
"Thor",
121+
"Tucker",
122+
"Winston",
123+
"Zeus",
124+
"Ziggy",
125+
"Ace",
126+
"Archie",
127+
"Bailey",
128+
"Beau",
129+
"Blue",
130+
"Bruno",
131+
"Cash",
132+
"Chase",
133+
"Dexter",
134+
"Frankie",
135+
"George",
136+
"Hank",
137+
"Jackson",
138+
"Jasper",
139+
"Joey",
140+
"Louie",
141+
"Lucky",
142+
"Moose",
143+
"Ollie",
144+
];
145+
146+
const CAT_NAMES = [
147+
"Angel",
148+
"Bella",
149+
"Boots",
150+
"Callie",
151+
"Chloe",
152+
"Cleo",
153+
"Coco",
154+
"Daisy",
155+
"Felix",
156+
"Finn",
157+
"Ginger",
158+
"Gracie",
159+
"Jack",
160+
"Jasper",
161+
"Kitty",
162+
"Leo",
163+
"Lily",
164+
"Loki",
165+
"Luna",
166+
"Maggie",
167+
"Max",
168+
"Mia",
169+
"Milo",
170+
"Mimi",
171+
"Missy",
172+
"Misty",
173+
"Mittens",
174+
"Mochi",
175+
"Nala",
176+
"Oliver",
177+
"Oreo",
178+
"Oscar",
179+
"Peanut",
180+
"Pumpkin",
181+
"Rosie",
182+
"Ruby",
183+
"Sadie",
184+
"Salem",
185+
"Sammy",
186+
"Shadow",
187+
"Simba",
188+
"Smokey",
189+
"Socks",
190+
"Sophie",
191+
"Stella",
192+
"Tiger",
193+
"Toby",
194+
"Willow",
195+
"Ziggy",
196+
"Zoe",
197+
];

0 commit comments

Comments
 (0)