File size: 1,475 Bytes
b80fc11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { relations } from "drizzle-orm";
import { pgTable, text, unique } from "drizzle-orm/pg-core";
import { createInsertSchema } from "drizzle-zod";
import { nanoid } from "nanoid";
import { z } from "zod";
import { applications } from "./application";

export const security = pgTable(
	"security",
	{
		securityId: text("securityId")
			.notNull()
			.primaryKey()
			.$defaultFn(() => nanoid()),
		username: text("username").notNull(),
		password: text("password").notNull(),
		createdAt: text("createdAt")
			.notNull()
			.$defaultFn(() => new Date().toISOString()),
		applicationId: text("applicationId")
			.notNull()
			.references(() => applications.applicationId, { onDelete: "cascade" }),
	},
	(t) => ({
		unq: unique().on(t.username, t.applicationId),
	}),
);

export const securityRelations = relations(security, ({ one }) => ({
	application: one(applications, {
		fields: [security.applicationId],
		references: [applications.applicationId],
	}),
}));
const createSchema = createInsertSchema(security, {
	securityId: z.string().min(1),
	username: z.string().min(1),
	password: z.string().min(1),
});

export const apiFindOneSecurity = createSchema
	.pick({
		securityId: true,
	})
	.required();

export const apiCreateSecurity = createSchema
	.pick({
		applicationId: true,
		username: true,
		password: true,
	})
	.required();

export const apiUpdateSecurity = createSchema
	.pick({
		securityId: true,
		username: true,
		password: true,
	})
	.required();