Skip to content

Commit

Permalink
api wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Aug 16, 2024
1 parent c88e8bb commit 4f96a9a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/experimental/src/VariantSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ type MissingSelfGeneric<Params extends string = ""> =
* @since 1.0.0
* @category constructors
*/
export const factory = <
export const make = <
const Variants extends ReadonlyArray<string>,
const Default extends Variants[number]
>(options: {
Expand Down
72 changes: 67 additions & 5 deletions packages/sql/src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
Class,
Field,
Struct
} = VariantSchema.factory({
} = VariantSchema.make({
variants: ["select", "insert", "update", "json", "jsonCreate", "jsonUpdate"],
defaultVariant: "select"
})
Expand Down Expand Up @@ -115,7 +115,7 @@ export interface GeneratedByApp<S extends Schema.Schema.All | Schema.PropertySig
/**
* A field that represents a column that is generated by the application.
*
* It is required by the database, but not by the application.
* It is required by the database, but not by the JSON variants.
*
* @since 1.0.0
* @category schemas
Expand Down Expand Up @@ -182,12 +182,17 @@ export const DateTimeFromDate: DateTimeFromDate = Schema.transform(
}
)

const DateTimeWithNow = Schema.DateTimeUtc.pipe(
Schema.propertySignature,
Schema.withConstructorDefault(DateTime.unsafeNow)
)

const DateTimeFromDateWithNow = DateTimeFromDate.pipe(
Schema.propertySignature,
Schema.withConstructorDefault(DateTime.unsafeNow)
)

const DateTimeWithNow = Schema.DateTimeUtc.pipe(
const DateTimeFromNumberWithNow = Schema.DateTimeUtcFromNumber.pipe(
Schema.propertySignature,
Schema.withConstructorDefault(DateTime.unsafeNow)
)
Expand All @@ -208,7 +213,7 @@ export interface DateTimeInsert extends
* 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.
*
* It is removed from updates and is available for selection.
* It is omitted from updates and is available for selection.
*
* @since 1.0.0
* @category schemas
Expand All @@ -235,7 +240,7 @@ export interface DateTimeInsertFromDate extends
* 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.
*
* It is removed from updates and is available for selection.
* It is omitted from updates and is available for selection.
*
* @since 1.0.0
* @category schemas
Expand All @@ -246,6 +251,33 @@ export const DateTimeInsertFromDate: DateTimeInsertFromDate = Field({
json: Schema.DateTimeUtc
})

/**
* @since 1.0.0
* @category models
*/
export interface DateTimeInsertFromNumber extends
VariantSchema.Field<{
readonly select: typeof Schema.DateTimeUtcFromNumber
readonly insert: Schema.PropertySignature<":", DateTime.Utc, never, ":", number, true>
readonly json: typeof Schema.DateTimeUtcFromNumber
}>
{}

/**
* A field that represents a date-time value that is inserted as the current
* `DateTime.Utc`. It is serialized as a `number`.
*
* It is omitted from updates and is available for selection.
*
* @since 1.0.0
* @category schemas
*/
export const DateTimeInsertFromNumber: DateTimeInsertFromNumber = Field({
select: Schema.DateTimeUtcFromNumber,
insert: DateTimeFromNumberWithNow,
json: Schema.DateTimeUtcFromNumber
})

/**
* @since 1.0.0
* @category models
Expand Down Expand Up @@ -306,6 +338,36 @@ export const DateTimeUpdateFromDate: DateTimeUpdateFromDate = Field({
json: Schema.DateTimeUtc
})

/**
* @since 1.0.0
* @category models
*/
export interface DateTimeUpdateFromNumber extends
VariantSchema.Field<{
readonly select: typeof Schema.DateTimeUtcFromNumber
readonly insert: Schema.PropertySignature<":", DateTime.Utc, never, ":", number, true>
readonly update: Schema.PropertySignature<":", DateTime.Utc, never, ":", number, true>
readonly json: typeof Schema.DateTimeUtcFromNumber
}>
{}

/**
* A field that represents a date-time value that is updated as the current
* `DateTime.Utc`. It is serialized as a `number`.
*
* It is set to the current `DateTime.Utc` on updates and inserts and is
* available for selection.
*
* @since 1.0.0
* @category schemas
*/
export const DateTimeUpdateFromNumber: DateTimeUpdateFromNumber = Field({
select: Schema.DateTimeUtcFromNumber,
insert: DateTimeFromNumberWithNow,
update: DateTimeFromNumberWithNow,
json: Schema.DateTimeUtcFromNumber
})

/**
* @since 1.0.0
* @category models
Expand Down

0 comments on commit 4f96a9a

Please sign in to comment.