Skip to main content
This guide will help you set up BD Scan Face, a face recognition and access control database system built with Prisma and PostgreSQL, and run your first queries.

What You’ll Build

By the end of this quickstart, you’ll have:
  • A running PostgreSQL database with Docker
  • The complete BD Scan Face database schema deployed
  • A working Prisma Client ready to query your data
  • Example queries for managing users and access logs
1

Clone and Install

First, clone the repository and install the dependencies:
git clone https://github.com/ErnestoJuarez2708/BD_Scan_Face.git
cd BD_Scan_Face
npm install
The project uses TypeScript and includes Prisma ORM with PostgreSQL adapter for optimal performance.
2

Configure Environment

Create a .env file in the project root with your database connection:
.env
DATABASE_URL="postgresql://postgres:prisma@localhost:5432/postgres?schema=public"
The default credentials match the Docker Compose configuration. If you’re using an existing PostgreSQL instance, update the connection string accordingly.
3

Start PostgreSQL with Docker

Launch the PostgreSQL database using Docker Compose:
docker-compose up -d
Verify the database is running:
docker ps
You should see the postgres:15 container running on port 5432.
The Docker setup includes automatic health checks and data persistence through Docker volumes.
4

Run Database Migrations

Apply the database schema migrations to create all tables:
npx prisma migrate deploy
This creates the following tables:
  • user_types - User classification (admin, employee, visitor, etc.)
  • users - User profiles with identification and contact info
  • faces - Face encodings for recognition
  • devices - Access control devices
  • access_logs - Complete audit trail of all access attempts
5

Generate Prisma Client

Generate the type-safe Prisma Client:
npx prisma generate
The client will be generated in ./generated/prisma/ as configured in your schema.
Prisma generates a fully type-safe database client based on your schema, providing autocomplete and type checking for all queries.
6

Run Your First Query

Create a file called example.ts and add this code:
example.ts
import { PrismaClient } from './generated/prisma';

const prisma = new PrismaClient();

async function main() {
  // Create a user type
  const userType = await prisma.userType.create({
    data: {
      type_name: 'Employee'
    }
  });

  console.log('Created user type:', userType);

  // Create a user
  const user = await prisma.user.create({
    data: {
      ci: '1234567890',
      first_name: 'John',
      last_name: 'Doe',
      email: '[email protected]',
      phone: '+1234567890',
      user_type_id: userType.user_type_id,
      code: 1001,
      status: true
    }
  });

  console.log('Created user:', user);

  // Query users with their type
  const users = await prisma.user.findMany({
    include: {
      user_type: true,
      faces: true
    }
  });

  console.log('All users:', JSON.stringify(users, null, 2));
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
Run the example:
npx tsx example.ts
The tsx package is included as a dev dependency for running TypeScript files directly.

Understanding the Schema

BD Scan Face uses a relational database schema optimized for face recognition and access control:

Core Models

model User {
  user_id           Int       @id @default(autoincrement())
  ci                String    @unique @db.VarChar(20)
  first_name        String    @db.VarChar(100)
  last_name         String    @db.VarChar(100)
  email             String    @unique @db.VarChar(100)
  phone             String?   @db.VarChar(20)
  user_type_id      Int
  code              Int       @unique
  status            Boolean   @default(true)
  registration_date DateTime  @default(now())

  user_type   UserType     @relation(fields: [user_type_id], references: [user_type_id])
  faces       Face[]
  access_logs AccessLog[]
}

Next Steps

Database Schema

Explore the complete database schema and relationships

Prisma Client Guide

Learn all available Prisma Client methods

Docker Setup

Configure Docker for your environment

API Reference

Browse the model API reference

Common Queries

Here are some frequently used queries:

Create an Access Log Entry

const accessLog = await prisma.accessLog.create({
  data: {
    user_id: 1,
    device_id: 1,
    confidence: 98.5,
    access_type: 'entry',
    status: 'granted',
    enterCode: false
  },
  include: {
    user: true,
    device: true
  }
});

Query Recent Access Logs

const recentLogs = await prisma.accessLog.findMany({
  where: {
    access_date: {
      gte: new Date(Date.now() - 24 * 60 * 60 * 1000) // Last 24 hours
    }
  },
  include: {
    user: {
      select: {
        first_name: true,
        last_name: true,
        email: true
      }
    },
    device: {
      select: {
        name: true,
        location: true
      }
    }
  },
  orderBy: {
    access_date: 'desc'
  }
});

Register a Device

const device = await prisma.device.create({
  data: {
    name: 'Front Entrance Scanner',
    location: 'Building A - Main Lobby',
    ip_address: '192.168.1.100',
    status: true
  }
});
Always remember to disconnect the Prisma Client when your application shuts down:
await prisma.$disconnect();

Build docs developers (and LLMs) love