System Schema Definition

The System Schema serves as a foundational sub-schema, designed to incorporate essential system fields into various documents throughout the project. It’s a pivotal part of the schema structure, ensuring consistency and reliability in tracking the state and lifecycle of documents.

Key Fields of the System Schema

The default System Schema includes the following fields:
  • createdAt The date and time when the document was created.
  • modifiedAt The date and time when the document was last modified.
  • archived A boolean indicating whether the document is archived.
  • archivedAt The date and time when the document was archived.

Integration with Other Schemas

This schema should be embedded in all project schemas. Fields that are universally required across various tables should be defined within this System Schema. Depending on specific project needs, it can be further expanded to accommodate additional global fields.

CreatedAt and ModifiedAt

createdAt & modifiedAt These fields are automatically generated and managed by Mongoose when the schema is defined with the following configuration:
@Schema({
  timestamps: { createdAt: 'system.createdAt', updatedAt: 'system.modifiedAt' },
})

Archived and ArchivedAt

archived & archivedAt These fields are optional and can be used to track the state of a document. They are not automatically managed by Mongoose and must be set manually. The archived field is a boolean that indicates whether the document is archived. The archivedAt field is a date that indicates when the document was archived. It’s important to note that the archival logic is considered and managed from the Generic Crud Client.

System Schema Definition

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type SystemDocument = System & Document;

@Schema({ _id: false })
export class System {
  @Prop()
  createdAt: Date;

  @Prop()
  modifiedAt: Date;

  @Prop({ default: false, required: false })
  archived?: boolean;

  @Prop({ required: false })
  archivedAt?: Date;
}

export const SystemSchema = SchemaFactory.createForClass(System);
The System Schema exemplifies a best practice in schema design, providing a unified approach to managing common fields and states across multiple document types within a project.