fix: values to filter with emtpy values (#4319)

* fix: values to filter with emtpy values

* chore: test

* fix: values to filter

* fix: test
This commit is contained in:
ChengLei Shao 2024-05-19 17:35:11 +08:00 committed by GitHub
parent afa9f3faa4
commit 8b3b9b3b26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 72 additions and 20 deletions

View File

@ -50,4 +50,49 @@ describe('update or create', () => {
expect(post).not.toBeNull();
expect(post['title']).toEqual('t1');
});
test('update or create with empty values', async () => {
await Post.repository.create({ values: { title: 't1' } });
const response = await app
.agent()
.resource('posts')
.updateOrCreate({
values: {},
filterKeys: ['title'],
});
expect(response.statusCode).toEqual(200);
const post = await Post.repository.findOne({
filter: {
'title.$empty': true,
},
});
expect(post).not.toBeNull();
await app
.agent()
.resource('posts')
.updateOrCreate({
values: {},
filterKeys: ['title'],
});
expect(
await Post.repository.count({
filter: {
'title.$empty': true,
},
}),
).toEqual(1);
expect(
await Post.repository.count({
filter: {
title: 't1',
},
}),
).toEqual(1);
});
});

View File

@ -191,8 +191,11 @@ describe('create', () => {
},
},
});
expect(u1.name).toEqual('u1');
const group = await u1.get('group');
expect(group.name).toEqual('g1');
const u2 = await User.repository.firstOrCreate({

View File

@ -242,11 +242,8 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
this.model = collection.model;
}
public static valuesToFilter(values: Values, filterKeys: Array<string>) {
const filterAnd = [];
const flattedValues = flatten(values);
const keyWithOutArrayIndex = (key) => {
public static valuesToFilter(values: Values = {}, filterKeys: Array<string>) {
const removeArrayIndexInKey = (key) => {
const chunks = key.split('.');
return chunks
.filter((chunk) => {
@ -255,29 +252,36 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
.join('.');
};
const filterAnd = [];
const flattedValues = flatten(values);
const flattedValuesObject = {};
for (const key in flattedValues) {
const keyWithoutArrayIndex = removeArrayIndexInKey(key);
if (flattedValuesObject[keyWithoutArrayIndex]) {
if (!Array.isArray(flattedValuesObject[keyWithoutArrayIndex])) {
flattedValuesObject[keyWithoutArrayIndex] = [flattedValuesObject[keyWithoutArrayIndex]];
}
flattedValuesObject[keyWithoutArrayIndex].push(flattedValues[key]);
} else {
flattedValuesObject[keyWithoutArrayIndex] = [flattedValues[key]];
}
}
for (const filterKey of filterKeys) {
let filterValue;
for (const flattedKey of Object.keys(flattedValues)) {
const flattedKeyWithoutIndex = keyWithOutArrayIndex(flattedKey);
if (flattedKeyWithoutIndex === filterKey) {
if (filterValue) {
if (Array.isArray(filterValue)) {
filterValue.push(flattedValues[flattedKey]);
} else {
filterValue = [filterValue, flattedValues[flattedKey]];
}
} else {
filterValue = flattedValues[flattedKey];
}
}
}
const filterValue = flattedValuesObject[filterKey]
? flattedValuesObject[filterKey]
: lodash.get(values, filterKey);
if (filterValue) {
filterAnd.push({
[filterKey]: filterValue,
});
} else {
filterAnd.push({
[filterKey]: null,
});
}
}