custom.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig } from "../../utils.js";
  3. import { SQLiteColumn, SQLiteColumnBuilder } from "./common.js";
  4. class SQLiteCustomColumnBuilder extends SQLiteColumnBuilder {
  5. static [entityKind] = "SQLiteCustomColumnBuilder";
  6. constructor(name, fieldConfig, customTypeParams) {
  7. super(name, "custom", "SQLiteCustomColumn");
  8. this.config.fieldConfig = fieldConfig;
  9. this.config.customTypeParams = customTypeParams;
  10. }
  11. /** @internal */
  12. build(table) {
  13. return new SQLiteCustomColumn(
  14. table,
  15. this.config
  16. );
  17. }
  18. }
  19. class SQLiteCustomColumn extends SQLiteColumn {
  20. static [entityKind] = "SQLiteCustomColumn";
  21. sqlName;
  22. mapTo;
  23. mapFrom;
  24. constructor(table, config) {
  25. super(table, config);
  26. this.sqlName = config.customTypeParams.dataType(config.fieldConfig);
  27. this.mapTo = config.customTypeParams.toDriver;
  28. this.mapFrom = config.customTypeParams.fromDriver;
  29. }
  30. getSQLType() {
  31. return this.sqlName;
  32. }
  33. mapFromDriverValue(value) {
  34. return typeof this.mapFrom === "function" ? this.mapFrom(value) : value;
  35. }
  36. mapToDriverValue(value) {
  37. return typeof this.mapTo === "function" ? this.mapTo(value) : value;
  38. }
  39. }
  40. function customType(customTypeParams) {
  41. return (a, b) => {
  42. const { name, config } = getColumnNameAndConfig(a, b);
  43. return new SQLiteCustomColumnBuilder(
  44. name,
  45. config,
  46. customTypeParams
  47. );
  48. };
  49. }
  50. export {
  51. SQLiteCustomColumn,
  52. SQLiteCustomColumnBuilder,
  53. customType
  54. };
  55. //# sourceMappingURL=custom.js.map