@24hr/seqcryptor
Advanced tools
| import { DataTypes, Model, Sequelize } from 'sequelize'; | ||
| import { getSequlize } from './db'; | ||
| import { seqCryptor } from '../'; | ||
| describe('FindAll', () => { | ||
| let sequelize: Sequelize; | ||
| beforeEach(async () => { | ||
| sequelize = await getSequlize(); | ||
| }); | ||
| it('FindAll parses the data correctly and returns JSON if thats the type', async () => { | ||
| const encryptor = seqCryptor('_SUPER_SECRET_KEY_WITH_32_BYTES_'); | ||
| type Data = { | ||
| foo: string; | ||
| }; | ||
| class User extends Model { | ||
| public data!: Data; | ||
| public guid!: string; | ||
| } | ||
| User.init({ | ||
| guid: { | ||
| allowNull: false, | ||
| unique: true, | ||
| type: DataTypes.UUID, | ||
| defaultValue: DataTypes.UUIDV4, | ||
| }, | ||
| data: encryptor({ | ||
| field: 'data', | ||
| allowNull: false, | ||
| type: DataTypes.JSONB, | ||
| defaultValue: '{}', | ||
| }), | ||
| }, { | ||
| sequelize, | ||
| paranoid: false, | ||
| }); | ||
| await User.sync(); | ||
| await User.create({ data: { foo: 'bar' }}); | ||
| const all = await User.findAll(); | ||
| expect(all[0].data.foo).toEqual('bar'); | ||
| }); | ||
| it('FindAndCount parses the data correctly and returns JSON if thats the type', async () => { | ||
| const encryptor = seqCryptor('_SUPER_SECRET_KEY_WITH_32_BYTES_'); | ||
| class User extends Model { | ||
| public data!: any; | ||
| public guid!: string; | ||
| } | ||
| User.init({ | ||
| guid: { | ||
| allowNull: false, | ||
| unique: true, | ||
| type: DataTypes.UUID, | ||
| defaultValue: DataTypes.UUIDV4, | ||
| }, | ||
| data: encryptor({ | ||
| type: DataTypes.JSONB, | ||
| field: 'data', | ||
| }), | ||
| }, { | ||
| sequelize, | ||
| paranoid: false, | ||
| }); | ||
| await User.sync(); | ||
| await User.create({ data: { foo: 'bar' }}); | ||
| const all = await User.findAndCountAll(); | ||
| expect(all.rows[0].data.foo).toEqual('bar'); | ||
| }); | ||
| }); | ||
+1
-0
@@ -8,4 +8,5 @@ import { DataTypes } from 'sequelize'; | ||
| field: string; | ||
| type: typeof DataTypes.STRING | typeof DataTypes.JSONB | typeof DataTypes.JSON | typeof DataTypes.TEXT; | ||
| baseType?: typeof DataTypes.BLOB | typeof DataTypes.TEXT; | ||
| returnRawOnUnecrypted?: boolean; | ||
| } |
+9
-2
@@ -16,4 +16,4 @@ "use strict"; | ||
| function field(options) { | ||
| if (options.type !== sequelize_1.DataTypes.STRING && options.type !== sequelize_1.DataTypes.JSON && options.type !== sequelize_1.DataTypes.JSONB) { | ||
| throw new Error('Data type not supported. Currently only STRING, JSON and JSONB are supported'); | ||
| if (options.type !== sequelize_1.DataTypes.TEXT && options.type !== sequelize_1.DataTypes.STRING && options.type !== sequelize_1.DataTypes.JSON && options.type !== sequelize_1.DataTypes.JSONB) { | ||
| throw new Error('Data type not supported. Currently only TEXT, STRING, JSON and JSONB are supported'); | ||
| } | ||
@@ -25,2 +25,5 @@ if (options.defaultValue && (options.type === sequelize_1.DataTypes.JSON || options.type === sequelize_1.DataTypes.JSONB)) { | ||
| const rawData = this.getDataValue(options.field); | ||
| if (rawData === null) { | ||
| return rawData; | ||
| } | ||
| const decryptedData = (0, exports.decrypt)(secret, rawData, options.returnRawOnUnecrypted, options.defaultValue); | ||
@@ -33,2 +36,6 @@ if (options.type === sequelize_1.DataTypes.JSONB || options.type === sequelize_1.DataTypes.JSON) { | ||
| set(value) { | ||
| if (value === null) { | ||
| this.setDataValue(options.field, null); | ||
| return; | ||
| } | ||
| let parsedValue = value; | ||
@@ -35,0 +42,0 @@ if (options.type === sequelize_1.DataTypes.JSONB || options.type === sequelize_1.DataTypes.JSON) { |
+1
-1
| { | ||
| "name": "@24hr/seqcryptor", | ||
| "version": "0.8.7", | ||
| "version": "0.8.8", | ||
| "description": "", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
+10
-2
@@ -18,4 +18,4 @@ import crypto from 'crypto'; | ||
| if (options.type !== DataTypes.STRING && options.type !== DataTypes.JSON && options.type !== DataTypes.JSONB) { | ||
| throw new Error('Data type not supported. Currently only STRING, JSON and JSONB are supported'); | ||
| if (options.type !== DataTypes.TEXT && options.type !== DataTypes.STRING && options.type !== DataTypes.JSON && options.type !== DataTypes.JSONB) { | ||
| throw new Error('Data type not supported. Currently only TEXT, STRING, JSON and JSONB are supported'); | ||
| } | ||
@@ -32,2 +32,5 @@ | ||
| const rawData = this.getDataValue(options.field); | ||
| if (rawData === null) { | ||
| return rawData; | ||
| } | ||
| const decryptedData = decrypt(secret, rawData, options.returnRawOnUnecrypted, options.defaultValue); | ||
@@ -40,2 +43,6 @@ if (options.type === DataTypes.JSONB || options.type === DataTypes.JSON) { | ||
| set (value: any) { | ||
| if (value === null) { | ||
| this.setDataValue(options.field, null); | ||
| return; | ||
| } | ||
| let parsedValue = value; | ||
@@ -87,4 +94,5 @@ if (options.type === DataTypes.JSONB || options.type === DataTypes.JSON) { | ||
| field: string, | ||
| type: typeof DataTypes.STRING | typeof DataTypes.JSONB | typeof DataTypes.JSON | typeof DataTypes.TEXT, | ||
| baseType?: typeof DataTypes.BLOB | typeof DataTypes.TEXT, | ||
| returnRawOnUnecrypted?: boolean, | ||
| }; |
@@ -100,4 +100,52 @@ import { DataTypes, Model, Sequelize } from 'sequelize'; | ||
| it('Can encrypt a field that has null as a value', async () => { | ||
| const encryptor = seqCryptor('_SUPER_SECRET_KEY_WITH_32_BYTES_'); | ||
| class User extends Model { | ||
| public name!: string; | ||
| public guid!: string; | ||
| } | ||
| User.init({ | ||
| guid: { | ||
| allowNull: false, | ||
| unique: true, | ||
| type: DataTypes.UUID, | ||
| defaultValue: DataTypes.UUIDV4, | ||
| }, | ||
| name: encryptor({ | ||
| allowNull: true, | ||
| type: DataTypes.STRING, | ||
| field: 'name', | ||
| }), | ||
| }, { | ||
| sequelize, | ||
| paranoid: false, | ||
| tableName: 'user', | ||
| }); | ||
| await User.sync(); | ||
| const guid = 'd7abcc8b-3096-4431-ae9b-93e6cf3f8d43'; | ||
| const user = await User.create({ | ||
| name: null, | ||
| guid, | ||
| }); | ||
| expect(user!.name).toEqual(null); | ||
| const userRaw = await User.findOne({ where: { guid: guid }, raw: true }); | ||
| expect(userRaw!.name).toEqual(null); | ||
| const userFromDb = await User.findOne({ where: { guid: guid } }); | ||
| await userFromDb!.update({ name: 'hej' }); | ||
| expect(userFromDb!.name).toEqual('hej'); | ||
| await userFromDb!.update({ name: null }); | ||
| expect(userFromDb!.name).toEqual(null); | ||
| }); | ||
| }); | ||
@@ -104,0 +152,0 @@ |
33184
14.86%16
6.67%669
21.86%