Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
wip refactoring dev server to level from sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Jul 6, 2024
1 parent 7f09aa4 commit f25c239
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 34 deletions.
3 changes: 0 additions & 3 deletions dev-server-api/routes/api/dev_package_examples/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { withWinterSpec } from "src/with-winter-spec"
import { NotFoundError } from "edgespec/middleware"
import { z } from "zod"

export default withWinterSpec({
Expand All @@ -23,8 +22,6 @@ export default withWinterSpec({
auth: "none",
})(async (req, ctx) => {
const tscircuit_soup = req.jsonBody.tscircuit_soup
? JSON.stringify(req.jsonBody.tscircuit_soup)
: undefined

const existingDevPackageExample = await ctx.db.find("dev_package_example", {
file_path: req.jsonBody.file_path,
Expand Down
31 changes: 11 additions & 20 deletions dev-server-api/routes/api/dev_package_examples/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,21 @@ export default withWinterSpec({
.datetime()
.nullable()
.default(null),
last_updated_at: z.string().datetime(),
last_updated_at: z.string().datetime().nullable(),
}),
}),
auth: "none",
})(async (req, ctx) => {
const dev_package_example = await ctx.db.get(
"dev_package_example",
req.commonParams.dev_package_example_id
)

if (!dev_package_example) {
throw new NotFoundError("Package not found")
}

return ctx.json({
dev_package_example: await ctx.db
.selectFrom("dev_package_example")
.selectAll()
.where(
"dev_package_example_id",
"=",
req.commonParams.dev_package_example_id
)
.executeTakeFirstOrThrow((e) => {
throw new NotFoundError("Package not found")
})
.then((r) => ({
...r,
is_loading: r.is_loading === 1,
tscircuit_soup: JSON.parse(r.tscircuit_soup),
completed_edit_events: r.completed_edit_events
? JSON.parse(r.completed_edit_events)
: null,
})),
dev_package_example,
})
})
9 changes: 1 addition & 8 deletions dev-server-api/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { withWinterSpec } from "../src/with-winter-spec"
import { z } from "zod"
import staticRoutes from "../static-routes"

export default withWinterSpec({
methods: ["GET"],
auth: "none",
})(async (req, ctx) => {
return new Response(
`<html><body>This is the dev server API <a href="/api/db/download">view database</a><br/><br/>${Object.entries(
staticRoutes
)
.map(
([routePath]) => `<div><a href="${routePath}">${routePath}</a></div>`
)
.join("")}</body></html>`,
`<html><body>This is the dev server API <a href="/api/db/download">view database</a></body></html>`,
{
headers: {
"content-type": "text/html",
Expand Down
2 changes: 1 addition & 1 deletion dev-server-api/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const DevPackageExampleSchema = z.object({
file_path: z.string(),
export_name: nullableText(),
tscircuit_soup: z.any().nullable(), // Using any for JSON type
completed_edit_events: z.any().nullable(), // Using any for JSON type
completed_edit_events: z.array(z.any()).nullable().default([]), // Using any for JSON type
error: nullableText(),
is_loading: z.boolean(),
soup_last_updated_at: nullableText(),
Expand Down
2 changes: 1 addition & 1 deletion dev-server-api/src/db/zod-level-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class ZodLevelDatabase {

async get<K extends keyof DBSchemaType>(
collection: K,
id: string
id: string | number
): Promise<DBSchemaType[K] | null> {
const key = `${collection}:${id}`
const data = await this.db.get(key)
Expand Down
6 changes: 5 additions & 1 deletion dev-server-api/static-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// import { WinterSpecRouteMap } from "@winterspec/types"

const routeMap = {
"/api/db/download": (await import('routes/api/db/download.ts')).default,
"/api/dev_package_examples/create": (await import('routes/api/dev_package_examples/create.ts')).default,
"/api/dev_package_examples/get": (await import('routes/api/dev_package_examples/get.ts')).default,
"/api/dev_package_examples/list": (await import('routes/api/dev_package_examples/list.ts')).default,
Expand All @@ -14,7 +15,10 @@ const routeMap = {
"/api/export_requests/list": (await import('routes/api/export_requests/list.ts')).default,
"/api/export_requests/update": (await import('routes/api/export_requests/update.ts')).default,
"/api/health": (await import('routes/api/health.ts')).default,
"/health": (await import('routes/health.ts')).default
"/api/package_info/create": (await import('routes/api/package_info/create.ts')).default,
"/api/package_info/get": (await import('routes/api/package_info/get.ts')).default,
"/health": (await import('routes/health.ts')).default,
"/": (await import('routes/index.ts')).default
}

export default routeMap
26 changes: 26 additions & 0 deletions dev-server-api/tests/routes/dev_package_examples/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { it, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-server"

it("POST /api/dev_package_examples/create", async () => {
const { axios } = await getTestFixture()

await axios
.post("/api/dev_package_examples/create", {
file_path: "examples/basic-resistor.tsx",
export_name: "default",
tscircuit_soup: [],
is_loading: true,
})
.then((r) => r.data)

const res = await axios
.post("/api/dev_package_examples/get", {
dev_package_example_id: 1,
})
.then((r) => r.data)

console.log(res.dev_package_example)
expect(res.dev_package_example.file_path).toEqual(
"examples/basic-resistor.tsx"
)
})

0 comments on commit f25c239

Please sign in to comment.