robodev 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/api/launch.ts +7 -5
- package/template/api/planets.ts +17 -22
- package/template/api/rockets.ts +10 -9
- package/template/database.ts +19 -16
- package/template/package.json +2 -2
package/package.json
CHANGED
package/template/api/launch.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rockets } from "../database";
|
|
2
|
+
import { defineApi, eq, z } from "robodev-lib";
|
|
2
3
|
|
|
3
4
|
const Rocket = z.object({
|
|
4
5
|
id: z.string(),
|
|
@@ -13,10 +14,11 @@ export const post = defineApi({
|
|
|
13
14
|
}),
|
|
14
15
|
response: Rocket,
|
|
15
16
|
handler: async ({ db, body }) => {
|
|
16
|
-
const rows = await db
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const rows = await db
|
|
18
|
+
.update(rockets)
|
|
19
|
+
.set({ launched: true })
|
|
20
|
+
.where(eq(rockets.id, body.id))
|
|
21
|
+
.returning();
|
|
20
22
|
const row = rows[0];
|
|
21
23
|
if (!row) {
|
|
22
24
|
throw new Error("Rocket not found");
|
package/template/api/planets.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { planets, rockets } from "../database";
|
|
1
2
|
import { defineApi, z } from "robodev-lib";
|
|
2
3
|
|
|
3
4
|
const EARTH = "11111111-1111-1111-1111-111111111111";
|
|
@@ -16,29 +17,23 @@ const Planet = z.object({
|
|
|
16
17
|
export const get = defineApi({
|
|
17
18
|
response: z.array(Planet),
|
|
18
19
|
handler: async ({ db }) => {
|
|
19
|
-
const existing = await db.from(
|
|
20
|
+
const existing = await db.select().from(planets).limit(1);
|
|
20
21
|
if (existing.length === 0) {
|
|
21
22
|
try {
|
|
22
|
-
await db.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
await db.from("rockets").create({
|
|
32
|
-
data: { id: VOYAGER, name: "Voyager", destinationId: MARS, launched: false },
|
|
33
|
-
});
|
|
34
|
-
await db.from("rockets").create({
|
|
35
|
-
data: { id: ODYSSEY, name: "Odyssey", destinationId: KEPLER, launched: true },
|
|
36
|
-
});
|
|
23
|
+
await db.insert(planets).values([
|
|
24
|
+
{ id: EARTH, name: "Earth", climate: "temperate", moons: 1 },
|
|
25
|
+
{ id: MARS, name: "Mars", climate: "arid", moons: 2 },
|
|
26
|
+
{ id: KEPLER, name: "Kepler-452b", climate: "unknown", moons: 0 },
|
|
27
|
+
]);
|
|
28
|
+
await db.insert(rockets).values([
|
|
29
|
+
{ id: VOYAGER, name: "Voyager", destinationId: MARS, launched: false },
|
|
30
|
+
{ id: ODYSSEY, name: "Odyssey", destinationId: KEPLER, launched: true },
|
|
31
|
+
]);
|
|
37
32
|
} catch {
|
|
38
33
|
// First request won the seed race.
|
|
39
34
|
}
|
|
40
35
|
}
|
|
41
|
-
const rows = await db.
|
|
36
|
+
const rows = await db.select().from(planets);
|
|
42
37
|
return Planet.array().parse(rows);
|
|
43
38
|
},
|
|
44
39
|
});
|
|
@@ -51,14 +46,14 @@ export const post = defineApi({
|
|
|
51
46
|
}),
|
|
52
47
|
response: Planet,
|
|
53
48
|
handler: async ({ db, body }) => {
|
|
54
|
-
const row = await db
|
|
55
|
-
|
|
56
|
-
|
|
49
|
+
const [row] = await db
|
|
50
|
+
.insert(planets)
|
|
51
|
+
.values({
|
|
57
52
|
name: body.name,
|
|
58
53
|
climate: body.climate,
|
|
59
54
|
moons: body.moons,
|
|
60
|
-
}
|
|
61
|
-
|
|
55
|
+
})
|
|
56
|
+
.returning();
|
|
62
57
|
return Planet.parse(row);
|
|
63
58
|
},
|
|
64
59
|
});
|
package/template/api/rockets.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rockets } from "../database";
|
|
2
|
+
import { defineApi, eq, z } from "robodev-lib";
|
|
2
3
|
|
|
3
4
|
const Rocket = z.object({
|
|
4
5
|
id: z.string(),
|
|
@@ -13,9 +14,9 @@ export const get = defineApi({
|
|
|
13
14
|
}),
|
|
14
15
|
response: z.array(Rocket),
|
|
15
16
|
handler: async ({ db, query }) => {
|
|
16
|
-
const rows =
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const rows = query.destinationId
|
|
18
|
+
? await db.select().from(rockets).where(eq(rockets.destinationId, query.destinationId))
|
|
19
|
+
: await db.select().from(rockets);
|
|
19
20
|
return Rocket.array().parse(rows);
|
|
20
21
|
},
|
|
21
22
|
});
|
|
@@ -28,14 +29,14 @@ export const post = defineApi({
|
|
|
28
29
|
}),
|
|
29
30
|
response: Rocket,
|
|
30
31
|
handler: async ({ db, body }) => {
|
|
31
|
-
const row = await db
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const [row] = await db
|
|
33
|
+
.insert(rockets)
|
|
34
|
+
.values({
|
|
34
35
|
name: body.name,
|
|
35
36
|
destinationId: body.destinationId,
|
|
36
37
|
launched: body.launched ?? false,
|
|
37
|
-
}
|
|
38
|
-
|
|
38
|
+
})
|
|
39
|
+
.returning();
|
|
39
40
|
return Rocket.parse(row);
|
|
40
41
|
},
|
|
41
42
|
});
|
package/template/database.ts
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { boolean, defineDatabase, integer,
|
|
1
|
+
import { boolean, defineDatabase, integer, pgTable, text, timestamp, uuid } from "robodev-lib";
|
|
2
|
+
|
|
3
|
+
export const planets = pgTable("planets", {
|
|
4
|
+
id: uuid("id").primaryKey().defaultRandom(),
|
|
5
|
+
name: text("name").notNull(),
|
|
6
|
+
climate: text("climate").notNull(),
|
|
7
|
+
moons: integer("moons").default(0),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export const rockets = pgTable("rockets", {
|
|
11
|
+
id: uuid("id").primaryKey().defaultRandom(),
|
|
12
|
+
name: text("name").notNull(),
|
|
13
|
+
destinationId: uuid("destinationId")
|
|
14
|
+
.notNull()
|
|
15
|
+
.references(() => planets.id, { onDelete: "cascade" }),
|
|
16
|
+
launched: boolean("launched").default(false),
|
|
17
|
+
createdAt: timestamp("createdAt", { withTimezone: true }).defaultNow(),
|
|
18
|
+
});
|
|
2
19
|
|
|
3
20
|
export default defineDatabase({
|
|
4
21
|
name: "space",
|
|
5
|
-
tables: {
|
|
6
|
-
planets: table({
|
|
7
|
-
id: uuid({ primaryKey: true }),
|
|
8
|
-
name: text({ notNull: true }),
|
|
9
|
-
climate: text({ notNull: true }),
|
|
10
|
-
moons: integer({ default: 0 }),
|
|
11
|
-
}),
|
|
12
|
-
rockets: table({
|
|
13
|
-
id: uuid({ primaryKey: true }),
|
|
14
|
-
name: text({ notNull: true }),
|
|
15
|
-
destinationId: uuid({ notNull: true }),
|
|
16
|
-
launched: boolean({ default: false }),
|
|
17
|
-
createdAt: timestamp({ default: "now()" }),
|
|
18
|
-
}),
|
|
19
|
-
},
|
|
22
|
+
tables: { planets, rockets },
|
|
20
23
|
});
|
package/template/package.json
CHANGED
|
@@ -10,13 +10,13 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"react": "^18.3.1",
|
|
12
12
|
"react-dom": "^18.3.1",
|
|
13
|
-
"robodev-lib": "^0.
|
|
13
|
+
"robodev-lib": "^0.2.0"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/react": "^18.3.24",
|
|
17
17
|
"@types/react-dom": "^18.3.7",
|
|
18
18
|
"@vitejs/plugin-react": "^4.7.0",
|
|
19
|
-
"robodev": "^0.
|
|
19
|
+
"robodev": "^0.2.0",
|
|
20
20
|
"typescript": "^5.9.2",
|
|
21
21
|
"vite": "^6.3.5"
|
|
22
22
|
}
|