mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-08-28 05:25:33 +00:00
db schema in packages
This commit is contained in:
parent
6e1d53e28a
commit
241276be58
36 changed files with 18629 additions and 23728 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { drizzle } from "drizzle-orm/d1";
|
||||
import { Env } from "../types";
|
||||
|
||||
import * as schema from "./schema";
|
||||
import * as schema from "@repo/db/schema";
|
||||
|
||||
export const database = (env: Env) =>
|
||||
drizzle(env.DATABASE, { schema, logger: true });
|
||||
|
|
|
|||
|
|
@ -1,93 +0,0 @@
|
|||
import {
|
||||
index,
|
||||
int,
|
||||
primaryKey,
|
||||
sqliteTableCreator,
|
||||
text,
|
||||
integer,
|
||||
} from "drizzle-orm/sqlite-core";
|
||||
|
||||
export const createTable = sqliteTableCreator((name) => `${name}`);
|
||||
|
||||
export const users = createTable(
|
||||
"user",
|
||||
{
|
||||
id: text("id")
|
||||
.primaryKey()
|
||||
.$defaultFn(() => crypto.randomUUID()),
|
||||
name: text("name"),
|
||||
email: text("email").notNull(),
|
||||
emailVerified: integer("emailVerified", { mode: "timestamp_ms" }),
|
||||
image: text("image"),
|
||||
telegramId: text("telegramId"),
|
||||
hasOnboarded: integer("hasOnboarded", { mode: "boolean" }).default(false),
|
||||
},
|
||||
(user) => ({
|
||||
emailIdx: index("users_email_idx").on(user.email),
|
||||
telegramIdx: index("users_telegram_idx").on(user.telegramId),
|
||||
idIdx: index("users_id_idx").on(user.id),
|
||||
}),
|
||||
);
|
||||
|
||||
export type User = typeof users.$inferSelect;
|
||||
|
||||
export const storedContent = createTable(
|
||||
"storedContent",
|
||||
{
|
||||
id: integer("id").notNull().primaryKey({ autoIncrement: true }),
|
||||
content: text("content").notNull(),
|
||||
title: text("title", { length: 255 }),
|
||||
description: text("description", { length: 255 }),
|
||||
url: text("url").notNull(),
|
||||
savedAt: int("savedAt", { mode: "timestamp" }).notNull(),
|
||||
baseUrl: text("baseUrl", { length: 255 }).unique(),
|
||||
ogImage: text("ogImage", { length: 255 }),
|
||||
type: text("type").default("page"),
|
||||
image: text("image", { length: 255 }),
|
||||
userId: text("user").references(() => users.id, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
noteId: integer("noteId"),
|
||||
},
|
||||
(sc) => ({
|
||||
urlIdx: index("storedContent_url_idx").on(sc.url),
|
||||
savedAtIdx: index("storedContent_savedAt_idx").on(sc.savedAt),
|
||||
titleInx: index("storedContent_title_idx").on(sc.title),
|
||||
userIdx: index("storedContent_user_idx").on(sc.userId),
|
||||
}),
|
||||
);
|
||||
|
||||
export type Content = typeof storedContent.$inferSelect;
|
||||
|
||||
|
||||
export const contentToSpace = createTable(
|
||||
"contentToSpace",
|
||||
{
|
||||
contentId: integer("contentId")
|
||||
.notNull()
|
||||
.references(() => storedContent.id, { onDelete: "cascade" }),
|
||||
spaceId: integer("spaceId")
|
||||
.notNull()
|
||||
.references(() => space.id, { onDelete: "cascade" }),
|
||||
},
|
||||
(cts) => ({
|
||||
compoundKey: primaryKey({ columns: [cts.contentId, cts.spaceId] }),
|
||||
}),
|
||||
);
|
||||
|
||||
export const space = createTable(
|
||||
"space",
|
||||
{
|
||||
id: integer("id").notNull().primaryKey({ autoIncrement: true }),
|
||||
name: text("name").notNull().unique().default("none"),
|
||||
user: text("user", { length: 255 }).references(() => users.id, {
|
||||
onDelete: "cascade",
|
||||
}),
|
||||
createdAt: int("createdAt", { mode: "timestamp" }).notNull(),
|
||||
numItems: integer("numItems").notNull().default(0),
|
||||
},
|
||||
(space) => ({
|
||||
nameIdx: index("spaces_name_idx").on(space.name),
|
||||
userIdx: index("spaces_user_idx").on(space.user),
|
||||
}),
|
||||
);
|
||||
|
|
@ -33,13 +33,6 @@ import { queue } from "./queueConsumer";
|
|||
|
||||
const app = new Hono<{ Bindings: Env }>();
|
||||
|
||||
app.get(
|
||||
"/ui",
|
||||
swaggerUI({
|
||||
url: "/doc",
|
||||
}),
|
||||
);
|
||||
|
||||
// ------- MIDDLEWARES -------
|
||||
app.use("*", poweredBy());
|
||||
app.use("*", timing());
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import { z } from "zod";
|
|||
import { Metadata } from "./utils/get-metadata";
|
||||
import { BaseError } from "../errors/baseError";
|
||||
import { database } from "../db";
|
||||
import { storedContent, space, contentToSpace } from "../db/schema";
|
||||
import { storedContent, space, contentToSpace } from "@repo/db/schema";
|
||||
import { and, eq, inArray, sql } from "drizzle-orm";
|
||||
|
||||
class VectorInsertError extends BaseError {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ main = "src/index.ts"
|
|||
compatibility_date = "2024-02-23"
|
||||
node_compat = true
|
||||
|
||||
tail_consumers = [{service = "new-cf-ai-backend-tail"}]
|
||||
|
||||
|
||||
# [env.preview]
|
||||
[[vectorize]]
|
||||
binding = "VECTORIZE_INDEX"
|
||||
|
|
@ -24,8 +27,24 @@ binding = "AI"
|
|||
|
||||
[[kv_namespaces]]
|
||||
binding = "KV"
|
||||
id = "37a90353da63401e84e20e71165531d0"
|
||||
preview_id = "c58b6202814f4224acea97627d0c18aa"
|
||||
id = "569c3f2a510943729019f5d1f798e7d9"
|
||||
preview_id = "569c3f2a510943729019f5d1f798e7d9"
|
||||
|
||||
[placement]
|
||||
mode = "smart"
|
||||
|
||||
[[queues.producers]]
|
||||
queue = "embedchunks-queue"
|
||||
binding ="EMBEDCHUNKS_QUEUE"
|
||||
|
||||
[[queues.consumers]]
|
||||
queue = "embedchunks-queue"
|
||||
max_batch_size = 100
|
||||
max_retries = 10
|
||||
dead_letter_queue = "embedchunks-dlq"
|
||||
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "DATABASE"
|
||||
database_name = "supermemlocal"
|
||||
database_id = "0f93c990-72fb-489c-8563-57a7bb18dc43"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
|
||||
import { Content, StoredSpace } from "@/server/db/schema";
|
||||
import { Content, StoredSpace } from "@repo/db/schema";
|
||||
import { MemoriesIcon, NextIcon, SearchIcon, UrlIcon } from "@repo/ui/icons";
|
||||
import {
|
||||
ArrowLeftIcon,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { redirect } from "next/navigation";
|
|||
import MemoriesPage from "../../content";
|
||||
import { db } from "@/server/db";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { spacesAccess } from "@/server/db/schema";
|
||||
import { spacesAccess } from "@repo/db/schema";
|
||||
import { auth } from "@/server/auth";
|
||||
|
||||
async function Page({ params: { spaceid } }: { params: { spaceid: number } }) {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { StoredSpace } from "@/server/db/schema";
|
||||
import { StoredSpace } from "@repo/db/schema";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { createMemory, createSpace } from "../actions/doers";
|
||||
import ComboboxWithCreate from "@repo/ui/shadcn/combobox";
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ import { getSpaces } from "../actions/fetchers";
|
|||
import { HomeIcon } from "@heroicons/react/24/solid";
|
||||
import { createMemory, createSpace } from "../actions/doers";
|
||||
import ComboboxWithCreate from "@repo/ui/shadcn/combobox";
|
||||
import { StoredSpace } from "@/server/db/schema";
|
||||
import { StoredSpace } from "@repo/db/schema";
|
||||
import useMeasure from "react-use-measure";
|
||||
|
||||
function Menu() {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import {
|
|||
spacesAccess,
|
||||
storedContent,
|
||||
users,
|
||||
} from "../../server/db/schema";
|
||||
} from "@repo/db/schema";
|
||||
import { ServerActionReturnType } from "./types";
|
||||
import { auth } from "../../server/auth";
|
||||
import { Tweet } from "react-tweet/api";
|
||||
|
|
@ -836,8 +836,7 @@ export async function getQuerySuggestions() {
|
|||
};
|
||||
}
|
||||
|
||||
const fullQuery = content
|
||||
.map((c) => `${c.title} \n\n${c.content}`)
|
||||
const fullQuery = (content?.map((c) => `${c.title} \n\n${c.content}`) ?? [])
|
||||
.join(" ");
|
||||
|
||||
const suggestionsCall = (await env.AI.run(
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import {
|
|||
StoredSpace,
|
||||
User,
|
||||
users,
|
||||
} from "../../server/db/schema";
|
||||
} from "@repo/db/schema";
|
||||
import { ServerActionReturnType } from "./types";
|
||||
import { auth } from "../../server/auth";
|
||||
import { ChatHistory, SourceZod } from "@repo/shared-types";
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { NextRequest } from "next/server";
|
|||
import { ensureAuth } from "../../ensureAuth";
|
||||
import { db } from "@/server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { chatThreads } from "@/server/db/schema";
|
||||
import { chatThreads } from "@repo/db/schema";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import {
|
|||
import { ensureAuth } from "../ensureAuth";
|
||||
import { z } from "zod";
|
||||
import { db } from "@/server/db";
|
||||
import { chatHistory as chatHistoryDb, chatThreads } from "@/server/db/schema";
|
||||
import { chatHistory as chatHistoryDb, chatThreads } from "@repo/db/schema";
|
||||
import { and, eq, gt, sql } from "drizzle-orm";
|
||||
import { join } from "path";
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { NextRequest } from "next/server";
|
||||
import { db } from "../../server/db";
|
||||
import { accounts, sessions, users } from "../../server/db/schema";
|
||||
import { accounts, sessions, users } from "@repo/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export async function ensureAuth(req: NextRequest) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { db } from "@/server/db";
|
||||
import { and, eq, ne, sql } from "drizzle-orm";
|
||||
import { sessions, storedContent, users } from "@/server/db/schema";
|
||||
import { sessions, storedContent, users } from "@repo/db/schema";
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { ensureAuth } from "../ensureAuth";
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { db } from "@/server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { sessions, users } from "@/server/db/schema";
|
||||
import { sessions, users } from "@repo/db/schema";
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import {
|
|||
contentToSpace,
|
||||
storedContent,
|
||||
users,
|
||||
} from "@/server/db/schema";
|
||||
} from "@repo/db/schema";
|
||||
import { ensureAuth } from "../ensureAuth";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { db } from "@/server/db";
|
||||
import { space } from "@/server/db/schema";
|
||||
import { space } from "@repo/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { ensureAuth } from "../ensureAuth";
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
import { type NextRequest } from "next/server";
|
||||
import { createMemoryFromAPI } from "../helper";
|
||||
|
||||
type FriendData = {
|
||||
id: string;
|
||||
created_at: string;
|
||||
transcript: string;
|
||||
structured: {
|
||||
title: string;
|
||||
overview: string;
|
||||
action_items: [
|
||||
{
|
||||
description: string;
|
||||
},
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const body: FriendData = await req.json();
|
||||
|
||||
const userId = new URL(req.url).searchParams.get("uid");
|
||||
|
||||
if (!userId) {
|
||||
return new Response(
|
||||
JSON.stringify({ status: 400, body: "Missing user ID" }),
|
||||
);
|
||||
}
|
||||
|
||||
await createMemoryFromAPI({
|
||||
data: {
|
||||
title: "Friend: " + body.structured.title,
|
||||
description: body.structured.overview,
|
||||
pageContent:
|
||||
body.transcript + "\n\n" + JSON.stringify(body.structured.action_items),
|
||||
spaces: [],
|
||||
type: "note",
|
||||
url: "https://basedhardware.com",
|
||||
},
|
||||
userId: userId,
|
||||
});
|
||||
|
||||
return new Response(JSON.stringify({ status: 200, body: "success" }));
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import { z } from "zod";
|
||||
import { db } from "@/server/db";
|
||||
import { contentToSpace, space, storedContent } from "@/server/db/schema";
|
||||
import { contentToSpace, space, storedContent } from "@repo/db/schema";
|
||||
import { and, eq, inArray } from "drizzle-orm";
|
||||
import { LIMITS } from "@/lib/constants";
|
||||
import { limit } from "@/app/actions/doers";
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { db } from "@/server/db";
|
||||
import { storedContent, users } from "@/server/db/schema";
|
||||
import { storedContent, users } from "@repo/db/schema";
|
||||
import { cipher } from "@/server/encrypt";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { Bot, webhookCallback } from "grammy";
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import { Button } from "@repo/ui/shadcn/button";
|
|||
import { auth, signIn, signOut } from "../../server/auth";
|
||||
import { db } from "../../server/db";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { users } from "../../server/db/schema";
|
||||
import { users } from "@repo/db/schema";
|
||||
import { getThemeToggler } from "../../lib/get-theme-button";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ const baseNextConfig = {
|
|||
env: {
|
||||
TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN,
|
||||
},
|
||||
eslint: {
|
||||
disableDuringBuilds: true
|
||||
}
|
||||
};
|
||||
|
||||
let selectedCofig = baseNextConfig;
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@
|
|||
"@types/node": "^20.11.24",
|
||||
"@types/react": "^18.2.61",
|
||||
"@types/react-dom": "^18.2.19",
|
||||
"drizzle-kit": "0.21.2",
|
||||
"eslint": "^8.57.0",
|
||||
"postcss": "^8.4.38",
|
||||
"typescript": "^5.3.3",
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import NextAuth, { NextAuthResult } from "next-auth";
|
|||
import Google from "next-auth/providers/google";
|
||||
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
||||
import { db } from "./db";
|
||||
import { accounts, sessions, users, verificationTokens } from "./db/schema";
|
||||
import { accounts, sessions, users, verificationTokens } from "@repo/db/schema";
|
||||
|
||||
export const {
|
||||
handlers: { GET, POST },
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { drizzle } from "drizzle-orm/d1";
|
||||
|
||||
import * as schema from "./schema";
|
||||
import * as schema from "@repo/db/schema";
|
||||
|
||||
export const db = drizzle(process.env.DATABASE, {
|
||||
schema,
|
||||
|
|
|
|||
|
|
@ -26,8 +26,9 @@ bucket_name = "dev-r2-anycontext"
|
|||
|
||||
[[d1_databases]]
|
||||
binding = "DATABASE"
|
||||
database_name = "dev-d1-anycontext"
|
||||
database_id = "fc562605-157a-4f60-b439-2a24ffed5b4c"
|
||||
database_name = "supermemlocal"
|
||||
database_id = "0f93c990-72fb-489c-8563-57a7bb18dc43"
|
||||
|
||||
|
||||
|
||||
[[env.production.d1_databases]]
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import { type Config } from "drizzle-kit";
|
||||
|
||||
export default {
|
||||
schema: "./server/db/schema.ts",
|
||||
schema: "./packages/db/schema.ts",
|
||||
dialect: "sqlite",
|
||||
driver: "d1",
|
||||
dbCredentials: {
|
||||
wranglerConfigPath: "./wrangler.toml",
|
||||
wranglerConfigPath: "./apps/web/wrangler.toml",
|
||||
dbName: "",
|
||||
},
|
||||
out: "migrations",
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
"@repo/tailwind-config": "*",
|
||||
"@repo/typescript-config": "*",
|
||||
"@repo/ui": "*",
|
||||
"@repo/db": "*",
|
||||
"lint-staged": "^15.2.5",
|
||||
"prettier": "^3.3.3",
|
||||
"rxjs": "^7.8.1",
|
||||
|
|
@ -99,7 +100,9 @@
|
|||
"turndown": "^7.2.0",
|
||||
"uploadthing": "^6.10.4",
|
||||
"vaul": "^0.9.1",
|
||||
"zod": "^3.23.8"
|
||||
"zod": "^3.23.8",
|
||||
"drizzle-kit": "0.21.2",
|
||||
"drizzle-orm": "0.30.0"
|
||||
},
|
||||
"trustedDependencies": [
|
||||
"core-js-pure",
|
||||
|
|
|
|||
13
packages/db/package.json
Normal file
13
packages/db/package.json
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "@repo/db",
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"compilerOptions": {
|
||||
"plugins": [{ "name": "next" }],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"allowJs": true,
|
||||
"jsx": "preserve",
|
||||
"noEmit": true
|
||||
}
|
||||
}
|
||||
7
packages/db/tsconfig.json
Normal file
7
packages/db/tsconfig.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"extends": "@repo/typescript-config/react-library.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist"
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
}
|
||||
42109
pnpm-lock.yaml
generated
42109
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue