@live-state/sync 0.0.6 → 0.0.7-canary-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.
- package/dist/chunk-LSAVBBUS.js +1 -0
- package/dist/chunk-NKM6VWL7.js +1 -0
- package/dist/chunk-RSSRYEM3.js +1 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/fetch-client.d.ts +2 -2
- package/dist/fetch-client.js +1 -1
- package/dist/index-CLkafi56.d.ts +1016 -0
- package/dist/index-hO1iQ-VU.d.cts +608 -0
- package/dist/index-hO1iQ-VU.d.ts +608 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/optimistic-client.d.ts +3 -0
- package/dist/optimistic-client.js +1 -0
- package/dist/server.cjs +10 -4
- package/dist/server.d.cts +262 -86
- package/dist/server.d.ts +262 -86
- package/dist/server.js +8 -2
- package/package.json +6 -1
- package/dist/chunk-AHF6GNMI.js +0 -1
- package/dist/chunk-IT5UC7TA.js +0 -1
- package/dist/index-BtsKDfTE.d.cts +0 -282
- package/dist/index-BtsKDfTE.d.ts +0 -282
- package/dist/index-P-s1Yf1S.d.ts +0 -520
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
/** biome-ignore-all lint/complexity/noBannedTypes: false positive */
|
|
2
|
+
/**
|
|
3
|
+
* Base metadata type for live types.
|
|
4
|
+
* All metadata types must extend this base.
|
|
5
|
+
*/
|
|
6
|
+
type BaseMeta = {};
|
|
7
|
+
/**
|
|
8
|
+
* Metadata type for atomic live types with timestamp for LWW conflict resolution.
|
|
9
|
+
*/
|
|
10
|
+
type AtomicMeta = {
|
|
11
|
+
timestamp: string | null;
|
|
12
|
+
} & BaseMeta;
|
|
13
|
+
type MutationType = "set";
|
|
14
|
+
/**
|
|
15
|
+
* Describes the storage field type for database mapping.
|
|
16
|
+
*/
|
|
17
|
+
type StorageFieldType = {
|
|
18
|
+
type: string;
|
|
19
|
+
nullable: boolean;
|
|
20
|
+
default?: any;
|
|
21
|
+
unique?: boolean;
|
|
22
|
+
index?: boolean;
|
|
23
|
+
primary?: boolean;
|
|
24
|
+
references?: string;
|
|
25
|
+
enumValues?: readonly string[];
|
|
26
|
+
enumName?: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Materialized representation of a LiveType value with metadata.
|
|
30
|
+
* Used internally for storage and sync operations.
|
|
31
|
+
*/
|
|
32
|
+
type MaterializedLiveType<T extends LiveTypeAny> = {
|
|
33
|
+
value: T["_value"] extends Record<string, LiveTypeAny> ? {
|
|
34
|
+
[K in keyof T["_value"]]: MaterializedLiveType<T["_value"][K]>;
|
|
35
|
+
} : T["_value"];
|
|
36
|
+
_meta: T["_meta"];
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Abstract base class for all live types.
|
|
40
|
+
*
|
|
41
|
+
* @template Value - The actual TypeScript value type
|
|
42
|
+
* @template Meta - Metadata type for sync resolution (defaults to BaseMeta)
|
|
43
|
+
* @template EncodeInput - Type accepted during mutations
|
|
44
|
+
* @template DecodeInput - Type returned after encoding
|
|
45
|
+
*/
|
|
46
|
+
declare abstract class LiveType<Value = any, Meta extends BaseMeta = BaseMeta, EncodeInput = Partial<Value> | Value, DecodeInput = {
|
|
47
|
+
value: Value;
|
|
48
|
+
_meta: keyof Meta extends never ? never : Meta;
|
|
49
|
+
}> {
|
|
50
|
+
readonly _value: Value;
|
|
51
|
+
readonly _meta: Meta;
|
|
52
|
+
readonly _encodeInput: EncodeInput;
|
|
53
|
+
readonly _decodeInput: DecodeInput;
|
|
54
|
+
abstract encodeMutation(mutationType: MutationType, input: EncodeInput, timestamp: string): DecodeInput;
|
|
55
|
+
/**
|
|
56
|
+
* Merges the materialized shape with the encoded mutation
|
|
57
|
+
* @param mutationType The type of mutation
|
|
58
|
+
* @param encodedMutation The encoded mutation
|
|
59
|
+
* @param materializedShape The materialized shape
|
|
60
|
+
* @returns A tuple of the new materialized shape and the accepted diff
|
|
61
|
+
*/
|
|
62
|
+
abstract mergeMutation(mutationType: MutationType, encodedMutation: DecodeInput, materializedShape?: MaterializedLiveType<LiveType<Value, Meta>>): [MaterializedLiveType<LiveType<Value, Meta>>, DecodeInput | null];
|
|
63
|
+
abstract getStorageFieldType(): StorageFieldType;
|
|
64
|
+
}
|
|
65
|
+
type LiveTypeAny = LiveType<any, BaseMeta, any, any>;
|
|
66
|
+
/**
|
|
67
|
+
* Extracts the TypeScript value type from a LiveType.
|
|
68
|
+
*/
|
|
69
|
+
type InferLiveType<T extends LiveTypeAny> = T["_value"] extends Record<string, LiveTypeAny> ? {
|
|
70
|
+
[K in keyof T["_value"]]: InferLiveType<T["_value"][K]>;
|
|
71
|
+
} : T["_value"];
|
|
72
|
+
type InferIndex<T extends LiveTypeAny> = string;
|
|
73
|
+
/** @deprecated Use `BaseMeta` instead */
|
|
74
|
+
type LiveTypeMeta = BaseMeta;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Helper type to extract Value type from LiveAtomicType
|
|
78
|
+
*/
|
|
79
|
+
type ExtractValue<T> = T extends LiveAtomicType<infer V, any, any> ? V : never;
|
|
80
|
+
/**
|
|
81
|
+
* Helper type to extract Meta type from LiveAtomicType
|
|
82
|
+
*/
|
|
83
|
+
type ExtractMeta<T> = T extends LiveAtomicType<any, any, infer M> ? M : AtomicMeta;
|
|
84
|
+
/**
|
|
85
|
+
* Wraps a LiveAtomicType to allow null values.
|
|
86
|
+
* Supports chainable modifiers like `.default()`, `.unique()`, `.index()`, `.primary()`.
|
|
87
|
+
*/
|
|
88
|
+
declare class NullableLiveType<T extends LiveAtomicType<any, any, any>> extends LiveType<T["_value"] | null, T["_meta"], T["_encodeInput"] | null, T["_decodeInput"]> {
|
|
89
|
+
readonly inner: T;
|
|
90
|
+
constructor(inner: T);
|
|
91
|
+
encodeMutation(mutationType: MutationType, input: T["_value"] | null, timestamp: string): T["_decodeInput"];
|
|
92
|
+
mergeMutation(mutationType: MutationType, encodedMutation: T["_decodeInput"], materializedShape?: MaterializedLiveType<LiveType<T["_value"] | null, T["_meta"], T["_value"] | Partial<T["_value"] | null>, T["_decodeInput"]>> | undefined): [
|
|
93
|
+
MaterializedLiveType<LiveType<T["_value"] | null, T["_meta"], T["_value"] | Partial<T["_value"] | null>, T["_decodeInput"]>>,
|
|
94
|
+
T["_decodeInput"] | null
|
|
95
|
+
];
|
|
96
|
+
getStorageFieldType(): StorageFieldType;
|
|
97
|
+
/**
|
|
98
|
+
* Returns a new nullable type with a unique constraint.
|
|
99
|
+
*/
|
|
100
|
+
unique(): NullableLiveType<LiveAtomicType<ExtractValue<T>, undefined, ExtractMeta<T>>>;
|
|
101
|
+
/**
|
|
102
|
+
* Returns a new nullable type with an index.
|
|
103
|
+
*/
|
|
104
|
+
index(): NullableLiveType<LiveAtomicType<ExtractValue<T>, undefined, ExtractMeta<T>>>;
|
|
105
|
+
/**
|
|
106
|
+
* Returns a new nullable type with a default value.
|
|
107
|
+
* Can be the value type or null.
|
|
108
|
+
*/
|
|
109
|
+
default(value: ExtractValue<T> | null): NullableLiveType<LiveAtomicType<ExtractValue<T>, ExtractValue<T> | null, ExtractMeta<T>>>;
|
|
110
|
+
/**
|
|
111
|
+
* Returns a new nullable type marked as primary key.
|
|
112
|
+
*/
|
|
113
|
+
primary(): NullableLiveType<LiveAtomicType<ExtractValue<T>, undefined, ExtractMeta<T>>>;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Base class for atomic (scalar) live types.
|
|
117
|
+
*
|
|
118
|
+
* @template Value - The value type (string, number, boolean, Date)
|
|
119
|
+
* @template DefaultValue - The default value type (undefined if no default)
|
|
120
|
+
* @template Meta - Metadata type for sync resolution
|
|
121
|
+
*/
|
|
122
|
+
declare class LiveAtomicType<Value, DefaultValue = undefined, Meta extends AtomicMeta = AtomicMeta> extends LiveType<Value, Meta, Value, {
|
|
123
|
+
value: Value;
|
|
124
|
+
_meta: Meta;
|
|
125
|
+
}> {
|
|
126
|
+
readonly storageType: string;
|
|
127
|
+
readonly convertFunc?: (value: any) => Value;
|
|
128
|
+
readonly isIndex: boolean;
|
|
129
|
+
readonly isUnique: boolean;
|
|
130
|
+
readonly defaultValue: DefaultValue;
|
|
131
|
+
readonly foreignReference?: string;
|
|
132
|
+
readonly isPrimary: boolean;
|
|
133
|
+
constructor(storageType: string, convertFunc?: (value: any) => Value, index?: boolean, unique?: boolean, defaultValue?: DefaultValue, references?: string, primary?: boolean);
|
|
134
|
+
encodeMutation(mutationType: MutationType, input: Value, timestamp: string): {
|
|
135
|
+
value: Value;
|
|
136
|
+
_meta: Meta;
|
|
137
|
+
};
|
|
138
|
+
mergeMutation(mutationType: MutationType, encodedMutation: {
|
|
139
|
+
value: Value;
|
|
140
|
+
_meta: Meta;
|
|
141
|
+
}, materializedShape?: MaterializedLiveType<LiveType<Value, Meta, Value | Partial<Value>, {
|
|
142
|
+
value: Value;
|
|
143
|
+
_meta: Meta;
|
|
144
|
+
}>>): [
|
|
145
|
+
MaterializedLiveType<LiveType<Value, Meta, Value | Partial<Value>, {
|
|
146
|
+
value: Value;
|
|
147
|
+
_meta: Meta;
|
|
148
|
+
}>>,
|
|
149
|
+
{
|
|
150
|
+
value: Value;
|
|
151
|
+
_meta: Meta;
|
|
152
|
+
} | null
|
|
153
|
+
];
|
|
154
|
+
getStorageFieldType(): StorageFieldType;
|
|
155
|
+
/**
|
|
156
|
+
* Returns a new atomic type with a unique constraint.
|
|
157
|
+
*/
|
|
158
|
+
unique(): LiveAtomicType<Value, undefined, Meta>;
|
|
159
|
+
/**
|
|
160
|
+
* Returns a new atomic type with an index.
|
|
161
|
+
*/
|
|
162
|
+
index(): LiveAtomicType<Value, undefined, Meta>;
|
|
163
|
+
/**
|
|
164
|
+
* Returns a new atomic type with a default value.
|
|
165
|
+
*/
|
|
166
|
+
default(value: Value): LiveAtomicType<Value, Value, Meta>;
|
|
167
|
+
/**
|
|
168
|
+
* Returns a new atomic type marked as primary key.
|
|
169
|
+
*/
|
|
170
|
+
primary(): LiveAtomicType<Value, undefined, Meta>;
|
|
171
|
+
/**
|
|
172
|
+
* Returns a nullable version of this type.
|
|
173
|
+
* If no default value was set, defaults to null.
|
|
174
|
+
*/
|
|
175
|
+
nullable(): NullableLiveType<LiveAtomicType<Value, DefaultValue extends undefined ? null : DefaultValue, Meta>>;
|
|
176
|
+
/**
|
|
177
|
+
* Returns a new atomic type with custom metadata type for advanced sync strategies.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* type VectorClockMeta = AtomicMeta & { vectorClock: Record<string, number> };
|
|
182
|
+
* const content = string().withMeta<VectorClockMeta>();
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
withMeta<TMeta extends AtomicMeta>(): LiveAtomicType<Value, DefaultValue, TMeta>;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Live number type.
|
|
189
|
+
*/
|
|
190
|
+
declare class LiveNumber extends LiveAtomicType<number> {
|
|
191
|
+
private constructor();
|
|
192
|
+
static create(): LiveNumber;
|
|
193
|
+
}
|
|
194
|
+
declare const number: typeof LiveNumber.create;
|
|
195
|
+
/**
|
|
196
|
+
* Live string type.
|
|
197
|
+
*/
|
|
198
|
+
declare class LiveString extends LiveAtomicType<string> {
|
|
199
|
+
private constructor();
|
|
200
|
+
static create(): LiveString;
|
|
201
|
+
static createId(): LiveAtomicType<string, undefined, {
|
|
202
|
+
timestamp: string | null;
|
|
203
|
+
}>;
|
|
204
|
+
static createReference(foreignField: `${string}.${string}`): LiveString;
|
|
205
|
+
}
|
|
206
|
+
declare const string: typeof LiveString.create;
|
|
207
|
+
declare const id: typeof LiveString.createId;
|
|
208
|
+
declare const reference: typeof LiveString.createReference;
|
|
209
|
+
/**
|
|
210
|
+
* Live boolean type.
|
|
211
|
+
*/
|
|
212
|
+
declare class LiveBoolean extends LiveAtomicType<boolean> {
|
|
213
|
+
private constructor();
|
|
214
|
+
static create(): LiveBoolean;
|
|
215
|
+
}
|
|
216
|
+
declare const boolean: typeof LiveBoolean.create;
|
|
217
|
+
/**
|
|
218
|
+
* Live timestamp type (maps to Date).
|
|
219
|
+
*/
|
|
220
|
+
declare class LiveTimestamp extends LiveAtomicType<Date> {
|
|
221
|
+
private constructor();
|
|
222
|
+
static create(): LiveTimestamp;
|
|
223
|
+
}
|
|
224
|
+
declare const timestamp: typeof LiveTimestamp.create;
|
|
225
|
+
/**
|
|
226
|
+
* Live enum type.
|
|
227
|
+
* Maps to native enum in Postgres, varchar in MySQL/SQLite.
|
|
228
|
+
*/
|
|
229
|
+
declare class LiveEnum<Value extends string, DefaultValue = undefined, Meta extends AtomicMeta = AtomicMeta> extends LiveAtomicType<Value, DefaultValue, Meta> {
|
|
230
|
+
readonly enumValues: readonly Value[];
|
|
231
|
+
readonly enumName: string;
|
|
232
|
+
constructor(values: readonly Value[], index?: boolean, unique?: boolean, defaultValue?: DefaultValue, primary?: boolean);
|
|
233
|
+
getStorageFieldType(): StorageFieldType;
|
|
234
|
+
unique(): LiveEnum<Value, undefined, Meta>;
|
|
235
|
+
index(): LiveEnum<Value, undefined, Meta>;
|
|
236
|
+
default(value: Value): LiveEnum<Value, Value, Meta>;
|
|
237
|
+
primary(): LiveEnum<Value, undefined, Meta>;
|
|
238
|
+
nullable(): NullableLiveType<LiveEnum<Value, DefaultValue extends undefined ? null : DefaultValue, Meta>>;
|
|
239
|
+
static create<const T extends readonly string[]>(values: T): LiveEnum<T[number]>;
|
|
240
|
+
}
|
|
241
|
+
declare const enumType: typeof LiveEnum.create;
|
|
242
|
+
/**
|
|
243
|
+
* Live JSON type.
|
|
244
|
+
* Maps to jsonb in Postgres, json in MySQL, text in SQLite.
|
|
245
|
+
*/
|
|
246
|
+
declare class LiveJson<Value, DefaultValue = undefined, Meta extends AtomicMeta = AtomicMeta> extends LiveAtomicType<Value, DefaultValue, Meta> {
|
|
247
|
+
constructor(index?: boolean, unique?: boolean, defaultValue?: DefaultValue, primary?: boolean);
|
|
248
|
+
getStorageFieldType(): StorageFieldType;
|
|
249
|
+
unique(): LiveJson<Value, undefined, Meta>;
|
|
250
|
+
index(): LiveJson<Value, undefined, Meta>;
|
|
251
|
+
default(value: Value): LiveJson<Value, Value, Meta>;
|
|
252
|
+
primary(): LiveJson<Value, undefined, Meta>;
|
|
253
|
+
nullable(): NullableLiveType<LiveJson<Value, DefaultValue extends undefined ? null : DefaultValue, Meta>>;
|
|
254
|
+
static create<T>(): LiveJson<T>;
|
|
255
|
+
}
|
|
256
|
+
declare const json: typeof LiveJson.create;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Forward declaration for LiveCollection to avoid circular dependency.
|
|
260
|
+
* The actual type is defined in collection.ts
|
|
261
|
+
*/
|
|
262
|
+
interface LiveCollectionAny$1 {
|
|
263
|
+
readonly name: string;
|
|
264
|
+
readonly fields: Record<string, LiveTypeAny>;
|
|
265
|
+
readonly relations: Record<string, RelationAny>;
|
|
266
|
+
setRelations<TRelations extends Record<string, RelationAny>>(relations: TRelations): LiveCollectionAny$1;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Represents a relation between two collections.
|
|
270
|
+
*
|
|
271
|
+
* @template TEntity - The target collection
|
|
272
|
+
* @template TSourceEntity - The source collection
|
|
273
|
+
* @template TType - "one" for many-to-one, "many" for one-to-many
|
|
274
|
+
* @template TRelationalColumn - The FK column on source (for "one" relations)
|
|
275
|
+
* @template TForeignColumn - The FK column on target (for "many" relations)
|
|
276
|
+
* @template TRequired - Whether the relation is required
|
|
277
|
+
*/
|
|
278
|
+
declare class Relation<TEntity extends LiveCollectionAny$1, TSourceEntity extends LiveCollectionAny$1, TType extends "one" | "many", TRelationalColumn extends keyof TSourceEntity["fields"], TForeignColumn extends keyof TEntity["fields"], TRequired extends boolean> extends LiveType<InferIndex<TEntity extends LiveTypeAny ? TEntity : LiveTypeAny>, {
|
|
279
|
+
timestamp: string | null;
|
|
280
|
+
} & BaseMeta> {
|
|
281
|
+
readonly entity: TEntity;
|
|
282
|
+
readonly type: TType;
|
|
283
|
+
readonly required: TRequired;
|
|
284
|
+
readonly relationalColumn?: TRelationalColumn;
|
|
285
|
+
readonly foreignColumn?: TForeignColumn;
|
|
286
|
+
readonly sourceEntity: TSourceEntity;
|
|
287
|
+
private constructor();
|
|
288
|
+
encodeMutation(mutationType: MutationType, input: string, timestamp: string): {
|
|
289
|
+
value: string;
|
|
290
|
+
_meta: {
|
|
291
|
+
timestamp: string;
|
|
292
|
+
};
|
|
293
|
+
};
|
|
294
|
+
mergeMutation(mutationType: MutationType, encodedMutation: {
|
|
295
|
+
value: string;
|
|
296
|
+
_meta: {
|
|
297
|
+
timestamp: string;
|
|
298
|
+
};
|
|
299
|
+
}, materializedShape?: MaterializedLiveType<LiveString> | undefined): [
|
|
300
|
+
MaterializedLiveType<LiveString>,
|
|
301
|
+
{
|
|
302
|
+
value: string;
|
|
303
|
+
_meta: {
|
|
304
|
+
timestamp: string;
|
|
305
|
+
};
|
|
306
|
+
} | null
|
|
307
|
+
];
|
|
308
|
+
getStorageFieldType(): StorageFieldType;
|
|
309
|
+
toJSON(): {
|
|
310
|
+
entityName: string;
|
|
311
|
+
type: TType;
|
|
312
|
+
required: TRequired;
|
|
313
|
+
relationalColumn: TRelationalColumn | undefined;
|
|
314
|
+
foreignColumn: TForeignColumn | undefined;
|
|
315
|
+
};
|
|
316
|
+
/**
|
|
317
|
+
* Creates a factory for many-to-one relations.
|
|
318
|
+
*/
|
|
319
|
+
static createOneFactory<TOriginEntity extends LiveCollectionAny$1>(): <TEntity extends LiveCollectionAny$1, TColumn extends keyof TOriginEntity["fields"], TRequired extends boolean = false>(entity: TEntity, column: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "one", TColumn, never, TRequired>;
|
|
320
|
+
/**
|
|
321
|
+
* Creates a factory for one-to-many relations.
|
|
322
|
+
*/
|
|
323
|
+
static createManyFactory<TOriginEntity extends LiveCollectionAny$1>(): <TEntity extends LiveCollectionAny$1, TColumn extends keyof TEntity["fields"], TRequired extends boolean = false>(entity: TEntity, foreignColumn: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "many", never, TColumn, TRequired>;
|
|
324
|
+
}
|
|
325
|
+
type RelationAny = Relation<LiveCollectionAny$1, LiveCollectionAny$1, any, any, any, any>;
|
|
326
|
+
/**
|
|
327
|
+
* Connectors for defining relations.
|
|
328
|
+
*/
|
|
329
|
+
type RelationConnectors<TSourceEntity extends LiveCollectionAny$1> = {
|
|
330
|
+
one: ReturnType<typeof Relation.createOneFactory<TSourceEntity>>;
|
|
331
|
+
many: ReturnType<typeof Relation.createManyFactory<TSourceEntity>>;
|
|
332
|
+
};
|
|
333
|
+
/**
|
|
334
|
+
* Declaration type for relations defined separately from collections.
|
|
335
|
+
*/
|
|
336
|
+
type RelationsDecl<TCollectionName extends string = string, TRelations extends Record<string, RelationAny> = Record<string, RelationAny>> = {
|
|
337
|
+
$type: "relations";
|
|
338
|
+
collectionName: TCollectionName;
|
|
339
|
+
/** @deprecated Use `collectionName` instead */
|
|
340
|
+
objectName: TCollectionName;
|
|
341
|
+
relations: TRelations;
|
|
342
|
+
};
|
|
343
|
+
/**
|
|
344
|
+
* Creates a relations declaration for a collection.
|
|
345
|
+
* Use this for circular dependencies or when defining relations separately.
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```ts
|
|
349
|
+
* const userRelations = createRelations(users, ({ one, many }) => ({
|
|
350
|
+
* posts: many(posts, 'authorId'),
|
|
351
|
+
* profile: one(profile, 'profileId'),
|
|
352
|
+
* }));
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
declare const createRelations: <TSourceCollection extends LiveCollectionAny$1, TRelations extends Record<string, RelationAny>>(collection: TSourceCollection, factory: (connectors: RelationConnectors<TSourceCollection>) => TRelations) => RelationsDecl<TSourceCollection["name"], TRelations>;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Mutation input type for collections.
|
|
359
|
+
*/
|
|
360
|
+
type LiveCollectionMutationInput<TSchema extends LiveCollectionAny> = Partial<{
|
|
361
|
+
[K in keyof TSchema["fields"]]: TSchema["fields"][K]["_value"];
|
|
362
|
+
}>;
|
|
363
|
+
/**
|
|
364
|
+
* Configuration object for creating a collection with inline relations.
|
|
365
|
+
*/
|
|
366
|
+
type CollectionConfig<TName extends string, TFields extends Record<string, LiveTypeAny>, TRelations extends Record<string, RelationAny> = Record<string, never>> = {
|
|
367
|
+
fields: TFields;
|
|
368
|
+
relations?: (connectors: RelationConnectors<LiveCollection<TName, TFields, any>>) => TRelations;
|
|
369
|
+
};
|
|
370
|
+
/**
|
|
371
|
+
* Represents a collection of entities with fields and relations.
|
|
372
|
+
*
|
|
373
|
+
* @template TName - The collection name (used as resource identifier)
|
|
374
|
+
* @template TFields - The field schema
|
|
375
|
+
* @template TRelations - The relations schema
|
|
376
|
+
*/
|
|
377
|
+
declare class LiveCollection<TName extends string, TFields extends Record<string, LiveTypeAny>, TRelations extends Record<string, RelationAny>> extends LiveType<TFields, BaseMeta, LiveCollectionMutationInput<any>, Record<string, MaterializedLiveType<LiveTypeAny>>> {
|
|
378
|
+
readonly name: TName;
|
|
379
|
+
readonly fields: TFields;
|
|
380
|
+
readonly relations: TRelations;
|
|
381
|
+
constructor(name: TName, fields: TFields, relations?: TRelations);
|
|
382
|
+
encodeMutation(_mutationType: MutationType, input: LiveCollectionMutationInput<this>, timestamp: string): Record<string, any>;
|
|
383
|
+
mergeMutation(mutationType: MutationType, encodedMutations: Record<string, MaterializedLiveType<LiveTypeAny>>, materializedShape?: MaterializedLiveType<this> | undefined): [MaterializedLiveType<this>, Record<string, any> | null];
|
|
384
|
+
/**
|
|
385
|
+
* Returns a new collection with the given relations attached.
|
|
386
|
+
*/
|
|
387
|
+
setRelations<TNewRelations extends Record<string, RelationAny>>(relations: TNewRelations): LiveCollection<this["name"], this["fields"], TNewRelations>;
|
|
388
|
+
getStorageFieldType(): StorageFieldType;
|
|
389
|
+
/**
|
|
390
|
+
* Creates a new collection with fields only.
|
|
391
|
+
*/
|
|
392
|
+
static create<TName extends string, TFields extends Record<string, LiveTypeAny>>(name: TName, fields: TFields): LiveCollection<TName, TFields, Record<string, never>>;
|
|
393
|
+
/**
|
|
394
|
+
* Creates a new collection with fields and inline relations.
|
|
395
|
+
*/
|
|
396
|
+
static create<TName extends string, TFields extends Record<string, LiveTypeAny>, TRelations extends Record<string, RelationAny>>(name: TName, config: CollectionConfig<TName, TFields, TRelations>): LiveCollection<TName, TFields, TRelations>;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Creates a new collection.
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```ts
|
|
403
|
+
* // Simple usage (fields only)
|
|
404
|
+
* const users = collection('users', {
|
|
405
|
+
* id: id(),
|
|
406
|
+
* name: string(),
|
|
407
|
+
* });
|
|
408
|
+
*
|
|
409
|
+
* // Full usage (fields + inline relations)
|
|
410
|
+
* const posts = collection('posts', {
|
|
411
|
+
* fields: {
|
|
412
|
+
* id: id(),
|
|
413
|
+
* title: string(),
|
|
414
|
+
* authorId: reference('users.id'),
|
|
415
|
+
* },
|
|
416
|
+
* relations: ({ one, many }) => ({
|
|
417
|
+
* author: one(users, 'authorId'),
|
|
418
|
+
* comments: many(comments, 'postId'),
|
|
419
|
+
* }),
|
|
420
|
+
* });
|
|
421
|
+
* ```
|
|
422
|
+
*/
|
|
423
|
+
declare const collection: typeof LiveCollection.create;
|
|
424
|
+
type LiveCollectionAny = LiveCollection<string, Record<string, LiveTypeAny>, any>;
|
|
425
|
+
/** @deprecated Use `collection` instead */
|
|
426
|
+
declare const object: typeof LiveCollection.create;
|
|
427
|
+
/** @deprecated Use `LiveCollection` instead */
|
|
428
|
+
declare const LiveObject: typeof LiveCollection;
|
|
429
|
+
/** @deprecated Use `LiveCollectionAny` instead */
|
|
430
|
+
type LiveObjectAny = LiveCollectionAny;
|
|
431
|
+
/** @deprecated Use `LiveCollectionMutationInput` instead */
|
|
432
|
+
type LiveObjectMutationInput<TSchema extends LiveCollectionAny> = LiveCollectionMutationInput<TSchema>;
|
|
433
|
+
|
|
434
|
+
/** biome-ignore-all lint/complexity/noBannedTypes: false positive */
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Where clause for filtering collections.
|
|
438
|
+
* Supports field comparisons, operators, and nested relation queries.
|
|
439
|
+
*/
|
|
440
|
+
type WhereClause<T extends LiveCollectionAny> = ({
|
|
441
|
+
[K in keyof T["fields"]]?: InferLiveType<T["fields"][K]> | ({
|
|
442
|
+
$eq?: InferLiveType<T["fields"][K]>;
|
|
443
|
+
$in?: InferLiveType<T["fields"][K]>[];
|
|
444
|
+
$not?: InferLiveType<T["fields"][K]> | {
|
|
445
|
+
$in?: InferLiveType<T["fields"][K]>[];
|
|
446
|
+
$eq?: InferLiveType<T["fields"][K]>;
|
|
447
|
+
};
|
|
448
|
+
} & (Exclude<InferLiveType<T["fields"][K]>, null | undefined> extends number | Date ? {
|
|
449
|
+
$gt?: InferLiveType<T["fields"][K]>;
|
|
450
|
+
$gte?: InferLiveType<T["fields"][K]>;
|
|
451
|
+
$lt?: InferLiveType<T["fields"][K]>;
|
|
452
|
+
$lte?: InferLiveType<T["fields"][K]>;
|
|
453
|
+
} : {}));
|
|
454
|
+
} & {
|
|
455
|
+
[K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
|
|
456
|
+
}) | {
|
|
457
|
+
$and?: WhereClause<T>[];
|
|
458
|
+
$or?: WhereClause<T>[];
|
|
459
|
+
};
|
|
460
|
+
/**
|
|
461
|
+
* Sub-query options for nested includes.
|
|
462
|
+
* Allows filtering, sorting, and pagination of related collections.
|
|
463
|
+
*/
|
|
464
|
+
type SubQueryInclude<T extends LiveCollectionAny> = {
|
|
465
|
+
where?: WhereClause<T>;
|
|
466
|
+
limit?: number;
|
|
467
|
+
orderBy?: {
|
|
468
|
+
key: keyof T["fields"] & string;
|
|
469
|
+
direction: "asc" | "desc";
|
|
470
|
+
}[];
|
|
471
|
+
include?: IncludeClause<T>;
|
|
472
|
+
};
|
|
473
|
+
/**
|
|
474
|
+
* Include clause for specifying which relations to fetch.
|
|
475
|
+
* Can be a boolean or a sub-query for fine-grained control.
|
|
476
|
+
*/
|
|
477
|
+
type IncludeClause<T extends LiveCollectionAny> = {
|
|
478
|
+
[K in keyof T["relations"]]?: boolean | SubQueryInclude<T["relations"][K]["entity"]>;
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
type Simplify<T> = T extends object ? {
|
|
482
|
+
[K in keyof T]: T[K];
|
|
483
|
+
} : T;
|
|
484
|
+
declare const LogLevel: {
|
|
485
|
+
readonly CRITICAL: 0;
|
|
486
|
+
readonly ERROR: 1;
|
|
487
|
+
readonly WARN: 2;
|
|
488
|
+
readonly INFO: 3;
|
|
489
|
+
readonly DEBUG: 4;
|
|
490
|
+
};
|
|
491
|
+
type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
|
|
492
|
+
/**
|
|
493
|
+
* Logger configuration options
|
|
494
|
+
*/
|
|
495
|
+
interface LoggerOptions {
|
|
496
|
+
/**
|
|
497
|
+
* Minimum log level to display. Anything below this level will be muted.
|
|
498
|
+
* @default LogLevel.INFO
|
|
499
|
+
*/
|
|
500
|
+
level?: LogLevel;
|
|
501
|
+
/**
|
|
502
|
+
* Optional prefix to add to all log messages
|
|
503
|
+
*/
|
|
504
|
+
prefix?: string;
|
|
505
|
+
}
|
|
506
|
+
declare class Logger {
|
|
507
|
+
private level;
|
|
508
|
+
private prefix;
|
|
509
|
+
constructor(options?: LoggerOptions);
|
|
510
|
+
critical(...args: any[]): void;
|
|
511
|
+
error(...args: any[]): void;
|
|
512
|
+
warn(...args: any[]): void;
|
|
513
|
+
info(...args: any[]): void;
|
|
514
|
+
debug(...args: any[]): void;
|
|
515
|
+
setLevel(level: LogLevel): void;
|
|
516
|
+
getLevel(): LogLevel;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Infers the TypeScript type of a collection without relations.
|
|
521
|
+
*/
|
|
522
|
+
type InferLiveCollectionWithoutRelations<T extends LiveCollectionAny> = {
|
|
523
|
+
[K in keyof T["fields"]]: InferLiveType<T["fields"][K]>;
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* Infers relational columns (FK fields) from relations.
|
|
527
|
+
* Note: We use `string` directly since InferIndex currently returns string
|
|
528
|
+
* and RelationAny uses an interface that doesn't extend LiveTypeAny.
|
|
529
|
+
*/
|
|
530
|
+
type InferRelationalColumns<T extends Record<string, RelationAny>> = {
|
|
531
|
+
[K in keyof T as T[K]["type"] extends "many" ? never : T[K]["relationalColumn"]]: T[K]["required"] extends true ? string : string | null;
|
|
532
|
+
};
|
|
533
|
+
/**
|
|
534
|
+
* Infers the TypeScript type of a collection with relational IDs.
|
|
535
|
+
* Used for mutations where you need to set FK values.
|
|
536
|
+
*/
|
|
537
|
+
type InferLiveCollectionWithRelationalIds<T extends LiveCollectionAny> = keyof T["relations"] extends string ? InferLiveCollectionWithoutRelations<T> & InferRelationalColumns<T["relations"]> : InferLiveCollectionWithoutRelations<T>;
|
|
538
|
+
/**
|
|
539
|
+
* Infers the full TypeScript type of a collection with optional relations.
|
|
540
|
+
*
|
|
541
|
+
* @template T - The collection type
|
|
542
|
+
* @template Include - The include clause specifying which relations to include
|
|
543
|
+
*/
|
|
544
|
+
type InferLiveCollection<T extends LiveCollectionAny, Include extends IncludeClause<T> | undefined = undefined> = Simplify<InferLiveCollectionWithoutRelations<T> & (Include extends IncludeClause<T> ? {
|
|
545
|
+
[K in keyof T["relations"] as Include[K] extends true | SubQueryInclude<T["relations"][K]["entity"]> ? K : never]: Include[K] extends true ? T["relations"][K]["type"] extends "one" ? T["fields"][Exclude<T["relations"][K]["relationalColumn"], undefined>] extends NullableLiveType<any> ? InferLiveCollection<T["relations"][K]["entity"]> | null : InferLiveCollection<T["relations"][K]["entity"]> : InferLiveCollection<T["relations"][K]["entity"]>[] : Include[K] extends SubQueryInclude<T["relations"][K]["entity"]> ? T["relations"][K]["type"] extends "one" ? T["fields"][Exclude<T["relations"][K]["relationalColumn"], undefined>] extends NullableLiveType<any> ? InferLiveCollection<T["relations"][K]["entity"], Include[K]["include"]> | null : InferLiveCollection<T["relations"][K]["entity"], Include[K]["include"]> : InferLiveCollection<T["relations"][K]["entity"], Include[K]["include"]>[] : never;
|
|
546
|
+
} : object)>;
|
|
547
|
+
type GetFieldType<T> = T extends NullableLiveType<any> ? T["inner"] : T;
|
|
548
|
+
type HasDefaultValue<T> = T extends {
|
|
549
|
+
defaultValue: undefined;
|
|
550
|
+
} ? false : true;
|
|
551
|
+
/**
|
|
552
|
+
* Infers the insert type for a collection.
|
|
553
|
+
* Fields without defaults are required, fields with defaults are optional.
|
|
554
|
+
*/
|
|
555
|
+
type InferInsert<T extends LiveCollectionAny> = Simplify<{
|
|
556
|
+
[K in keyof T["fields"] as HasDefaultValue<GetFieldType<T["fields"][K]>> extends true ? never : K]: InferLiveType<T["fields"][K]>;
|
|
557
|
+
} & {
|
|
558
|
+
[K in keyof T["fields"] as HasDefaultValue<GetFieldType<T["fields"][K]>> extends false ? never : K]?: InferLiveType<T["fields"][K]>;
|
|
559
|
+
}>;
|
|
560
|
+
/**
|
|
561
|
+
* Infers the update type for a collection.
|
|
562
|
+
* All fields are optional except id which is excluded.
|
|
563
|
+
*/
|
|
564
|
+
type InferUpdate<T extends LiveCollectionAny> = Simplify<Omit<LiveCollectionMutationInput<T>, "id">>;
|
|
565
|
+
/**
|
|
566
|
+
* Extracts the value from a MaterializedLiveType at runtime.
|
|
567
|
+
*/
|
|
568
|
+
declare const inferValue: <T extends LiveTypeAny>(type?: MaterializedLiveType<T>) => InferLiveType<T> | undefined;
|
|
569
|
+
/** @deprecated Use `InferLiveCollection` instead */
|
|
570
|
+
type InferLiveObject<T extends LiveCollectionAny, Include extends IncludeClause<T> | undefined = undefined> = InferLiveCollection<T, Include>;
|
|
571
|
+
/** @deprecated Use `InferLiveCollectionWithoutRelations` instead */
|
|
572
|
+
type InferLiveObjectWithoutRelations<T extends LiveCollectionAny> = InferLiveCollectionWithoutRelations<T>;
|
|
573
|
+
/** @deprecated Use `InferLiveCollectionWithRelationalIds` instead */
|
|
574
|
+
type InferLiveObjectWithRelationalIds<T extends LiveCollectionAny> = InferLiveCollectionWithRelationalIds<T>;
|
|
575
|
+
|
|
576
|
+
type ExtractObjectValues<T> = T[keyof T];
|
|
577
|
+
type ParseRelationsFromSchema<TRawSchema extends RawSchema, TCollectionName extends string> = ExtractObjectValues<{
|
|
578
|
+
[K in keyof TRawSchema]: TRawSchema[K] extends RelationsDecl<infer TCollectionName_, any> ? TCollectionName_ extends TCollectionName ? {
|
|
579
|
+
[K2 in keyof TRawSchema[K]["relations"]]: Relation<ParseCollectionFromSchema<TRawSchema, TRawSchema[K]["relations"][K2]["entity"]["name"]>, TRawSchema[K]["relations"][K2]["sourceEntity"], TRawSchema[K]["relations"][K2]["type"], Exclude<TRawSchema[K]["relations"][K2]["relationalColumn"], undefined>, Exclude<TRawSchema[K]["relations"][K2]["foreignColumn"], undefined>, TRawSchema[K]["relations"][K2]["required"]>;
|
|
580
|
+
} : never : never;
|
|
581
|
+
}>;
|
|
582
|
+
type ParseCollectionFromSchema<TRawSchema extends RawSchema, TCollectionName extends string> = ExtractObjectValues<{
|
|
583
|
+
[K in keyof TRawSchema]: TRawSchema[K] extends LiveCollectionAny ? TRawSchema[K]["name"] extends TCollectionName ? LiveCollection<TRawSchema[K]["name"], TRawSchema[K]["fields"], ParseRelationsFromSchema<TRawSchema, TRawSchema[K]["name"]>> : never : never;
|
|
584
|
+
}>;
|
|
585
|
+
type RawSchema = Record<string, LiveCollectionAny | RelationsDecl>;
|
|
586
|
+
/**
|
|
587
|
+
* The final schema type with all collections and their relations resolved.
|
|
588
|
+
*/
|
|
589
|
+
type Schema<TRawSchema extends RawSchema> = {
|
|
590
|
+
[K in keyof TRawSchema as TRawSchema[K] extends LiveCollectionAny ? TRawSchema[K]["name"] : never]: TRawSchema[K] extends LiveCollectionAny ? ParseCollectionFromSchema<TRawSchema, TRawSchema[K]["name"]> : never;
|
|
591
|
+
};
|
|
592
|
+
/**
|
|
593
|
+
* Creates a schema from a raw schema object.
|
|
594
|
+
* Attaches relations to their corresponding collections.
|
|
595
|
+
*
|
|
596
|
+
* @example
|
|
597
|
+
* ```ts
|
|
598
|
+
* const schema = createSchema({
|
|
599
|
+
* users,
|
|
600
|
+
* userRelations,
|
|
601
|
+
* posts,
|
|
602
|
+
* postRelations,
|
|
603
|
+
* });
|
|
604
|
+
* ```
|
|
605
|
+
*/
|
|
606
|
+
declare const createSchema: <TRawSchema extends RawSchema>(schema: TRawSchema) => Schema<TRawSchema>;
|
|
607
|
+
|
|
608
|
+
export { inferValue as $, type AtomicMeta as A, type BaseMeta as B, timestamp as C, id as D, reference as E, enumType as F, json as G, createRelations as H, type IncludeClause as I, type RelationAny as J, type RelationConnectors as K, type LiveObjectAny as L, type MaterializedLiveType as M, NullableLiveType as N, type RelationsDecl as O, type LiveCollectionAny$1 as P, LiveCollection as Q, Relation as R, type Simplify as S, collection as T, type LiveCollectionMutationInput as U, type CollectionConfig as V, type WhereClause as W, object as X, LiveObject as Y, type LiveObjectMutationInput as Z, type SubQueryInclude as _, type InferLiveObject as a, type InferLiveCollectionWithoutRelations as a0, type InferLiveCollectionWithRelationalIds as a1, type InferLiveObjectWithoutRelations as a2, createSchema as a3, type InferInsert as b, type InferUpdate as c, type Schema as d, type LiveCollectionAny as e, type InferLiveCollection as f, type InferLiveObjectWithRelationalIds as g, Logger as h, LogLevel as i, LiveType as j, type LiveTypeAny as k, type MutationType as l, type StorageFieldType as m, type InferLiveType as n, type InferIndex as o, type LiveTypeMeta as p, LiveAtomicType as q, LiveNumber as r, LiveString as s, LiveBoolean as t, LiveTimestamp as u, LiveEnum as v, LiveJson as w, number as x, string as y, boolean as z };
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';require('js-xxhash');var
|
|
1
|
+
'use strict';require('js-xxhash');var r=class{_value;_meta;_encodeInput;_decodeInput};var s=class e extends r{inner;constructor(t){super(),this.inner=t;}encodeMutation(t,i,n){return this.inner.encodeMutation(t,i,n)}mergeMutation(t,i,n){return this.inner.mergeMutation(t,i,n)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}unique(){return new e(this.inner.unique())}index(){return new e(this.inner.index())}default(t){let i=new o(this.inner.storageType,this.inner.convertFunc,this.inner.isIndex,this.inner.isUnique,t,this.inner.foreignReference,this.inner.isPrimary);return new e(i)}primary(){return new e(this.inner.primary())}},o=class e extends r{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(t,i,n,a,l,d,T){super(),this.storageType=t,this.convertFunc=i,this.isIndex=n??false,this.isUnique=a??false,this.defaultValue=l,this.foreignReference=d,this.isPrimary=T??false;}encodeMutation(t,i,n){return {value:i,_meta:{timestamp:n}}}mergeMutation(t,i,n){return n&&n._meta.timestamp&&i._meta.timestamp&&n._meta.timestamp.localeCompare(i._meta.timestamp)>0?[n,null]:[{value:this.convertFunc?this.convertFunc(i.value):i.value,_meta:i._meta},i]}getStorageFieldType(){return {type:this.storageType,nullable:false,index:this.isIndex,unique:this.isUnique,default:this.defaultValue,references:this.foreignReference,primary:this.isPrimary}}unique(){return new e(this.storageType,this.convertFunc,this.isIndex,true,void 0,this.foreignReference,this.isPrimary)}index(){return new e(this.storageType,this.convertFunc,true,this.isUnique,void 0,this.foreignReference,this.isPrimary)}default(t){return new e(this.storageType,this.convertFunc,this.isIndex,this.isUnique,t,this.foreignReference,this.isPrimary)}primary(){return new e(this.storageType,this.convertFunc,this.isIndex,this.isUnique,void 0,this.foreignReference,true)}nullable(){if(this.defaultValue===void 0){let t=new e(this.storageType,this.convertFunc,this.isIndex,this.isUnique,null,this.foreignReference,this.isPrimary);return new s(t)}return new s(this)}withMeta(){return new e(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}},p=class e extends o{constructor(){super("integer",t=>Number(t));}static create(){return new e}},V=p.create,y=class e extends o{constructor(t){super("varchar",void 0,void 0,void 0,void 0,t);}static create(){return new e}static createId(){return new e().index().unique().primary()}static createReference(t){return new e(t)}},M=y.create,I=y.createId,A=y.createReference,f=class e extends o{constructor(){super("boolean",t=>typeof t=="string"?t.toLowerCase()==="true":!!t);}static create(){return new e}},b=f.create,m=class e extends o{constructor(){super("timestamp",t=>typeof t=="string"?new Date(t):t);}static create(){return new e}},w=m.create,v=class e extends o{enumValues;enumName;constructor(t,i,n,a,l){super("varchar",void 0,i,n,a,void 0,l),this.enumValues=t,this.enumName=`enum_${t.slice(0,5).join("_").slice(0,50)}`;}getStorageFieldType(){return {type:"varchar",nullable:false,index:this.isIndex,unique:this.isUnique,default:this.defaultValue,primary:this.isPrimary,enumValues:this.enumValues,enumName:this.enumName}}unique(){return new e(this.enumValues,this.isIndex,true,void 0,this.isPrimary)}index(){return new e(this.enumValues,true,this.isUnique,void 0,this.isPrimary)}default(t){return new e(this.enumValues,this.isIndex,this.isUnique,t,this.isPrimary)}primary(){return new e(this.enumValues,this.isIndex,this.isUnique,void 0,true)}nullable(){if(this.defaultValue===void 0){let t=new e(this.enumValues,this.isIndex,this.isUnique,null,this.isPrimary);return new s(t)}return new s(this)}static create(t){return new e(t)}},N=v.create,x=class e extends o{constructor(t,i,n,a){super("jsonb",l=>typeof l=="string"?JSON.parse(l):l,t,i,n,void 0,a);}getStorageFieldType(){return {type:"jsonb",nullable:false,index:this.isIndex,unique:this.isUnique,default:this.defaultValue!==void 0?JSON.stringify(this.defaultValue):void 0,primary:this.isPrimary}}unique(){return new e(this.isIndex,true,void 0,this.isPrimary)}index(){return new e(true,this.isUnique,void 0,this.isPrimary)}default(t){return new e(this.isIndex,this.isUnique,t,this.isPrimary)}primary(){return new e(this.isIndex,this.isUnique,void 0,true)}nullable(){if(this.defaultValue===void 0){let t=new e(this.isIndex,this.isUnique,null,this.isPrimary);return new s(t)}return new s(this)}static create(){return new e}},S=x.create;var u=class e extends r{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(t,i,n,a,l){super(),this.entity=t,this.type=i,this.required=l??false,this.relationalColumn=n,this.foreignColumn=a;}encodeMutation(t,i,n){if(t!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:i,_meta:{timestamp:n}}}mergeMutation(t,i,n){var a;return this.type==="many"?n?[n,null]:[i,i]:(a=n==null?void 0:n._meta)!=null&&a.timestamp&&i._meta.timestamp&&n._meta.timestamp.localeCompare(i._meta.timestamp)>=0?[n,null]:[i,i]}getStorageFieldType(){return {type:"varchar",nullable:!this.required,references:`${this.entity.name}.${String(this.foreignColumn??this.relationalColumn??"id")}`}}toJSON(){return {entityName:this.entity.name,type:this.type,required:this.required,relationalColumn:this.relationalColumn,foreignColumn:this.foreignColumn}}static createOneFactory(){return (t,i,n)=>new e(t,"one",i,void 0,n??false)}static createManyFactory(){return (t,i,n)=>new e(t,"many",void 0,i,n??false)}},F=(e,t)=>({$type:"relations",collectionName:e.name,objectName:e.name,relations:t({one:u.createOneFactory(),many:u.createManyFactory()})});var c=class e extends r{name;fields;relations;constructor(t,i,n){super(),this.name=t,this.fields=i,this.relations=n??{};}encodeMutation(t,i,n){return Object.fromEntries(Object.entries(i).map(([a,l])=>[a,(this.fields[a]??this.relations[a]).encodeMutation("set",l,n)]))}mergeMutation(t,i,n){let a={};return [{value:{...(n==null?void 0:n.value)??{},...Object.fromEntries(Object.entries(i).map(([l,d])=>{let T=this.fields[l]??this.relations[l];if(!T)return [l,d];let[C,R]=T.mergeMutation(t,d,n==null?void 0:n.value[l]);return R&&(a[l]=R),[l,C]}))}},a]}setRelations(t){return new e(this.name,this.fields,t)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(t,i){if(E(i)){let n=i,a=new e(t,n.fields);if(n.relations){let l=n.relations({one:u.createOneFactory(),many:u.createManyFactory()});return new e(t,n.fields,l)}return a}return new e(t,i,void 0)}};function E(e){return typeof e=="object"&&e!==null&&"fields"in e&&typeof e.fields=="object"}var g=c.create,K=g,D=c;var h=e=>typeof e=="object"&&e!==null&&"value"in e&&!Array.isArray(e),L=e=>e?Array.isArray(e.value)?e.value.map(i=>h(i)?L(i):i):typeof e.value!="object"||e.value===null||e.value instanceof Date?e.value:Object.fromEntries(Object.entries(e.value).map(([i,n])=>Array.isArray(n)?[i,n.map(a=>h(a)?L(a):a)]:h(n)?[i,L(n)]:[i,n])):void 0;var O=e=>Object.fromEntries(Object.entries(e).flatMap(([t,i])=>{if(i.$type==="relations")return [];let n=i,a=Object.values(e).find(l=>l.$type==="relations"&&l.collectionName===i.name);return a&&(n=n.setRelations(a.relations)),[[n.name,n]]}));var q={CRITICAL:0,ERROR:1,WARN:2,INFO:3,DEBUG:4};exports.LiveAtomicType=o;exports.LiveBoolean=f;exports.LiveCollection=c;exports.LiveEnum=v;exports.LiveJson=x;exports.LiveNumber=p;exports.LiveObject=D;exports.LiveString=y;exports.LiveTimestamp=m;exports.LiveType=r;exports.LogLevel=q;exports.NullableLiveType=s;exports.Relation=u;exports.boolean=b;exports.collection=g;exports.createRelations=F;exports.createSchema=O;exports.enumType=N;exports.id=I;exports.inferValue=L;exports.json=S;exports.number=V;exports.object=K;exports.reference=A;exports.string=M;exports.timestamp=w;
|
package/dist/index.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { A as AtomicMeta, B as BaseMeta, V as CollectionConfig, I as IncludeClause, o as InferIndex, b as InferInsert, f as InferLiveCollection, a1 as InferLiveCollectionWithRelationalIds, a0 as InferLiveCollectionWithoutRelations, a as InferLiveObject, g as InferLiveObjectWithRelationalIds, a2 as InferLiveObjectWithoutRelations, n as InferLiveType, c as InferUpdate, q as LiveAtomicType, t as LiveBoolean, Q as LiveCollection, e as LiveCollectionAny, U as LiveCollectionMutationInput, v as LiveEnum, w as LiveJson, r as LiveNumber, Y as LiveObject, L as LiveObjectAny, Z as LiveObjectMutationInput, s as LiveString, u as LiveTimestamp, j as LiveType, k as LiveTypeAny, p as LiveTypeMeta, i as LogLevel, M as MaterializedLiveType, l as MutationType, N as NullableLiveType, R as Relation, J as RelationAny, K as RelationConnectors, O as RelationsDecl, d as Schema, m as StorageFieldType, _ as SubQueryInclude, W as WhereClause, P as _ILiveCollectionAny, z as boolean, T as collection, H as createRelations, a3 as createSchema, F as enumType, D as id, $ as inferValue, G as json, x as number, X as object, E as reference, y as string, C as timestamp } from './index-hO1iQ-VU.cjs';
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { A as AtomicMeta, B as BaseMeta, V as CollectionConfig, I as IncludeClause, o as InferIndex, b as InferInsert, f as InferLiveCollection, a1 as InferLiveCollectionWithRelationalIds, a0 as InferLiveCollectionWithoutRelations, a as InferLiveObject, g as InferLiveObjectWithRelationalIds, a2 as InferLiveObjectWithoutRelations, n as InferLiveType, c as InferUpdate, q as LiveAtomicType, t as LiveBoolean, Q as LiveCollection, e as LiveCollectionAny, U as LiveCollectionMutationInput, v as LiveEnum, w as LiveJson, r as LiveNumber, Y as LiveObject, L as LiveObjectAny, Z as LiveObjectMutationInput, s as LiveString, u as LiveTimestamp, j as LiveType, k as LiveTypeAny, p as LiveTypeMeta, i as LogLevel, M as MaterializedLiveType, l as MutationType, N as NullableLiveType, R as Relation, J as RelationAny, K as RelationConnectors, O as RelationsDecl, d as Schema, m as StorageFieldType, _ as SubQueryInclude, W as WhereClause, P as _ILiveCollectionAny, z as boolean, T as collection, H as createRelations, a3 as createSchema, F as enumType, D as id, $ as inferValue, G as json, x as number, X as object, E as reference, y as string, C as timestamp } from './index-hO1iQ-VU.js';
|