@vendure/core
Version:
A modern, headless ecommerce framework
217 lines (216 loc) • 8.62 kB
TypeScript
import { OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { SettingsStoreScopeType } from '@vendure/common/lib/generated-types';
import { JsonCompatible } from '@vendure/common/lib/shared-types';
import { RequestContext } from '../../../api/common/request-context';
import { ConfigService } from '../../../config/config.service';
import { CleanupOrphanedSettingsStoreEntriesOptions, CleanupOrphanedSettingsStoreEntriesResult, OrphanedSettingsStoreEntry, SetSettingsStoreValueResult, SettingsStoreFieldConfig, SettingsStoreRegistration } from '../../../config/settings-store/settings-store-types';
import { TransactionalConnection } from '../../../connection/transactional-connection';
/**
* @description
* The SettingsStoreService provides a flexible settings storage system with support for
* scoping, permissions, and validation. It allows plugins and the core system to
* store and retrieve configuration data with fine-grained control over access and isolation.
*
* ## Usage
*
* Values are automatically scoped according to their field configuration:
*
* @example
* ```ts
* // In a service
* const userTheme = await this.settingsStoreService.get('dashboard.theme', ctx);
* await this.settingsStoreService.set('dashboard.theme', 'dark', ctx);
*
* // Get multiple values
* const settings = await this.settingsStoreService.getMany([
* 'dashboard.theme',
* 'dashboard.tableFilters'
* ], ctx);
* ```
*
* @docsCategory services
* @since 3.4.0
*/
export declare class SettingsStoreService implements OnModuleInit {
private readonly connection;
private readonly moduleRef;
private readonly configService;
private readonly fieldRegistry;
private readonly injector;
constructor(connection: TransactionalConnection, moduleRef: ModuleRef, configService: ConfigService);
onModuleInit(): void;
/**
* @description
* Initialize field registrations from the Vendure configuration.
* Called during module initialization.
*/
private initializeFieldRegistrations;
/**
* @description
* Register settings store fields. This is typically called during application
* bootstrap when processing the VendureConfig.
*/
register(registration: SettingsStoreRegistration): void;
/**
* @description
* Get a value for the specified key. The value is automatically scoped
* according to the field's scope configuration.
*
* @param key - The full key (namespace.field)
* @param ctx - Request context for scoping and permissions
* @returns The stored value or undefined if not found or access denied
*/
get<T = JsonCompatible<any>>(ctx: RequestContext, key: string): Promise<T | undefined>;
/**
* @deprecated Use the `ctx` arg in the first position
*/
get<T = JsonCompatible<any>>(key: string, ctx: RequestContext): Promise<T | undefined>;
/**
* @description
* Get multiple values efficiently. Each key is scoped according to
* its individual field configuration.
*
* @param keys - Array of full keys to retrieve
* @param ctx - Request context for scoping and permissions
* @returns Object mapping keys to their values
*/
getMany(ctx: RequestContext, keys: string[]): Promise<Record<string, JsonCompatible<any>>>;
/**
* @deprecated Use `ctx` as the first argument
*/
getMany(keys: string[], ctx: RequestContext): Promise<Record<string, JsonCompatible<any>>>;
/**
* @description
* Set a value for the specified key with structured result feedback.
* This version returns detailed information about the success or failure
* of the operation instead of throwing errors.
*
* @param key - The full key (namespace.field)
* @param value - The value to store (must be JSON serializable)
* @param ctx - Request context for scoping and permissions
* @returns SetSettingsStoreValueResult with operation status and error details
*/
set<T extends JsonCompatible<any> = JsonCompatible<any>>(ctx: RequestContext, key: string, value: T): Promise<SetSettingsStoreValueResult>;
/**
* @deprecated Use `ctx` as the first argument
*/
set<T extends JsonCompatible<any> = JsonCompatible<any>>(key: string, value: T, ctx: RequestContext): Promise<SetSettingsStoreValueResult>;
/**
* @description
* Set multiple values with structured result feedback for each operation.
* This method will not throw errors but will return
* detailed results for each key-value pair.
*/
setMany(ctx: RequestContext, values: Record<string, JsonCompatible<any>>): Promise<SetSettingsStoreValueResult[]>;
/**
* @deprecated Use `ctx` as the first argument
*/
setMany(values: Record<string, JsonCompatible<any>>, ctx: RequestContext): Promise<SetSettingsStoreValueResult[]>;
/**
* @description
* Get the field configuration for a key.
*/
getFieldDefinition(key: string): SettingsStoreFieldConfig | undefined;
/**
* @description
* Returns all registered field definitions with their full keys.
*
* @since 3.6.0
*/
getAllFieldDefinitions(): Array<{
key: string;
config: SettingsStoreFieldConfig;
}>;
/**
* @description
* Determines the scope type of a field by comparing its scope function reference
* to the pre-built SettingsStoreScopes functions.
*
* @since 3.6.0
*/
getScopeType(fieldConfig: SettingsStoreFieldConfig): SettingsStoreScopeType;
/**
* @description
* Validate a value against its field definition.
*/
validateValue(key: string, value: any, ctx: RequestContext): Promise<string | void>;
/**
* @description
* Generate the scope key for a given field and context.
*/
private generateScope;
/**
* @description
* Get field configuration, throwing if not found.
*/
private getFieldConfig;
/**
* @description
* Find orphaned settings store entries that no longer have corresponding field definitions.
*
* @param options - Options for filtering orphaned entries
* @returns Array of orphaned entries
*/
findOrphanedEntries(options?: CleanupOrphanedSettingsStoreEntriesOptions): Promise<OrphanedSettingsStoreEntry[]>;
/**
* @description
* Clean up orphaned settings store entries from the database.
*
* @param options - Options for the cleanup operation
* @returns Result of the cleanup operation
*/
cleanupOrphanedEntries(options?: CleanupOrphanedSettingsStoreEntriesOptions): Promise<CleanupOrphanedSettingsStoreEntriesResult>;
/**
* @description
* Parse a duration string (e.g., '7d', '30m', '2h') into a Date object.
*/
private parseDuration;
/**
* @description
* Get a preview of a value for logging purposes, truncating if too large.
*/
private getValuePreview;
/**
* @description
* Check if the current user has permission to access a field.
* This is not called internally in the get and set methods, so should
* be used by any methods which are exposing these methods via the GraphQL
* APIs.
* @deprecated Use `hasReadPermission` or `hasWritePermission` for granular control
*/
hasPermission(ctx: RequestContext, key: string): boolean;
/**
* @description
* Check if the current user has permission to read a field.
* @since 3.5.0
*/
hasReadPermission(ctx: RequestContext, key: string): boolean;
/**
* @description
* Check if the current user has permission to write a field.
* @since 3.5.0
*/
hasWritePermission(ctx: RequestContext, key: string): boolean;
/**
* @description
* Helper method to check if a permission configuration is a read/write object.
*/
private isReadWritePermissionObject;
/**
* @description
* Helper method to check simple permissions (single permission, array, or string).
*/
private checkSimplePermissions;
/**
* @description
* Returns true if the settings field has the `readonly: true` configuration.
*/
isReadonly(key: string): boolean;
/**
* This unfortunate workaround is here because in the first version of the SettingsStore we have the
* ctx arg last, which goes against all patterns in the rest of the code base. In v3.4.2 we overload
* the methods to allow the correct ordering, and deprecate the original order.
*/
private determineCtx;
}