@memberjunction/server-bootstrap
MemberJunction Server Bootstrap - Encapsulates all server initialization logic into a single, simple function.
Overview
Section titled “Overview”In MemberJunction 3.0, server applications (MJAPI) became minimal bootstrapping files (~6 lines of code) that import all functionality from NPM packages. This package provides the createMJServer() function that handles all the complex initialization logic.
graph TD
A["createMJServer()"] --> B["Load Configuration<br/>(mj.config.cjs / .mjrc)"]
B --> C["Discover Generated Packages<br/>(entities, actions, resolvers)"]
C --> D["Auto-Import via<br/>@RegisterClass Side Effects"]
D --> E["Build Resolver Paths"]
E --> F["Run beforeStart Hook"]
F --> G["serve() from @memberjunction/server"]
G --> H["Database Connection Pool"]
G --> I["GraphQL Schema Builder"]
G --> J["WebSocket Subscriptions"]
G --> K["REST API Endpoints"]
G --> L["Graceful Shutdown"]
G --> M["Run afterStart Hook"]
style A fill:#2d6a9f,stroke:#1a4971,color:#fff
style B fill:#7c5295,stroke:#563a6b,color:#fff
style C fill:#7c5295,stroke:#563a6b,color:#fff
style D fill:#2d8659,stroke:#1a5c3a,color:#fff
style G fill:#2d6a9f,stroke:#1a4971,color:#fff
style H fill:#b8762f,stroke:#8a5722,color:#fff
style I fill:#b8762f,stroke:#8a5722,color:#fff
style J fill:#b8762f,stroke:#8a5722,color:#fff
style K fill:#b8762f,stroke:#8a5722,color:#fff
Installation
Section titled “Installation”npm install @memberjunction/server-bootstrapBasic Usage (Minimal MJAPI)
Section titled “Basic Usage (Minimal MJAPI)”Create your packages/api/src/index.ts:
import { createMJServer } from '@memberjunction/server-bootstrap';
// Import generated packages to trigger registrationimport '@mycompany/generated-entities';import '@mycompany/generated-actions';import '@mycompany/generated-resolvers';
createMJServer().catch(console.error);That’s it! Your entire MJAPI application in 6 lines.
With Custom Configuration
Section titled “With Custom Configuration”import { createMJServer } from '@memberjunction/server-bootstrap';
createMJServer({ // Add custom resolver paths resolverPaths: ['./custom-resolvers/**/*Resolver.{js,ts}'],
// Run custom logic before server starts beforeStart: async () => { console.log('Initializing custom services...'); // Your initialization code here },
// Run custom logic after server starts afterStart: async () => { console.log('Server is ready!'); // Your post-startup code here },
// Configure REST API restApiOptions: { enabled: true, includeEntities: ['Users', 'Companies'], excludeEntities: ['SystemSettings'] }}).catch(console.error);What It Does
Section titled “What It Does”The createMJServer() function handles:
- Configuration Loading - Automatically finds and loads
mj.config.cjsor.mjrc - Generated Package Discovery - Auto-imports generated entities, actions, forms, and resolvers
- Database Connection - Sets up connection pooling with configured credentials
- GraphQL Schema Building - Discovers resolvers and builds the GraphQL schema
- WebSocket Setup - Configures WebSocket support for GraphQL subscriptions
- REST API Setup - Registers REST endpoints based on configuration
- Graceful Shutdown - Handles SIGTERM/SIGINT signals properly
- Scheduled Jobs - Initializes and starts scheduled job service if enabled
Configuration
Section titled “Configuration”The function uses your mj.config.cjs for all database and server settings:
module.exports = { dbHost: process.env.DB_HOST, dbPort: process.env.DB_PORT || 1433, dbDatabase: process.env.DB_DATABASE, dbUsername: process.env.DB_USERNAME, dbPassword: process.env.DB_PASSWORD,
graphqlPort: process.env.PORT || 4000,
// Code Generation Configuration codeGeneration: { outputMode: 'packages', packageScope: '@mycompany', packages: { entities: { path: './packages/generated-entities', name: '@mycompany/generated-entities' }, actions: { path: './packages/generated-actions', name: '@mycompany/generated-actions' }, graphqlResolvers: { path: './packages/generated-resolvers', name: '@mycompany/generated-resolvers' } } }};API Reference
Section titled “API Reference”createMJServer(options?: MJServerConfig): Promise<void>
Section titled “createMJServer(options?: MJServerConfig): Promise<void>”Creates and starts a MemberJunction API server.
Parameters
Section titled “Parameters”options.configPath- Path to config file (optional, auto-discovers if not provided)options.resolverPaths- Additional resolver paths beyond defaultsoptions.beforeStart- Hook function to run before server startsoptions.afterStart- Hook function to run after server startsoptions.restApiOptions- REST API configuration options
Returns
Section titled “Returns”Promise that resolves when the server is running.
Migration from 2.x
Section titled “Migration from 2.x”In MemberJunction 2.x, your MJAPI/src/index.ts had ~34 lines of boilerplate code for database setup, schema building, etc.
Before (2.x):
// 34 lines of configuration, pool setup, schema building, etc.import { serve } from '@memberjunction/server';// ... lots of setup code ...await serve(resolverPaths);After (3.0):
import { createMJServer } from '@memberjunction/server-bootstrap';createMJServer().catch(console.error);All the complexity is now encapsulated in this package and updated via NPM.
Benefits
Section titled “Benefits”- Zero-copy updates -
npm updatebrings all improvements automatically - No stale code - Bootstrap logic stays up-to-date with MJ releases
- Minimal surface area - Fewer lines of code means fewer places for bugs
- Standard patterns - Everyone uses the same initialization flow
- Extensible - Hooks allow custom logic without modifying core
License
Section titled “License”MIT