fix(storage): Text bei Mongo nicht durch Default '' überschreiben
Pre-Save-Hook deflatet text nur bei neuem/geändertem Text; update() markiert explizit gesetzten Text als geändert. Regressionstest im Contract. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e05096fd6
commit
bbd04b3eb5
3 files changed
+13
-1
No files matched your search
@@ -33,7 +33,8 @@ const HomebrewSchema = mongoose.Schema({
|
||||
}, { versionKey: false });
|
||||
|
||||
HomebrewSchema.pre('save', function() {
|
||||
if(this.text !== undefined) {
|
||||
// Nur bei neuem/geändertem Text: der Default '' beim Laden darf textBin nicht überschreiben.
|
||||
if(this.text !== undefined && (this.isNew || this.isModified('text'))) {
|
||||
this.textBin = zlib.deflateRawSync(this.text);
|
||||
this.text = undefined;
|
||||
}
|
||||
|
||||
@@ -164,6 +164,16 @@ export const runBrewRepositoryContract = (createBrews)=>{
|
||||
expect(new Date(r2.lastViewed).getTime()).toBeGreaterThan(before.getTime());
|
||||
expect((await brews.findByShareId(b.shareId)).views).toBe(2);
|
||||
});
|
||||
|
||||
test('Text bleibt nach incrementViews und update ohne text erhalten', async ()=>{
|
||||
const b = await brews.create(sample());
|
||||
await brews.incrementViews(b.shareId);
|
||||
expect((await brews.findByShareId(b.shareId)).text).toBe(sample().text);
|
||||
await brews.update({ id: b.id, title: 'Neu' });
|
||||
expect((await brews.findByShareId(b.shareId)).text).toBe(sample().text);
|
||||
await brews.update({ id: b.id, text: '' });
|
||||
expect((await brews.findByShareId(b.shareId)).text).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Vault-Suche', ()=>{
|
||||
|
||||
@@ -99,6 +99,7 @@ export const createBrewRepository = ()=>{
|
||||
const doc = await HomebrewModel.findOne({ _id: brew.id });
|
||||
if(!doc) return null;
|
||||
const merged = _.assign(doc, withoutId(brew));
|
||||
if(brew.text !== undefined) merged.markModified('text'); // auch bei text === '' (Default) speichern
|
||||
const saved = await merged.save();
|
||||
const plain = toPlain(saved);
|
||||
if(brew.text !== undefined) plain.text = brew.text;
|
||||
|
||||
Reference in new issue
Block a user