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

View File

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