custom.d.cts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import type { ColumnBuilderBaseConfig } from "../../column-builder.cjs";
  2. import type { ColumnBaseConfig } from "../../column.cjs";
  3. import { entityKind } from "../../entity.cjs";
  4. import type { AnyGelTable } from "../table.cjs";
  5. import type { SQL } from "../../sql/sql.cjs";
  6. import { type Equal } from "../../utils.cjs";
  7. import { GelColumn, GelColumnBuilder } from "./common.cjs";
  8. export type ConvertCustomConfig<TName extends string, T extends Partial<CustomTypeValues>> = {
  9. name: TName;
  10. dataType: 'custom';
  11. columnType: 'GelCustomColumn';
  12. data: T['data'];
  13. driverParam: T['driverData'];
  14. enumValues: undefined;
  15. } & (T['notNull'] extends true ? {
  16. notNull: true;
  17. } : {}) & (T['default'] extends true ? {
  18. hasDefault: true;
  19. } : {});
  20. export interface GelCustomColumnInnerConfig {
  21. customTypeValues: CustomTypeValues;
  22. }
  23. export declare class GelCustomColumnBuilder<T extends ColumnBuilderBaseConfig<'custom', 'GelCustomColumn'>> extends GelColumnBuilder<T, {
  24. fieldConfig: CustomTypeValues['config'];
  25. customTypeParams: CustomTypeParams<any>;
  26. }, {
  27. gelColumnBuilderBrand: 'GelCustomColumnBuilderBrand';
  28. }> {
  29. static readonly [entityKind]: string;
  30. constructor(name: T['name'], fieldConfig: CustomTypeValues['config'], customTypeParams: CustomTypeParams<any>);
  31. }
  32. export declare class GelCustomColumn<T extends ColumnBaseConfig<'custom', 'GelCustomColumn'>> extends GelColumn<T> {
  33. static readonly [entityKind]: string;
  34. private sqlName;
  35. private mapTo?;
  36. private mapFrom?;
  37. constructor(table: AnyGelTable<{
  38. name: T['tableName'];
  39. }>, config: GelCustomColumnBuilder<T>['config']);
  40. getSQLType(): string;
  41. mapFromDriverValue(value: T['driverParam']): T['data'];
  42. mapToDriverValue(value: T['data']): T['driverParam'];
  43. }
  44. export type CustomTypeValues = {
  45. /**
  46. * Required type for custom column, that will infer proper type model
  47. *
  48. * Examples:
  49. *
  50. * If you want your column to be `string` type after selecting/or on inserting - use `data: string`. Like `text`, `varchar`
  51. *
  52. * If you want your column to be `number` type after selecting/or on inserting - use `data: number`. Like `integer`
  53. */
  54. data: unknown;
  55. /**
  56. * Type helper, that represents what type database driver is accepting for specific database data type
  57. */
  58. driverData?: unknown;
  59. /**
  60. * What config type should be used for {@link CustomTypeParams} `dataType` generation
  61. */
  62. config?: Record<string, any>;
  63. /**
  64. * Whether the config argument should be required or not
  65. * @default false
  66. */
  67. configRequired?: boolean;
  68. /**
  69. * If your custom data type should be notNull by default you can use `notNull: true`
  70. *
  71. * @example
  72. * const customSerial = customType<{ data: number, notNull: true, default: true }>({
  73. * dataType() {
  74. * return 'serial';
  75. * },
  76. * });
  77. */
  78. notNull?: boolean;
  79. /**
  80. * If your custom data type has default you can use `default: true`
  81. *
  82. * @example
  83. * const customSerial = customType<{ data: number, notNull: true, default: true }>({
  84. * dataType() {
  85. * return 'serial';
  86. * },
  87. * });
  88. */
  89. default?: boolean;
  90. };
  91. export interface CustomTypeParams<T extends CustomTypeValues> {
  92. /**
  93. * Database data type string representation, that is used for migrations
  94. * @example
  95. * ```
  96. * `jsonb`, `text`
  97. * ```
  98. *
  99. * If database data type needs additional params you can use them from `config` param
  100. * @example
  101. * ```
  102. * `varchar(256)`, `numeric(2,3)`
  103. * ```
  104. *
  105. * To make `config` be of specific type please use config generic in {@link CustomTypeValues}
  106. *
  107. * @example
  108. * Usage example
  109. * ```
  110. * dataType() {
  111. * return 'boolean';
  112. * },
  113. * ```
  114. * Or
  115. * ```
  116. * dataType(config) {
  117. * return typeof config.length !== 'undefined' ? `varchar(${config.length})` : `varchar`;
  118. * }
  119. * ```
  120. */
  121. dataType: (config: T['config'] | (Equal<T['configRequired'], true> extends true ? never : undefined)) => string;
  122. /**
  123. * Optional mapping function, between user input and driver
  124. * @example
  125. * For example, when using jsonb we need to map JS/TS object to string before writing to database
  126. * ```
  127. * toDriver(value: TData): string {
  128. * return JSON.stringify(value);
  129. * }
  130. * ```
  131. */
  132. toDriver?: (value: T['data']) => T['driverData'] | SQL;
  133. /**
  134. * Optional mapping function, that is responsible for data mapping from database to JS/TS code
  135. * @example
  136. * For example, when using timestamp we need to map string Date representation to JS Date
  137. * ```
  138. * fromDriver(value: string): Date {
  139. * return new Date(value);
  140. * },
  141. * ```
  142. */
  143. fromDriver?: (value: T['driverData']) => T['data'];
  144. }
  145. /**
  146. * Custom gel database data type generator
  147. */
  148. export declare function customType<T extends CustomTypeValues = CustomTypeValues>(customTypeParams: CustomTypeParams<T>): Equal<T['configRequired'], true> extends true ? {
  149. <TConfig extends Record<string, any> & T['config']>(fieldConfig: TConfig): GelCustomColumnBuilder<ConvertCustomConfig<'', T>>;
  150. <TName extends string>(dbName: TName, fieldConfig: T['config']): GelCustomColumnBuilder<ConvertCustomConfig<TName, T>>;
  151. } : {
  152. (): GelCustomColumnBuilder<ConvertCustomConfig<'', T>>;
  153. <TConfig extends Record<string, any> & T['config']>(fieldConfig?: TConfig): GelCustomColumnBuilder<ConvertCustomConfig<'', T>>;
  154. <TName extends string>(dbName: TName, fieldConfig?: T['config']): GelCustomColumnBuilder<ConvertCustomConfig<TName, T>>;
  155. };