text.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig } from "../../utils.js";
  3. import { SQLiteColumn, SQLiteColumnBuilder } from "./common.js";
  4. class SQLiteTextBuilder extends SQLiteColumnBuilder {
  5. static [entityKind] = "SQLiteTextBuilder";
  6. constructor(name, config) {
  7. super(name, "string", "SQLiteText");
  8. this.config.enumValues = config.enum;
  9. this.config.length = config.length;
  10. }
  11. /** @internal */
  12. build(table) {
  13. return new SQLiteText(
  14. table,
  15. this.config
  16. );
  17. }
  18. }
  19. class SQLiteText extends SQLiteColumn {
  20. static [entityKind] = "SQLiteText";
  21. enumValues = this.config.enumValues;
  22. length = this.config.length;
  23. constructor(table, config) {
  24. super(table, config);
  25. }
  26. getSQLType() {
  27. return `text${this.config.length ? `(${this.config.length})` : ""}`;
  28. }
  29. }
  30. class SQLiteTextJsonBuilder extends SQLiteColumnBuilder {
  31. static [entityKind] = "SQLiteTextJsonBuilder";
  32. constructor(name) {
  33. super(name, "json", "SQLiteTextJson");
  34. }
  35. /** @internal */
  36. build(table) {
  37. return new SQLiteTextJson(
  38. table,
  39. this.config
  40. );
  41. }
  42. }
  43. class SQLiteTextJson extends SQLiteColumn {
  44. static [entityKind] = "SQLiteTextJson";
  45. getSQLType() {
  46. return "text";
  47. }
  48. mapFromDriverValue(value) {
  49. return JSON.parse(value);
  50. }
  51. mapToDriverValue(value) {
  52. return JSON.stringify(value);
  53. }
  54. }
  55. function text(a, b = {}) {
  56. const { name, config } = getColumnNameAndConfig(a, b);
  57. if (config.mode === "json") {
  58. return new SQLiteTextJsonBuilder(name);
  59. }
  60. return new SQLiteTextBuilder(name, config);
  61. }
  62. export {
  63. SQLiteText,
  64. SQLiteTextBuilder,
  65. SQLiteTextJson,
  66. SQLiteTextJsonBuilder,
  67. text
  68. };
  69. //# sourceMappingURL=text.js.map