This commit is contained in:
Turbobot
2024-03-21 13:41:16 +08:00
committed by giancarlo
commit bb58169fe9
204 changed files with 26228 additions and 0 deletions

22
packages/db/index.ts Normal file
View File

@@ -0,0 +1,22 @@
// Generated by prisma/post-generate.ts
import { Kysely } from "kysely";
import { PlanetScaleDialect } from "kysely-planetscale";
import { customAlphabet } from "nanoid";
import type { DB } from "./prisma/types";
export { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/postgres";
export * from "./prisma/types";
export * from "./prisma/enums";
export const db = new Kysely<DB>({
dialect: new PlanetScaleDialect({
url: process.env.DATABASE_URL,
}),
});
// Use custom alphabet without special chars for less chaotic, copy-able URLs
// Will not collide for a long long time: https://zelark.github.io/nano-id-cc/
export const genId = customAlphabet("0123456789abcdefghijklmnopqrstuvwxyz", 16);

45
packages/db/package.json Normal file
View File

@@ -0,0 +1,45 @@
{
"name": "@acme/db",
"private": true,
"version": "0.1.0",
"exports": {
".": "./index.ts"
},
"license": "MIT",
"scripts": {
"clean": "rm -rf .turbo node_modules",
"db:generate": "pnpm with-env prisma generate",
"db:push": "pnpm with-env prisma db push --skip-generate",
"studio": "pnpm with-env prisma studio --port 5556",
"format": "prisma format && prettier --check \"**/*.{mjs,ts,json}\"",
"lint": "eslint .",
"typecheck": "tsc --noEmit",
"with-env": "dotenv -e ../../.env.local --"
},
"dependencies": {
"@planetscale/database": "^1.14.0",
"kysely": "^0.27.2",
"kysely-planetscale": "^1.4.0",
"nanoid": "^5.0.4"
},
"devDependencies": {
"@acme/eslint-config": "^0.2.0",
"@acme/prettier-config": "^0.1.0",
"@acme/tsconfig": "^0.1.0",
"dotenv-cli": "^7.3.0",
"eslint": "^8.56.0",
"prettier": "^3.2.4",
"prisma": "^5.8.1",
"prisma-kysely": "^1.7.1",
"typescript": "^5.3.3"
},
"eslintConfig": {
"extends": [
"@acme/eslint-config/base"
],
"rules": {
"@typescript-eslint/consistent-type-definitions": "off"
}
},
"prettier": "@acme/prettier-config"
}

View File

@@ -0,0 +1,12 @@
export const ProjectTier = {
FREE: "FREE",
PRO: "PRO",
} as const;
export type ProjectTier = (typeof ProjectTier)[keyof typeof ProjectTier];
export const SubscriptionPlan = {
FREE: "FREE",
STANDARD: "STANDARD",
PRO: "PRO",
} as const;
export type SubscriptionPlan =
(typeof SubscriptionPlan)[keyof typeof SubscriptionPlan];

View File

@@ -0,0 +1,83 @@
generator kysely {
provider = "prisma-kysely"
output = "."
enumFileName = "enums.ts"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
enum ProjectTier {
FREE
PRO
}
model Project {
id String @id @db.VarChar(30) // prefix_ + nanoid (16)
createdAt DateTime @default(now())
// A project is tied to a Clerk User or Organization
organizationId String? @db.VarChar(36) // uuid v4
userId String? @db.VarChar(36) // uuid v4
name String
tier ProjectTier @default(FREE)
url String?
@@index([organizationId])
@@index([userId])
}
enum SubscriptionPlan {
FREE
STANDARD
PRO
}
model Customer {
id String @id @db.VarChar(30) // prefix_ + nanoid (16)
stripeId String @unique
subscriptionId String?
clerkUserId String
clerkOrganizationId String?
name String?
plan SubscriptionPlan?
paidUntil DateTime?
endsAt DateTime?
@@index([clerkUserId])
}
model ApiKey {
id String @id @db.VarChar(30) // prefix_ + nanoid (16)
createdAt DateTime @default(now())
expiresAt DateTime?
lastUsed DateTime?
revokedAt DateTime?
projectId String @db.VarChar(30) // prefix_ + nanoid (16)
clerkUserId String @db.VarChar(36) // uuid v4
name String @default("Secret Key")
key String @unique
@@index([projectId])
}
model Ingestion {
id String @id @db.VarChar(30) // prefix_ + nanoid (16)
createdAt DateTime @default(now())
projectId String @db.VarChar(30) // prefix_ + nanoid (16)
apiKeyId String @db.VarChar(30) // prefix_ + nanoid (16)
schema Json
hash String @db.VarChar(40) // sha1
parent String? @db.VarChar(40) // sha1
origin String @db.VarChar(100)
@@index([projectId])
}

View File

@@ -0,0 +1,57 @@
import type { ColumnType } from "kysely";
import type { ProjectTier, SubscriptionPlan } from "./enums";
export type Generated<T> =
T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export type ApiKey = {
id: string;
createdAt: Generated<Timestamp>;
expiresAt: Timestamp | null;
lastUsed: Timestamp | null;
revokedAt: Timestamp | null;
projectId: string;
clerkUserId: string;
name: Generated<string>;
key: string;
};
export type Customer = {
id: string;
stripeId: string;
subscriptionId: string | null;
clerkUserId: string;
clerkOrganizationId: string | null;
name: string | null;
plan: SubscriptionPlan | null;
paidUntil: Timestamp | null;
endsAt: Timestamp | null;
};
export type Ingestion = {
id: string;
createdAt: Generated<Timestamp>;
projectId: string;
apiKeyId: string;
schema: unknown;
hash: string;
parent: string | null;
origin: string;
};
export type Project = {
id: string;
createdAt: Generated<Timestamp>;
organizationId: string | null;
userId: string | null;
name: string;
tier: Generated<ProjectTier>;
url: string | null;
};
export type DB = {
ApiKey: ApiKey;
Customer: Customer;
Ingestion: Ingestion;
Project: Project;
};

View File

@@ -0,0 +1,8 @@
{
"extends": "@acme/tsconfig/base.json",
"compilerOptions": {
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json",
},
"include": ["*.ts", "prisma", "src"],
"exclude": ["node_modules"],
}