edinburgh 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,301 @@
1
+ import { Bytes } from "./bytes.js";
2
+ import { DatabaseError } from "olmdb";
3
+ import { Model } from "./models.js";
4
+ /**
5
+ * @internal Abstract base class for all type wrappers in the Edinburgh ORM system.
6
+ *
7
+ * This is an implementation detail and should not be referenced directly in user code.
8
+ * Type wrappers define how values are serialized to/from the database and how they are validated.
9
+ * Each type wrapper must implement serialization, deserialization, and validation logic.
10
+ *
11
+ * @template T - The TypeScript type this wrapper represents.
12
+ */
13
+ export declare abstract class TypeWrapper<const T> {
14
+ /** @internal Used for TypeScript type inference - this field is required for the type system */
15
+ _T: T;
16
+ /** A string identifier for this type, used during serialization */
17
+ abstract kind: string;
18
+ constructor();
19
+ /**
20
+ * Serialize a value from an object property to bytes.
21
+ * @param obj - The object containing the value.
22
+ * @param prop - The property name or index.
23
+ * @param bytes - The Bytes instance to write to.
24
+ * @param model - Optional model instance for context.
25
+ */
26
+ abstract serialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
27
+ /**
28
+ * Deserialize a value from bytes into an object property.
29
+ * @param obj - The object to set the value on.
30
+ * @param prop - The property name or index.
31
+ * @param bytes - The Bytes instance to read from.
32
+ * @param model - Optional model instance for context.
33
+ */
34
+ abstract deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
35
+ /**
36
+ * Validate a value and return any validation errors.
37
+ * @param obj - The object containing the value.
38
+ * @param prop - The property name or index.
39
+ * @returns Array of validation errors (empty if valid).
40
+ */
41
+ abstract getErrors(obj: any, prop: string | number): DatabaseError[];
42
+ /**
43
+ * Validate a value and serialize it, throwing on validation errors.
44
+ * @param obj - The object containing the value.
45
+ * @param prop - The property name or index.
46
+ * @param bytes - The Bytes instance to write to.
47
+ * @param model - Optional model instance for context.
48
+ * @throws {DatabaseError} If validation fails.
49
+ */
50
+ validateAndSerialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
51
+ /**
52
+ * Serialize type metadata to bytes (for schema serialization).
53
+ * @param bytes - The Bytes instance to write to.
54
+ */
55
+ serializeType(bytes: Bytes): void;
56
+ /**
57
+ * Check if indexing should be skipped for this field value.
58
+ * @param obj - The object containing the value.
59
+ * @param prop - The property name or index.
60
+ * @returns true if indexing should be skipped.
61
+ */
62
+ checkSkipIndex(obj: any, prop: string | number): boolean;
63
+ toString(): string;
64
+ }
65
+ /**
66
+ * Optional interface for type wrappers that can provide default values.
67
+ * @template T - The TypeScript type this wrapper represents.
68
+ */
69
+ export interface TypeWrapper<T> {
70
+ /**
71
+ * Generate a default value for this type.
72
+ * @param model - The model instance.
73
+ * @returns The default value.
74
+ */
75
+ default?(model: any): T;
76
+ }
77
+ /**
78
+ * @internal Type wrapper for string values.
79
+ */
80
+ declare class StringType extends TypeWrapper<string> {
81
+ kind: string;
82
+ serialize(obj: any, prop: string, bytes: Bytes, model?: any): void;
83
+ deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
84
+ getErrors(obj: any, prop: string | number): DatabaseError[];
85
+ }
86
+ /**
87
+ * @internal Type wrapper for number values.
88
+ */
89
+ declare class NumberType extends TypeWrapper<number> {
90
+ kind: string;
91
+ serialize(obj: any, prop: string, bytes: Bytes, model?: any): void;
92
+ deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
93
+ getErrors(obj: any, prop: string | number): DatabaseError[];
94
+ }
95
+ /**
96
+ * @internal Type wrapper for boolean values.
97
+ */
98
+ declare class BooleanType extends TypeWrapper<boolean> {
99
+ kind: string;
100
+ serialize(obj: any, prop: string, bytes: Bytes, model?: any): void;
101
+ deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
102
+ getErrors(obj: any, prop: string | number): DatabaseError[];
103
+ }
104
+ /**
105
+ * @internal Type wrapper for array values with optional length constraints.
106
+ * @template T - The type of array elements.
107
+ */
108
+ declare class ArrayType<T> extends TypeWrapper<T[]> {
109
+ inner: TypeWrapper<T>;
110
+ opts: {
111
+ min?: number;
112
+ max?: number;
113
+ };
114
+ kind: string;
115
+ /**
116
+ * Create a new ArrayType.
117
+ * @param inner - Type wrapper for array elements.
118
+ * @param opts - Array constraints (min/max length).
119
+ */
120
+ constructor(inner: TypeWrapper<T>, opts?: {
121
+ min?: number;
122
+ max?: number;
123
+ });
124
+ serialize(obj: any, prop: string, bytes: Bytes, model?: any): void;
125
+ deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
126
+ getErrors(obj: any, prop: string | number): DatabaseError[];
127
+ serializeType(bytes: Bytes): void;
128
+ static deserializeType(bytes: Bytes, featureFlags: number): ArrayType<any>;
129
+ }
130
+ /**
131
+ * @internal Type wrapper for union/discriminated union types.
132
+ * @template T - The union type this wrapper represents.
133
+ */
134
+ declare class OrType<const T> extends TypeWrapper<T> {
135
+ choices: TypeWrapper<T>[];
136
+ kind: string;
137
+ /**
138
+ * Create a new OrType.
139
+ * @param choices - Array of type wrappers representing the union choices.
140
+ */
141
+ constructor(choices: TypeWrapper<T>[]);
142
+ _getChoiceIndex(obj: any, prop: string | number): number;
143
+ serialize(obj: any, prop: string, bytes: Bytes, model?: any): void;
144
+ deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
145
+ getErrors(obj: any, prop: string | number): DatabaseError[];
146
+ checkSkipIndex(obj: any, prop: string | number): boolean;
147
+ serializeType(bytes: Bytes): void;
148
+ static deserializeType(bytes: Bytes, featureFlags: number): OrType<any>;
149
+ }
150
+ /**
151
+ * @internal Type wrapper for literal values (constants).
152
+ * @template T - The literal type this wrapper represents.
153
+ */
154
+ declare class LiteralType<const T> extends TypeWrapper<T> {
155
+ value: T;
156
+ kind: string;
157
+ /**
158
+ * Create a new LiteralType.
159
+ * @param value - The literal value this type represents.
160
+ */
161
+ constructor(value: T);
162
+ serialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
163
+ deserialize(obj: any, prop: string | number, bytes: Bytes, model?: any): void;
164
+ getErrors(obj: any, prop: string | number): DatabaseError[];
165
+ serializeType(bytes: Bytes): void;
166
+ checkSkipIndex(obj: any, prop: string | number): boolean;
167
+ static deserializeType(bytes: Bytes, featureFlags: number): LiteralType<any>;
168
+ default(model: any): T;
169
+ }
170
+ /**
171
+ * @internal Type wrapper for auto-generated unique identifier strings.
172
+ */
173
+ declare class IdentifierType extends TypeWrapper<string> {
174
+ kind: string;
175
+ serialize(obj: any, prop: string | number, bytes: Bytes): void;
176
+ deserialize(obj: any, prop: string | number, bytes: Bytes): void;
177
+ getErrors(obj: any, prop: string | number): DatabaseError[];
178
+ serializeType(bytes: Bytes): void;
179
+ static deserializeType(bytes: Bytes, featureFlags: number): IdentifierType;
180
+ default(model: Model<any>): string;
181
+ }
182
+ /**
183
+ * @internal Type wrapper for model relationships (foreign keys).
184
+ * @template T - The target model class type.
185
+ */
186
+ export declare class LinkType<T extends typeof Model<any>> extends TypeWrapper<InstanceType<T>> {
187
+ kind: string;
188
+ TargetModel: T;
189
+ /**
190
+ * Create a new LinkType.
191
+ * @param TargetModel - The model class this link points to.
192
+ */
193
+ constructor(TargetModel: T);
194
+ serialize(obj: any, prop: string | number, bytes: Bytes, model: Model<InstanceType<T>>): void;
195
+ deserialize(obj: any, prop: string, bytes: Bytes, sourceModel: Model<unknown>): void;
196
+ getErrors(obj: any, prop: string | number): DatabaseError[];
197
+ serializeType(bytes: Bytes): void;
198
+ static deserializeType(bytes: Bytes, featureFlags: number): LinkType<any>;
199
+ }
200
+ /** Constant representing the string type. */
201
+ export declare const string: StringType;
202
+ /** Constant representing the number type. */
203
+ export declare const number: NumberType;
204
+ /** Constant representing the boolean type. */
205
+ export declare const boolean: BooleanType;
206
+ /** Constant representing the identifier type. */
207
+ export declare const identifier: IdentifierType;
208
+ /**
209
+ * Create a literal type wrapper for a constant value.
210
+ * @template T - The literal type.
211
+ * @param value - The literal value.
212
+ * @returns A literal type instance.
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * const statusType = E.literal("active");
217
+ * const countType = E.literal(42);
218
+ * ```
219
+ */
220
+ export declare function literal<const T>(value: T): LiteralType<T>;
221
+ /**
222
+ * Create a union type wrapper from multiple type choices.
223
+ * @template T - Array of type wrapper or basic types.
224
+ * @param choices - The type choices for the union.
225
+ * @returns A union type instance.
226
+ *
227
+ * @example
228
+ * ```typescript
229
+ * const stringOrNumber = E.or(E.string, E.number);
230
+ * const status = E.or("active", "inactive", "pending");
231
+ * ```
232
+ */
233
+ export declare function or<const T extends (TypeWrapper<unknown> | BasicType)[]>(...choices: T): OrType<UnwrapTypes<T>>;
234
+ /** Constant representing the 'undefined' type. */
235
+ export declare const undef: LiteralType<undefined>;
236
+ /**
237
+ * Create an optional type wrapper (allows undefined).
238
+ * @template T - Type wrapper or basic type to make optional.
239
+ * @param inner - The inner type to make optional.
240
+ * @returns A union type that accepts the inner type or undefined.
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * const optionalString = E.opt(E.string);
245
+ * const optionalNumber = E.opt(E.number);
246
+ * ```
247
+ */
248
+ export declare function opt<const T extends TypeWrapper<unknown> | BasicType>(inner: T): OrType<(T extends TypeWrapper<infer U> ? U : T) | undefined>;
249
+ /**
250
+ * Create an array type wrapper with optional length constraints.
251
+ * @template T - The element type.
252
+ * @param inner - Type wrapper for array elements.
253
+ * @param opts - Optional constraints (min/max length).
254
+ * @returns An array type instance.
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * const stringArray = E.array(E.string);
259
+ * const boundedArray = E.array(E.number, {min: 1, max: 10});
260
+ * ```
261
+ */
262
+ export declare function array<const T>(inner: TypeWrapper<T>, opts?: {
263
+ min?: number;
264
+ max?: number;
265
+ }): ArrayType<T>;
266
+ /**
267
+ * Create a link type wrapper for model relationships.
268
+ * @template T - The target model class.
269
+ * @param TargetModel - The model class this link points to.
270
+ * @returns A link type instance.
271
+ *
272
+ * @example
273
+ * ```typescript
274
+ * class User extends E.Model<User> {
275
+ * posts = E.field(E.array(E.link(Post, 'author')));
276
+ * }
277
+ *
278
+ * class Post extends E.Model<Post> {
279
+ * author = E.field(E.link(User));
280
+ * }
281
+ * ```
282
+ */
283
+ export declare function link<const T extends typeof Model<any>>(TargetModel: T): LinkType<T>;
284
+ export type BasicType = TypeWrapper<any> | string | number | boolean | undefined | null;
285
+ export type UnwrapTypes<T extends BasicType[]> = {
286
+ [K in keyof T]: T[K] extends TypeWrapper<infer U> ? U : T[K];
287
+ }[number];
288
+ /**
289
+ * Serialize a type wrapper to bytes for schema persistence.
290
+ * @param arg - The type wrapper to serialize.
291
+ * @param bytes - The Bytes instance to write to.
292
+ */
293
+ export declare function serializeType(arg: TypeWrapper<any>, bytes: Bytes): void;
294
+ /**
295
+ * Deserialize a type wrapper from bytes.
296
+ * @param bytes - The Bytes instance to read from.
297
+ * @param featureFlags - Feature flags for version compatibility.
298
+ * @returns The deserialized type wrapper.
299
+ */
300
+ export declare function deserializeType(bytes: Bytes, featureFlags: number): TypeWrapper<any>;
301
+ export {};