Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/schema/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { user } from "./user";
import { sensor } from "./sensor";
import { logEntry } from "./log-entry";
import { deviceToIntegrations } from "./integration";

/**
* Table
Expand Down Expand Up @@ -55,6 +56,10 @@ export const deviceRelations = relations(device, ({ one, many }) => ({
}),
sensors: many(sensor),
logEntries: many(logEntry),
integrations: one(deviceToIntegrations, {
fields: [device.id],
references: [deviceToIntegrations.deviceId],
}),
}));

/**
Expand Down
23 changes: 18 additions & 5 deletions app/schema/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ export const DeviceExposureEnum = pgEnum("exposure", [
"unknown",
]);

export const MqttMessageFormatEnum = pgEnum("message_format", [
"json",
"csv",
"application/json",
"text/csv",
"debug_plain",
"",
]);

export const TtnProfileEnum = pgEnum("ttn_profile", [
"json",
"debug",
"sensebox/home",
"lora-serialization",
"cayenne-lpp",
]);

// Zod schema for validating device exposure types
export const DeviceExposureZodEnum = z.enum(DeviceExposureEnum.enumValues);

// Type inferred from the Zod schema for device exposure types
export type DeviceExposureType = z.infer<typeof DeviceExposureZodEnum>;

// Enum for device status types
export const DeviceStatusEnum = pgEnum("status", [
"active",
"inactive",
"old",
]);
export const DeviceStatusEnum = pgEnum("status", ["active", "inactive", "old"]);

// Zod schema for validating device status types
export const DeviceStatusZodEnum = z.enum(DeviceStatusEnum.enumValues);
Expand Down
3 changes: 2 additions & 1 deletion app/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from "./profile-image";
export * from "./types";
export * from "./sensor";
export * from "./user";
export * from "./log-entry";
export * from "./log-entry";
export * from "./integration";
86 changes: 86 additions & 0 deletions app/schema/integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { createId } from "@paralleldrive/cuid2";
import {
boolean,
integer,
json,
pgTable,
primaryKey,
text,
} from "drizzle-orm/pg-core";
import { MqttMessageFormatEnum, TtnProfileEnum } from "./enum";
import { device } from "./device";
import { relations, sql } from "drizzle-orm";

export const mqttIntegration = pgTable("mqtt_integration", {
id: text("id")
.primaryKey()
.notNull()
.$defaultFn(() => createId()),
enabled: boolean("enabled").default(false).notNull(),
url: text("url").notNull(),
topic: text("topic").notNull(),
messageFormat: MqttMessageFormatEnum("message_format")
.default("json")
.notNull(),
decodeOptions: json("decode_options"),
connectionOptions: json("connection_options"),
deviceId: text("device_id").references(() => device.id, {
onDelete: "cascade",
}),
});

export const ttnIntegration = pgTable("ttn_integration", {
id: text("id")
.primaryKey()
.notNull()
.$defaultFn(() => createId()),
enabled: boolean("enabled").default(false).notNull(),
devId: text("dev_id").notNull(),
appId: text("app_id").notNull(),
port: integer("port"),
profile: TtnProfileEnum("profile").default("json").notNull(),
decodeOptions: json("decode_options")
.$type<string[]>()
.default(sql`'{}'::json`),
deviceId: text("device_id").references(() => device.id, {
onDelete: "cascade",
}),
});

export const deviceToIntegrations = pgTable(
"device_to_integrations",
{
deviceId: text("device_id")
.notNull()
.references(() => device.id, { onDelete: "cascade" }),
mqttIntegrationId: text("mqtt_integration_id").references(
() => mqttIntegration.id,
{
onDelete: "set null",
},
),
ttnIntegrationId: text("ttn_integration_id").references(
() => ttnIntegration.id,
{
onDelete: "set null",
},
),
},
(t) => ({
pk: primaryKey({ columns: [t.deviceId] }),
}),
);

export const deviceToIntegrationsRelations = relations(
deviceToIntegrations,
({ one }) => ({
mqttIntegration: one(mqttIntegration, {
fields: [deviceToIntegrations.mqttIntegrationId],
references: [mqttIntegration.id],
}),
ttnIntegration: one(ttnIntegration, {
fields: [deviceToIntegrations.ttnIntegrationId],
references: [ttnIntegration.id],
}),
}),
);
Loading