> ## Documentation Index
> Fetch the complete documentation index at: https://docs.spurtcommerce.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Blogs

The  **Blogs** package (`@spurtcommerce/blogs`) provides blog management and storefront blog functionality for SpurtCommerce Community and Enterprise editions.

It includes blog post management for vendors and admins, hierarchical blog categories, tags, related blogs, and storefront browsing with comments.

Unlike add-ons that rely on automatic package discovery, this module is integrated explicitly through its exported initialization function, TypeORM entities, and `routing-controllers` controllers.

## Key Features

* **Vendor and Admin blog management:** Create, view, update, and delete blog posts, generate slugs, assign categories, upload cover images, manage publication status, and associate related blogs.
* **Hierarchical categories:** Organize categories using parent-child relationships with category path calculations through `BlogCategoryPath`.
* **Storefront blog browsing:** List published blogs, view blog details, search, and filter by category or tag.
* **Comments and replies:** Accept public comments, support nested replies, and provide moderation functionality.
* **Tag management:** Create tags and associate them with blog posts through a many-to-many relationship.
* **Related blogs:** Associate blog posts to provide related-content recommendations.
* **Host-application adapters:** Integrate with the host application's TypeORM DataSource, media storage, user resolution, and authentication middleware without hardcoded relative imports into the host project.

## Requirements

<table>
  <colgroup>
    <col width="171" />

    <col width="147" />
  </colgroup>

  <thead>
    <tr>
      <th>Requirement</th>
      <th>Version</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Node.js</td>
      <td>`>=18.x`</td>
    </tr>

    <tr>
      <td>TypeScript</td>
      <td>`>=4.9.x` / `5.x`</td>
    </tr>

    <tr>
      <td>TypeORM</td>
      <td>`>=0.3.x`</td>
    </tr>

    <tr>
      <td>routing-controllers</td>
      <td>`>=0.11.x`</td>
    </tr>

    <tr>
      <td>typedi</td>
      <td>`>=0.10.x`</td>
    </tr>
  </tbody>
</table>

The package also uses the following runtime and host-provided dependencies.

**Runtime dependency**

| Package | Version |
| - | - |
| `moment` | `^2.31.0` |

**Peer dependencies**

These dependencies are expected to be provided by the host SpurtCommerce application:

| Package | Version |
| - | - |
| `class-transformer` | `^0.5.1` |
| `class-validator` | `>=0.14.0` |
| `reflect-metadata` | `^0.2.2` |
| `routing-controllers` | `^0.11.3` |
| `typedi` | `^0.10.0` |
| `typeorm` | `>=0.3.0` |

## Installation

Run the following command from the root directory of your SpurtCommerce project:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-light-default"}}
npm install @spurtcommerce/blogs
```

For local development or workspace integration, install the package from its local path:

```bash theme={"theme":{"light":"github-dark-default","dark":"github-light-default"}}
npm install /path/to/spurtcommerce-blogs
```

## Setup and Integration

The package exposes an initialization function, entities, and controllers. Register these components with the host application.

### 1. Configure the TypeORM DataSource

Import `BlogEntities` and include them in the application's TypeORM DataSource configuration.

```typescript theme={"theme":{"light":"github-dark-default","dark":"github-light-default"}}
import { DataSource } from 'typeorm';
import { BlogEntities } from 'spurtcommerce-blogs';

export const AppDataSource = new DataSource({
  type: 'mysql',
  // Add your existing database connection settings.
  entities: [
    ...BlogEntities,
    // Add your host application entities.
  ],
});
```

Use the DataSource configured for your application when initializing the blog module.

### 2. Initialize the Blog Module

Import `initBlog` and pass the application's DataSource. The module also accepts logger, media, and user adapters.

```typescript theme={"theme":{"light":"github-dark-default","dark":"github-light-default"}}
import { initBlog } from 'spurtcommerce-blogs';
import { AppDataSource } from './dataSource';

initBlog({
  dataSource: AppDataSource,
  logger: {
    info: (msg, ...args) => console.log(`[INFO] ${msg}`, ...args),
    error: (msg, ...args) => console.error(`[ERROR] ${msg}`, ...args),
    warn: (msg, ...args) => console.warn(`[WARN] ${msg}`, ...args),
  },
  media: {
    // Configure your media handling adapter
    // (for example, S3 or local file storage).
  },
  user: {
    // Configure your host application's user resolution adapter.
  },
});
```

The `media` adapter connects blog cover-image handling to the host application's storage solution. The `user` adapter connects blog operations to the host application's user-resolution logic.

### 3. Register the Blog Controllers

Import `BlogControllers` and register them with `routing-controllers` when creating the Express application.

```typescript theme={"theme":{"light":"github-dark-default","dark":"github-light-default"}}
import { createExpressServer } from 'routing-controllers';
import { BlogControllers } from 'spurtcommerce-blogs';

const app = createExpressServer({
  controllers: [
    ...BlogControllers,
    // Add your host application controllers.
  ],
});
```

Ensure the host application's authentication middleware is configured as required for the protected routes and the application's user-resolution flow.

## Exported Components

### Entities

The package exports the following entities and base model:

| Export | Purpose |
| - | - |
| `Blog` | Blog post records |
| `BlogCategory` | Blog category records |
| `BlogCategoryPath` | Parent-child category path relationships |
| `BlogRelated` | Related blog associations |
| `BlogComment` | Blog comments and replies |
| `Tag` | Blog tags |
| `BlogTag` | Blog-to-tag relationships |
| `BaseModel` | Shared base model |

### Controllers

| Controller | Responsibility |
| - | - |
| `VendorBlogController` | Vendor blog management |
| `BlogCategoryController` | Blog category management |
| `StoreBlogController` | Storefront blog browsing |
| `StoreBlogCommentController` | Storefront blog comments |

### Services and Repositories

The package also exports service and repository layers for its core modules:

* `BlogService` / `BlogRepository`
* `BlogCategoryService` / `BlogCategoryRepository`
* `BlogCategoryPathService` / `BlogCategoryPathRepository`
* `BlogRelatedService` / `BlogRelatedRepository`
* `BlogCommentService` / `BlogCommentRepository`
* `TagService` / `TagRepository`
* `BlogTagService` / `BlogTagRepository`

## Functional Overview

### Blog Posts

Vendors and admins can manage blog posts, including their content, slug, category assignment, cover image, publication status, and related blog associations.

### Blog Categories

Categories support nested parent-child structures. The `BlogCategoryPath` model stores the calculated paths used to represent the category hierarchy.

### Tags

Tags provide an additional way to organize and filter blog content. The `Tag` and `BlogTag` models provide the tag records and their associations with blog posts.

### Storefront and Comments

The storefront controllers expose blog browsing functionality for published content. Visitors can browse and search blogs, filter by category or tag, open blog details, and submit comments. The module also supports nested replies and comment moderation.

## Media and Host Application Integration

The blog module is designed to remain decoupled from host application internals. It uses the supplied TypeORM DataSource and adapters rather than relying on hardcoded relative imports into the SpurtCommerce application.

Configure the adapters to match the host application's implementation for:

* **Media storage:** Cover image handling, such as S3 or local file storage.
* **User resolution:** Resolving the current application user for blog operations.
* **Authentication middleware:** Applying the host application's authentication rules to the relevant routes.

The exact adapter contracts and authentication configuration should follow the package's exported types and implementation.

## License

The package is distributed under the **ISC License**.

## Summary

`@spurtcommerce/blogs` adds a complete blog module to SpurtCommerce, covering vendor and admin blog management, hierarchical categories, tags, related content, and storefront comments. Integration is performed by configuring the TypeORM DataSource, calling `initBlog`, and registering `BlogControllers` with `routing-controllers`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.