Skip to content

Commit 32a9b9b

Browse files
Merge pull request #1 from contentstack/feature/global-field
Feature/global field
2 parents 641cbfe + ec9bc32 commit 32a9b9b

File tree

20 files changed

+306
-67
lines changed

20 files changed

+306
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.ledger
44
.checkpoint
55
_contents
6+
_development_contents
67
unprocessible
78
_mongo_assets
89
_dev_assets
-35.7 KB
Binary file not shown.

dist/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ exports.config = {
4747
options: 'ig',
4848
port: 443,
4949
protocol: 'https:',
50+
query: {
51+
include_global_field_schema: true,
52+
},
5053
regexp: {
5154
asset_pattern: {
5255
options: 'ig',

dist/core/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ const fire = (req) => {
217217
return new Promise((mapResolve, mapReject) => {
218218
return api_1.get({
219219
path: `${Contentstack.apis.content_types}${uid}`,
220+
qs: {
221+
include_global_field_schema: config.contentstack.query.include_global_field_schema,
222+
},
220223
}).then((contentTypeSchemaResponse) => {
221224
const schemaResponse = contentTypeSchemaResponse;
222225
if (schemaResponse.content_type) {

dist/plugins/helper.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
"use strict";
22
const { cloneDeep } = require('lodash');
33
const { getConfig } = require('../index');
4+
const fieldType = {
5+
REFERENCE: 'reference',
6+
GLOBAL_FIELD: 'global_field',
7+
GROUP: 'group',
8+
BLOCKS: 'blocks',
9+
FILE: 'file'
10+
};
411
exports.buildReferences = (entry, schema, parent = []) => {
5-
for (let i = 0, c = schema.length; i < c; i++) {
6-
switch (schema[i].data_type) {
7-
case 'reference':
8-
if (!(schema[i].reference_to instanceof Array)) {
9-
parent.push(schema[i].uid);
10-
update(parent, schema[i].reference_to, entry);
11-
parent.pop();
12-
}
13-
break;
14-
case 'group':
15-
parent.push(schema[i].uid);
16-
this.buildReferences(entry, schema[i].schema, parent);
17-
parent.pop();
18-
break;
19-
case 'blocks':
20-
for (let j = 0, d = schema[i].blocks.length; j < d; j++) {
12+
if (schema && Array.isArray(schema)) {
13+
for (let i = 0, c = schema.length; i < c; i++) {
14+
switch (schema[i].data_type) {
15+
case fieldType.REFERENCE:
16+
if (!(schema[i].reference_to instanceof Array)) {
17+
parent.push(schema[i].uid);
18+
update(parent, schema[i].reference_to, entry);
19+
parent.pop();
20+
}
21+
break;
22+
case fieldType.GLOBAL_FIELD:
23+
case fieldType.GROUP:
2124
parent.push(schema[i].uid);
22-
parent.push(schema[i].blocks[j].uid);
23-
this.buildReferences(entry, schema[i].blocks[j].schema, parent);
25+
this.buildReferences(entry, schema[i].schema, parent);
2426
parent.pop();
25-
parent.pop();
26-
}
27-
break;
27+
break;
28+
case fieldType.BLOCKS:
29+
for (let j = 0, d = schema[i].blocks.length; j < d; j++) {
30+
parent.push(schema[i].uid);
31+
parent.push(schema[i].blocks[j].uid);
32+
this.buildReferences(entry, schema[i].blocks[j].schema, parent);
33+
parent.pop();
34+
parent.pop();
35+
}
36+
break;
37+
}
2838
}
2939
}
3040
return entry;
@@ -33,20 +43,20 @@ exports.buildReferencePaths = (schema, entryReferences = {}, assetReferences = {
3343
for (let i = 0, l = schema.length; i < l; i++) {
3444
const field = schema[i];
3545
if (field && field.data_type) {
36-
if (field.data_type === 'reference') {
46+
if (field.data_type === fieldType.REFERENCE) {
3747
const fieldPath = ((parent) ? `${parent}.${schema[i].uid}` : field.uid);
3848
// all references will now be an array
3949
// example: { _reference: { field1.product: [''] } }
4050
entryReferences[fieldPath] = (typeof field.reference_to === 'string') ? [field.reference_to] : field.reference_to;
4151
}
42-
else if (field.data_type === 'file') {
52+
else if (field.data_type === fieldType.FILE) {
4353
const fieldPath = ((parent) ? `${parent}.${field.uid}` : field.uid);
4454
assetReferences[fieldPath] = '_assets';
4555
}
46-
else if (field.data_type === 'group' && field.schema) {
56+
else if ((field.data_type === fieldType.GROUP || field.data_type === fieldType.GLOBAL_FIELD) && field.schema) {
4757
this.buildReferencePaths(field.schema, entryReferences, assetReferences, ((parent) ? `${parent}.${field.uid}` : field.uid));
4858
}
49-
else if (field.data_type === 'blocks' && Array.isArray(field.blocks)) {
59+
else if (field.data_type === fieldType.BLOCKS && Array.isArray(field.blocks)) {
5060
const blockParent = ((parent)) ? `${parent}.${field.uid}` : `${field.uid}`;
5161
field.blocks.forEach((block) => {
5262
if (block && block.schema && Array.isArray(block.schema)) {
@@ -110,13 +120,13 @@ exports.hasRteOrMarkdown = (schema) => {
110120
else if (field && field.field_metadata && field.field_metadata.allow_rich_text) {
111121
return true;
112122
}
113-
else if (field && field.data_type === 'group' && field.schema) {
123+
else if (field && (field.data_type === fieldType.GROUP || field.data_type === fieldType.GLOBAL_FIELD) && field.schema) {
114124
if (this.hasRteOrMarkdown(field.schema)) {
115125
return true;
116126
}
117127
continue;
118128
}
119-
else if (field && field.data_type === 'blocks' && field.blocks) {
129+
else if (field && field.data_type === fieldType.BLOCKS && field.blocks) {
120130
for (let x = 0, y = field.blocks; x < y; x++) {
121131
if (this.hasRteOrMarkdown(field.blocks[x].schema)) {
122132
return true;
@@ -143,19 +153,19 @@ const checkReferences = (schema, key) => {
143153
if (typeof schema === 'object' && Array.isArray(schema)) {
144154
for (let i = 0, j = schema.length; i < j; i++) {
145155
const field = schema[i];
146-
if (field && field.data_type === 'file' && typeof key === 'object') {
156+
if (field && field.data_type === fieldType.FILE && typeof key === 'object') {
147157
return true;
148158
}
149159
else if (field && field.reference_to) {
150160
return true;
151161
}
152-
else if (field && field.data_type === 'group' && field.schema) {
162+
else if (field && (field.data_type === fieldType.GROUP || field.data_type === fieldType.GLOBAL_FIELD) && field.schema) {
153163
if (checkReferences(field.schema, key)) {
154164
return true;
155165
}
156166
continue;
157167
}
158-
else if (field && field.data_type === 'blocks' && field.blocks) {
168+
else if (field && field.data_type === fieldType.BLOCKS && field.blocks) {
159169
for (let x = 0, y = field.blocks; x < y; x++) {
160170
if (checkReferences(field.blocks[x].schema, key)) {
161171
return true;

dist/util/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ exports.getOrSetRTEMarkdownAssets = (schema, entry, bucket = [], isFindNotReplac
354354
iterate(schema[i], entry, bucket, isFindNotReplace, parent);
355355
parent.pop();
356356
}
357-
else if (schema[i].data_type === 'group') {
357+
else if ((schema[i].data_type === 'group' || schema[i].data_type === 'global_field') && schema[i].schema) {
358358
parent.push(schema[i].uid);
359359
exports.getOrSetRTEMarkdownAssets(schema[i].schema, entry, bucket, isFindNotReplace, parent);
360360
parent.pop();

example/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const assetStore = require('@contentstack/datasync-asset-store-filesystem')
2-
const contentStore = require('@contentstack/datasync-content-store-mongodb')
2+
//const contentStore = require('@contentstack/datasync-content-store-mongodb')
3+
4+
const contentStore = require('@contentstack/datasync-content-store-filesystem')
35
const listener = require('@contentstack/webhook-listener')
46
const syncManager = require('../dist/index')
57
const config = require('./config')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@contentstack/datasync-manager",
33
"author": "Contentstack LLC <support@contentstack.com>",
4-
"version": "1.0.1",
4+
"version": "1.1.0",
55
"description": "The primary module of Contentstack DataSync. Syncs Contentstack data with your server using Contentstack Sync API",
66
"main": "dist/index.js",
77
"dependencies": {

src/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const get = (req, RETRY = 1) => {
4444
}
4545
req.method = Contentstack.verbs.get
4646
req.path = req.path || Contentstack.apis.sync
47-
4847
if (req.qs) {
4948
req.path += `?${stringify(req.qs)}`
5049
}
@@ -70,7 +69,6 @@ export const get = (req, RETRY = 1) => {
7069
.on('end', () => {
7170
debug(`status: ${response.statusCode}.`)
7271
if (response.statusCode >= 200 && response.statusCode <= 399) {
73-
7472
return resolve(JSON.parse(body))
7573
} else if (response.statusCode === 429) {
7674
timeDelay = Math.pow(Math.SQRT2, RETRY) * 200

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export const config = {
4646
options: 'ig',
4747
port: 443,
4848
protocol: 'https:',
49+
query: {
50+
include_global_field_schema: true,
51+
},
4952
regexp: {
5053
asset_pattern: {
5154
options: 'ig',

0 commit comments

Comments
 (0)