feat: add some more versining stuff

This commit is contained in:
max_richter 2024-12-20 14:06:33 +01:00
parent 4ca36b324b
commit a70e8195a2
3 changed files with 108 additions and 27 deletions

File diff suppressed because one or more lines are too long

View File

@ -28,7 +28,7 @@ const getUserNodesRoute = createRoute({
path: "/{user}.json",
request: {
params: z.object({
user: SingleParam("user"),
user: SingleParam("user").optional(),
}),
},
responses: {
@ -44,7 +44,9 @@ const getUserNodesRoute = createRoute({
});
nodeRouter.openapi(getUserNodesRoute, async (c) => {
const userId = c.req.param("user.json").replace(/\.json$/, "");
const nodes = await service.getNodeDefinitionsByUser(userId);
const nodes = await service.getNodeDefinitionsByUser(
userId,
);
return c.json(nodes);
});
@ -80,7 +82,11 @@ const getNodeDefinitionRoute = createRoute({
method: "get",
path: "/{user}/{system}/{nodeId}.json",
request: {
params: ParamsSchema,
params: z.object({
user: SingleParam("user"),
system: SingleParam("system"),
nodeId: SingleParam("nodeId").optional(),
}),
},
responses: {
200: {
@ -94,12 +100,13 @@ const getNodeDefinitionRoute = createRoute({
},
});
nodeRouter.openapi(getNodeDefinitionRoute, async (c) => {
const { user, system, nodeId } = c.req.valid("param");
const { user, system } = c.req.valid("param");
const nodeId = c.req.param("nodeId.json").replace(/\.json$/, "");
const node = await service.getNodeDefinitionById(
user,
system,
nodeId.replace(/\.json$/, ""),
nodeId,
);
if (!node) {
@ -140,17 +147,22 @@ nodeRouter.openapi(getNodeWasmRoute, async (c) => {
return c.body(wasmContent);
});
const getNodeVersionRoute = createRoute({
const getNodeVersionWasmRoute = createRoute({
method: "get",
path: "/{user}/{system}/{nodeId}@{hash}.json",
path: "/{user}/{system}/{nodeId}@{hash}.wasm",
request: {
params: ParamsSchema,
params: z.object({
user: SingleParam("user"),
system: SingleParam("system"),
nodeId: SingleParam("nodeId"),
hash: SingleParam("hash"),
}),
},
responses: {
200: {
content: {
"application/json": {
schema: NodeDefinitionSchema,
"application/wasm": {
schema: z.any(),
},
},
description: "Create a single node",
@ -158,17 +170,17 @@ const getNodeVersionRoute = createRoute({
},
});
nodeRouter.openapi(getNodeVersionRoute, async (c) => {
const { user, system, nodeId } = c.req.valid("param");
nodeRouter.openapi(getNodeVersionWasmRoute, async (c) => {
const { user, system, nodeId, hash } = c.req.valid("param");
const nodes = await service.getNodeVersions(user, system, nodeId);
const nodes = await service.getNodeVersionWasm(user, system, nodeId, hash);
return c.json(nodes);
});
const getNodeVersionsRoute = createRoute({
const getNodeVersionRoute = createRoute({
method: "get",
path: "/{user}/{system}/{nodeId}/versions.json",
path: "/{user}/{system}/{nodeId}@{hash}.json",
request: {
params: z.object({
user: SingleParam("user"),
@ -189,10 +201,40 @@ const getNodeVersionsRoute = createRoute({
},
});
nodeRouter.openapi(getNodeVersionsRoute, async (c) => {
nodeRouter.openapi(getNodeVersionRoute, async (c) => {
const { user, system, nodeId, hash } = c.req.valid("param");
const node = await service.getNodeVersion(user, system, nodeId, hash);
const nodes = await service.getNodeVersion(user, system, nodeId, hash);
return c.json(nodes);
});
const getNodeVersionsRoute = createRoute({
method: "get",
path: "/{user}/{system}/{nodeId}/versions.json",
request: {
params: z.object({
user: SingleParam("user"),
system: SingleParam("system"),
nodeId: SingleParam("nodeId"),
}),
},
responses: {
200: {
content: {
"application/json": {
schema: z.array(NodeDefinitionSchema),
},
},
description: "Create a single node",
},
},
});
nodeRouter.openapi(getNodeVersionsRoute, async (c) => {
const { user, system, nodeId } = c.req.valid("param");
const node = await service.getNodeVersions(user, system, nodeId);
return c.json(node);
});

View File

@ -83,7 +83,10 @@ export async function createNode(
}
export async function getNodeDefinitionsByUser(userName: string) {
const nodes = await db.select({ definition: nodeTable.definition }).from(
const nodes = await db.select({
definition: nodeTable.definition,
hash: nodeTable.hash,
}).from(
nodeTable,
)
.where(
@ -92,7 +95,10 @@ export async function getNodeDefinitionsByUser(userName: string) {
),
);
return nodes.map((n) => n.definition);
return nodes.map((n) => ({
...n.definition,
id: n.definition.id + "@" + n.hash,
}));
}
export async function getNodesBySystem(
@ -102,7 +108,7 @@ export async function getNodesBySystem(
const nodes = await db
.selectDistinctOn(
[nodeTable.userId, nodeTable.systemId, nodeTable.nodeId],
{ definition: nodeTable.definition },
{ definition: nodeTable.definition, hash: nodeTable.hash },
)
.from(nodeTable)
.where(
@ -110,9 +116,14 @@ export async function getNodesBySystem(
).orderBy(nodeTable.userId, nodeTable.systemId, nodeTable.nodeId);
const definitions = nodes
.map((node) => NodeDefinitionSchema.safeParse(node.definition))
.filter((v) => v.success)
.map((v) => v.data);
.map((node) =>
[NodeDefinitionSchema.safeParse(node.definition), node.hash] as const
)
.filter(([v]) => v.success)
.map(([v, hash]) => ({
...v.data,
id: v?.data?.id + "@" + hash,
}));
return definitions;
}
@ -147,7 +158,10 @@ export async function getNodeDefinitionById(
systemId: string,
nodeId: string,
) {
const node = await db.select({ definition: nodeTable.definition }).from(
const node = await db.select({
definition: nodeTable.definition,
hash: nodeTable.hash,
}).from(
nodeTable,
).where(
and(
@ -169,7 +183,7 @@ export async function getNodeDefinitionById(
throw new Error("Invalid definition");
}
return definition.data;
return { ...definition.data, id: definition.data.id + "@" + node[0].hash };
}
export async function getNodeVersions(
@ -205,7 +219,6 @@ export async function getNodeVersion(
) {
const nodes = await db.select({
definition: nodeTable.definition,
hash: nodeTable.hash,
}).from(
nodeTable,
).where(
@ -223,3 +236,29 @@ export async function getNodeVersion(
return nodes[0].definition;
}
export async function getNodeVersionWasm(
user: string,
system: string,
nodeId: string,
hash: string,
) {
const node = await db.select({
content: nodeTable.content,
}).from(
nodeTable,
).where(
and(
eq(nodeTable.userId, user),
eq(nodeTable.systemId, system),
eq(nodeTable.nodeId, nodeId),
eq(nodeTable.hash, hash),
),
).limit(1);
if (node.length === 0) {
throw new Error("Node not found");
}
return node[0].content;
}