Skip to content

Latest commit

 

History

History
184 lines (140 loc) · 4.93 KB

File metadata and controls

184 lines (140 loc) · 4.93 KB

ObjectQL Plugin Implementation Summary

Overview

This implementation addresses the requirement to allow ObjectQL to be registered via plugins instead of being hardcoded in the ObjectStackKernel constructor. This provides flexibility for users who have separate ObjectQL implementations or need custom configuration.

What Changed

1. New Files

packages/runtime/src/objectql-plugin.ts

  • Purpose: Runtime plugin for ObjectQL engine registration
  • Key Features:
    • Implements RuntimePlugin interface
    • Accepts optional custom ObjectQL instance
    • Supports custom host context configuration
    • Registers ObjectQL during the install phase

packages/runtime/README.md

  • Purpose: Documentation for the runtime package
  • Contents:
    • Usage examples for both plugin-based and backward-compatible approaches
    • API reference for ObjectStackKernel and ObjectQLPlugin
    • Migration guide from hardcoded to plugin-based registration

examples/custom-objectql-example.ts

  • Purpose: Demonstrates custom ObjectQL instance usage
  • Shows:
    • How to create a custom ObjectQL instance
    • How to pre-configure hooks
    • How to pass custom host context

2. Modified Files

packages/runtime/src/kernel.ts

  • Changes:
    • Changed ql property from direct initialization to deferred (using ! assertion)
    • Added plugin detection logic in constructor
    • Maintains backward compatibility by auto-initializing ObjectQL if no plugin detected
    • Added warning message for backward compatibility mode

packages/runtime/src/index.ts

  • Changes:
    • Added export for ObjectQLPlugin

examples/host/src/index.ts

  • Changes:
    • Updated to use ObjectQLPlugin() in the plugins array
    • Added comments showing both default and custom usage options

examples/host/debug-registry.ts

  • Changes:
    • Updated to use ObjectQLPlugin() for consistency

examples/msw-react-crud/src/mocks/browser.ts

  • Changes:
    • Updated to use ObjectQLPlugin() in the plugins array

Technical Design

Plugin Detection Logic

const hasObjectQLPlugin = plugins.some(p => 
  p && typeof p === 'object' && 'install' in p && p.name?.includes('objectql')
);

This detection:

  • Checks for object type
  • Verifies install method exists
  • Matches plugin name containing 'objectql'

Installation Flow

  1. Construction Phase: Kernel constructor checks for ObjectQLPlugin
  2. Install Phase: ObjectQLPlugin.install() attaches ObjectQL to kernel
  3. Start Phase: Kernel uses the registered ObjectQL instance

Backward Compatibility

If no ObjectQLPlugin is detected:

  • Kernel auto-initializes ObjectQL with default settings
  • Warning message suggests using ObjectQLPlugin
  • Existing code continues to work without changes

Usage Patterns

Pattern 1: Default ObjectQL (Recommended)

const kernel = new ObjectStackKernel([
  new ObjectQLPlugin(),
  // ... other plugins
]);

Pattern 2: Custom ObjectQL Instance

const customQL = new ObjectQL({ env: 'production' });
const kernel = new ObjectStackKernel([
  new ObjectQLPlugin(customQL),
  // ... other plugins
]);

Pattern 3: Backward Compatible (Legacy)

// Still works, shows warning
const kernel = new ObjectStackKernel([
  // ... plugins without ObjectQLPlugin
]);

Benefits

  1. Flexibility: Users can provide custom ObjectQL implementations
  2. Separation of Concerns: ObjectQL is now a first-class plugin
  3. Testability: Easier to mock or replace ObjectQL for testing
  4. Explicit Dependencies: Makes ObjectQL dependency visible in plugin list
  5. Configuration: Custom host context can be passed to ObjectQL
  6. Backward Compatible: Existing code continues to work

Migration Guide

For New Projects

Use Pattern 1 (recommended):

new ObjectStackKernel([
  new ObjectQLPlugin(),
  // other plugins
]);

For Existing Projects

No changes required! But consider migrating:

Before:

const kernel = new ObjectStackKernel([appConfig, driver]);

After:

const kernel = new ObjectStackKernel([
  new ObjectQLPlugin(),
  appConfig,
  driver
]);

For Custom ObjectQL Projects

Use Pattern 2:

import { MyCustomObjectQL } from './my-objectql';

const customQL = new MyCustomObjectQL({ /* config */ });
const kernel = new ObjectStackKernel([
  new ObjectQLPlugin(customQL),
  // other plugins
]);

Validation

All validations passed:

  • ✅ Structural validation (files exist, exports correct)
  • ✅ Plugin detection logic (6 test cases)
  • ✅ Example usage (3 examples validated)
  • ✅ Backward compatibility

Future Enhancements

Possible future improvements:

  1. Add TypeScript strict mode compatibility
  2. Add comprehensive test suite
  3. Support multiple ObjectQL instances for multi-tenant scenarios
  4. Add ObjectQL instance validation in plugin
  5. Support lazy initialization for better performance