| /** | |
| * Defines schema-backed domain models with separate database and JSON shapes. | |
| * | |
| * A model keeps one field declaration as the source of truth and derives | |
| * variants for selecting, inserting, updating, and JSON encoding. This is useful | |
| * when the database shape is not exactly the same as the public API shape, such | |
| * as generated ids, audit timestamps, nullable columns, private fields, or | |
| * values that need different encodings at different boundaries. Each generated | |
| * variant is its own schema, so callers can validate or encode the shape that | |
| * matches the operation they are performing. | |
| * | |
| * @since 4.0.0 | |
| */ | |
| import * as Uuid from "uuid"; | |
| import * as DateTime from "../../DateTime.js"; | |
| import * as Effect from "../../Effect.js"; | |
| import * as Option from "../../Option.js"; | |
| import * as Predicate from "../../Predicate.js"; | |
| import * as Schema from "../../Schema.js"; | |
| import * as SchemaGetter from "../../SchemaGetter.js"; | |
| import * as SchemaTransformation from "../../SchemaTransformation.js"; | |
| import * as VariantSchema from "./VariantSchema.js"; | |
| const { | |
| Class, | |
| Field, | |
| FieldExcept, | |
| FieldOnly, | |
| Struct, | |
| Union, | |
| extract, | |
| fieldEvolve | |
| } = /*#__PURE__*/VariantSchema.make({ | |
| variants: ["select", "insert", "update", "json", "jsonCreate", "jsonUpdate"], | |
| defaultVariant: "select" | |
| }); | |
| export { | |
| /** | |
| * Creates domain model schemas with common database and JSON API variants. | |
| * | |
| * **Example** (Defining a variant model class) | |
| * | |
| * ```ts | |
| * import { Schema } from "effect" | |
| * import { Model } from "effect/unstable/schema" | |
| * | |
| * export const GroupId = Schema.Number.pipe(Schema.brand("GroupId")) | |
| * | |
| * export class Group extends Model.Class<Group>("Group")({ | |
| * id: Model.GeneratedByDb(GroupId), | |
| * name: Schema.String, | |
| * createdAt: Model.DateTimeInsertFromDate, | |
| * updatedAt: Model.DateTimeUpdateFromDate | |
| * }) {} | |
| * | |
| * // schema used for selects | |
| * Group | |
| * | |
| * // schema used for inserts | |
| * Group.insert | |
| * | |
| * // schema used for updates | |
| * Group.update | |
| * | |
| * // schema used for json api | |
| * Group.json | |
| * Group.jsonCreate | |
| * Group.jsonUpdate | |
| * | |
| * // you can also turn them into classes | |
| * class GroupJson extends Schema.Class<GroupJson>("GroupJson")(Group.json) { | |
| * get upperName() { | |
| * return this.name.toUpperCase() | |
| * } | |
| * } | |
| * ``` | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| Class, | |
| /** | |
| * Extracts a generated variant schema from a model or variant struct. | |
| * | |
| * @category extraction | |
| * @since 4.0.0 | |
| */ | |
| extract, | |
| /** | |
| * Creates a variant field from schemas keyed by variant name. | |
| * | |
| * @category fields | |
| * @since 4.0.0 | |
| */ | |
| Field, | |
| /** | |
| * Transforms schemas inside a variant field or plain schema by variant name. | |
| * | |
| * @category fields | |
| * @since 4.0.0 | |
| */ | |
| fieldEvolve, | |
| /** | |
| * Creates a variant field that applies a schema to every variant except the | |
| * supplied keys. | |
| * | |
| * @category fields | |
| * @since 4.0.0 | |
| */ | |
| FieldExcept, | |
| /** | |
| * Creates a variant field that applies a schema only to the supplied variants. | |
| * | |
| * @category fields | |
| * @since 4.0.0 | |
| */ | |
| FieldOnly, | |
| /** | |
| * Creates a variant struct from model field definitions. | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| Struct, | |
| /** | |
| * Creates a union over the default and generated variant schemas of multiple | |
| * variant structs. | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| Union }; | |
| /** | |
| * Returns the variant field definitions stored on a model or variant struct. | |
| * | |
| * @category fields | |
| * @since 4.0.0 | |
| */ | |
| export const fields = VariantSchema.fields; | |
| /** | |
| * Marks a value as an explicit override for fields that otherwise use an | |
| * overrideable default. | |
| * | |
| * @category overrideable | |
| * @since 4.0.0 | |
| */ | |
| export const Override = VariantSchema.Override; | |
| /** | |
| * Creates a variant field for a database-generated column available in read | |
| * variants only. | |
| * | |
| * **Details** | |
| * | |
| * The field is included in `select` and `json`, and omitted from `insert`, | |
| * `update`, `jsonCreate`, and `jsonUpdate`. | |
| * | |
| * @see {@link Field} for generated columns that need a custom variant set, such | |
| * as primary keys used in update payloads. | |
| * | |
| * @category generated | |
| * @since 4.0.0 | |
| */ | |
| export const GeneratedByDb = schema => Field({ | |
| select: schema, | |
| json: schema | |
| }); | |
| /** | |
| * A field that represents a value generated by the application and present in database | |
| * variants and the read JSON variant, but omitted from JSON create and update | |
| * variants. | |
| * | |
| * @category generated | |
| * @since 4.0.0 | |
| */ | |
| export const GeneratedByApp = schema => Field({ | |
| select: schema, | |
| insert: schema, | |
| update: schema, | |
| json: schema | |
| }); | |
| /** | |
| * A field that represents a sensitive value that should not be exposed in the | |
| * JSON variants. | |
| * | |
| * @category sensitive | |
| * @since 4.0.0 | |
| */ | |
| export const Sensitive = schema => Field({ | |
| select: schema, | |
| insert: schema, | |
| update: schema | |
| }); | |
| /** | |
| * Creates a schema for optional keys that decodes missing or null encoded values | |
| * through `Option` and encodes `Option` values back to optional nullable keys. | |
| * | |
| * @category optional | |
| * @since 4.0.0 | |
| */ | |
| export const optionalOption = schema => Schema.optionalKey(Schema.NullOr(schema)).pipe(Schema.decodeTo(Schema.Option(Schema.toType(schema)), SchemaTransformation.transformOptional({ | |
| decode: oe => oe.pipe(Option.filter(Predicate.isNotNull), Option.some), | |
| encode: Option.flatten | |
| }))); | |
| /** | |
| * Converts a field to one that is optional for all variants. | |
| * | |
| * **Details** | |
| * | |
| * For the database variants, it will accept `null`able values. | |
| * For the JSON variants, it will also accept missing keys. | |
| * | |
| * @category optional | |
| * @since 4.0.0 | |
| */ | |
| export const FieldOption = /*#__PURE__*/fieldEvolve({ | |
| select: Schema.OptionFromNullOr, | |
| insert: Schema.OptionFromNullOr, | |
| update: Schema.OptionFromNullOr, | |
| json: optionalOption, | |
| jsonCreate: optionalOption, | |
| jsonUpdate: optionalOption | |
| }); | |
| /** | |
| * Schema for sqlite booleans that are represented as `0 | 1` in database | |
| * variants and `boolean` in JSON variants. | |
| * | |
| * @category booleans | |
| * @since 4.0.0 | |
| */ | |
| export const BooleanSqlite = /*#__PURE__*/Field({ | |
| select: Schema.BooleanFromBit, | |
| insert: Schema.BooleanFromBit, | |
| update: Schema.BooleanFromBit, | |
| json: Schema.Boolean, | |
| jsonCreate: Schema.Boolean, | |
| jsonUpdate: Schema.Boolean | |
| }); | |
| /** | |
| * Schema for a `DateTime.Utc` that is serialized as a date string in the | |
| * format `YYYY-MM-DD`. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const Date = /*#__PURE__*/Schema.String.pipe(/*#__PURE__*/Schema.decodeTo(Schema.DateTimeUtc, { | |
| decode: /*#__PURE__*/SchemaGetter.dateTimeUtcFromInput().map(DateTime.removeTime), | |
| encode: /*#__PURE__*/SchemaGetter.transform(DateTime.formatIsoDate) | |
| })); | |
| /** | |
| * Schema for an overrideable UTC date-only field whose constructor default is | |
| * the current date with the time component removed. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateWithNow = /*#__PURE__*/VariantSchema.Overrideable(Date, { | |
| defaultValue: /*#__PURE__*/Effect.map(DateTime.now, DateTime.removeTime) | |
| }); | |
| /** | |
| * Schema for an overrideable UTC date-time field encoded as a string and | |
| * defaulted to the current `DateTime.Utc`. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeWithNow = /*#__PURE__*/VariantSchema.Overrideable(Schema.DateTimeUtcFromString, { | |
| defaultValue: DateTime.now | |
| }); | |
| /** | |
| * Schema for an overrideable UTC date-time field encoded as a JavaScript `Date` | |
| * and defaulted to the current `DateTime.Utc`. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeFromDateWithNow = /*#__PURE__*/VariantSchema.Overrideable(Schema.DateTimeUtcFromDate, { | |
| defaultValue: DateTime.now | |
| }); | |
| /** | |
| * Schema for an overrideable UTC date-time field encoded as milliseconds and | |
| * defaulted to the current `DateTime.Utc`. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeFromNumberWithNow = /*#__PURE__*/VariantSchema.Overrideable(Schema.DateTimeUtcFromMillis, { | |
| defaultValue: DateTime.now | |
| }); | |
| /** | |
| * A field that represents a date-time value that is inserted as the current | |
| * `DateTime.Utc`. It is serialized as a string for the database. | |
| * | |
| * **Details** | |
| * | |
| * It is omitted from updates and is available for selection. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeInsert = /*#__PURE__*/Field({ | |
| select: Schema.DateTimeUtcFromString, | |
| insert: DateTimeWithNow, | |
| json: Schema.DateTimeUtcFromString | |
| }); | |
| /** | |
| * A field that represents a date-time value that is inserted as the current | |
| * `DateTime.Utc`. It is serialized as a `Date` for the database. | |
| * | |
| * **Details** | |
| * | |
| * It is omitted from updates and is available for selection. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeInsertFromDate = /*#__PURE__*/Field({ | |
| select: Schema.DateTimeUtcFromDate, | |
| insert: DateTimeFromDateWithNow, | |
| json: Schema.DateTimeUtcFromString | |
| }); | |
| /** | |
| * A field that represents a date-time value that is inserted as the current | |
| * `DateTime.Utc`. It is serialized as a `number`. | |
| * | |
| * **Details** | |
| * | |
| * It is omitted from updates and is available for selection. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeInsertFromNumber = /*#__PURE__*/Field({ | |
| select: Schema.DateTimeUtcFromMillis, | |
| insert: DateTimeFromNumberWithNow, | |
| json: Schema.DateTimeUtcFromMillis | |
| }); | |
| /** | |
| * A field that represents a date-time value that is updated as the current | |
| * `DateTime.Utc`. It is serialized as a string for the database. | |
| * | |
| * **Details** | |
| * | |
| * It is set to the current `DateTime.Utc` on updates and inserts and is | |
| * available for selection. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeUpdate = /*#__PURE__*/Field({ | |
| select: Schema.DateTimeUtcFromString, | |
| insert: DateTimeWithNow, | |
| update: DateTimeWithNow, | |
| json: Schema.DateTimeUtcFromString | |
| }); | |
| /** | |
| * A field that represents a date-time value that is updated as the current | |
| * `DateTime.Utc`. It is serialized as a `Date` for the database. | |
| * | |
| * **Details** | |
| * | |
| * It is set to the current `DateTime.Utc` on updates and inserts and is | |
| * available for selection. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeUpdateFromDate = /*#__PURE__*/Field({ | |
| select: Schema.DateTimeUtcFromDate, | |
| insert: DateTimeFromDateWithNow, | |
| update: DateTimeFromDateWithNow, | |
| json: Schema.DateTimeUtcFromString | |
| }); | |
| /** | |
| * A field that represents a date-time value that is updated as the current | |
| * `DateTime.Utc`. It is serialized as a `number`. | |
| * | |
| * **Details** | |
| * | |
| * It is set to the current `DateTime.Utc` on updates and inserts and is | |
| * available for selection. | |
| * | |
| * @category DateTime | |
| * @since 4.0.0 | |
| */ | |
| export const DateTimeUpdateFromNumber = /*#__PURE__*/Field({ | |
| select: Schema.DateTimeUtcFromMillis, | |
| insert: DateTimeFromNumberWithNow, | |
| update: DateTimeFromNumberWithNow, | |
| json: Schema.DateTimeUtcFromMillis | |
| }); | |
| /** | |
| * A field that represents a JSON value stored as text in the database. | |
| * | |
| * **Details** | |
| * | |
| * The "json" variants will use the object schema directly. | |
| * | |
| * @category constructors | |
| * @since 4.0.0 | |
| */ | |
| export const JsonFromString = schema => { | |
| const parsed = Schema.fromJsonString(Schema.toCodecJson(schema)); | |
| return Field({ | |
| select: parsed, | |
| insert: parsed, | |
| update: parsed, | |
| json: schema, | |
| jsonCreate: schema, | |
| jsonUpdate: schema | |
| }); | |
| }; | |
| /** | |
| * Schema for binary `Uint8Array` values backed by an `ArrayBuffer`. | |
| * | |
| * @category Uint8Array | |
| * @since 4.0.0 | |
| */ | |
| export const Uint8Array = Schema.Uint8Array; | |
| /** | |
| * Adds a constructor default that generates a binary UUID v4 for a branded | |
| * `Uint8Array` schema. | |
| * | |
| * @category uuid | |
| * @since 4.0.0 | |
| */ | |
| export const UuidV4BytesWithGenerate = schema => schema.pipe(Schema.withConstructorDefault(Effect.sync(() => Uuid.v4({}, new globalThis.Uint8Array(16))))); | |
| /** | |
| * A field that represents a binary UUID v4 that is generated on inserts. | |
| * | |
| * @category uuid | |
| * @since 4.0.0 | |
| */ | |
| export const UuidV4BytesInsert = schema => Field({ | |
| select: schema, | |
| insert: UuidV4BytesWithGenerate(schema), | |
| update: schema, | |
| json: schema | |
| }); | |
| /** | |
| * Adds a constructor default that generates a string UUID v4. | |
| * | |
| * @category uuid | |
| * @since 4.0.0 | |
| */ | |
| export const UuidV4WithGenerate = schema => schema.pipe(Schema.withConstructorDefault(Effect.sync(() => Uuid.v4()))); | |
| /** | |
| * A field that represents a string UUID v4 that is generated on inserts. | |
| * | |
| * @category uuid | |
| * @since 4.0.0 | |
| */ | |
| export const UuidV4Insert = schema => Field({ | |
| select: schema, | |
| insert: UuidV4WithGenerate(schema), | |
| update: schema, | |
| json: schema | |
| }); | |
| /** | |
| * Adds a constructor default that generates a string UUID v7. | |
| * | |
| * @category uuid | |
| * @since 4.0.0 | |
| */ | |
| export const UuidV7WithGenerate = schema => schema.pipe(Schema.withConstructorDefault(Effect.clockWith(clock => Effect.succeed(Uuid.v7({ | |
| msecs: clock.currentTimeMillisUnsafe() | |
| }))))); | |
| /** | |
| * A field that represents a string UUID v7 that is generated on inserts. | |
| * | |
| * @category uuid | |
| * @since 4.0.0 | |
| */ | |
| export const UuidV7Insert = schema => Field({ | |
| select: schema, | |
| insert: UuidV7WithGenerate(schema), | |
| update: schema, | |
| json: schema | |
| }); | |
| //# sourceMappingURL=Model.js.map |
Xet Storage Details
- Size:
- 13 kB
- Xet hash:
- 8088eb7dd5d765be2c0fd3b908eef5b04f4ad9fa556c6258f4e4a4a75f72ced0
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.