aboutsummaryrefslogtreecommitdiff
path: root/src/fields/Schema.ts
blob: 7ad376a2827abf4793204039822393e91d97b17a (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { Interface, ToInterface, Cast, ToConstructor, HasTail, Head, Tail, ListSpec, ToType, DefaultFieldConstructor } from "./Types";
import { Doc, Field } from "./Doc";
import { ObjectField } from "./ObjectField";
import { RefField } from "./RefField";
import { SelfProxy } from "./FieldSymbols";
import { List } from "./List";

type AllToInterface<T extends Interface[]> = {
    1: ToInterface<Head<T>> & AllToInterface<Tail<T>>,
    0: ToInterface<Head<T>>
}[HasTail<T> extends true ? 1 : 0];

export const emptySchema = createSchema({});
export const Document = makeInterface(emptySchema);
export type Document = makeInterface<[typeof emptySchema]>;

export interface InterfaceFunc<T extends Interface[]> {
    (docs: Doc[]): makeInterface<T>[];
    (): makeInterface<T>;
    (doc: Doc): makeInterface<T>;
}

export type makeInterface<T extends Interface[]> = AllToInterface<T> & Doc & { proto: Doc | undefined };
export function makeInterface<T extends Interface[]>(...schemas: T): InterfaceFunc<T> {
    const schema: Interface = {};
    for (const s of schemas) {
        for (const key in s) {
            schema[key] = s[key];
        }
    }
    const proto = new Proxy({}, {
        get(target: any, prop, receiver) {
            const field = receiver.doc?.[prop];
            if (prop in schema) {
                const desc = prop === "proto" ? Doc : (schema as any)[prop]; // bcz: proto doesn't appear in schemas ... maybe it should?
                if (typeof desc === "object" && "defaultVal" in desc && "type" in desc) {//defaultSpec
                    return Cast(field, desc.type, desc.defaultVal);
                }
                if (typeof desc === "function" && !ObjectField.isPrototypeOf(desc) && !RefField.isPrototypeOf(desc)) {
                    const doc = Cast(field, Doc);
                    if (doc === undefined) {
                        return undefined;
                    }
                    if (doc instanceof Doc) {
                        return desc(doc);
                    }
                    return doc.then(doc => doc && desc(doc));
                }
                return Cast(field, desc);
            }
            return field;
        },
        set(target: any, prop, value, receiver) {
            receiver.doc && (receiver.doc[prop] = value);  // receiver.doc may be undefined as the result of a change in acls
            return true;
        }
    });
    // !(doc instanceof Doc) && (throw new Error("Currently wrapping a schema in another schema isn't supported"));
    const fn = (doc: Doc) => Object.create(proto, { doc: { value: doc[SelfProxy], writable: false } });
    return ((doc?: Doc | Doc[]) => (doc instanceof List ? doc : undefined)?.map?.(fn) ?? fn((doc as Doc) ?? new Doc)) as InterfaceFunc<T>;
}

export type makeStrictInterface<T extends Interface> = Partial<ToInterface<T>>;
export function makeStrictInterface<T extends Interface>(schema: T): (doc: Doc) => makeStrictInterface<T> {
    const proto = {};
    for (const key in schema) {
        const type = schema[key];
        Object.defineProperty(proto, key, {
            get() {
                return Cast(this.__doc[key], type as any);
            },
            set(value) {
                value = Cast(value, type as any);
                if (value !== undefined) {
                    this.__doc[key] = value;
                    return;
                }
                throw new TypeError("Expected type " + type);
            }
        });
    }
    return function (doc: any) {
        if (!(doc instanceof Doc)) {
            throw new Error("Currently wrapping a schema in another schema isn't supported");
        }
        const obj = Object.create(proto);
        obj.__doc = doc;
        return obj;
    };
}

export function createSchema<T extends Interface>(schema: T): T & { proto: ToConstructor<Doc> } {
    (schema as any).proto = Doc;
    return schema as any;
}

export function listSpec<U extends ToConstructor<Field>>(type: U): ListSpec<ToType<U>> {
    return { List: type as any };//TODO Types
}

export function defaultSpec<T extends ToConstructor<Field>>(type: T, defaultVal: ToType<T>): DefaultFieldConstructor<ToType<T>> {
    return {
        type: type as any,
        defaultVal
    };
}