Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
fix: multilanguage insert missing fields
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Dec 12, 2023
1 parent a9c47ea commit f9380ce
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/controller/multi-language.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import DeepMerge from '@fastify/deepmerge'
import AggregateBuilder from '@kakang/mongodb-aggregate-builder'
import { isEmpty, isExist, isNull, isUndefined } from '@kakang/validator'
import { type AggregateOptions, type AnyBulkWriteOperation, type BulkWriteOptions, type Collection, type Document, type Filter, type FindOptions, type UpdateFilter } from 'mongodb'
import { appendBasicSchema, appendUpdateSchema } from '../utils/append'
import { computeSharedOption } from '../utils/option'
import { normalizeQueryDate, retrieveUpdateQueryData } from '../utils/query'
import { Controller, type ControllerOptions, type SearchOptions } from './default'
const deepmerge = DeepMerge()

export interface MultiLanguageControllerOptions<TSchema extends Document = Document> extends Partial<ControllerOptions> {
slugField: keyof TSchema
Expand Down Expand Up @@ -64,8 +66,10 @@ export class MultiLanguageController<TSchema extends Document = Document> extend

// insert when it is fallback, update when item exist
if (isFallback) {
// we need to merge the old and new document to prevent missing fields
const doc: any = deepmerge(item, retrieveUpdateQueryData(docs))
// we append needed info for the document
const document: any = appendBasicSchema(retrieveUpdateQueryData(docs), this.appendBasicSchema)
const document: any = appendBasicSchema(doc as TSchema, this.appendBasicSchema)
// ensure slug is exist
document[this.slugField] = item[this.slugField]
// insert if language is not exist
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"typescript": "~5.3.3"
},
"dependencies": {
"@fastify/deepmerge": "^1.3.0",
"@kakang/eventemitter": "^0.2.1",
"@kakang/mongodb-aggregate-builder": "^0.2.2",
"@kakang/validator": "^0.1.4",
Expand Down

0 comments on commit f9380ce

Please sign in to comment.