# Object and Record Creation Test ## Goal Test that the Objection.js model system properly handles system-managed fields: - ownerId (should be auto-set from userId) - created_at (should be auto-set to current timestamp) - updated_at (should be auto-set to current timestamp) - id (should be auto-generated UUID) Users should NOT need to provide these fields when creating records. ## Test Sequence ### 1. Create an Object (if not exists) ```bash curl -X POST http://localhost:3001/api/objects \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "X-Tenant-ID: tenant1" \ -d '{ "apiName": "TestContact", "label": "Test Contact", "pluralLabel": "Test Contacts", "description": "Test object for model validation" }' ``` Expected response: ```json { "id": "uuid...", "apiName": "TestContact", "label": "Test Contact", "tableName": "test_contacts", "...": "..." } ``` ### 2. Create a Record WITHOUT System Fields This should succeed and system fields should be auto-populated: ```bash curl -X POST http://localhost:3001/api/records/TestContact \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "X-Tenant-ID: tenant1" \ -d '{ "name": "John Doe", "email": "john@example.com" }' ``` Expected response: ```json { "id": "uuid-auto-generated", "name": "John Doe", "email": "john@example.com", "ownerId": "current-user-id", "created_at": "2025-01-26T...", "updated_at": "2025-01-26T...", "tenantId": "tenant-uuid" } ``` ### 3. Verify Fields Were Set Automatically ```bash curl -X GET http://localhost:3001/api/records/TestContact/RECORD_ID \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "X-Tenant-ID: tenant1" ``` Verify response includes: - ✅ id (UUID) - ✅ ownerId (matches current user ID) - ✅ created_at (timestamp) - ✅ updated_at (timestamp) - ✅ name, email (provided fields) ### 4. Update Record and Verify updated_at Changes Get the created_at value, wait a second, then update: ```bash curl -X PATCH http://localhost:3001/api/records/TestContact/RECORD_ID \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_JWT_TOKEN" \ -H "X-Tenant-ID: tenant1" \ -d '{ "name": "Jane Doe" }' ``` Verify in response: - ✅ name is updated to "Jane Doe" - ✅ updated_at is newer than original created_at - ✅ created_at is unchanged - ✅ ownerId is unchanged (not overwritable) ## Key Points to Verify 1. **System Fields Not Required**: Record creation succeeds without ownerId, created_at, updated_at 2. **Auto-Population**: System fields are populated automatically by model hooks 3. **Immutable Owner**: ownerId cannot be changed via update (filtered out in ObjectService.updateRecord) 4. **Timestamp Management**: created_at stays same, updated_at changes on update 5. **Model Used**: Debug logs should show model is being used (look for "Registered model" logs) ## Troubleshooting If tests fail, check: 1. **Model Registration**: Verify model appears in logs after object creation 2. **Hook Execution**: Add debug logs to DynamicModel.$beforeInsert and $beforeUpdate 3. **Model Binding**: Verify getBoundModel returns properly bound model with correct knex instance 4. **Field Validation**: Check if JSON schema validation is preventing record creation ## Related Files - [backend/src/object/models/dynamic-model.factory.ts](backend/src/object/models/dynamic-model.factory.ts) - Model creation with hooks - [backend/src/object/models/model.service.ts](backend/src/object/models/model.service.ts) - Model lifecycle management - [backend/src/object/object.service.ts](backend/src/object/object.service.ts) - Updated CRUD to use models