Skip to main content

Overview

Churches are the central organizational unit in the Church Management System. Each church has a pastor (administrator), members, contact information, and associated donation campaigns.

Church Schema

The church data model contains all essential information about a church organization:
{
  pastor: {
    type: ObjectId,
    ref: "User",
    required: true
  },
  name: {
    type: String,
    required: true,
    unique: true
  },
  address: {
    type: String,
    required: true
  },
  supportcontact: {
    phone: {
      type: String,
      required: true
    },
    email: {
      type: String,
      required: true,
      lowercase: true,
      match: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
    },
    website: String
  },
  members: [
    {
      type: ObjectId,
      ref: "User"
    }
  ],
  createdAt: Date,
  updatedAt: Date
}
See the complete schema in churchmodel.js:3-43

Church Hierarchy

Pastor (Administrator)

Each church has exactly one pastor who serves as the primary administrator:
  • Referenced by ObjectId - Links to a User document with role: "pastor"
  • Full administrative control - Can manage all church settings and members
  • Cannot be removed - The pastor relationship is permanent for the church entity
The pastor field is required during church creation and must reference a user with the pastor role.

Members

Churches maintain an array of member references:
  • Array of ObjectIds - Each references a User document
  • Flexible membership - Members can be added or removed by the pastor
  • Cross-referenced - Members can belong to multiple churches if needed
{
  "members": [
    "507f1f77bcf86cd799439011",
    "507f1f77bcf86cd799439012",
    "507f1f77bcf86cd799439013"
  ]
}

Church Properties

Basic Information

  • Required: Yes
  • Unique: Must be unique across all churches
  • Type: String
  • Used for identification and display throughout the system
  • Required: Yes
  • Type: String
  • Physical location of the church
  • Used for member information and mapping features

Support Contact Information

The supportcontact object contains multiple contact methods:
supportcontact: {
  phone: "555-0123-4567",      // Required
  email: "[email protected]",  // Required, validated format
  website: "https://church.com" // Optional
}
The support contact email is validated using a regex pattern to ensure proper email format. Invalid emails will be rejected during church creation or updates.

Email Validation

Support contact emails must match the pattern:
/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
This ensures:
  • Valid email structure
  • Proper domain format
  • Automatic lowercase conversion

Virtual Fields

Active Members Count

The church schema includes a virtual field that calculates the total number of members:
churchSchema.virtual("activeMembersCount").get(function () {
  return this.members?.length || 0;
});
See implementation in churchmodel.js:45-47
Virtual fields are computed at runtime and are not stored in the database. They provide convenient access to derived data without additional storage overhead.
Usage Example:
{
  "_id": "507f1f77bcf86cd799439011",
  "name": "Grace Community Church",
  "members": ["...", "...", "..."],
  "activeMembersCount": 3
}

Timestamps

All church documents automatically include timestamp fields:
  • createdAt - When the church was first created
  • updatedAt - Last modification timestamp
These are managed automatically by Mongoose with the { timestamps: true } option.

Relationships

Church → User (Pastor)

  • One-to-one relationship
  • Required reference
  • Pastor must have role: "pastor"

Church → Users (Members)

  • One-to-many relationship
  • Array of references
  • Members can be added/removed dynamically

Church → Donations

  • One-to-many relationship
  • Donations reference their parent church
  • See Donations for details

Common Operations

Creating a Church

Only users with the pastor role can create churches.
Required fields:
  • Pastor ID (must be a valid User ObjectId with pastor role)
  • Unique church name
  • Physical address
  • Support contact (phone and email)
Example request:
{
  "pastor": "507f1f77bcf86cd799439011",
  "name": "Grace Community Church",
  "address": "123 Main Street, Springfield, ST 12345",
  "supportcontact": {
    "phone": "555-0123-4567",
    "email": "[email protected]",
    "website": "https://gracechurch.com"
  }
}

Managing Members

Pastors can add or remove members from their church: Adding a member:
  • Push the user’s ObjectId to the members array
  • User must exist in the system
  • No duplicate checking is enforced at schema level
Removing a member:
  • Remove the user’s ObjectId from the members array
  • Does not delete the user account
  • User retains access to their profile

Updating Church Information

All church fields except pastor can be updated:
The church name must remain unique. Attempting to update to an existing name will result in a validation error.

Validation Rules

FieldValidation
pastorRequired, must be valid ObjectId
nameRequired, must be unique
addressRequired
supportcontact.phoneRequired
supportcontact.emailRequired, must match email pattern
supportcontact.websiteOptional
membersOptional array of valid ObjectIds

Best Practices

  1. Verify pastor role - Always ensure the pastor field references a user with role: "pastor"
  2. Validate member IDs - Check that member ObjectIds exist before adding them
  3. Use activeMembersCount - Leverage the virtual field instead of calculating manually
  4. Maintain unique names - Church names should be descriptive and unique
  5. Keep contact info current - Regularly update support contact information

Build docs developers (and LLMs) love