/
schema.types.ts
67 lines (60 loc) · 2.32 KB
/
schema.types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { HOOKS } from '../../utils/hooks';
import { CoreType } from '../types';
import { CAST_STRATEGY } from '../../utils/cast-strategy';
export type SchemaDef = Record<string, any>;
export type FieldMap = { [key: string]: IOttomanType };
export type PluginConstructor = (Schema) => void;
export type FactoryFunction = (name, options) => IOttomanType;
/**
* Should throw all errors detected.
*/
export type OttomanSchemaTypes = 'String' | 'Boolean' | 'Number' | 'Date' | 'Array' | 'Reference' | 'Embed' | 'Mixed';
export type ValidatorFunction = (value: unknown) => void;
export type AutoFunction = () => unknown;
export type SupportFactoryTypes = { [key in OttomanSchemaTypes]: FactoryFunction };
export type SupportTypes = { [key in OttomanSchemaTypes]: CoreType };
export type CustomValidations = { [key: string]: ValidatorFunction };
export type RequiredFunction = () => boolean | RequiredOption;
export type HookHandler = (IDocument) => void;
export type Hook = {
[key in HOOKS]?: HookHandler[] | HookHandler;
};
export interface ValidatorOption {
regexp: RegExp;
message: string;
}
export interface RequiredOption {
val: boolean;
message: string;
}
interface SchemaTimestampsConfig {
createdAt?: boolean | string;
updatedAt?: boolean | string;
currentTime?: () => Date | number;
}
export interface CoreTypeOptions {
required?: boolean | RequiredOption | RequiredFunction;
/**
* If truthy, Ottoman will disallow changes to this path once the document is saved to the database for the first time.
**/
immutable?: boolean;
default?: unknown;
validator?: ValidatorOption | ValidatorFunction | string;
}
export abstract class IOttomanType {
protected constructor(public name: string, public typeName: string) {}
abstract cast(value: unknown, strategy?: CAST_STRATEGY): unknown;
abstract validate(value: unknown, strict?: boolean): unknown;
}
export interface SchemaOptions {
strict?: boolean;
preHooks?: Hook;
postHooks?: Hook;
/**
* The timestamps option tells Ottoman to assign createdAt and updatedAt fields to your schema. The type
* assigned is `Date`. By default, the names of the fields are createdAt and updatedAt. Customize the
* field names by setting timestamps.createdAt and timestamps.updatedAt.
*/
timestamps?: boolean | SchemaTimestampsConfig;
enforceRefCheck?: boolean | 'throw';
}