nicot 1.4.2 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -678,6 +678,8 @@ import { MergePropertyDecorators as MergePropertyDecorators2 } from "nesties";
678
678
  import { Column, Index } from "typeorm";
679
679
  import {
680
680
  buildMessage,
681
+ IsArray,
682
+ IsBoolean,
681
683
  IsDate,
682
684
  IsEnum,
683
685
  IsInt,
@@ -853,15 +855,31 @@ function binaryToBuffer(value) {
853
855
  }
854
856
  return Buffer.from(new Uint8Array(value));
855
857
  }
858
+ function binaryToPostgresByteaHex(value) {
859
+ return `\\x${binaryToBuffer(value).toString("hex")}`;
860
+ }
861
+ function base64OrBinaryToBuffer(value) {
862
+ if (isBinaryLike(value)) {
863
+ return binaryToBuffer(value);
864
+ }
865
+ return Buffer.from(String(value), "base64");
866
+ }
867
+ function base64OrBinaryToDatabaseValue(value, storage = "postgres-bytea") {
868
+ const buffer = base64OrBinaryToBuffer(value);
869
+ if (storage === "postgres-bytea") {
870
+ return binaryToPostgresByteaHex(buffer);
871
+ }
872
+ return buffer;
873
+ }
856
874
  var Base64BinaryTransformer = class {
875
+ constructor(storage = "postgres-bytea") {
876
+ this.storage = storage;
877
+ }
857
878
  to(entValue) {
858
879
  if (entValue == null) {
859
880
  return entValue;
860
881
  }
861
- if (isBinaryLike(entValue)) {
862
- return binaryToBuffer(entValue);
863
- }
864
- return Buffer.from(String(entValue), "base64");
882
+ return base64OrBinaryToDatabaseValue(entValue, this.storage);
865
883
  }
866
884
  from(dbValue) {
867
885
  if (dbValue == null) {
@@ -870,7 +888,11 @@ var Base64BinaryTransformer = class {
870
888
  if (isBinaryLike(dbValue)) {
871
889
  return binaryToBuffer(dbValue).toString("base64");
872
890
  }
873
- return Buffer.from(String(dbValue)).toString("base64");
891
+ const text = String(dbValue);
892
+ if (/^\\x[0-9a-f]*$/i.test(text)) {
893
+ return Buffer.from(text.slice(2), "hex").toString("base64");
894
+ }
895
+ return Buffer.from(text).toString("base64");
874
896
  }
875
897
  };
876
898
 
@@ -1076,12 +1098,30 @@ var BoolColumn = (options = {}) => MergePropertyDecorators2([
1076
1098
  swaggerDecorator(options, { type: Boolean }),
1077
1099
  GetMutatorBool()
1078
1100
  ]);
1101
+ var jsonColumnValidators = (definition) => {
1102
+ const isArray = Array.isArray(definition);
1103
+ const cl = getClassFromClassOrArray2(definition);
1104
+ const validationOptions = isArray ? { each: true } : {};
1105
+ const decorators = isArray ? [IsArray()] : [];
1106
+ if (cl === String) {
1107
+ decorators.push(IsString(validationOptions));
1108
+ } else if (cl === Number) {
1109
+ decorators.push(IsNumber({}, validationOptions));
1110
+ } else if (cl === Boolean) {
1111
+ decorators.push(IsBoolean(validationOptions));
1112
+ } else if (cl === Date) {
1113
+ decorators.push(IsDate(validationOptions));
1114
+ } else {
1115
+ decorators.push(ValidateNested2(validationOptions));
1116
+ }
1117
+ return decorators;
1118
+ };
1079
1119
  var createJsonColumnDef = (columnType = "jsonb", typeTransformerClass = TypeTransformer) => (definition, options = {}) => {
1080
1120
  const cl = getClassFromClassOrArray2(definition);
1081
1121
  return MergePropertyDecorators2([
1082
1122
  RequireGetMutator(),
1083
1123
  Type2(() => cl),
1084
- ValidateNested2(),
1124
+ ...jsonColumnValidators(definition),
1085
1125
  Column(options.columnType || columnType, {
1086
1126
  ...columnDecoratorOptions(options),
1087
1127
  transformer: new typeTransformerClass(definition)
@@ -1113,7 +1153,9 @@ var Base64BinaryColumn = (options = {}) => MergePropertyDecorators2([
1113
1153
  Column(options.columnType || "bytea", {
1114
1154
  ...columnDecoratorOptions(options),
1115
1155
  default: void 0,
1116
- transformer: new Base64BinaryTransformer()
1156
+ transformer: new Base64BinaryTransformer(
1157
+ options.binaryStorage ?? (options.columnType && options.columnType !== "bytea" ? "binary" : "postgres-bytea")
1158
+ )
1117
1159
  }),
1118
1160
  IsBase64OrBinary(),
1119
1161
  validatorDecorator(options),
@@ -1342,19 +1384,20 @@ var createQueryOperatorArrayify = (operator, singleFallback) => createQueryArray
1342
1384
  );
1343
1385
  var QueryIn = createQueryOperatorArrayify("IN", "=");
1344
1386
  var QueryNotIn = createQueryOperatorArrayify("NOT IN", "!=");
1345
- var toBase64QueryBuffer = (value) => {
1387
+ var normalizeQueryBase64Options = (fieldOrOptions) => typeof fieldOrOptions === "string" ? { field: fieldOrOptions } : fieldOrOptions ?? {};
1388
+ var toBase64QueryValue = (value, storage = "postgres-bytea") => {
1346
1389
  if (value == null) {
1347
1390
  return value;
1348
1391
  }
1349
- if (isBinaryLike(value)) {
1350
- return binaryToBuffer(value);
1351
- }
1352
- return Buffer.from(String(value), "base64");
1392
+ return base64OrBinaryToDatabaseValue(value, storage);
1393
+ };
1394
+ var createQueryBase64Operator = (operator) => (fieldOrOptions) => {
1395
+ const options = normalizeQueryBase64Options(fieldOrOptions);
1396
+ return QueryWrap((entityExpr, varExpr, info) => {
1397
+ info.mutateValue(toBase64QueryValue(info.value, options.binaryStorage));
1398
+ return `${entityExpr} ${operator} ${varExpr}`;
1399
+ }, options.field);
1353
1400
  };
1354
- var createQueryBase64Operator = (operator) => (field) => QueryWrap((entityExpr, varExpr, info) => {
1355
- info.mutateValue(toBase64QueryBuffer(info.value));
1356
- return `${entityExpr} ${operator} ${varExpr}`;
1357
- }, field);
1358
1401
  var QueryBase64Equal = createQueryBase64Operator("=");
1359
1402
  var QueryBase64NotEqual = createQueryBase64Operator("!=");
1360
1403
  var QueryFullText = (options = {}) => {
@@ -4329,7 +4372,10 @@ export {
4329
4372
  applyQueryPropertyLike,
4330
4373
  applyQueryPropertySearch,
4331
4374
  applyQueryPropertyZeroNullable,
4375
+ base64OrBinaryToBuffer,
4376
+ base64OrBinaryToDatabaseValue,
4332
4377
  binaryToBuffer,
4378
+ binaryToPostgresByteaHex,
4333
4379
  createGetMutator,
4334
4380
  createQueryArrayify,
4335
4381
  createQueryBase64Operator,