100 lines
3.8 KiB
TypeScript
100 lines
3.8 KiB
TypeScript
import { sqliteTable, text, integer, uniqueIndex } from 'drizzle-orm/sqlite-core';
|
|
import { relations, sql } from 'drizzle-orm';
|
|
|
|
// Battery types (AA, AAA, 18650, etc.)
|
|
export const batteryTypes = sqliteTable('battery_types', {
|
|
id: integer('id').primaryKey({ autoIncrement: true }),
|
|
name: text('name').notNull().unique(),
|
|
isCustom: integer('is_custom', { mode: 'boolean' }).default(false),
|
|
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
|
});
|
|
|
|
// Brands (Panasonic, Eneloop, etc.)
|
|
export const brands = sqliteTable('brands', {
|
|
id: integer('id').primaryKey({ autoIncrement: true }),
|
|
name: text('name').notNull().unique(),
|
|
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
|
});
|
|
|
|
// Battery groups (Brand + Type combination with counts)
|
|
export const batteryGroups = sqliteTable('battery_groups', {
|
|
id: integer('id').primaryKey({ autoIncrement: true }),
|
|
brandId: integer('brand_id').notNull().references(() => brands.id),
|
|
typeId: integer('type_id').notNull().references(() => batteryTypes.id),
|
|
availableCount: integer('available_count').default(0).notNull(),
|
|
chargingCount: integer('charging_count').default(0).notNull(),
|
|
notes: text('notes'),
|
|
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
|
updatedAt: text('updated_at').default(sql`(datetime('now'))`),
|
|
}, (table) => [
|
|
uniqueIndex('brand_type_idx').on(table.brandId, table.typeId),
|
|
]);
|
|
|
|
// Devices
|
|
export const devices = sqliteTable('devices', {
|
|
id: integer('id').primaryKey({ autoIncrement: true }),
|
|
name: text('name').notNull(),
|
|
description: text('description'),
|
|
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
|
updatedAt: text('updated_at').default(sql`(datetime('now'))`),
|
|
});
|
|
|
|
// Device-Battery assignments
|
|
export const deviceBatteries = sqliteTable('device_batteries', {
|
|
id: integer('id').primaryKey({ autoIncrement: true }),
|
|
deviceId: integer('device_id').notNull().references(() => devices.id, { onDelete: 'cascade' }),
|
|
batteryGroupId: integer('battery_group_id').notNull().references(() => batteryGroups.id),
|
|
quantity: integer('quantity').notNull().default(1),
|
|
assignedAt: text('assigned_at').default(sql`(datetime('now'))`),
|
|
}, (table) => [
|
|
uniqueIndex('device_battery_idx').on(table.deviceId, table.batteryGroupId),
|
|
]);
|
|
|
|
// Relations
|
|
export const batteryTypesRelations = relations(batteryTypes, ({ many }) => ({
|
|
batteryGroups: many(batteryGroups),
|
|
}));
|
|
|
|
export const brandsRelations = relations(brands, ({ many }) => ({
|
|
batteryGroups: many(batteryGroups),
|
|
}));
|
|
|
|
export const batteryGroupsRelations = relations(batteryGroups, ({ one, many }) => ({
|
|
brand: one(brands, {
|
|
fields: [batteryGroups.brandId],
|
|
references: [brands.id],
|
|
}),
|
|
type: one(batteryTypes, {
|
|
fields: [batteryGroups.typeId],
|
|
references: [batteryTypes.id],
|
|
}),
|
|
deviceBatteries: many(deviceBatteries),
|
|
}));
|
|
|
|
export const devicesRelations = relations(devices, ({ many }) => ({
|
|
deviceBatteries: many(deviceBatteries),
|
|
}));
|
|
|
|
export const deviceBatteriesRelations = relations(deviceBatteries, ({ one }) => ({
|
|
device: one(devices, {
|
|
fields: [deviceBatteries.deviceId],
|
|
references: [devices.id],
|
|
}),
|
|
batteryGroup: one(batteryGroups, {
|
|
fields: [deviceBatteries.batteryGroupId],
|
|
references: [batteryGroups.id],
|
|
}),
|
|
}));
|
|
|
|
// Type exports
|
|
export type BatteryType = typeof batteryTypes.$inferSelect;
|
|
export type NewBatteryType = typeof batteryTypes.$inferInsert;
|
|
export type Brand = typeof brands.$inferSelect;
|
|
export type NewBrand = typeof brands.$inferInsert;
|
|
export type BatteryGroup = typeof batteryGroups.$inferSelect;
|
|
export type NewBatteryGroup = typeof batteryGroups.$inferInsert;
|
|
export type Device = typeof devices.$inferSelect;
|
|
export type NewDevice = typeof devices.$inferInsert;
|
|
export type DeviceBattery = typeof deviceBatteries.$inferSelect;
|
|
export type NewDeviceBattery = typeof deviceBatteries.$inferInsert;
|