pull/715/head
Travis Fischer 2025-04-26 19:01:05 +07:00
rodzic f2f33948ef
commit bf6fbb0e65
7 zmienionych plików z 214 dodań i 75 usunięć

Wyświetl plik

@ -18,6 +18,7 @@ export {
and,
arrayContained,
arrayContains,
arrayOverlaps,
between,
eq,
exists,

Wyświetl plik

@ -18,6 +18,8 @@ import {
createUpdateSchema,
cuid,
deploymentId,
optionalCuid,
optionalText,
projectId,
timestamps
} from './utils'
@ -30,18 +32,18 @@ export const deployments = pgTable(
...timestamps,
hash: text().notNull(),
version: text(),
version: optionalText(),
enabled: boolean().notNull().default(true),
published: boolean().notNull().default(false),
enabled: boolean().default(true).notNull(),
published: boolean().default(false).notNull(),
description: text().notNull().default(''),
readme: text().notNull().default(''),
description: text().default('').notNull(),
readme: text().default('').notNull(),
userId: cuid()
.notNull()
.references(() => users.id),
teamId: cuid().references(() => teams.id),
teamId: optionalCuid().references(() => teams.id),
projectId: projectId()
.notNull()
.references(() => projects.id, {
@ -51,9 +53,9 @@ export const deployments = pgTable(
// TODO: tools?
// services: jsonb().$type<Service[]>().default([]),
// Environment variables & secrets
build: jsonb().$type<object>(),
env: jsonb().$type<object>(),
// TODO: Environment variables & secrets
// build: jsonb().$type<object>(),
// env: jsonb().$type<object>(),
// TODO: metadata config (logo, keywords, etc)
// TODO: webhooks
@ -63,7 +65,7 @@ export const deployments = pgTable(
_url: text().notNull(),
pricingPlans: jsonb().$type<PricingPlan[]>().notNull(),
coupons: jsonb().$type<Coupon[]>().notNull().default([])
coupons: jsonb().$type<Coupon[]>().default([]).notNull()
},
(table) => [
index('deployment_userId_idx').on(table.userId),
@ -113,15 +115,18 @@ export const deploymentInsertSchema = createInsertSchema(deployments, {
_url: (schema) => schema.url(),
build: z.object({}),
env: z.object({}),
// build: z.object({}),
// env: z.object({}),
pricingPlans: z.array(pricingPlanSchema),
coupons: z.array(couponSchema).optional()
})
export const deploymentSelectSchema = createSelectSchema(deployments, {
build: z.object({}),
env: z.object({}),
version: z.string().nonempty().optional(),
teamId: z.string().cuid2().optional(),
// build: z.object({}),
// env: z.object({}),
pricingPlans: z.array(pricingPlanSchema),
coupons: z.array(couponSchema)
})

Wyświetl plik

@ -21,9 +21,10 @@ import {
createSelectSchema,
createUpdateSchema,
cuid,
deploymentId,
optionalDeploymentId,
optionalStripeId,
optionalText,
projectId,
stripeId,
timestamps
} from './utils'
@ -35,7 +36,7 @@ export const projects = pgTable(
...timestamps,
name: text().notNull(),
alias: text(),
alias: optionalText(),
userId: cuid()
.notNull()
@ -43,38 +44,44 @@ export const projects = pgTable(
teamId: cuid().notNull(),
// Most recently published Deployment if one exists
lastPublishedDeploymentId: deploymentId(),
lastPublishedDeploymentId: optionalDeploymentId(),
// Most recent Deployment if one exists
lastDeploymentId: deploymentId(),
lastDeploymentId: optionalDeploymentId(),
applicationFeePercent: integer().notNull().default(20),
applicationFeePercent: integer().default(20).notNull(),
// TODO: This is going to need to vary from dev to prod
isStripeConnectEnabled: boolean().notNull().default(false),
isStripeConnectEnabled: boolean().default(false).notNull(),
// All deployments share the same underlying proxy secret
_secret: text(),
_secret: optionalText(),
// Auth token used to access the saasify API on behalf of this project
_providerToken: text().notNull(),
// TODO: Full-text search
_text: text().default(''),
_text: text().default('').notNull(),
_webhooks: jsonb().$type<Webhook[]>().default([]),
_webhooks: jsonb().$type<Webhook[]>().default([]).notNull(),
// Stripe products corresponding to the stripe plans across deployments
stripeBaseProductId: stripeId(),
stripeRequestProductId: stripeId(),
stripeBaseProductId: optionalStripeId(),
stripeRequestProductId: optionalStripeId(),
// [metricSlug: string]: string
stripeMetricProductIds: jsonb().$type<Record<string, string>>().default({}),
stripeMetricProductIds: jsonb()
.$type<Record<string, string>>()
.default({})
.notNull(),
// Stripe coupons associated with this project, mapping from unique coupon
// hash to stripe coupon id.
// `[hash: string]: string`
_stripeCouponIds: jsonb().$type<Record<string, string>>().default({}),
_stripeCouponIds: jsonb()
.$type<Record<string, string>>()
.default({})
.notNull(),
// Stripe billing plans associated with this project (created lazily),
// mapping from unique plan hash to stripe plan ids for base and request
@ -82,7 +89,8 @@ export const projects = pgTable(
// `[hash: string]: { basePlanId: string, requestPlanId: string }`
_stripePlanIds: jsonb()
.$type<Record<string, { basePlanId: string; requestPlanId: string }>>()
.default({}),
.default({})
.notNull(),
// Connected Stripe account (standard or express).
// If not defined, then subscriptions for this project route through our
@ -91,7 +99,7 @@ export const projects = pgTable(
// the stripeID utility.
// TODO: is it wise to share this between dev and prod?
// TODO: is it okay for this to be public?
_stripeAccountId: stripeId()
_stripeAccountId: optionalStripeId()
},
(table) => [
index('project_userId_idx').on(table.userId),
@ -151,7 +159,17 @@ export const projectInsertSchema = createInsertSchema(projects, {
})
export const projectSelectSchema = createSelectSchema(projects, {
stripeMetricProductIds: z.record(z.string(), z.string()).optional()
alias: z.string().nonempty().optional(),
lastPublishedDeploymentId: z.string().nonempty().optional(),
lastDeploymentId: z.string().nonempty().optional(),
_secret: z.string().nonempty().optional(),
stripeBaseProductId: z.string().nonempty().optional(),
stripeRequestProductId: z.string().nonempty().optional(),
stripeMetricProductIds: z.record(z.string(), z.string()).optional(),
// _webhooks: z.array(webhookSchema),
// _stripeCouponIds: z.record(z.string(), z.string()).optional(),
// _stripePlanIds: z
@ -163,6 +181,7 @@ export const projectSelectSchema = createSelectSchema(projects, {
// })
// )
// .optional()
_stripeAccountId: z.string().nonempty().optional()
})
.omit({
_secret: true,

Wyświetl plik

@ -1,17 +1,14 @@
import { z } from '@hono/zod-openapi'
import { relations } from 'drizzle-orm'
import {
boolean,
index,
pgTable,
primaryKey,
timestamp
} from 'drizzle-orm/pg-core'
import { index, pgTable, primaryKey } from 'drizzle-orm/pg-core'
import { teams } from './team'
import { users } from './user'
import {
createSelectSchema,
cuid,
optionalBoolean,
optionalTimestamp,
teamMemberRoleEnum,
timestamps
} from './utils'
@ -29,8 +26,8 @@ export const teamMembers = pgTable(
.references(() => teams.id, { onDelete: 'cascade' }),
role: teamMemberRoleEnum().default('user').notNull(),
confirmed: boolean().default(false),
confirmedAt: timestamp({ mode: 'string' })
confirmed: optionalBoolean().default(false).notNull(),
confirmedAt: optionalTimestamp()
},
(table) => [
primaryKey({ columns: [table.userId, table.teamId] }),
@ -52,5 +49,7 @@ export const teamMembersRelations = relations(teamMembers, ({ one }) => ({
})
}))
export const teamMemberSelectSchema =
createSelectSchema(teamMembers).openapi('TeamMember')
export const teamMemberSelectSchema = createSelectSchema(teamMembers, {
confirmed: z.boolean(),
confirmedAt: z.string().datetime().optional()
}).openapi('TeamMember')

Wyświetl plik

@ -1,4 +1,5 @@
import { validators } from '@agentic/validators'
import { z } from '@hono/zod-openapi'
import { relations } from 'drizzle-orm'
import {
boolean,
@ -6,7 +7,6 @@ import {
jsonb,
pgTable,
text,
timestamp,
uniqueIndex
} from 'drizzle-orm/pg-core'
@ -19,7 +19,9 @@ import {
createSelectSchema,
createUpdateSchema,
id,
stripeId,
optionalStripeId,
optionalText,
optionalTimestamp,
timestamps,
userRoleEnum
} from './utils'
@ -33,25 +35,25 @@ export const users = pgTable(
username: text().notNull().unique(),
role: userRoleEnum().default('user').notNull(),
email: text().unique(),
password: text(),
email: optionalText().unique(),
password: optionalText(),
// metadata
firstName: text(),
lastName: text(),
image: text(),
firstName: optionalText(),
lastName: optionalText(),
image: optionalText(),
emailConfirmed: boolean().default(false),
emailConfirmedAt: timestamp({ mode: 'string' }),
emailConfirmToken: text().unique().default(sha256()),
passwordResetToken: text().unique(),
emailConfirmed: boolean().default(false).notNull(),
emailConfirmedAt: optionalTimestamp(),
emailConfirmToken: text().unique().default(sha256()).notNull(),
passwordResetToken: optionalText().unique(),
isStripeConnectEnabledByDefault: boolean().default(true),
isStripeConnectEnabledByDefault: boolean().default(true).notNull(),
// third-party auth providers
providers: jsonb().$type<AuthProviders>().default({}),
providers: jsonb().$type<AuthProviders>().default({}).notNull(),
stripeCustomerId: stripeId().unique()
stripeCustomerId: optionalStripeId().unique()
},
(table) => [
uniqueIndex('user_email_idx').on(table.email),
@ -98,7 +100,19 @@ export const userInsertSchema = createInsertSchema(users, {
})
export const userSelectSchema = createSelectSchema(users, {
providers: authProvidersSchema
email: z.string().email().optional(),
password: z.string().nonempty().optional(),
firstName: z.string().optional(),
lastName: z.string().optional(),
image: z.string().nonempty().optional(),
emailConfirmedAt: z.string().datetime().optional(),
passwordResetToken: z.string().nonempty().optional(),
providers: authProvidersSchema,
stripeCustomerId: z.string().nonempty().optional()
}).openapi('User')
export const userUpdateSchema = createUpdateSchema(users)

Wyświetl plik

@ -3,6 +3,7 @@ import { z } from '@hono/zod-openapi'
import { createId } from '@paralleldrive/cuid2'
import { sql, type Writable } from 'drizzle-orm'
import {
customType,
pgEnum,
type PgVarcharBuilderInitial,
type PgVarcharConfig,
@ -84,3 +85,94 @@ export const { createInsertSchema, createSelectSchema, createUpdateSchema } =
date: true
}
})
export type ColumnType =
// string
| 'text'
| 'varchar'
| 'timestamp'
| 'stripeId'
| 'projectId'
| 'deploymentId'
| 'cuid'
// boolean
| 'boolean'
// number
| 'integer'
| 'smallint'
| 'bigint'
// json
| 'json'
| 'jsonb'
export type ColumnTypeToTSType<T extends ColumnType> = T extends
| 'text'
| 'varchar'
| 'timestamp'
| 'cuid'
| 'stripeId'
| 'projectId'
| 'deploymentId'
? string
: T extends 'boolean'
? boolean
: T extends 'integer' | 'smallint' | 'bigint'
? number
: never
/**
* @see https://github.com/drizzle-team/drizzle-orm/issues/2745
*/
function optional<
T extends ColumnType,
InferredType extends
| string
| boolean
| number
| object = ColumnTypeToTSType<T>
>(dataType: T) {
return customType<{
data: InferredType | undefined
driverData: InferredType | null
config: T extends 'stripeId'
? {
length: number
}
: never
}>({
dataType() {
if (dataType === 'stripeId') {
return 'varchar({ length: 255 })'
}
if (dataType === 'cuid') {
return 'varchar({ length: 24 })'
}
if (dataType === 'projectId') {
return 'varchar({ length: 130 })'
}
if (dataType === 'deploymentId') {
return 'varchar({ length: 160 })'
}
if (dataType === 'timestamp') {
return 'timestamp({ mode: "string" })'
}
return dataType
},
fromDriver: (v) => v ?? undefined,
toDriver: (v) => v ?? null
})
}
export const optionalText = optional('text')
export const optionalTimestamp = optional('timestamp')
export const optionalBoolean = optional('boolean')
export const optionalVarchar = optional('varchar')
export const optionalCuid = optional('cuid')
export const optionalStripeId = optional('stripeId')
export const optionalProjectId = optional('projectId')
export const optionalDeploymentId = optional('deploymentId')

Wyświetl plik

@ -7,30 +7,39 @@ export type Tables = ExtractTablesWithRelations<typeof schema>
export type User = z.infer<typeof schema.userSelectSchema>
// export type User2 = typeof schema.users.$inferSelect
// export type User3 = NullToUndefinedDeep<typeof schema.users.$inferSelect>
export type Team = z.infer<typeof schema.teamSelectSchema>
export type TeamWithMembers = BuildQueryResult<
Tables,
Tables['teams'],
{ with: { members: true } }
export type TeamWithMembers = NullToUndefinedDeep<
BuildQueryResult<Tables, Tables['teams'], { with: { members: true } }>
>
export type TeamMember = z.infer<typeof schema.teamMemberSelectSchema>
export type TeamMemberWithTeam = BuildQueryResult<
Tables,
Tables['teamMembers'],
{ with: { team: true } }
export type TeamMemberWithTeam = NullToUndefinedDeep<
BuildQueryResult<Tables, Tables['teamMembers'], { with: { team: true } }>
>
export type Project = typeof schema.projects.$inferSelect
export type ProjectWithLastPublishedDeployment = BuildQueryResult<
Tables,
Tables['projects'],
{ with: { lastPublishedDeployment: true } }
export type Project = z.infer<typeof schema.projectSelectSchema>
export type ProjectWithLastPublishedDeployment = NullToUndefinedDeep<
BuildQueryResult<
Tables,
Tables['projects'],
{ with: { lastPublishedDeployment: true } }
>
>
export type Deployment = typeof schema.deployments.$inferSelect
export type DeploymentWithProject = BuildQueryResult<
Tables,
Tables['deployments'],
{ with: { project: true } }
export type Deployment = z.infer<typeof schema.deploymentSelectSchema>
export type DeploymentWithProject = NullToUndefinedDeep<
BuildQueryResult<Tables, Tables['deployments'], { with: { project: true } }>
>
export type NullToUndefinedDeep<T> = T extends null
? undefined
: T extends Date
? T
: T extends readonly (infer U)[]
? NullToUndefinedDeep<U>[]
: T extends object
? { [K in keyof T]: NullToUndefinedDeep<T[K]> }
: T