Fix types
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Michael Zhang 2023-04-18 20:07:51 -05:00
parent 97774d5852
commit b69b497cdd
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 24 additions and 25 deletions

View file

@ -1,4 +1,4 @@
import { Node, App, PrismaClient } from "@prisma/client"; import { Node, App, PrismaClient, Prisma } from "@prisma/client";
import { import {
FetchedNode, FetchedNode,
ICreateNodeRequest, ICreateNodeRequest,
@ -8,8 +8,11 @@ import {
IRemoveNodeRequest, IRemoveNodeRequest,
IUpdateNodeRequest, IUpdateNodeRequest,
} from "./types"; } from "./types";
import { inspect } from "util";
/**
* Server-side wrapper class for interfacing with the underlying data storage
* tool.
*/
export class Database { export class Database {
private prisma: PrismaClient; private prisma: PrismaClient;
@ -23,24 +26,21 @@ export class Database {
public async findManyNodes( public async findManyNodes(
request: IFindManyNodesRequest request: IFindManyNodesRequest
): Promise<FetchedNode[] | null> { ): Promise<FetchedNode[] | null> {
const query = { const select_keys = [];
take: request.take,
select: { id: true, label: true, metadata: true },
where: { metadata: { some: { OR: [] } } },
};
if (request.select_keys) { if (request.select_keys) {
for (const key of request.select_keys) { for (const key of request.select_keys) {
query.where.metadata.some.OR.push({ select_keys.push({
appKey: key.appKey, appKey: key.appKey,
appId: key.appId, appId: key.appId,
}); });
} }
} }
console.log("query", inspect(query, false, 10)); const nodes = await this.prisma.node.findMany({
take: request.take,
const nodes = await this.prisma.node.findMany(query); select: { id: true, label: true, metadata: true },
where: { metadata: { some: { OR: [...select_keys] } } },
});
return nodes; return nodes;
} }
@ -94,7 +94,6 @@ export class Database {
value: Buffer.from(value), value: Buffer.from(value),
}, },
}); });
console.log("Creating metadata keys", key, value, meta);
} }
} }
@ -102,7 +101,9 @@ export class Database {
}); });
} }
public async updateNode(request: IUpdateNodeRequest): Promise<Node | null> { public async updateNode(
request: IUpdateNodeRequest
): Promise<FetchedNode | null> {
const node = await this.prisma.$transaction(async (client) => { const node = await this.prisma.$transaction(async (client) => {
await client.node.update({ where: { id: request.id }, data: {} }); await client.node.update({ where: { id: request.id }, data: {} });
@ -138,7 +139,6 @@ export class Database {
await this.prisma.$transaction(async (client) => { await this.prisma.$transaction(async (client) => {
await this.prisma.node.delete({ where: { id: request.id } }); await this.prisma.node.delete({ where: { id: request.id } });
}); });
console.log("Removed", request);
} }
} }

View file

@ -2,20 +2,20 @@ import { For, batch, createEffect, createSignal, JSX } from "solid-js";
import { SetStoreFunction, Store, createStore } from "solid-js/store"; import { SetStoreFunction, Store, createStore } from "solid-js/store";
import { useRouteData } from "solid-start"; import { useRouteData } from "solid-start";
import { createServerAction$, createServerData$ } from "solid-start/server"; import { createServerAction$, createServerData$ } from "solid-start/server";
import { FetchedNode } from "~/core/types";
import { db, todosApp } from "~/db"; import { db, todosApp } from "~/db";
const retrieveMeta = (node, key) => { const retrieveMeta = (node: FetchedNode, key: string) => {
const meta = node.metadata.find( const meta = node.metadata.find(
(meta) => meta.appId == todosApp.id && meta.appKey == key (meta) => meta.appId == todosApp.id && meta.appKey == key
); );
if (!meta) return null; if (!meta) return null;
const metaValue = meta.value.toString(); const metaValue = meta.value.toString();
console.log("s", key, metaValue);
return JSON.parse(metaValue); return JSON.parse(metaValue);
}; };
const nodeToTodoItem = (node) => ({ const nodeToTodoItem = (node: FetchedNode) => ({
id: node.id, id: node.id,
title: retrieveMeta(node, "title"), title: retrieveMeta(node, "title"),
completed: retrieveMeta(node, "completed"), completed: retrieveMeta(node, "completed"),
@ -47,9 +47,10 @@ export function routeData() {
} }
export default function Todos() { export default function Todos() {
const ouais = useRouteData<typeof routeData>(); const getRouteData = useRouteData<typeof routeData>();
if (!ouais()) return <>Loading...</>; const data = getRouteData();
const { initTodos } = ouais(); if (!data) return <>Loading...</>;
const { initTodos } = data;
const [newTitle, setTitle] = createSignal(""); const [newTitle, setTitle] = createSignal("");
const [todos, setTodos] = createTodoStore<TodoItem[]>("todos", initTodos); const [todos, setTodos] = createTodoStore<TodoItem[]>("todos", initTodos);
@ -69,7 +70,6 @@ export default function Todos() {
label: `todo-${title}`, label: `todo-${title}`,
metadata_keys, metadata_keys,
}); });
console.log("Created node", node);
return node; return node;
}, },
@ -121,7 +121,7 @@ export default function Todos() {
setMeta("completed", completed); setMeta("completed", completed);
const node = await db.updateNode({ id, metadata_keys }); const node = await db.updateNode({ id, metadata_keys });
console.log("Updated node", node); if (!node) return;
return nodeToTodoItem(node); return nodeToTodoItem(node);
} }
@ -166,7 +166,6 @@ export default function Todos() {
}); });
await removeTodoAction({ id: todo.id }); await removeTodoAction({ id: todo.id });
console.log("Removed", todo);
// TODO: Is there a race condition here with the index? // TODO: Is there a race condition here with the index?
setTodos((t) => removeIndex(t, idx)); setTodos((t) => removeIndex(t, idx));
}; };
@ -233,7 +232,6 @@ function createTodoStore<T extends object>(
); );
createEffect(() => { createEffect(() => {
console.log("Calling local storage");
// localStorage.setItem(name, JSON.stringify(state)); // localStorage.setItem(name, JSON.stringify(state));
}); });

View file

@ -7,6 +7,7 @@
"moduleResolution": "node", "moduleResolution": "node",
"jsxImportSource": "solid-js", "jsxImportSource": "solid-js",
"jsx": "preserve", "jsx": "preserve",
"noEmit": true,
"strict": true, "strict": true,
"types": ["solid-start/env"], "types": ["solid-start/env"],
"baseUrl": "./", "baseUrl": "./",