62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { Node, PrismaClient } from "@prisma/client";
|
|
|
|
interface ICreateNodeRequest {
|
|
/**
|
|
* A label for humans. This is not used by the database in any way, it's just
|
|
* something to print when debugging.
|
|
*/
|
|
label: string;
|
|
|
|
/**
|
|
* A mapping from labels to node creation requests.
|
|
*/
|
|
other_nodes: Map<string, ICreateNodeRequest>;
|
|
|
|
/**
|
|
* A set of edges, using the IDs in auxiliary_nodes.
|
|
*/
|
|
edges: Set<ICreateEdgeRequest>;
|
|
}
|
|
|
|
interface ICreateEdgeRequest {
|
|
label: string;
|
|
from_node: string;
|
|
to_node: string;
|
|
metadata: unknown;
|
|
}
|
|
|
|
export default class Database {
|
|
public static async createNode(request: ICreateNodeRequest): Promise<Node> {
|
|
// TODO: Calculate the interfaces that must be implemented
|
|
// - Use the interface application rules
|
|
// - Insert them into the database
|
|
|
|
// TODO: Figure out if there are any auxiliary nodes that must be created
|
|
// - Check to see if definitions for these are provided in the NodeCreationRequest
|
|
// - Create these auxiliary nodes
|
|
|
|
// TODO: Run pre-creation hooks
|
|
|
|
// TODO: Create all the nodes according to their requests
|
|
|
|
// TODO: Run all validations on all of the nodes
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
return await prisma.$transaction(async (client) => {
|
|
const createdNode = client.node.create({
|
|
data: { label: request.label },
|
|
});
|
|
|
|
return createdNode;
|
|
});
|
|
}
|
|
}
|
|
|
|
export class NodeWrapper {
|
|
constructor(private node: Node) {}
|
|
|
|
public id(): string {
|
|
return this.node.id;
|
|
}
|
|
}
|