Skip to content

Instantly share code, notes, and snippets.

@dfee
Last active August 8, 2020 17:22
Show Gist options
  • Save dfee/f5163df6290f5ccad3452864cb80df49 to your computer and use it in GitHub Desktop.
Save dfee/f5163df6290f5ccad3452864cb80df49 to your computer and use it in GitHub Desktop.
import { AnyEntity, Property, PropertyOptions } from "@mikro-orm/core";
import { TimestampType } from "./types";
export type DecoratorType = (
target: AnyEntity<unknown>,
propertyName: string,
) => void;
export type ConcretePropertyOptions<T> = Omit<PropertyOptions<T>, "columnType">;
export type BooleanOptions<T> = Omit<
PropertyOptions<T>,
"columnType" | "length" | "type" | "unsigned"
>;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Boolean = <T>(options: BooleanOptions<T> = {}): DecoratorType =>
Property<T>({
...options,
columnType: "boolean",
});
export type BpcharOptions<T> = Omit<
PropertyOptions<T>,
"columnType" | "type" | "unsigned"
>;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Bpchar = <T>({
length,
...options
}: BpcharOptions<T> = {}): DecoratorType =>
Property<T>({
...options,
columnType: `bpchar(${length})`,
});
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Char = Bpchar;
export type TimeOptions<T> = { timeZone?: boolean } & Omit<
PropertyOptions<T>,
"columnType" | "type" | "unsigned"
>;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Time = <T>({
length,
timeZone,
...options
}: TimeOptions<T> = {}): DecoratorType => {
return Property<T>({
...options,
columnType: `time${length ? `(${length})` : ""}${
timeZone ? "with time zone" : ""
}`,
});
};
export type TimestampOptions<T> = { timeZone?: boolean } & Omit<
PropertyOptions<T>,
"columnType" | "type" | "unsigned"
>;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Timestamp = <T>(
options: TimestampOptions<T> = {},
): DecoratorType => {
return Property<T>({
...options,
type: TimestampType,
// columnType: `timestamp${length ? `(${length})` : ""}${
// timeZone ? "with time zone" : ""
// }`,
});
};
export type UUIDOptions<T> = Omit<
PropertyOptions<T>,
"columnType" | "length" | "type" | "unsigned"
>;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const UUID = <T>(options: UUIDOptions<T> = {}): DecoratorType =>
Property<T>({
...options,
columnType: "uuid",
});
export type VarcharOptions<T> = Omit<
PropertyOptions<T>,
"columnType" | "type" | "unsigned"
>;
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Varchar = <T>({
length,
...options
}: VarcharOptions<T> = {}): DecoratorType =>
Property<T>({
...options,
columnType: length !== undefined ? "varchar" : `varchar(${length})`,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment