API Reference

ObjectProvider

Documentation for the ObjectProvider interface.

ObjectProvider

The ObjectProvider interface defines a standard contract for modules to provide access to specific types of data. Its primary purpose is to centralize data access logic (fetching, updating, deleting) in a consistent manner across the application.

It is simply a structured interface for data retrieval and manipulation.

Interface Definition

The interface uses generics to define the type of keys (K) and the type of values (V) it handles.

import type { ObjectCache } from "jopijs";

/**
 * Base parameters passed to ObjectProvider methods.
 */
export interface ObjectProviderParams {
    /**
     * The cache instance associated with this provider. 
     * Can be used to manually store or retrieve data from the cache.
     */
    cache: ObjectCache;

    /**
     * The name of the provider.
     */
    providerName: string;
}

/**
 * Interface representing a provider for a specific type of object.
 * ObjectProviders are used to centralize data fetching logic.
 * They are typically defined in a module and shared across the application.
 */
export interface ObjectProvider<K=any,V=any> {
    get(keys: K, params: ObjectProviderParams): Promise<V>;
    set?(keys: K, value: V, params: ObjectProviderParams): Promise<void>;
    delete?(keys: K, params: ObjectProviderParams): Promise<void>;
}

Methods

get(keys, params)

Required. Retrieves the value(s) associated with the provided keys.

  • keys: The criteria or identifier used to fetch the data (e.g., an ID, a query object).
  • params: Contains context like the cache instance and providerName.
  • Returns: A Promise resolving to the requested data.

set(keys, value, params)

Optional. Creates or updates the value for a given key.

  • keys: Identifier for the data.
  • value: The data to save.
  • params: Context parameters.

delete(keys, params)

Optional. Removes the data associated with the key.

  • keys: Identifier for the data to delete.
  • params: Context parameters.

Implementation Example

To create a new ObjectProvider using the JopiJS linker system, you should create a file structure like: src/.../@alias/objectProviders/<provider_name>/index.ts

Here is a simple example for a user.profile provider:

import type { ObjectProvider } from "jopijs";

// Define the shape of the data you return
interface UserProfile {
    id: string;
    username: string;
    email: string;
}

// Define the keys required to fetch the data
type UserKey = {
    userId: string;
};

// Export the provider
// The generic signature is: ObjectProvider<Keys, Result>
export default <ObjectProvider<UserKey, UserProfile>>{
    async get(keys, params) {        
        // Mock response
        // In a real app, you would fetch from a DB or API
        return {
            id: keys.userId,
            username: "jdoe",
            email: "john@example.com"
        };
    },

    // Optional: Implement set/delete
}

Usage

Once defined, you can import and use your provider from anywhere in your application (pages, API routes, other modules) using the @/objectProviders/ alias.

// Import using the provider name
import userProfileProvider from "@/objectProviders/user.profile";

async function displayUser(id: string) {
    // Call the .get() method with your keys
    const user = await userProfileProvider.get({ userId: id });

    console.log(`Hello, ${user.username}!`);
}