# tabletcommand-backend-models

Mongoose/TypeScript library exporting 64+ data models for the Tablet Command backend. Covers CAD, incidents, users, devices, integrations, and more.

## Installation

```sh
npm install tabletcommand-backend-models
```

## Usage

The primary API is `connect()`, which opens a MongoDB connection and wires all models.

```ts
import { connect } from "tabletcommand-backend-models";

const { connection, models, mongoose } = await connect(process.env.MONGO_URL);

const dept = await models.Department.findById(id);
```

`connect(mongoUrl, opts?)` accepts an optional Mongoose connection options object and returns `{ connection, models, mongoose }`.

## Project Structure

```
src/
├── index.ts          # connect(), wireModels(), exported constants & helpers
├── constants.ts      # Shared enums (AccountIndustry, IncidentFilterKind, etc.)
├── helpers.ts        # Mongoose/TS utilities, ObjectId helpers
├── models/           # 64 Mongoose model files
│   └── schema/       # 17 reusable sub-schemas (geojson, oauth, webhook, etc.)
└── types/            # TypeScript interfaces mirroring each model
```

## Models

| Category | Key Models |
|---|---|
| **CAD** | `CadIncident`, `CadVehicle`, `CadStatus`, `CadVehicleStatusHistory`, `CadSimulation` |
| **Incidents** | `ManagedIncident`, `IncidentEvent`, `IncidentFilter`, `IncidentNotified`, `IncidentTakeover` |
| **Users & Auth** | `User`, `UserDevice`, `Session`, `UserRegistration`, `AudioStreamAuthentication` |
| **Departments & Config** | `Department`, `GlobalSettings`, `Battalion`, `Agency`, `Assignment` |
| **Integrations** | `Esri`, `DeviceMapping`, `GstMapping`, `Partner`, `Mark43IncidentRetry`, `Saml` |
| **Personnel** | `PersonnelRoster`, `PersonnelKnown`, `PersonnelImport` |
| **Beacons** | `StatusBeacon`, `StatusBeaconTransition`, `BeaconLog` |
| **Logging & Comms** | `ActionLog`, `JobLog`, `MailLog`, `SmsLog`, `SmtpUnhandled`, `Message` |
| **Charts / Analytics** | `ChartIncident`, `ChartUser`, `ChartDeviceStats`, `ChartManagedIncident` |
| **Misc** | `RateLimit`, `CsvImport`, `Monitor`, `ValidationReport`, `RemoteLog`, `ReleaseNote` |

## Key Call-outs

**Lazy-loading** — `wireModels()` uses dynamic imports internally. Models are not instantiated until `connect()` is called, avoiding premature Mongoose registration.

**Type safety** — The `BackendModels` type gives fully typed access to every model. 99.79% type coverage is enforced at build time.

**Schema composition** — `src/models/schema/` contains reusable sub-schemas (GeoJSON, OAuth, webhooks, colors, etc.) that are composed into larger models rather than duplicated.

**Decoupled types** — Interfaces live in `src/types/`, Mongoose schemas in `src/models/`. Import types directly without pulling in Mongoose as a dependency.

**Top-level exports** — `connect`, `wireModels`, `BackendModels`, `MongooseTypes`, all constants, and helper utilities are exported from the package root.

## Development

```sh
npm run build   # compile via gulp
npm test        # run tests with tsx
npm run lint    # eslint src/
```
