Skip to content

Commit 42358ec

Browse files
committed
Add ref properties as include query parameters in pull request
1 parent 4907739 commit 42358ec

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/helpers.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
Request,
66
Transporter,
77
} from './types';
8-
import { compact, get, isEmpty, isNil, omit, omitBy } from 'lodash';
8+
import { compact, get, isEmpty, isNil, omit, omitBy, snakeCase } from 'lodash';
99

1010
export function buildUrl(parts: (number | string)[]): string {
1111
return compact(parts)
@@ -83,6 +83,9 @@ export async function executePull({
8383
deletedField,
8484
transporter,
8585
}: OrionPullExecuteOptions): Promise<any[]> {
86+
const references = extractReferences(collection.schema);
87+
const keys = Object.keys(references);
88+
8689
const request = {
8790
url,
8891
wrap,
@@ -96,27 +99,28 @@ export async function executePull({
9699
limit: batchSize,
97100
...params,
98101
},
99-
};
102+
} as any;
103+
104+
if (keys.length) {
105+
request.params.include = keys.join(',');
106+
}
100107

101108
const response = await executeRequest(transporter, request);
102109

103110
if (collection && response.length) {
104-
const primaryPath = collection.schema.primaryPath;
105-
const references = extractReferences(collection.schema);
106-
107-
for (const item of response) {
108-
if (!item[deletedField]) {
109-
for (const [key, value] of Object.entries(references)) {
110-
const rows = await executePull({
111-
url: `${url}/${item[primaryPath]}/${key}`,
112-
transporter,
113-
wrap,
114-
batchSize,
115-
deletedField,
116-
});
111+
return response;
112+
}
113+
114+
for (const item of response) {
115+
if (!item[deletedField]) {
116+
for (const [key, value] of Object.entries(references)) {
117+
const property = snakeCase(value);
118+
const reference = collection.database.collections[value];
117119

118-
const schema = collection.database.collections[value].schema;
119-
item[key] = rows.map((row) => row[schema.primaryPath]);
120+
if (reference) {
121+
item[key] = item[property].map(
122+
(row: any) => row[reference.schema.primaryPath]
123+
);
120124
}
121125
}
122126
}

tests/replication.mock.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import nock from 'nock';
33
nock('http://api.fake.push')
44
.post('/users/search')
55
.times(3)
6-
.query({ limit: 3, with_trashed: true })
6+
.query({ limit: 3, include: 'roles', with_trashed: true })
77
.reply(200, { data: [] })
88
.post('/users')
99
.reply(200, { data: { id: '1', name: 'Jeff' } })
@@ -26,8 +26,8 @@ nock('http://api.fake.pull')
2626
.query({ limit: 3, include: 'roles', with_trashed: true })
2727
.reply(200, {
2828
data: [
29-
{ id: '10', name: 'Jeff' },
30-
{ id: '11', name: 'Mark' },
29+
{ id: '10', name: 'Jeff', 'roles': ['100'] },
30+
{ id: '11', name: 'Mark', 'roles': ['200'] },
3131
],
3232
})
3333
.post('/users/search')

0 commit comments

Comments
 (0)