Skip to content

Commit

Permalink
feat: 🎸 Now undefined values don't modify the persisted field
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytyr committed Apr 18, 2020
1 parent 42ac4b1 commit 997a1b9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -243,6 +243,7 @@ const user = await redisk.getOne(User, id);
user.name = 'Bar';
await redisk.save(user);
```
#### Note: Null fields will be removed from the persisted entity, undefined fields will not be modified from persisted entity.

### Get by primary key

Expand Down
4 changes: 4 additions & 0 deletions src/redisk.ts
Expand Up @@ -42,6 +42,10 @@ export class Redisk {
const changedFields = [];

for (const property of Object.keys(properties).map(key => properties[key])) {
if (entity[property.name] === undefined) {
entity[property.name] = persistedEntity[property.name];
}

if (entity[property.name] !== persistedEntity[property.name]) {
changedFields.push(property.name);

Expand Down
5 changes: 5 additions & 0 deletions test/entities/user.entity.ts
Expand Up @@ -16,6 +16,9 @@ export class User {
@Property({searchable: true})
public readonly name: string;

@Property()
public readonly description: string;

@Unique()
@Property()
public readonly email: string;
Expand All @@ -36,6 +39,7 @@ export class User {
constructor(
id: string,
name: string,
description: string,
email: string,
color: string,
food: string,
Expand All @@ -44,6 +48,7 @@ export class User {
) {
this.id = id;
this.name = name;
this.description = description;
this.email = email;
this.color = color;
this.food = food;
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/users.ts
Expand Up @@ -2,9 +2,9 @@ import { User } from '../entities/user.entity';
import { groups } from './groups';

export const users = [
new User('4409', 'John', 'john@email.com', 'red', 'tofu', groups[0], new Date('2020-02-22 10:00:00')),
new User('F8AC', 'Anthony', 'anthony@email.com', 'blue', 'avocado', groups[0], new Date('2020-02-22 12:34:00')),
new User('ACEF', 'Juan', 'juan@email.com', 'blue', 'tofu', null, new Date('2020-02-22 18:10:00')),
new User('A366', 'Jim', 'jim@email.com', 'blue', 'tofu', groups[1], new Date('2020-02-23 22:35:00')),
new User('D162', 'Rick', 'rick@email.com', 'red', 'rice', groups[1], new Date('2020-02-24 12:00:00')),
new User('4409', 'John', '', 'john@email.com', 'red', 'tofu', groups[0], new Date('2020-02-22 10:00:00')),
new User('F8AC', 'Anthony', '', 'anthony@email.com', 'blue', 'avocado', groups[0], new Date('2020-02-22 12:34:00')),
new User('ACEF', 'Juan', '', 'juan@email.com', 'blue', 'tofu', null, new Date('2020-02-22 18:10:00')),
new User('A366', 'Jim', '', 'jim@email.com', 'blue', 'tofu', groups[1], new Date('2020-02-23 22:35:00')),
new User('D162', 'Rick', '', 'rick@email.com', 'red', 'rice', groups[1], new Date('2020-02-24 12:00:00')),
];
42 changes: 35 additions & 7 deletions test/save.spec.ts
Expand Up @@ -24,6 +24,7 @@ beforeEach(async () => {

const id = '__id__';
const name = 'Anna';
const description = 'Description';
const email = 'anna@email.com';
const color = 'red';
const food = 'bread';
Expand All @@ -32,14 +33,15 @@ const group = new Group('4E2F', 'Group name');

describe('Save with cascade insert', () => {
it('should persist entity', async () => {
const user = new User(id, name, email, color, food, group, created);
const user = new User(id, name, description, email, color, food, group, created);

await utils.redisk.save(user);

const storedUser = await utils.redisk.getClient().hgetall('user:' + id);
expect(storedUser).toEqual({
id,
name,
description,
email,
color,
food,
Expand Down Expand Up @@ -69,9 +71,9 @@ describe('Save with cascade insert', () => {

describe('Save with unique in-use', () => {
it('should throw error', async () => {
const user = new User(id, name, email, color, food, null, created);
const user = new User(id, name, description, email, color, food, null, created);
await utils.redisk.save(user);
const user2 = new User('foo', name, email, color, food, null, created);
const user2 = new User('foo', name, description, email, color, food, null, created);
let exception;
try {
await utils.redisk.save(user2);
Expand All @@ -85,7 +87,7 @@ describe('Save with unique in-use', () => {

describe('Update persisted entity with cascade update', () => {
it('should update entities', async () => {
const user = new User(id, name, email, color, food, group, created);
const user = new User(id, name, description, email, color, food, group, created);
await utils.redisk.save(user);

const newName = 'Riley';
Expand All @@ -94,13 +96,14 @@ describe('Update persisted entity with cascade update', () => {
const newCreated = new Date('2020-03-25 12:10:04');

const group2 = new Group(group.id, 'Foo');
const updatedUser = new User(id, newName, newEmail, newColor, food, group2, newCreated);
const updatedUser = new User(id, newName, description, newEmail, newColor, food, group2, newCreated);
await utils.redisk.save(updatedUser);

const storedUser = await utils.redisk.getClient().hgetall('user:' + id);
expect(storedUser).toEqual({
id,
name: newName,
description,
email: newEmail,
color: newColor,
food,
Expand Down Expand Up @@ -130,16 +133,41 @@ describe('Update persisted entity with cascade update', () => {
});
});


describe('Update persisted entity with undefineds', () => {
it('should ignore those properties', async () => {
const newName = 'Riley';

const user = new User(id, name, description, email, color, food, group, created);
await utils.redisk.save(user);

await utils.redisk.save(new User(id, newName, undefined, email, color, food, group, created));
const storedUser = await utils.redisk.getClient().hgetall('user:' + id);
expect(storedUser).toEqual({
id,
name: newName,
description,
email,
color,
food,
group: group.id,
created: String(created.valueOf()),
});

});
});

describe('Update persisted entity', () => {
it('should update entity', async () => {
const user = new User(id, name, email, color, food, group, created);
const user = new User(id, name, description, email, color, food, group, created);
await utils.redisk.save(user);

const newName = 'Riley';
const newEmail = 'riley@email.com';
const newDescription = null;
const newColor = 'purple';
const newCreated = new Date('2020-03-25 12:10:04');
const updatedUser = new User(id, newName, newEmail, newColor, food, null, newCreated);
const updatedUser = new User(id, newName, newDescription, newEmail, newColor, food, null, newCreated);
await utils.redisk.save(updatedUser);

const storedUser = await utils.redisk.getClient().hgetall('user:' + id);
Expand Down

0 comments on commit 997a1b9

Please sign in to comment.