### Install Adonis Auditing (Ace Command) Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/installation.md Installs the Adonis Auditing package and automatically configures it using the `node ace add` command for a streamlined setup. ```sh node ace add @stouder-io/adonis-auditing ``` -------------------------------- ### Install Adonis Auditing (Manual) Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/installation.md Manually installs the Adonis Auditing package using package managers like npm, pnpm, or yarn, followed by the configuration command to integrate it into your AdonisJS project. ```npm npm install @stouder-io/adonis-auditing node ace configure @stouder-io/adonis-auditing ``` ```pnpm pnpm install @stouder-io/adonis-auditing node ace configure @stouder-io/adonis-auditing ``` ```yarn yarn add @stouder-io/adonis-auditing node ace configure @stouder-io/adonis-auditing ``` -------------------------------- ### Add Auditable Mixin to Model Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/model-setup.md Demonstrates how to extend an AdonisJS model with auditing capabilities by composing the `Auditable` mixin. This requires importing `compose` from `@adonisjs/core/helpers` and `Auditable` from `@stouder-io/adonis-auditing`. ```typescript import { DateTime } from 'luxon' import { BaseModel, column } from '@adonisjs/lucid/orm' import { compose } from '@adonisjs/core/helpers' import { Auditable } from '@stouder-io/adonis-auditing' export default class Book extends compose(BaseModel, Auditable) { @column({ isPrimary: true }) declare id: number @column() declare title: string @column.dateTime({ autoCreate: true }) declare createdAt: DateTime @column.dateTime({ autoCreate: true, autoUpdate: true }) declare updatedAt: DateTime } ``` -------------------------------- ### Custom Resolver Implementation Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/audit-resolvers.md Demonstrates how to create a custom resolver by implementing the `Resolver` interface. The `resolve` method takes an `HttpContext` and returns a `Promise` of `unknown`. This example returns a random number. ```typescript import { HttpContext } from '@adonisjs/core/http' import { Resolver } from '@stouder-io/adonis-auditing' export default class RandomResolver implements Resolver { async resolve(ctx: HttpContext) { return Math.random() * 100 } } ``` -------------------------------- ### Adonis Auditing Configuration Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/general-configuration.md Demonstrates the default configuration for Adonis Auditing in `config/auditing.ts`. It shows how to set up a custom user resolver and define additional metadata resolvers for IP address, user agent, and URL. ```typescript import { defineConfig } from '@stouder-io/adonis-auditing/setup' export default defineConfig({ userResolver: () => import('#audit_resolvers/user_resolver'), resolvers: { ip_address: () => import('#audit_resolvers/ip_address_resolver'), user_agent: () => import('#audit_resolvers/user_agent_resolver'), url: () => import('#audit_resolvers/url_resolver'), }, }) ``` -------------------------------- ### Audit Model Columns Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/getting-audits.md Defines the structure and fields of the `Audit` Lucid model, which represents an audit record. This model stores details about changes made to auditable entities. ```APIDOC Audit Model Schema: - id: The unique identifier of the audit. - auditableId: The identifier of the audited model. - auditableType: The type of the audited model. - event: The event that triggered the audit (e.g., 'created', 'updated', 'deleted'). - oldValues: The state of the model's attributes before the event. - newValues: The state of the model's attributes after the event. - metadata: Resolved metadata associated with the audit. - createdAt: The date and time when the audit was created. - updatedAt: The date and time when the audit was last updated. ``` -------------------------------- ### Retrieve Audits for Model Instance Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/getting-audits.md Demonstrates fetching all associated audits, the first audit, and the last audit for a given model instance using the Auditable mixin. Requires an instance of an auditable model (e.g., `Book`). ```typescript // Get the first available book\nconst book = await Book.firstOrFail()\n\n// Get all associated audits\nconst audits = await book.audits()\n\n// Get the first audit\nconst first = await book.audits().first()\n\n// Get the last audit\nconst last = await book.audits().last() ``` -------------------------------- ### Package.json Scripts for Stubs Source: https://github.com/stouderio/adonis-auditing/blob/main/stubs/README.md Defines scripts in package.json to copy stubs to the build directory and manage npm publishing. ```json { "scripts": { "copy:templates": "cp -R stubs build/" }, "files": [ "build/stubs" ] } ``` -------------------------------- ### Registering a Custom User Resolver Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/user-resolver.md Illustrates how to register a custom user resolver implementation by adding it to the `userResolver` field in the `config/auditing.ts` file. This configuration step is crucial for the package to utilize your custom resolver. ```typescript import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext' import { AuditingConfig } from '@ioc:Adonis/Addons/Auditing' // Assume MyCustomUserResolver is your implementation of the UserResolver interface import MyCustomUserResolver from 'App/Resolvers/MyCustomUserResolver' const auditingConfig: AuditingConfig = { // ... other configurations userResolver: MyCustomUserResolver // ... other configurations } export default auditingConfig ``` -------------------------------- ### User Resolver Interface Method Source: https://github.com/stouderio/adonis-auditing/blob/main/docs/guide/user-resolver.md Defines the contract for a user resolver in AdonisJS Auditing. The `resolve` method takes an HttpContext instance and returns a Promise resolving to user identification metadata ({ id: string; type: string }) or null, used to store auditableId and auditableType. ```APIDOC UserResolver Interface: resolve(httpContext: HttpContext): Promise<{ id: string; type: string } | null> - Description: Resolves the current authenticated user's ID and type. - Parameters: - httpContext: The AdonisJS HttpContext instance. - Returns: - A Promise that resolves to an object containing 'id' (string) and 'type' (string) of the user, or null if no user is found. - Usage: - Called by the auditing package when an audit is created to capture auditable metadata. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.