@routier/core 0.2.0 → 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.
- package/LICENSE +21 -0
- package/{readme.md → README.md} +2 -2
- package/dist/assertions/index.cjs +191 -0
- package/dist/assertions/index.cjs.map +1 -0
- package/dist/assertions/index.d.ts +30 -0
- package/dist/assertions/index.js +94 -30
- package/dist/assertions/index.js.map +1 -1
- package/dist/capabilities/index.cjs +820 -0
- package/dist/capabilities/index.cjs.map +1 -0
- package/dist/capabilities/index.js +580 -545
- package/dist/capabilities/index.js.map +1 -1
- package/dist/codegen/handlers/CloneHandlerBuilder.d.ts +10 -1
- package/dist/codegen/handlers/SetHandlerBuilder.d.ts +4 -0
- package/dist/codegen/handlers/clone/CloneArrayHandler.d.ts +3 -0
- package/dist/codegen/handlers/clone/CloneDateHandler.d.ts +13 -0
- package/dist/codegen/handlers/clone/CloneValueHandler.d.ts +16 -0
- package/dist/codegen/handlers/compare/CompareComputedHandler.d.ts +10 -0
- package/dist/codegen/handlers/compare/CompareFunctionHandler.d.ts +10 -0
- package/dist/codegen/handlers/deserialize/DeserializeArrayHandler.d.ts +14 -0
- package/dist/codegen/handlers/enableChangeTracking/EnableChangeTrackingArrayHandler.d.ts +11 -0
- package/dist/codegen/handlers/enrichment/EnrichmentArrayHandler.d.ts +11 -0
- package/dist/codegen/handlers/freeze/FreezeArrayHandler.d.ts +6 -0
- package/dist/codegen/handlers/hash/HashArrayHandler.d.ts +10 -0
- package/dist/codegen/handlers/hash/HashFileHandler.d.ts +24 -0
- package/dist/codegen/handlers/hash/HashObjectHandler.d.ts +10 -0
- package/dist/codegen/handlers/hash/HashTransformHandler.d.ts +23 -0
- package/dist/codegen/handlers/index.d.ts +1 -0
- package/dist/codegen/handlers/merge/MergeArrayHandler.d.ts +20 -0
- package/dist/codegen/handlers/merge/MergeObjectHandler.d.ts +10 -0
- package/dist/codegen/handlers/serialize/SerializeArrayHandler.d.ts +19 -0
- package/dist/codegen/handlers/serialize/SerializeComputedHandler.d.ts +6 -0
- package/dist/codegen/handlers/set/SetComplexHandler.d.ts +6 -0
- package/dist/codegen/handlers/strip/StripComputedHandler.d.ts +10 -0
- package/dist/codegen/handlers/strip/StripFunctionHandler.d.ts +9 -0
- package/dist/codegen/handlers/types.d.ts +55 -1
- package/dist/codegen/index.cjs +618 -0
- package/dist/codegen/index.cjs.map +1 -0
- package/dist/codegen/index.js +100 -127
- package/dist/codegen/index.js.map +1 -1
- package/dist/collections/Changes.d.ts +2 -2
- package/dist/collections/MemoryDataCollection.d.ts +15 -1
- package/dist/collections/TagCollection.d.ts +7 -8
- package/dist/collections/index.cjs +702 -0
- package/dist/collections/index.cjs.map +1 -0
- package/dist/collections/index.js +282 -276
- package/dist/collections/index.js.map +1 -1
- package/dist/errors/OptimisticConcurrencyError.d.ts +18 -0
- package/dist/errors/PluginDestroyedError.d.ts +10 -0
- package/dist/errors/index.cjs +132 -0
- package/dist/errors/index.cjs.map +1 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +61 -19
- package/dist/errors/index.js.map +1 -1
- package/dist/expressions/constants.d.ts +2 -0
- package/dist/expressions/evaluate.d.ts +53 -0
- package/dist/expressions/index.cjs +2087 -0
- package/dist/expressions/index.cjs.map +1 -0
- package/dist/expressions/index.d.ts +2 -0
- package/dist/expressions/index.js +1784 -772
- package/dist/expressions/index.js.map +1 -1
- package/dist/expressions/parser.d.ts +19 -0
- package/dist/expressions/types.d.ts +87 -2
- package/dist/expressions/utils.d.ts +1 -2
- package/dist/index.cjs +13925 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +12094 -7049
- package/dist/index.js.map +1 -1
- package/dist/performance/index.cjs +217 -0
- package/dist/performance/index.cjs.map +1 -0
- package/dist/performance/index.js +126 -50
- package/dist/performance/index.js.map +1 -1
- package/dist/pipeline/index.cjs +576 -0
- package/dist/pipeline/index.cjs.map +1 -0
- package/dist/pipeline/index.js +229 -156
- package/dist/pipeline/index.js.map +1 -1
- package/dist/plugins/BatchingDbPlugin.d.ts +139 -0
- package/dist/plugins/CacheDbPlugin.d.ts +70 -0
- package/dist/plugins/ConcurrencyDbPlugin.d.ts +78 -0
- package/dist/plugins/EphemeralDataPlugin.d.ts +57 -1
- package/dist/plugins/RetryDbPlugin.d.ts +77 -0
- package/dist/plugins/index.cjs +6104 -0
- package/dist/plugins/index.cjs.map +1 -0
- package/dist/plugins/index.d.ts +5 -1
- package/dist/plugins/index.js +5572 -1491
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/query/QueryOptionsCollection.d.ts +22 -0
- package/dist/plugins/query/index.d.ts +2 -0
- package/dist/plugins/query/join.d.ts +210 -0
- package/dist/plugins/query/similarity.d.ts +38 -0
- package/dist/plugins/query/types.d.ts +64 -1
- package/dist/plugins/translators/DataTranslator.d.ts +28 -0
- package/dist/plugins/translators/JsonTranslator.d.ts +27 -0
- package/dist/plugins/translators/SqlTranslator.d.ts +45 -0
- package/dist/plugins/translators/TranslatedArrayValue.d.ts +3 -1
- package/dist/plugins/translators/TranslatedGroupValue.d.ts +3 -1
- package/dist/plugins/translators/TranslatedSingleValue.d.ts +3 -1
- package/dist/plugins/translators/TupleTranslator.d.ts +45 -0
- package/dist/plugins/translators/index.d.ts +1 -0
- package/dist/plugins/translators/types.d.ts +8 -1
- package/dist/plugins/types.d.ts +87 -11
- package/dist/plugins/wire/handler.d.ts +122 -0
- package/dist/plugins/wire/index.d.ts +4 -0
- package/dist/plugins/wire/persist.d.ts +43 -0
- package/dist/plugins/wire/query.d.ts +49 -0
- package/dist/plugins/wire/types.d.ts +123 -0
- package/dist/results/Result.d.ts +2 -2
- package/dist/results/index.cjs +174 -0
- package/dist/results/index.cjs.map +1 -0
- package/dist/results/index.js +29 -44
- package/dist/results/index.js.map +1 -1
- package/dist/schema/PropertyInfo.d.ts +35 -2
- package/dist/schema/SchemaDefinition.d.ts +51 -1
- package/dist/schema/builder.d.ts +35 -1
- package/dist/schema/communication/broadcast.d.ts +17 -1
- package/dist/schema/index.cjs +6911 -0
- package/dist/schema/index.cjs.map +1 -0
- package/dist/schema/index.d.ts +4 -0
- package/dist/schema/index.js +5781 -3973
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/property/base/SchemaBase.d.ts +50 -2
- package/dist/schema/property/modifiers/SchemaDeserialize.d.ts +5 -1
- package/dist/schema/property/modifiers/SchemaForeignKey.d.ts +15 -0
- package/dist/schema/property/modifiers/SchemaKey.d.ts +2 -0
- package/dist/schema/property/modifiers/SchemaNullable.d.ts +15 -1
- package/dist/schema/property/modifiers/SchemaOptional.d.ts +22 -1
- package/dist/schema/property/modifiers/SchemaSearchable.d.ts +34 -0
- package/dist/schema/property/modifiers/SchemaTag.d.ts +27 -0
- package/dist/schema/property/modifiers/SchemaTracked.d.ts +5 -1
- package/dist/schema/property/modifiers/index.d.ts +3 -0
- package/dist/schema/property/types/SchemaArray.d.ts +2 -0
- package/dist/schema/property/types/SchemaBoolean.d.ts +2 -0
- package/dist/schema/property/types/SchemaDate.d.ts +2 -0
- package/dist/schema/property/types/SchemaFile.d.ts +60 -0
- package/dist/schema/property/types/SchemaNumber.d.ts +5 -1
- package/dist/schema/property/types/SchemaObject.d.ts +2 -0
- package/dist/schema/property/types/SchemaString.d.ts +25 -1
- package/dist/schema/property/types/SchemaVector.d.ts +61 -0
- package/dist/schema/property/types/index.d.ts +2 -0
- package/dist/schema/table/SchemaTransform.d.ts +40 -0
- package/dist/schema/table/index.d.ts +1 -0
- package/dist/schema/types.d.ts +200 -32
- package/dist/schema/utils/propertyKind.d.ts +4 -0
- package/dist/schema/utils/standardJsonSchema.d.ts +90 -0
- package/dist/types/index.cjs +23 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.js +0 -14
- package/dist/types/index.js.map +1 -1
- package/dist/utilities/functions.d.ts +1 -1
- package/dist/utilities/index.cjs +982 -0
- package/dist/utilities/index.cjs.map +1 -0
- package/dist/utilities/index.js +590 -321
- package/dist/utilities/index.js.map +1 -1
- package/dist/utilities/logger.d.ts +48 -0
- package/package.json +152 -98
- package/dist/collections/Changes.test.d.ts +0 -1
- package/dist/collections/MemoryDataCollection.test.d.ts +0 -1
- package/dist/collections/TagCollection.test.d.ts +0 -1
- package/dist/expressions/parser.test.d.ts +0 -1
- package/dist/expressions/utils.test.d.ts +0 -1
- package/dist/pipeline/SyncronousQueue.test.d.ts +0 -1
- package/dist/pipeline/WorkPipeline.test.d.ts +0 -1
- package/dist/plugins/EphemeralDataPlugin.test.d.ts +0 -1
- package/dist/plugins/query/QueryOptionsCollection.test.d.ts +0 -1
- package/dist/plugins/replication/OptimisticReplicationDbPlugin.d.ts +0 -23
- package/dist/plugins/replication/ReplicationDbPlugin.d.ts +0 -22
- package/dist/plugins/replication/index.d.ts +0 -3
- package/dist/plugins/replication/types.d.ts +0 -5
- package/dist/plugins/translators/JsonTranslator.test.d.ts +0 -1
- package/dist/schema/PropertyInfo.test.d.ts +0 -1
- package/dist/schema/schemaGeneration.test.d.ts +0 -1
- package/dist/schema/testSchemas.test.d.ts +0 -314
- package/dist/utilities/arrays.test.d.ts +0 -1
- package/dist/utilities/dates.test.d.ts +0 -1
- package/dist/utilities/strings.test.d.ts +0 -1
package/dist/schema/types.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import { SchemaDefinition } from "./SchemaDefinition";
|
|
2
|
-
import { SchemaBase } from "./property/base/SchemaBase";
|
|
3
|
-
import { SchemaArray } from "./property/types/SchemaArray";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
1
|
+
import type { SchemaDefinition } from "./SchemaDefinition";
|
|
2
|
+
import type { SchemaBase } from "./property/base/SchemaBase";
|
|
3
|
+
import type { SchemaArray } from "./property/types/SchemaArray";
|
|
4
|
+
import type { SchemaVector } from "./property/types/SchemaVector";
|
|
5
|
+
import type { SchemaObject } from "./property/types/SchemaObject";
|
|
6
|
+
import type { PropertyInfo } from "./PropertyInfo";
|
|
7
|
+
import type { DeepPartial } from "../types";
|
|
8
|
+
import type { SchemaFunction } from "./table";
|
|
9
|
+
import type { SchemaOptional, SchemaTag } from "./property/modifiers";
|
|
10
|
+
import type { Branded } from "../utilities/types";
|
|
11
|
+
import type { SchemaSubscriptionOptions } from "./communication/broadcast";
|
|
10
12
|
export type DefaultValue<T, I = never> = T | ((injected: I) => T);
|
|
11
13
|
export type FunctionBody<TEntity, TResult> = (entity: TEntity, collectionName: CollectionName) => TResult;
|
|
12
14
|
export type IdType = string | number;
|
|
15
|
+
export type ForeignKey<T extends {}> = {
|
|
16
|
+
schema: CompiledSchema<T>;
|
|
17
|
+
property: PropertyInfo<T>;
|
|
18
|
+
};
|
|
13
19
|
export declare enum SchemaTypes {
|
|
14
20
|
Array = "Array",
|
|
15
21
|
Boolean = "Boolean",
|
|
@@ -19,9 +25,70 @@ export declare enum SchemaTypes {
|
|
|
19
25
|
String = "String",
|
|
20
26
|
Definition = "Definition",
|
|
21
27
|
Function = "Function",
|
|
22
|
-
Computed = "Computed"
|
|
28
|
+
Computed = "Computed",
|
|
29
|
+
/**
|
|
30
|
+
* Content in, reference out. The only type whose write shape differs from its stored
|
|
31
|
+
* shape, and a leaf on purpose — see `SchemaFile`.
|
|
32
|
+
*/
|
|
33
|
+
File = "File",
|
|
34
|
+
/**
|
|
35
|
+
* A fixed-length list of numbers, carrying its dimension count — see `SchemaVector`.
|
|
36
|
+
*
|
|
37
|
+
* Value-shaped exactly like `s.array(s.number())`, which is why every array codegen
|
|
38
|
+
* handler accepts it. It is a distinct type only so a backend can recognise it and store
|
|
39
|
+
* it natively; nothing else needs to tell the two apart.
|
|
40
|
+
*/
|
|
41
|
+
Vector = "Vector"
|
|
23
42
|
}
|
|
24
43
|
export type ArrayShape = string | number | Date | {};
|
|
44
|
+
/**
|
|
45
|
+
* What a file property gives back: where the bytes are and what they are.
|
|
46
|
+
*
|
|
47
|
+
* Declared in core so `InferType` can name it. Core never reads or writes the bytes — it only
|
|
48
|
+
* carries this shape — and `@routier/blob-plugin` is what puts one here.
|
|
49
|
+
*/
|
|
50
|
+
export type FileReferenceValue = {
|
|
51
|
+
/** Where the bytes live, content-addressed by the blob plugin. */
|
|
52
|
+
key: string;
|
|
53
|
+
/** Byte length. */
|
|
54
|
+
size: number;
|
|
55
|
+
/** Media type as supplied at upload. */
|
|
56
|
+
contentType: string;
|
|
57
|
+
/** SHA-256 of the bytes, lowercase hex. */
|
|
58
|
+
checksum: string;
|
|
59
|
+
/** The name to show a user. Not part of the key. */
|
|
60
|
+
fileName: string;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* What a file property ACCEPTS: content, or a reference you already have.
|
|
64
|
+
*
|
|
65
|
+
* `Blob` covers `File`, which is what an `<input type="file">` yields. A reference is accepted
|
|
66
|
+
* too, so re-saving an entity that was read from the database does not have to re-upload it.
|
|
67
|
+
*/
|
|
68
|
+
export type FileContentValue = FileReferenceValue | Uint8Array | ArrayBuffer | Blob | string;
|
|
69
|
+
/**
|
|
70
|
+
* What a vector property holds, in and out: a plain list of numbers.
|
|
71
|
+
*
|
|
72
|
+
* Named rather than written inline because the inference rules have to recognise it after a
|
|
73
|
+
* modifier has erased which class produced it — the same problem `FileReferenceValue` solves
|
|
74
|
+
* above, and for the same reason.
|
|
75
|
+
*/
|
|
76
|
+
export type VectorValue = number[];
|
|
77
|
+
/**
|
|
78
|
+
* What `s.string({ ... })` accepts.
|
|
79
|
+
*
|
|
80
|
+
* Declarations only. Core stores them and never acts on them; a backend that can use one does.
|
|
81
|
+
*/
|
|
82
|
+
export type StringOptions = {
|
|
83
|
+
/**
|
|
84
|
+
* The longest value the property is declared to hold.
|
|
85
|
+
*
|
|
86
|
+
* MySQL uses it for `VARCHAR(maxLength)`; without it every string column is
|
|
87
|
+
* `VARCHAR(255)`, which silently truncates longer values. Other backends ignore it. Core
|
|
88
|
+
* never validates a value against it — see `SchemaBase.maxLength`.
|
|
89
|
+
*/
|
|
90
|
+
maxLength?: number;
|
|
91
|
+
};
|
|
25
92
|
export type ExpandedProperty = ExpandedChildProperty & {
|
|
26
93
|
assignmentPath: string;
|
|
27
94
|
selectorPath: string;
|
|
@@ -83,7 +150,7 @@ export interface ISchemaSubscription<T extends {}> extends Disposable {
|
|
|
83
150
|
send(changes: SubscriptionChanges<T>): void;
|
|
84
151
|
onMessage(callback: (changes: SubscriptionChanges<T>) => void): void;
|
|
85
152
|
}
|
|
86
|
-
type Enrich<TEntity extends {}> = {
|
|
153
|
+
export type Enrich<TEntity extends {}> = {
|
|
87
154
|
(entity: InferType<TEntity>, changeTrackingType: ChangeTrackingType): InferType<TEntity>;
|
|
88
155
|
(entity: InferCreateType<TEntity>, changeTrackingType: ChangeTrackingType): InferCreateType<TEntity>;
|
|
89
156
|
};
|
|
@@ -95,19 +162,32 @@ export type Preprocess<TEntity extends {}> = {
|
|
|
95
162
|
(entity: InferCreateType<TEntity>): InferType<TEntity>;
|
|
96
163
|
(entity: InferType<TEntity>): InferType<TEntity>;
|
|
97
164
|
};
|
|
165
|
+
export type SetProperties<TEntity extends {}> = (destination: DeepPartial<InferType<TEntity> | InferCreateType<TEntity>>, source: DeepPartial<InferType<TEntity> | InferCreateType<TEntity>>) => void;
|
|
98
166
|
export type CompiledSchemaCore<TEntity extends {}> = Omit<CompiledSchema<TEntity>, "createSubscription">;
|
|
167
|
+
export type CompiledSchemaWithMetadata<TEntity extends {}, TMetadata> = {
|
|
168
|
+
readonly metadata: TMetadata;
|
|
169
|
+
} & CompiledSchema<TEntity>;
|
|
99
170
|
/**
|
|
100
171
|
* Represents a fully compiled schema with all utilities and metadata for an entity type.
|
|
101
172
|
*/
|
|
102
173
|
export type CompiledSchema<TEntity extends {}> = {
|
|
103
174
|
deserializePartial: (item: Record<string, unknown>, properties: PropertyInfo<TEntity>[]) => DeepPartial<InferType<TEntity>>;
|
|
104
|
-
createSubscription: (abortSignal?: AbortSignal) => ISchemaSubscription<TEntity>;
|
|
175
|
+
createSubscription: (abortSignal?: AbortSignal, scope?: string, options?: SchemaSubscriptionOptions) => ISchemaSubscription<TEntity>;
|
|
105
176
|
/** Returns the property info for a given id (full path) */
|
|
106
177
|
getProperty: (id: string) => PropertyInfo<TEntity>;
|
|
107
178
|
/** Returns the ID of the given entity. */
|
|
108
179
|
getId: (entity: InferType<TEntity>) => IdType;
|
|
109
180
|
/** Returns a deep clone of the given entity. */
|
|
110
181
|
clone: (entity: InferType<TEntity>) => InferType<TEntity>;
|
|
182
|
+
/**
|
|
183
|
+
* Returns a deep clone of a record that is still in the STORAGE shape — renamed properties
|
|
184
|
+
* under their `from` names rather than their in-memory names.
|
|
185
|
+
*
|
|
186
|
+
* `clone` reads in-memory names, so it returns `undefined` for every renamed property of a
|
|
187
|
+
* stored record. Use this when copying rows a store holds before they have been deserialized.
|
|
188
|
+
* Generated on first call; schemas that are never cloned in storage shape never build it.
|
|
189
|
+
*/
|
|
190
|
+
cloneStorage: (entity: InferType<TEntity>) => InferType<TEntity>;
|
|
111
191
|
/** Removes unmapped or extraneous properties from the entity. */
|
|
112
192
|
strip: (entity: InferType<TEntity>) => InferType<TEntity>;
|
|
113
193
|
/** Prepares a new entity for creation, applying defaults and transformations. */
|
|
@@ -130,6 +210,8 @@ export type CompiledSchema<TEntity extends {}> = {
|
|
|
130
210
|
compare: (a: InferType<TEntity>, fromDb: InferType<TEntity>) => boolean;
|
|
131
211
|
/** Deserializes an entity from storage format. */
|
|
132
212
|
deserialize: (entity: InferType<TEntity>) => InferType<TEntity>;
|
|
213
|
+
/** Sets 1 or many properties from the source object onto the destination object with change tracking. */
|
|
214
|
+
set: SetProperties<TEntity>;
|
|
133
215
|
/** Combines serializing and preparing an entity for saving. */
|
|
134
216
|
preprocess: Preprocess<TEntity>;
|
|
135
217
|
/** Combines deserializing and enriching an entity for selection. */
|
|
@@ -159,17 +241,85 @@ export type CompiledSchema<TEntity extends {}> = {
|
|
|
159
241
|
};
|
|
160
242
|
export type PropertySerializer<T extends any> = (value: T) => string | number;
|
|
161
243
|
export type PropertyDeserializer<T extends any> = (value: string | number) => T;
|
|
244
|
+
/**
|
|
245
|
+
* A two-way transform between the application value and the stored value.
|
|
246
|
+
*
|
|
247
|
+
* Both directions may be async. Held as a live reference rather than stringified, so a
|
|
248
|
+
* closure works and `injected` is a convenience rather than the only way in.
|
|
249
|
+
*/
|
|
250
|
+
export type PropertyTransform<T extends any> = {
|
|
251
|
+
/**
|
|
252
|
+
* Application value to stored value. Runs before the plugin sees it. May be async.
|
|
253
|
+
*
|
|
254
|
+
* `entity` is there for the one-way case: a transform with no `from` derives a value
|
|
255
|
+
* rather than converting one, which is what `computed` does.
|
|
256
|
+
*/
|
|
257
|
+
to: (value: T, entity: Record<string, unknown>) => unknown | Promise<unknown>;
|
|
258
|
+
/**
|
|
259
|
+
* Stored value back to application value. Runs after the plugin returns it.
|
|
260
|
+
*
|
|
261
|
+
* Optional. Leave it out and the transform is one-way: the stored value is the value.
|
|
262
|
+
*/
|
|
263
|
+
from?: (value: unknown) => T | Promise<T>;
|
|
264
|
+
/**
|
|
265
|
+
* What the column becomes, when the stored form is not the property's own type.
|
|
266
|
+
*
|
|
267
|
+
* Defaults to the property's own type, so nothing changes unless you say it does. A
|
|
268
|
+
* library that always produces text — a cipher, a compressor — sets this once, and the
|
|
269
|
+
* caller who uses that library never writes it.
|
|
270
|
+
*/
|
|
271
|
+
stores?: SchemaTypes;
|
|
272
|
+
/**
|
|
273
|
+
* Whether a filter on this property can still run in the database.
|
|
274
|
+
*
|
|
275
|
+
* Defaults to `none`, which rejects the filter rather than returning wrong rows. Set
|
|
276
|
+
* `equality` only when `to` is deterministic.
|
|
277
|
+
*/
|
|
278
|
+
comparable?: 'equality' | 'none';
|
|
279
|
+
};
|
|
162
280
|
export type SchemaId = Branded<number, "SchemaId">;
|
|
163
281
|
export type CollectionName = Branded<string, "CollectionName">;
|
|
164
|
-
export type SchemaModifiers = "default" | "deserialize" | "identity" | "key" | "nullable" | "optional" | "readonly" | "serialize" | "unmapped" | "computed" | "distinct";
|
|
165
|
-
|
|
282
|
+
export type SchemaModifiers = "default" | "deserialize" | "identity" | "key" | "nullable" | "optional" | "readonly" | "serialize" | "unmapped" | "computed" | "distinct" | "searchable";
|
|
283
|
+
/**
|
|
284
|
+
* What a tagged property infers to.
|
|
285
|
+
*
|
|
286
|
+
* `tag()` is metadata and must not change a type, but `SchemaTag<T>` carries the same `T` as
|
|
287
|
+
* whatever it wrapped without carrying which class that was. For a string `T` is already
|
|
288
|
+
* `string`; for an object `T` is the map of child schemas, which only the `SchemaObject`
|
|
289
|
+
* branch below knows how to unwrap. Falling through to the generic `SchemaBase` branch
|
|
290
|
+
* therefore handed the raw map back, so `s.object({ key: s.string() }).tag('x')` typed
|
|
291
|
+
* `key` as `SchemaString` instead of `string` — everything ran, and only the types lied.
|
|
292
|
+
*
|
|
293
|
+
* An array is distinguishable because `SchemaArray`'s parameter is the ELEMENT schema, so a
|
|
294
|
+
* tagged array arrives here as a `SchemaBase` rather than a plain map.
|
|
295
|
+
*/
|
|
296
|
+
type InferTagged<C> = ResolveWrapped<C>;
|
|
297
|
+
/**
|
|
298
|
+
* What a wrapping modifier's inner type resolves to.
|
|
299
|
+
*
|
|
300
|
+
* `SchemaOptional`, `SchemaNullable` and `SchemaTag` all carry the same `C` as whatever they
|
|
301
|
+
* wrapped, without carrying which class that was, so each has to work out what it is holding.
|
|
302
|
+
* Three shapes are possible:
|
|
303
|
+
*
|
|
304
|
+
* - an already-resolved value (`string` from `s.string()`, a file reference from `s.file()`)
|
|
305
|
+
* - an ELEMENT schema, which is what `SchemaArray` parameterises on
|
|
306
|
+
* - a map of child schemas, which is what `SchemaObject` parameterises on
|
|
307
|
+
*
|
|
308
|
+
* Getting this wrong is silent. The map branch applied to an already-resolved object walks
|
|
309
|
+
* its keys and infers `never` for each, so `s.file().optional()` typed as
|
|
310
|
+
* `{ key: never, size: never, ... }` — which no value can satisfy and no test would catch at
|
|
311
|
+
* runtime.
|
|
312
|
+
*/
|
|
313
|
+
type ResolveWrapped<C> = C extends string | number | boolean | Date | FileReferenceValue ? C : C extends VectorValue ? C : C extends SchemaBase<any, any> ? InferPrimitive<C>[] : {
|
|
166
314
|
[K in keyof C]: InferPrimitive<C[K]>;
|
|
167
|
-
}
|
|
315
|
+
};
|
|
316
|
+
type InferPrimitive<T> = T extends SchemaOptional<infer C, infer __> ? ResolveWrapped<C> : T extends SchemaTag<infer C, infer __> ? InferTagged<C> : T extends SchemaVector<infer __, infer ___> ? VectorValue : T extends SchemaArray<infer Y, infer __> ? InferPrimitive<Y>[] : T extends SchemaObject<infer Obj, infer _> ? {
|
|
168
317
|
[K in keyof Obj]: InferPrimitive<Obj[K]>;
|
|
169
318
|
} : T extends SchemaFunction<infer F, infer __> ? F : T extends SchemaBase<infer X, infer _> ? X extends Array<infer A> ? InferPrimitive<A>[] : X : never;
|
|
170
319
|
export type InferType<T> = T extends CompiledSchema<infer R> ? InferCompiledSchema<R> : T extends {} ? InferCompiledSchema<T> : T;
|
|
171
320
|
export type InferCreateType<T> = T extends CompiledSchema<infer R> ? InferCompiledCreateSchema<R> : T extends {} ? InferCompiledCreateSchema<T> : unknown;
|
|
172
321
|
export type InferMappedType<T> = T extends SchemaBase<infer K, infer __> ? InferType<K> : InferCompiledSchema<T>;
|
|
322
|
+
export type InferRoot<T> = T extends CompiledSchema<infer R> ? R : never;
|
|
173
323
|
type HasModifier<T, K extends keyof T, M extends SchemaModifiers> = T[K] extends SchemaBase<any, infer Mods> ? M extends Mods ? true : false : false;
|
|
174
324
|
type IsPlainProperty<T, K extends keyof T> = [
|
|
175
325
|
HasModifier<T, K, "readonly">,
|
|
@@ -180,21 +330,43 @@ type IsPlainProperty<T, K extends keyof T> = [
|
|
|
180
330
|
false,
|
|
181
331
|
false
|
|
182
332
|
] ? true : false;
|
|
183
|
-
type
|
|
333
|
+
type IsCreateExcluded<T, K extends keyof T> = [
|
|
184
334
|
HasModifier<T, K, "identity">,
|
|
185
|
-
HasModifier<T, K, "default">,
|
|
186
335
|
HasModifier<T, K, "computed">,
|
|
187
|
-
HasModifier<T, K, "unmapped"
|
|
188
|
-
HasModifier<T, K, "optional">,
|
|
189
|
-
HasModifier<T, K, "nullable">
|
|
336
|
+
HasModifier<T, K, "unmapped">
|
|
190
337
|
] extends [
|
|
191
338
|
false,
|
|
192
339
|
false,
|
|
193
|
-
false
|
|
194
|
-
|
|
340
|
+
false
|
|
341
|
+
] ? false : true;
|
|
342
|
+
type IsCreateOptional<T, K extends keyof T> = [
|
|
343
|
+
HasModifier<T, K, "optional">,
|
|
344
|
+
HasModifier<T, K, "default">
|
|
345
|
+
] extends [
|
|
195
346
|
false,
|
|
196
347
|
false
|
|
197
|
-
] ?
|
|
348
|
+
] ? false : true;
|
|
349
|
+
type IsCreateNullable<T, K extends keyof T> = HasModifier<T, K, "nullable"> extends true ? true : false;
|
|
350
|
+
/**
|
|
351
|
+
* What a property ACCEPTS on the way in, which is not always what it gives back.
|
|
352
|
+
*
|
|
353
|
+
* Only a file differs today: you assign content and read a reference. Matching on the read
|
|
354
|
+
* type rather than on `SchemaFile` itself is deliberate — it keeps working through every
|
|
355
|
+
* modifier. `s.file().optional()` is a `SchemaOptional`, `s.file().tag('x')` is a
|
|
356
|
+
* `SchemaTag`, and neither carries the original class, so a check against the class alone
|
|
357
|
+
* would silently stop accepting content the moment anyone added a modifier.
|
|
358
|
+
*
|
|
359
|
+
* Assignability is required in BOTH directions, and the tuple wrappers are load-bearing.
|
|
360
|
+
* One-way `extends` matches `never` — which is assignable to everything — so a generic
|
|
361
|
+
* property over `Record<string, unknown>` resolved to file content and broke the Dexie
|
|
362
|
+
* plugin's types. It also matched any object that merely happens to have these five fields
|
|
363
|
+
* plus more. Mutual assignability admits the reference shape and nothing else, and the
|
|
364
|
+
* tuples stop the conditional distributing over a union.
|
|
365
|
+
*/
|
|
366
|
+
type InferWritePrimitive<T> = [
|
|
367
|
+
InferPrimitive<T>
|
|
368
|
+
] extends [FileReferenceValue] ? [FileReferenceValue] extends [InferPrimitive<T>] ? FileContentValue : InferPrimitive<T> : InferPrimitive<T>;
|
|
369
|
+
type InferCreateProperty<T, K extends keyof T> = IsCreateNullable<T, K> extends true ? null | InferWritePrimitive<T[K]> : InferWritePrimitive<T[K]>;
|
|
198
370
|
type InferCompiledSchema<T> = CoalesceEmpty<{
|
|
199
371
|
[K in keyof T as IsPlainProperty<T, K> extends true ? K : never]: InferPrimitive<T[K]>;
|
|
200
372
|
}, {
|
|
@@ -204,15 +376,11 @@ type InferCompiledSchema<T> = CoalesceEmpty<{
|
|
|
204
376
|
}, {
|
|
205
377
|
[K in keyof T as HasModifier<T, K, "nullable"> extends true ? K : never]: null | InferPrimitive<T[K]>;
|
|
206
378
|
}>;
|
|
207
|
-
type InferCompiledCreateSchema<T> =
|
|
208
|
-
[K in keyof T as
|
|
209
|
-
}
|
|
210
|
-
[K in keyof T as
|
|
211
|
-
}
|
|
212
|
-
[K in keyof T as HasModifier<T, K, "default"> extends true ? K : never]?: InferPrimitive<T[K]>;
|
|
213
|
-
}, {
|
|
214
|
-
[K in keyof T as HasModifier<T, K, "nullable"> extends true ? K : never]: null | InferPrimitive<T[K]>;
|
|
215
|
-
}>;
|
|
379
|
+
type InferCompiledCreateSchema<T> = {
|
|
380
|
+
[K in keyof T as IsCreateExcluded<T, K> extends true ? never : IsCreateOptional<T, K> extends true ? K : never]?: InferCreateProperty<T, K>;
|
|
381
|
+
} & {
|
|
382
|
+
[K in keyof T as IsCreateExcluded<T, K> extends true ? never : IsCreateOptional<T, K> extends true ? never : K]: InferCreateProperty<T, K>;
|
|
383
|
+
};
|
|
216
384
|
type IsEmptyObject<T> = keyof T extends never ? true : false;
|
|
217
385
|
type CoalesceEmpty<T1 extends {}, T2 extends {}, T3 extends {}, T4 extends {}> = (IsEmptyObject<T1> extends true ? {} : T1) & (IsEmptyObject<T2> extends true ? {} : T2) & (IsEmptyObject<T3> extends true ? {} : T3) & (IsEmptyObject<T4> extends true ? {} : T4);
|
|
218
386
|
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { SchemaTypes } from "../types";
|
|
2
|
+
/** True when the property's value is a JS array and needs value rather than reference semantics. */
|
|
3
|
+
export declare const isArrayValued: (type: SchemaTypes) => boolean;
|
|
4
|
+
export declare const hasPrimitiveElements: (type: SchemaTypes, elementType: SchemaTypes | undefined) => boolean;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { PropertyInfo } from '../PropertyInfo';
|
|
2
|
+
import { CompiledSchema, InferCreateType, InferType } from '../types';
|
|
3
|
+
import { SchemaDefinition } from '../SchemaDefinition';
|
|
4
|
+
/**
|
|
5
|
+
* Standard JSON Schema V1 interface types
|
|
6
|
+
* Based on https://standardschema.dev/json-schema
|
|
7
|
+
*/
|
|
8
|
+
export interface StandardTypedV1<Input = unknown, Output = Input> {
|
|
9
|
+
readonly '~standard': StandardTypedV1.Props<Input, Output>;
|
|
10
|
+
}
|
|
11
|
+
export declare namespace StandardTypedV1 {
|
|
12
|
+
interface Props<Input = unknown, Output = Input> {
|
|
13
|
+
readonly version: 1;
|
|
14
|
+
readonly vendor: string;
|
|
15
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
16
|
+
}
|
|
17
|
+
interface Types<Input = unknown, Output = Input> {
|
|
18
|
+
readonly input: Input;
|
|
19
|
+
readonly output: Output;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
|
|
23
|
+
readonly '~standard': StandardJSONSchemaV1.Props<Input, Output>;
|
|
24
|
+
}
|
|
25
|
+
export declare namespace StandardJSONSchemaV1 {
|
|
26
|
+
interface Props<Input = unknown, Output = Input> extends StandardTypedV1.Props<Input, Output> {
|
|
27
|
+
readonly jsonSchema: StandardJSONSchemaV1.Converter;
|
|
28
|
+
}
|
|
29
|
+
interface Converter {
|
|
30
|
+
readonly input: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
|
|
31
|
+
readonly output: (options: StandardJSONSchemaV1.Options) => Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
type Target = 'draft-2020-12' | 'draft-07' | 'openapi-3.0' | ({} & string);
|
|
34
|
+
interface Options {
|
|
35
|
+
readonly target: Target;
|
|
36
|
+
readonly libraryOptions?: Record<string, unknown> | undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Converts a Routier PropertyInfo to a JSON Schema property definition.
|
|
41
|
+
*/
|
|
42
|
+
export declare function propertyInfoToJsonSchema(property: PropertyInfo<any>, target: StandardJSONSchemaV1.Target, visited?: Set<string>, useOutputType?: boolean): Record<string, unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Converts a CompiledSchema to a JSON Schema object schema.
|
|
45
|
+
*/
|
|
46
|
+
export declare function compiledSchemaToJsonSchema<T extends {}>(compiledSchema: CompiledSchema<T>, target: StandardJSONSchemaV1.Target, useOutputType?: boolean): Record<string, unknown>;
|
|
47
|
+
/**
|
|
48
|
+
* Creates Standard JSON Schema V1 props for a compiled schema.
|
|
49
|
+
*/
|
|
50
|
+
export declare function createStandardJsonSchemaProps<T extends {}>(compiledSchema: CompiledSchema<T>): StandardJSONSchemaV1.Props<InferCreateType<T>, InferType<T>>;
|
|
51
|
+
/**
|
|
52
|
+
* Parses a JSON string containing a JSON Schema and rehydrates it into a Routier SchemaDefinition.
|
|
53
|
+
* This is a convenience wrapper around `rehydrateSchemaFromJsonSchema` that handles JSON parsing.
|
|
54
|
+
*
|
|
55
|
+
* @param jsonString - The JSON string containing the JSON Schema
|
|
56
|
+
* @param collectionName - The collection name for the schema (if not in x-routier metadata)
|
|
57
|
+
* @returns A SchemaDefinition that can be compiled
|
|
58
|
+
* @throws Error if the JSON string is invalid or doesn't contain a valid JSON Schema
|
|
59
|
+
*/
|
|
60
|
+
export declare function rehydrateSchemaFromJsonString(jsonString: string, collectionName?: string): SchemaDefinition<any>;
|
|
61
|
+
/**
|
|
62
|
+
* Rehydrates a JSON Schema back into a Routier SchemaDefinition.
|
|
63
|
+
* This parses the JSON Schema structure and reconstructs the schema using Routier's builder API.
|
|
64
|
+
*
|
|
65
|
+
* @param jsonSchema - The JSON Schema object to rehydrate
|
|
66
|
+
* @param collectionName - The collection name for the schema (if not in x-routier metadata)
|
|
67
|
+
* @returns A SchemaDefinition that can be compiled
|
|
68
|
+
*/
|
|
69
|
+
export declare function rehydrateSchemaFromJsonSchema(jsonSchema: Record<string, unknown>, collectionName?: string): SchemaDefinition<any>;
|
|
70
|
+
/**
|
|
71
|
+
* Attempts to extract type information from a compiled schema for better type inference.
|
|
72
|
+
*
|
|
73
|
+
* Note: TypeScript types are compile-time only, so we can't extract actual type information at runtime.
|
|
74
|
+
* The Standard JSON Schema spec allows libraries to advertise their inferred types via the `types` property,
|
|
75
|
+
* but this requires compile-time type information that isn't available at runtime.
|
|
76
|
+
*
|
|
77
|
+
* For proper type inference, consumers should use TypeScript's type system:
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const schema = s.define("users", { ... }).compile();
|
|
80
|
+
* type User = InferType<typeof schema>;
|
|
81
|
+
* type CreateUser = InferCreateType<typeof schema>;
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @param compiledSchema - The compiled schema to extract type info from
|
|
85
|
+
* @returns A description of the type names (for documentation purposes only)
|
|
86
|
+
*/
|
|
87
|
+
export declare function extractTypeInfo<T extends {}>(compiledSchema: CompiledSchema<T>): {
|
|
88
|
+
inputType: string;
|
|
89
|
+
outputType: string;
|
|
90
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
"use strict";
|
|
3
|
+
// The require scope
|
|
4
|
+
var __webpack_require__ = {};
|
|
5
|
+
|
|
6
|
+
// webpack/runtime/make_namespace_object
|
|
7
|
+
(() => {
|
|
8
|
+
// define __esModule on exports
|
|
9
|
+
__webpack_require__.r = (exports) => {
|
|
10
|
+
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
11
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var __webpack_exports__ = {};
|
|
17
|
+
__webpack_require__.r(__webpack_exports__);
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
module.exports = __webpack_exports__;
|
|
21
|
+
})()
|
|
22
|
+
;
|
|
23
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types/index.cjs","sources":["webpack://@routier/core/webpack/runtime/make_namespace_object","webpack://@routier/core/./src/types/index.ts"],"sourcesContent":["// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","export type DeepPartial<T> = T extends object ? {\n [P in keyof T]?: DeepPartial<T[P]>;\n} : T;\n\nexport type GenericFunction<T, R> = (value: T) => R;"],"names":[],"mappings":";;;;;;;AAAA;AACA;AACA;AACA,uDAAuD,iBAAiB;AACxE;AACA,gDAAgD,aAAa;AAC7D,E;;;;ACFoD"}
|
package/dist/types/index.js
CHANGED
|
@@ -1,18 +1,4 @@
|
|
|
1
|
-
// The require scope
|
|
2
|
-
var __webpack_require__ = {};
|
|
3
|
-
|
|
4
|
-
// webpack/runtime/make_namespace_object
|
|
5
|
-
(() => {
|
|
6
|
-
// define __esModule on exports
|
|
7
|
-
__webpack_require__.r = (exports) => {
|
|
8
|
-
if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
9
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
10
|
-
}
|
|
11
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
12
|
-
};
|
|
13
|
-
})();
|
|
14
1
|
var __webpack_exports__ = {};
|
|
15
|
-
__webpack_require__.r(__webpack_exports__);
|
|
16
2
|
|
|
17
3
|
|
|
18
4
|
|
package/dist/types/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types/index.js","sources":["webpack://@routier/core
|
|
1
|
+
{"version":3,"file":"types/index.js","sources":["webpack://@routier/core/./src/types/index.ts"],"sourcesContent":["export type DeepPartial<T> = T extends object ? {\n [P in keyof T]?: DeepPartial<T[P]>;\n} : T;\n\nexport type GenericFunction<T, R> = (value: T) => R;"],"names":[],"mappings":";AAIoD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const noop: (...
|
|
1
|
+
export declare const noop: (..._args: any[]) => void;
|