ottoman
Version:
Ottoman Couchbase ODM
49 lines (48 loc) • 2.13 kB
TypeScript
import { CoreType, IOttomanType } from '../schema';
import { TransactionAttemptContext } from 'couchbase';
/**
* Cast Strategies
* 'keep' | 'drop' | 'throw' | 'defaultOrDrop' | 'defaultOrKeep'
*
* @desc
* When trying to cast a value to a given type if it fail Cast Strategy will behave this ways for:
* + 'keep' -> The original value will be returned
* + 'drop' -> Return undefined and the object field will be removed.
* + 'throw' -> Throw exception 'Value couldn't be casted to given type'
* + 'defaultOrDrop' -> Try to return default value if exists, if no default value was provided for the current field then it will be removed
* + 'defaultOrDrop' -> Try to return default value if exists, if no default value was provided for the current field then it will keep original value.
*/
export declare enum CAST_STRATEGY {
KEEP = "keep",
DROP = "drop",
THROW = "throw",
DEFAULT_OR_DROP = "defaultOrDrop",
DEFAULT_OR_KEEP = "defaultOrKeep"
}
export declare const ensureArrayItemsType: (array: any, field: any, strategy: any) => any;
export declare const ensureTypes: (item: any, field: IOttomanType, strategy?: CAST_STRATEGY) => unknown;
export interface CastOptions {
strategy?: CAST_STRATEGY;
strict?: boolean;
skip?: string[];
}
/**
* @desc
* Used when trying to apply a value to a given immutable property:
* + **false** -> Allow apply the new value
* + **true** -> Don't allow apply the new value
* + **'throw'** -> Throw exception "ImmutableError: Field 'field_name' is immutable and current cast strategy is set to 'throw'"
*/
export type ApplyStrategy = boolean | CAST_STRATEGY.THROW;
/**
* @desc
* Used by the mutation functions to apply the defined strategy.
*/
export type MutationFunctionOptions = {
strict?: ApplyStrategy;
maxExpiry?: number;
enforceRefCheck?: boolean | 'throw';
transactionContext?: TransactionAttemptContext;
};
export declare const cast: (data: any, schema: any, options?: CastOptions) => any;
export declare const checkCastStrategy: (value: unknown, strategy: CAST_STRATEGY, type: CoreType | IOttomanType) => unknown;