Skip to content

Commit 7909ebd

Browse files
Refactor (readability): move public methods to top of SplitgraphImportPlugin in base-import-plugin`
1 parent c06ea72 commit 7909ebd

File tree

1 file changed

+86
-86
lines changed

1 file changed

+86
-86
lines changed

packages/db-splitgraph/plugins/importers/base-import-plugin.ts

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,65 @@ export abstract class SplitgraphImportPlugin<
128128
return new (Object.getPrototypeOf(this).constructor)(mergedInjectOpts);
129129
}
130130

131+
public async importData(
132+
rawSourceOptions: ConcreteImportSourceOptions,
133+
rawDestOptions: ConcreteImportDestOptions
134+
) {
135+
const {
136+
sourceOptions = rawSourceOptions,
137+
destOptions = rawDestOptions,
138+
...importCtx
139+
} = await this.beforeImport(rawSourceOptions, rawDestOptions);
140+
141+
const {
142+
response: loadResponse,
143+
error: loadError,
144+
info: loadInfo,
145+
} = await this.startLoad(sourceOptions, destOptions);
146+
147+
if (loadError || !loadResponse) {
148+
return {
149+
response: null,
150+
error: loadError,
151+
info: { ...importCtx.info, ...loadInfo },
152+
};
153+
}
154+
155+
const { taskId } = loadResponse.startExternalRepositoryLoad;
156+
157+
const { response: statusResponse, error: statusError } =
158+
await this.waitForTask(taskId, destOptions);
159+
160+
const lastKnownJobStatus = statusResponse?.jobStatus.status;
161+
162+
const info = {
163+
...importCtx.info,
164+
...statusResponse,
165+
};
166+
167+
if (lastKnownJobStatus != TaskStatus.Success) {
168+
return {
169+
response: {
170+
success: false,
171+
},
172+
error: {
173+
success: false,
174+
pending: lastKnownJobStatus && taskUnresolved(lastKnownJobStatus),
175+
...statusError,
176+
},
177+
info,
178+
};
179+
}
180+
181+
return {
182+
response: {
183+
success: true,
184+
},
185+
error: null,
186+
info,
187+
};
188+
}
189+
131190
/**
132191
* Return the params and tables variable for the load
133192
*/
@@ -136,6 +195,33 @@ export abstract class SplitgraphImportPlugin<
136195
destOptions: ConcreteImportDestOptions
137196
): ProvidedExternalLoadMutationVariables;
138197

198+
// TODO: Clean this up to use an intermediate private member inside the derived class
199+
// instead of passing some magical object through some magical pipeline
200+
201+
/**
202+
* Derived classes should implement this method to perform any pre-import steps,
203+
* such as uploading a CSV file to object storage. It should return sourceOptions
204+
* and destOptions if they are mutated in the process.
205+
*/
206+
protected async beforeImport(
207+
sourceOptions: ConcreteImportSourceOptions,
208+
destOptions: ConcreteImportDestOptions
209+
): Promise<{
210+
response: null | Response;
211+
error: unknown;
212+
info: object;
213+
sourceOptions: ConcreteImportSourceOptions;
214+
destOptions: ConcreteImportDestOptions;
215+
}> {
216+
return Promise.resolve({
217+
sourceOptions,
218+
destOptions,
219+
info: {},
220+
response: null,
221+
error: null,
222+
});
223+
}
224+
139225
// TODO: preview step should return available table names
140226

141227
private async startLoad(
@@ -369,92 +455,6 @@ export abstract class SplitgraphImportPlugin<
369455
},
370456
};
371457
}
372-
373-
// TODO: Clean this up to use an intermediate private member inside the derived class
374-
// instead of passing some magical object through some magical pipeline
375-
376-
/**
377-
* Derived classes should implement this method to perform any pre-import steps,
378-
* such as uploading a CSV file to object storage. It should return sourceOptions
379-
* and destOptions if they are mutated in the process.
380-
*/
381-
protected async beforeImport(
382-
sourceOptions: ConcreteImportSourceOptions,
383-
destOptions: ConcreteImportDestOptions
384-
): Promise<{
385-
response: null | Response;
386-
error: unknown;
387-
info: object;
388-
sourceOptions: ConcreteImportSourceOptions;
389-
destOptions: ConcreteImportDestOptions;
390-
}> {
391-
return Promise.resolve({
392-
sourceOptions,
393-
destOptions,
394-
info: {},
395-
response: null,
396-
error: null,
397-
});
398-
}
399-
400-
public async importData(
401-
rawSourceOptions: ConcreteImportSourceOptions,
402-
rawDestOptions: ConcreteImportDestOptions
403-
) {
404-
const {
405-
sourceOptions = rawSourceOptions,
406-
destOptions = rawDestOptions,
407-
...importCtx
408-
} = await this.beforeImport(rawSourceOptions, rawDestOptions);
409-
410-
const {
411-
response: loadResponse,
412-
error: loadError,
413-
info: loadInfo,
414-
} = await this.startLoad(sourceOptions, destOptions);
415-
416-
if (loadError || !loadResponse) {
417-
return {
418-
response: null,
419-
error: loadError,
420-
info: { ...importCtx.info, ...loadInfo },
421-
};
422-
}
423-
424-
const { taskId } = loadResponse.startExternalRepositoryLoad;
425-
426-
const { response: statusResponse, error: statusError } =
427-
await this.waitForTask(taskId, destOptions);
428-
429-
const lastKnownJobStatus = statusResponse?.jobStatus.status;
430-
431-
const info = {
432-
...importCtx.info,
433-
...statusResponse,
434-
};
435-
436-
if (lastKnownJobStatus != TaskStatus.Success) {
437-
return {
438-
response: {
439-
success: false,
440-
},
441-
error: {
442-
success: false,
443-
pending: lastKnownJobStatus && taskUnresolved(lastKnownJobStatus),
444-
...statusError,
445-
},
446-
info,
447-
};
448-
}
449-
450-
return {
451-
response: {
452-
success: true,
453-
},
454-
error: null,
455-
info,
456-
};
457-
}
458458
}
459459

460460
const IdentityFunc = <T>(x: T) => x;

0 commit comments

Comments
 (0)