-- CreateTable
CREATE TABLE "Node" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "label" TEXT
);

-- CreateTable
CREATE TABLE "Interface" (
    "id" TEXT NOT NULL PRIMARY KEY
);

-- CreateTable
CREATE TABLE "Edge" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "label" TEXT,
    "appId" TEXT NOT NULL,
    "appKey" TEXT NOT NULL,
    "fromId" TEXT NOT NULL,
    "toId" TEXT NOT NULL,
    CONSTRAINT "Edge_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "Edge_fromId_fkey" FOREIGN KEY ("fromId") REFERENCES "Node" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "Edge_toId_fkey" FOREIGN KEY ("toId") REFERENCES "Node" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "NodeMeta" (
    "nodeId" TEXT NOT NULL,
    "appId" TEXT NOT NULL,
    "appKey" TEXT NOT NULL,
    "value" BLOB NOT NULL,

    PRIMARY KEY ("nodeId", "appId", "appKey"),
    CONSTRAINT "NodeMeta_nodeId_fkey" FOREIGN KEY ("nodeId") REFERENCES "Node" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "NodeMeta_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "NodeImplements" (
    "nodeId" TEXT NOT NULL,
    "interfaceId" TEXT NOT NULL,

    PRIMARY KEY ("nodeId", "interfaceId"),
    CONSTRAINT "NodeImplements_nodeId_fkey" FOREIGN KEY ("nodeId") REFERENCES "Node" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "NodeImplements_interfaceId_fkey" FOREIGN KEY ("interfaceId") REFERENCES "Interface" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "App" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "localName" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "installed" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "Patterns" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "pattern" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "appId" TEXT NOT NULL,
    "functionName" TEXT NOT NULL,
    CONSTRAINT "Patterns_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE INDEX "Edge_appId_appKey_idx" ON "Edge"("appId", "appKey");

-- CreateIndex
CREATE UNIQUE INDEX "Edge_fromId_toId_key" ON "Edge"("fromId", "toId");

-- CreateIndex
CREATE INDEX "NodeMeta_appId_appKey_idx" ON "NodeMeta"("appId", "appKey");

-- CreateIndex
CREATE UNIQUE INDEX "App_localName_key" ON "App"("localName");
