Overview
The Device model represents physical or virtual facial recognition devices deployed in the BD Scan Face system. Devices are entry points where users attempt access, and they log all access attempts with associated confidence scores.
Model Fields
Primary key. Auto-incremented unique identifier for the device.
Descriptive name for the device. Maximum 100 characters. Example: “Main Entrance Scanner”, “Office Door 2A”.
Physical location of the device. Maximum 100 characters. Example: “Building A - First Floor”, “Parking Garage Exit”.
Network IP address of the device. Maximum 50 characters. Used for device communication and management.
Indicates whether the device is active and operational. Defaults to true.
Timestamp when the device was registered in the system. Automatically set to current time.
Relationships
Array of AccessLog records tracking all access attempts made at this device.
Query Examples
Find All Devices
const devices = await prisma.device.findMany({
include: {
_count: {
select: { access_logs: true }
}
},
orderBy: {
name: 'asc'
}
});
Find Device by ID
const device = await prisma.device.findUnique({
where: {
device_id: 1
},
include: {
access_logs: {
take: 20,
orderBy: {
access_date: 'desc'
},
include: {
user: {
select: {
first_name: true,
last_name: true,
ci: true
}
}
}
}
}
});
Create New Device
const newDevice = await prisma.device.create({
data: {
name: 'Main Entrance Scanner',
location: 'Building A - Main Lobby',
ip_address: '192.168.1.100',
status: true
}
});
Update Device
const updatedDevice = await prisma.device.update({
where: {
device_id: 1
},
data: {
location: 'Building B - Reception',
ip_address: '192.168.1.101',
status: false
}
});
Delete Device
// Note: Consider cascading deletes for related access_logs
const deletedDevice = await prisma.device.delete({
where: {
device_id: 1
}
});
Find Active Devices
const activeDevices = await prisma.device.findMany({
where: {
status: true
},
include: {
_count: {
select: { access_logs: true }
}
},
orderBy: {
name: 'asc'
}
});
Find Devices by Location
const buildingDevices = await prisma.device.findMany({
where: {
location: {
contains: 'Building A',
mode: 'insensitive'
}
},
include: {
_count: {
select: { access_logs: true }
}
}
});
Find Device by IP Address
const deviceByIp = await prisma.device.findFirst({
where: {
ip_address: '192.168.1.100'
},
include: {
access_logs: {
take: 10,
orderBy: {
access_date: 'desc'
}
}
}
});
Get Device with Recent Access Logs
const deviceWithRecentAccess = await prisma.device.findUnique({
where: {
device_id: 1
},
include: {
access_logs: {
where: {
access_date: {
gte: new Date(Date.now() - 24 * 60 * 60 * 1000) // Last 24 hours
}
},
orderBy: {
access_date: 'desc'
},
include: {
user: {
select: {
first_name: true,
last_name: true,
ci: true
}
}
}
}
}
});
Get Devices with Access Statistics
const devicesWithStats = await prisma.device.findMany({
where: {
status: true
},
include: {
access_logs: {
where: {
access_date: {
gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) // Last 7 days
}
},
select: {
log_id: true,
access_date: true,
confidence: true,
status: true
}
},
_count: {
select: { access_logs: true }
}
}
});
Find Most Active Devices
const devices = await prisma.device.findMany({
include: {
_count: {
select: { access_logs: true }
}
}
});
// Sort by access log count in application code
const mostActiveDevices = devices.sort(
(a, b) => b._count.access_logs - a._count.access_logs
).slice(0, 10);
Update Device Status
const disabledDevice = await prisma.device.update({
where: {
device_id: 1
},
data: {
status: false
}
});
Find Recently Registered Devices
const recentDevices = await prisma.device.findMany({
where: {
registration_date: {
gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) // Last 30 days
}
},
orderBy: {
registration_date: 'desc'
}
});
Device Management Use Cases
Status Monitoring
The status field enables:
- Temporarily disabling devices for maintenance
- Managing device lifecycle
- Filtering active devices for reporting
Network Configuration
The ip_address field supports:
- Device discovery and connection
- Network diagnostics
- Remote device management
Location Tracking
The location field provides:
- Physical device mapping
- Access pattern analysis by location
- Security zone management
Access Analytics
Through the access_logs relationship:
- Track usage patterns per device
- Identify high-traffic entry points
- Monitor device performance and reliability
Database Mapping
This model maps to the devices table in the database.