upd: upsert apps
This commit is contained in:
+34
-19
@@ -1,26 +1,41 @@
|
||||
import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
|
||||
import {
|
||||
pgTable,
|
||||
timestamp,
|
||||
uniqueIndex,
|
||||
uuid,
|
||||
varchar,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { companiesTable } from "./companies";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { serversTable } from "./servers";
|
||||
|
||||
export const appsTable = pgTable("apps", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
name: varchar("name").notNull(),
|
||||
fileName: varchar("filename").notNull(),
|
||||
serverId: uuid("server_id")
|
||||
.notNull()
|
||||
.references(() => serversTable.id),
|
||||
companyId: uuid("company_id")
|
||||
.notNull()
|
||||
.references(() => companiesTable.id),
|
||||
createdAt: timestamp("created_at", { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow()
|
||||
.$onUpdate(() => new Date()),
|
||||
});
|
||||
export const appsTable = pgTable(
|
||||
"apps",
|
||||
{
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
name: varchar("name").notNull(),
|
||||
fileName: varchar("filename").notNull(),
|
||||
serverId: uuid("server_id")
|
||||
.notNull()
|
||||
.references(() => serversTable.id),
|
||||
companyId: uuid("company_id")
|
||||
.notNull()
|
||||
.references(() => companiesTable.id),
|
||||
createdAt: timestamp("created_at", { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow(),
|
||||
updatedAt: timestamp("updated_at", { withTimezone: true })
|
||||
.notNull()
|
||||
.defaultNow()
|
||||
.$onUpdate(() => new Date()),
|
||||
},
|
||||
(table) => ({
|
||||
uniqueFileNameServerId: uniqueIndex("unique_file_name_server_id").on(
|
||||
table.fileName,
|
||||
table.serverId
|
||||
),
|
||||
})
|
||||
);
|
||||
|
||||
export const appsRelations = relations(appsTable, ({ one }) => ({
|
||||
company: one(companiesTable, {
|
||||
|
||||
+13
-13
@@ -1,7 +1,7 @@
|
||||
import { error } from "elysia";
|
||||
import db from "../../db";
|
||||
import { appsTable, serversTable } from "../../db/schema";
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import { appsTable } from "../../db/schema";
|
||||
import { sql } from "drizzle-orm";
|
||||
import type { AuthContext } from "./../../middlewares/auth";
|
||||
|
||||
export default async function createApp(
|
||||
@@ -12,6 +12,7 @@ export default async function createApp(
|
||||
companyId: string;
|
||||
}
|
||||
) {
|
||||
console.log(auth);
|
||||
if (auth.userId !== "hmac-user") {
|
||||
return error(403, {
|
||||
error: "Forbidden",
|
||||
@@ -19,16 +20,6 @@ export default async function createApp(
|
||||
}
|
||||
|
||||
try {
|
||||
const existingApp = await db.query.appsTable.findMany({
|
||||
where: inArray(appsTable.name, body.apps),
|
||||
});
|
||||
|
||||
if (existingApp.length) {
|
||||
return error(400, {
|
||||
error: "App with this name already exists",
|
||||
});
|
||||
}
|
||||
|
||||
const apps = await db
|
||||
.insert(appsTable)
|
||||
.values(
|
||||
@@ -39,7 +30,16 @@ export default async function createApp(
|
||||
serverId: body.serverId,
|
||||
}))
|
||||
)
|
||||
.returning();
|
||||
.returning()
|
||||
.onConflictDoUpdate({
|
||||
target: [appsTable.fileName, appsTable.serverId],
|
||||
set: {
|
||||
name: sql`excluded.name`,
|
||||
fileName: sql`excluded.fileName`,
|
||||
companyId: sql`excluded.company_id`,
|
||||
serverId: sql`excluded.server_id`,
|
||||
},
|
||||
});
|
||||
|
||||
return apps;
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user