blob.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { entityKind } from "../../entity.js";
  2. import { getColumnNameAndConfig, textDecoder } from "../../utils.js";
  3. import { SQLiteColumn, SQLiteColumnBuilder } from "./common.js";
  4. class SQLiteBigIntBuilder extends SQLiteColumnBuilder {
  5. static [entityKind] = "SQLiteBigIntBuilder";
  6. constructor(name) {
  7. super(name, "bigint", "SQLiteBigInt");
  8. }
  9. /** @internal */
  10. build(table) {
  11. return new SQLiteBigInt(table, this.config);
  12. }
  13. }
  14. class SQLiteBigInt extends SQLiteColumn {
  15. static [entityKind] = "SQLiteBigInt";
  16. getSQLType() {
  17. return "blob";
  18. }
  19. mapFromDriverValue(value) {
  20. if (typeof Buffer !== "undefined" && Buffer.from) {
  21. const buf = Buffer.isBuffer(value) ? value : value instanceof ArrayBuffer ? Buffer.from(value) : value.buffer ? Buffer.from(value.buffer, value.byteOffset, value.byteLength) : Buffer.from(value);
  22. return BigInt(buf.toString("utf8"));
  23. }
  24. return BigInt(textDecoder.decode(value));
  25. }
  26. mapToDriverValue(value) {
  27. return Buffer.from(value.toString());
  28. }
  29. }
  30. class SQLiteBlobJsonBuilder extends SQLiteColumnBuilder {
  31. static [entityKind] = "SQLiteBlobJsonBuilder";
  32. constructor(name) {
  33. super(name, "json", "SQLiteBlobJson");
  34. }
  35. /** @internal */
  36. build(table) {
  37. return new SQLiteBlobJson(
  38. table,
  39. this.config
  40. );
  41. }
  42. }
  43. class SQLiteBlobJson extends SQLiteColumn {
  44. static [entityKind] = "SQLiteBlobJson";
  45. getSQLType() {
  46. return "blob";
  47. }
  48. mapFromDriverValue(value) {
  49. if (typeof Buffer !== "undefined" && Buffer.from) {
  50. const buf = Buffer.isBuffer(value) ? value : value instanceof ArrayBuffer ? Buffer.from(value) : value.buffer ? Buffer.from(value.buffer, value.byteOffset, value.byteLength) : Buffer.from(value);
  51. return JSON.parse(buf.toString("utf8"));
  52. }
  53. return JSON.parse(textDecoder.decode(value));
  54. }
  55. mapToDriverValue(value) {
  56. return Buffer.from(JSON.stringify(value));
  57. }
  58. }
  59. class SQLiteBlobBufferBuilder extends SQLiteColumnBuilder {
  60. static [entityKind] = "SQLiteBlobBufferBuilder";
  61. constructor(name) {
  62. super(name, "buffer", "SQLiteBlobBuffer");
  63. }
  64. /** @internal */
  65. build(table) {
  66. return new SQLiteBlobBuffer(table, this.config);
  67. }
  68. }
  69. class SQLiteBlobBuffer extends SQLiteColumn {
  70. static [entityKind] = "SQLiteBlobBuffer";
  71. mapFromDriverValue(value) {
  72. if (Buffer.isBuffer(value)) {
  73. return value;
  74. }
  75. return Buffer.from(value);
  76. }
  77. getSQLType() {
  78. return "blob";
  79. }
  80. }
  81. function blob(a, b) {
  82. const { name, config } = getColumnNameAndConfig(a, b);
  83. if (config?.mode === "json") {
  84. return new SQLiteBlobJsonBuilder(name);
  85. }
  86. if (config?.mode === "bigint") {
  87. return new SQLiteBigIntBuilder(name);
  88. }
  89. return new SQLiteBlobBufferBuilder(name);
  90. }
  91. export {
  92. SQLiteBigInt,
  93. SQLiteBigIntBuilder,
  94. SQLiteBlobBuffer,
  95. SQLiteBlobBufferBuilder,
  96. SQLiteBlobJson,
  97. SQLiteBlobJsonBuilder,
  98. blob
  99. };
  100. //# sourceMappingURL=blob.js.map