# Module Wiring

Module wiring is split across two files:

- `src/modules.ts` — composes erp-kit modules and exports module objects for resolver use
- `src/modules-db.ts` — destructures DB types into individual named exports for the DB scanner (`tailor.config.ts` db.files)

### `src/modules.ts`

```ts
import {
  definePrimitivesModule,
  defineUserManagementModule,
  defineItemManagementModule,
  defineAuditModule,
} from "@tailor-platform/erp-kit/module";

const primitivesModules = definePrimitivesModule({});

export const umModules = defineUserManagementModule({
  dbNamespace: "main-db",
  user: {
    fields: {
      /* custom fields */
    },
  },
});

// Pass inter-module dependencies explicitly
export const imModules = defineItemManagementModule({
  primitives: {
    db: { unit: primitivesModules.db.unit },
    queries: { getUnit: primitivesModules.queries.getUnit },
  },
});

export const auditModules = defineAuditModule();

export const primitivesModulesExport = primitivesModules;
```

### `src/modules-db.ts`

```ts
import { auditCompanyStub } from "@tailor-platform/erp-kit/module";
import { umModules, imModules, primitivesModulesExport, auditModules } from "./modules";

// Destructured for DB scanner (tailor.config.ts db.files).
// This file must NOT be imported by resolvers — property access prevents tree-shaking.

// user-management
export const { user, permission, role, userRole, rolePermission } = umModules.db;

// primitives
export const { uomCategory, unit, currency, exchangeRate } = primitivesModulesExport.db;

// item-management
export const { item, taxonomyNode, itemTaxonomyAssignment } = imModules.db;

// audit
export const { auditableEntity, auditPolicy, policyFieldRule, auditEntry, changeDetail } =
  auditModules.db;
export { auditCompanyStub as company };
```

Key conventions:

- Prefix exports to avoid collisions (`umModules`, `imModules`, `auditModules`)
- `modules-db.ts` must NOT be imported by resolvers — it exists solely for the DB scanner
- Every module's `db` must be destructured into individual table exports in `modules-db.ts` — the DB scanner only picks up named exports
- Extract individual executors for re-export in executor files
