edinburgh 0.1.3 → 0.3.0

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.
@@ -1,15 +1,16 @@
1
- import { Bytes } from "./bytes.js";
1
+ import { DataPack } from "./datapack.js";
2
+ import * as olmdb from "olmdb";
2
3
  import { DatabaseError } from "olmdb";
3
4
  import { Model } from "./models.js";
4
5
  /**
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
- */
6
+ * @internal Abstract base class for all type wrappers in the Edinburgh ORM system.
7
+ *
8
+ * This is an implementation detail and should not be referenced directly in user code.
9
+ * Type wrappers define how values are serialized to/from the database and how they are validated.
10
+ * Each type wrapper must implement serialization, deserialization, and validation logic.
11
+ *
12
+ * @template T - The TypeScript type this wrapper represents.
13
+ */
13
14
  export declare abstract class TypeWrapper<const T> {
14
15
  /** @internal Used for TypeScript type inference - this field is required for the type system */
15
16
  _T: T;
@@ -17,95 +18,51 @@ export declare abstract class TypeWrapper<const T> {
17
18
  abstract kind: string;
18
19
  constructor();
19
20
  /**
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;
21
+ * Serialize a value from an object property to a Pack.
22
+ * @param value - The value to serialize.
23
+ * @param pack - The Pack instance to write to.
24
+ */
25
+ abstract serialize(value: T, pack: DataPack): void;
35
26
  /**
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[];
27
+ * Deserialize a value from a Pack into an object property.
28
+ * @param pack - The Pack instance to read from.
29
+ */
30
+ abstract deserialize(pack: DataPack): T;
42
31
  /**
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;
32
+ * Validate a value.
33
+ * @param value - The value to validate.
34
+ * @returns - A DatabaseError if validation fails.
35
+ */
36
+ abstract getError(value: T): DatabaseError | void;
51
37
  /**
52
- * Serialize type metadata to bytes (for schema serialization).
53
- * @param bytes - The Bytes instance to write to.
54
- */
55
- serializeType(bytes: Bytes): void;
38
+ * Serialize type metadata to a Pack (for schema serialization).
39
+ * @param pack - The Pack instance to write to.
40
+ */
41
+ serializeType(pack: DataPack): void;
56
42
  /**
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;
43
+ * Check if indexing should be skipped for this field value.
44
+ * @param obj - The object containing the value.
45
+ * @param prop - The property name or index.
46
+ * @returns true if indexing should be skipped.
47
+ */
48
+ containsNull(value: T): boolean;
63
49
  toString(): string;
50
+ clone(value: T): T;
51
+ equals(value1: T, value2: T): boolean;
64
52
  }
65
- /**
66
- * Optional interface for type wrappers that can provide default values.
67
- * @template T - The TypeScript type this wrapper represents.
68
- */
69
53
  export interface TypeWrapper<T> {
70
54
  /**
71
- * Generate a default value for this type.
72
- * @param model - The model instance.
73
- * @returns The default value.
74
- */
55
+ * Generate a default value for this type.
56
+ * @param model - The model instance.
57
+ * @returns The default value.
58
+ */
75
59
  default?(model: any): T;
76
60
  }
77
61
  /**
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[]> {
62
+ * @internal Type wrapper for array values with optional length constraints.
63
+ * @template T - The type of array elements.
64
+ */
65
+ export declare class SetType<T> extends TypeWrapper<Set<T>> {
109
66
  inner: TypeWrapper<T>;
110
67
  opts: {
111
68
  min?: number;
@@ -113,189 +70,164 @@ declare class ArrayType<T> extends TypeWrapper<T[]> {
113
70
  };
114
71
  kind: string;
115
72
  /**
116
- * Create a new ArrayType.
117
- * @param inner - Type wrapper for array elements.
118
- * @param opts - Array constraints (min/max length).
119
- */
73
+ * Create a new SetType.
74
+ * @param inner - Type wrapper for set elements.
75
+ */
120
76
  constructor(inner: TypeWrapper<T>, opts?: {
121
77
  min?: number;
122
78
  max?: number;
123
79
  });
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>;
80
+ serialize(value: Set<T>, pack: DataPack): void;
81
+ deserialize(pack: DataPack): Set<T>;
82
+ getError(value: Set<T>): olmdb.DatabaseError | undefined;
83
+ serializeType(pack: DataPack): void;
84
+ static deserializeType(pack: DataPack, featureFlags: number): SetType<any>;
85
+ default(): Set<T>;
86
+ clone(value: Set<T>): Set<T>;
87
+ equals(a: Set<T>, b: Set<T>): boolean;
149
88
  }
150
89
  /**
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>> {
90
+ * @internal Type wrapper for model relationships (foreign keys).
91
+ * @template T - The target model class type.
92
+ */
93
+ export declare class LinkType<T extends typeof Model<unknown>> extends TypeWrapper<InstanceType<T>> {
187
94
  kind: string;
188
95
  TargetModel: T;
189
96
  /**
190
- * Create a new LinkType.
191
- * @param TargetModel - The model class this link points to.
192
- */
97
+ * Create a new LinkType.
98
+ * @param TargetModel - The model class this link points to.
99
+ */
193
100
  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>;
101
+ serialize(model: InstanceType<T>, pack: DataPack): void;
102
+ deserialize(pack: DataPack): any;
103
+ getError(value: InstanceType<T>): olmdb.DatabaseError | undefined;
104
+ serializeType(pack: DataPack): void;
105
+ static deserializeType(pack: DataPack, featureFlags: number): LinkType<any>;
199
106
  }
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
- */
107
+ /** Type wrapper instance for the string type. */
108
+ export declare const string: TypeWrapper<string>;
109
+ /** Type wrapper instance for the ordered string type, which is just like a string
110
+ * except that it sorts lexicographically in the database (instead of by incrementing
111
+ * length first), making it suitable for index fields that want lexicographic range
112
+ * scans. Ordered strings are implemented as null-terminated UTF-8 strings, so they
113
+ * may not contain null characters.
114
+ */
115
+ export declare const orderedString: TypeWrapper<string>;
116
+ /** Type wrapper instance for the number type. */
117
+ export declare const number: TypeWrapper<number>;
118
+ /** Type wrapper instance for the date/time type. */
119
+ export declare const dateTime: TypeWrapper<Date>;
120
+ /** Type wrapper instance for the boolean type. */
121
+ export declare const boolean: TypeWrapper<boolean>;
122
+ /** Type wrapper instance for the identifier type. */
123
+ export declare const identifier: TypeWrapper<string>;
124
+ /** Type wrapper instance for the 'undefined' type. */
125
+ export declare const undef: TypeWrapper<undefined>;
126
+ /**
127
+ * Create a literal type wrapper for a constant value.
128
+ * @template T - The literal type.
129
+ * @param value - The literal value.
130
+ * @returns A literal type instance.
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * const statusType = E.literal("active");
135
+ * const countType = E.literal(42);
136
+ * ```
137
+ */
138
+ export declare function literal<const T>(value: T): TypeWrapper<T>;
139
+ /**
140
+ * Create a union type wrapper from multiple type choices.
141
+ * @template T - Array of type wrapper or basic types.
142
+ * @param choices - The type choices for the union.
143
+ * @returns A union type instance.
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * const stringOrNumber = E.or(E.string, E.number);
148
+ * const status = E.or("active", "inactive", "pending");
149
+ * ```
150
+ */
151
+ export declare function or<const T extends (TypeWrapper<unknown> | BasicType)[]>(...choices: T): TypeWrapper<UnwrapTypes<T>>;
152
+ /**
153
+ * Create an optional type wrapper (allows undefined).
154
+ * @template T - Type wrapper or basic type to make optional.
155
+ * @param inner - The inner type to make optional.
156
+ * @returns A union type that accepts the inner type or undefined.
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * const optionalString = E.opt(E.string);
161
+ * const optionalNumber = E.opt(E.number);
162
+ * ```
163
+ */
164
+ export declare function opt<const T extends TypeWrapper<unknown> | BasicType>(inner: T): TypeWrapper<UnwrapTypes<[T, typeof undef]>>;
165
+ /**
166
+ * Create an array type wrapper with optional length constraints.
167
+ * @template T - The element type.
168
+ * @param inner - Type wrapper for array elements.
169
+ * @param opts - Optional constraints (min/max length).
170
+ * @returns An array type instance.
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * const stringArray = E.array(E.string);
175
+ * const boundedArray = E.array(E.number, {min: 1, max: 10});
176
+ * ```
177
+ */
262
178
  export declare function array<const T>(inner: TypeWrapper<T>, opts?: {
263
179
  min?: number;
264
180
  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[]> = {
181
+ }): TypeWrapper<T[]>;
182
+ /**
183
+ * Create a Set type wrapper with optional length constraints.
184
+ * @template T - The element type.
185
+ * @param inner - Type wrapper for set elements.
186
+ * @param opts - Optional constraints (min/max length).
187
+ * @returns A set type instance.
188
+ *
189
+ * @example
190
+ * ```typescript
191
+ * const stringSet = E.set(E.string);
192
+ * const boundedSet = E.set(E.number, {min: 1, max: 10});
193
+ * ```
194
+ */
195
+ export declare function set<const T>(inner: TypeWrapper<T>, opts?: {
196
+ min?: number;
197
+ max?: number;
198
+ }): TypeWrapper<Set<T>>;
199
+ /**
200
+ * Create a link type wrapper for model relationships.
201
+ * @template T - The target model class.
202
+ * @param TargetModel - The model class this link points to.
203
+ * @returns A link type instance.
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * class User extends E.Model<User> {
208
+ * posts = E.field(E.array(E.link(Post, 'author')));
209
+ * }
210
+ *
211
+ * class Post extends E.Model<Post> {
212
+ * author = E.field(E.link(User));
213
+ * }
214
+ * ```
215
+ */
216
+ export declare function link<const T extends typeof Model<any>>(TargetModel: T): TypeWrapper<InstanceType<T>>;
217
+ export type BasicType = string | number | boolean | undefined | null;
218
+ export type UnwrapTypes<T extends (TypeWrapper<unknown> | BasicType)[]> = {
286
219
  [K in keyof T]: T[K] extends TypeWrapper<infer U> ? U : T[K];
287
220
  }[number];
288
221
  /**
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 {};
222
+ * Serialize a type wrapper to a Pack for schema persistence.
223
+ * @param arg - The type wrapper to serialize.
224
+ * @param pack - The Pack instance to write to.
225
+ */
226
+ export declare function serializeType(arg: TypeWrapper<any>, pack: DataPack): void;
227
+ /**
228
+ * Deserialize a type wrapper from a Pack.
229
+ * @param pack - The Pack instance to read from.
230
+ * @param featureFlags - Feature flags for version compatibility.
231
+ * @returns The deserialized type wrapper.
232
+ */
233
+ export declare function deserializeType(pack: DataPack, featureFlags: number): TypeWrapper<any>;