diff --git a/package-lock.json b/package-lock.json index 5149c4f..31c07ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/datasync-content-store-filesystem", - "version": "2.0.5", + "version": "2.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/datasync-content-store-filesystem", - "version": "2.0.5", + "version": "2.1.0", "license": "MIT", "dependencies": { "debug": "^4.3.4", diff --git a/package.json b/package.json index acf3c25..8d83f23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/datasync-content-store-filesystem", - "version": "2.0.5", + "version": "2.1.0", "description": "Datasync content store library - saves data in filesystem", "main": "./dist", "types": "./typings", diff --git a/src/fs.ts b/src/fs.ts index 05c905a..584fbee 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -30,6 +30,7 @@ if ( export class FilesystemStore { private readonly assetStore: any private readonly config: any + private readonly _config: any; private readonly pattern: { contentTypeKeys: string[], entryKeys: string[], @@ -40,6 +41,7 @@ export class FilesystemStore { constructor(assetStore, config) { this.assetStore = assetStore + this._config = config; this.config = config.contentStore const baseDirKeys = this.config.baseDir.split(sep) this.pattern = ({} as any) @@ -454,6 +456,14 @@ export class FilesystemStore { private publishEntry(data) { return new Promise(async (resolve, reject) => { try { + if (this._config?.enableContentReferences) { + // Check if reference exists and update the field + this._updateReferenceFields(data, data._content_type?.schema, 'reference'); + } + if (this._config?.enableAssetReferences) { + // Check if reference exists and update the field + this._updateAssetFields(data, data._content_type?.schema, 'file'); + } let entry = cloneDeep(data) entry = filter(entry) // to remove _content_type and checkpoint from entry data @@ -651,4 +661,32 @@ export class FilesystemStore { } } // tslint:disable-next-line: max-file-line-count + + private _updateReferenceFields(data, ctSchema, fieldType) { + for (const field of ctSchema) { + if (field.data_type === fieldType) { + data[field.uid] = { + reference_to: field.reference_to?.[0], + value: data[field.uid]?.[0]?.uid, + }; + } + if (field.schema) { + this._updateReferenceFields(data[field.uid], field.schema, fieldType); + } + } + } + + private _updateAssetFields(data, ctSchema, fieldType) { + for (const field of ctSchema) { + if (field.data_type === fieldType) { + data[field.uid] = { + reference_to: "_assets", + value: data[field.uid], + }; + } + if (field.schema) { + this._updateAssetFields(data[field.uid], field.schema, fieldType); + } + } + } } diff --git a/typings/fs.d.ts b/typings/fs.d.ts index 2794c06..a22aaf6 100644 --- a/typings/fs.d.ts +++ b/typings/fs.d.ts @@ -6,6 +6,7 @@ export declare class FilesystemStore { private readonly assetStore; private readonly config; + private readonly _config; private readonly pattern; private readonly unwanted; private readonly localePath; @@ -28,4 +29,6 @@ export declare class FilesystemStore { private _getAssetFieldsHelper; private _updateEntryAssetReference; private _nullifyDeletedAssetField; + private _updateReferenceFields; + private _updateAssetFields; }