@24hr/seqcryptor
Advanced tools
+9
-1
@@ -38,6 +38,14 @@ "use strict"; | ||
| if (value === null) { | ||
| this.setDataValue(options.field, null); | ||
| if (options.defaultValue && !options.allowNull) { | ||
| this.setDataValue(options.field, options.defaultValue); | ||
| } | ||
| else { | ||
| this.setDataValue(options.field, null); | ||
| } | ||
| return; | ||
| } | ||
| if (value === undefined) { | ||
| if (options.defaultValue && !options.allowNull) { | ||
| this.setDataValue(options.field, options.defaultValue); | ||
| } | ||
| return; | ||
@@ -44,0 +52,0 @@ } |
+1
-1
| { | ||
| "name": "@24hr/seqcryptor", | ||
| "version": "0.8.9", | ||
| "version": "0.8.10", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
+8
-1
@@ -45,6 +45,13 @@ import crypto from 'crypto'; | ||
| if (value === null) { | ||
| this.setDataValue(options.field, null); | ||
| if (options.defaultValue && !options.allowNull) { | ||
| this.setDataValue(options.field, options.defaultValue); | ||
| } else { | ||
| this.setDataValue(options.field, null); | ||
| } | ||
| return; | ||
| } | ||
| if (value === undefined) { | ||
| if (options.defaultValue && !options.allowNull) { | ||
| this.setDataValue(options.field, options.defaultValue); | ||
| } | ||
| return; | ||
@@ -51,0 +58,0 @@ } |
@@ -55,4 +55,57 @@ import { DataTypes, Model, Sequelize } from 'sequelize'; | ||
| const user2 = await User.create({ info: null, data: { foo: 1 }, name: 'bar' }); | ||
| const userParsed2 = await User.findOne({ where: { guid: user2.guid } }); | ||
| expect(userParsed2!.info).toEqual({}); | ||
| const user3 = await User.create({ info: undefined, data: { foo: 1 }, name: 'bar' }); | ||
| const userParsed3 = await User.findOne({ where: { guid: user3.guid } }); | ||
| expect(userParsed3!.info).toEqual({}); | ||
| }); | ||
| it('Can set null to a jsonb even if a default value but allownull is true', async () => { | ||
| const encryptor = seqCryptor('_SUPER_SECRET_KEY_WITH_32_BYTES_'); | ||
| class User extends Model { | ||
| public info!: any; | ||
| public data!: any; | ||
| public guid!: string; | ||
| } | ||
| User.init({ | ||
| guid: { | ||
| allowNull: false, | ||
| unique: true, | ||
| type: DataTypes.UUID, | ||
| defaultValue: DataTypes.UUIDV4, | ||
| }, | ||
| info: encryptor({ | ||
| type: DataTypes.JSONB, | ||
| field: 'info', | ||
| defaultValue: {}, | ||
| allowNull: true, | ||
| }), | ||
| data: encryptor({ | ||
| type: DataTypes.JSONB, | ||
| field: 'data', | ||
| defaultValue: {}, | ||
| }), | ||
| }, { | ||
| sequelize, | ||
| paranoid: false, | ||
| }); | ||
| await User.sync(); | ||
| const user = await User.create({ info: null, data: { foo: 1 }, name: 'bar' }); | ||
| const userParsed = await User.findOne({ where: { guid: user.guid } }); | ||
| expect(userParsed!.info).toEqual(null); | ||
| }); | ||
| }); | ||
@@ -59,0 +112,0 @@ |
36283
7.08%743
7.99%