Skip to content

Commit d85b212

Browse files
committed
Add disableGetMany, should fix #84
1 parent 55803a0 commit d85b212

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ const config: ImportConfig = {
111111
disableCreateMany?: boolean,
112112
// Disable the attempt to use "updateMany", will instead just use "update" calls
113113
disableUpdateMany?: boolean,
114+
// Disable the attempt to use "getMany", will instead just use "getSingle" calls
115+
disableGetMany?: boolean,
114116
// Disable "import new" button
115117
disableImportNew?: boolean;
116118
// Disable "import overwrite" button
@@ -152,6 +154,7 @@ Your dataprovider will need to implement the `.createMany()` method in order to
152154
| ----------------- | ------------------ | --------------- |
153155
| Creating from CSV | .createMany() | .create() |
154156
| Updating from CSV | .updateManyArray() | .update() |
157+
| Checking which exist | .getMany() | .getSingle() |
155158

156159
*Note: You can disable this feature setting `disableCreateMany: true` or `disableUpdateMany: true` in the configuration.*
157160
### Interfaces

demo/src/posts.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const ListActions = (props) => {
4343
},
4444
// disableImportNew: true,
4545
// disableImportOverwrite: true,
46+
// disableGetMany: true,
4647
};
4748
return (
4849
<TopToolbar className={className}>

src/config.interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export interface ImportConfig {
77
disableCreateMany?: boolean,
88
// Disable the attempt to use "updateMany", will instead just use "update" calls
99
disableUpdateMany?: boolean,
10+
// Disable the attempt to use "getMany", will instead just use "getOne" calls
11+
disableGetMany?: boolean,
1012
// Disable "import new" button
1113
disableImportNew?: boolean;
1214
// Disable "import overwrite" button

src/import-controller.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export async function GetIdsColliding(
1515
dataProvider: DataProvider,
1616
csvValues: any[],
1717
resourceName: string,
18+
disableGetMany: boolean | undefined,
1819
): Promise<Identifier[]> {
1920
const logger = makeLogger(logging);
2021
const hasIds = csvValues.some((v) => v.id);
@@ -23,13 +24,65 @@ export async function GetIdsColliding(
2324
}
2425
try {
2526
const csvIds: Identifier[] = csvValues.filter(v => !!v.id).map((v) => v.id);
27+
const recordsIdsColliding = await (disableGetMany
28+
? GetIdsCollidingGetSingle( logging, translate, dataProvider, csvIds, resourceName )
29+
: GetIdsCollidingGetMany( logging, translate, dataProvider, csvIds, resourceName )
30+
);
31+
return recordsIdsColliding;
32+
} catch (error) {
33+
logger.error("GetIdsColliding", { csvValues }, error);
34+
throw translate("csv.parsing.collidingIds");
35+
}
36+
}
37+
38+
export async function GetIdsCollidingGetSingle(
39+
logging: boolean,
40+
translate: Translate,
41+
dataProvider: DataProvider,
42+
csvIds: Identifier[],
43+
resourceName: string,
44+
): Promise<Identifier[]> {
45+
const logger = makeLogger(logging);
46+
try {
47+
const recordsColliding = await Promise.all(csvIds.map(id => IsIdColliding(
48+
dataProvider,
49+
id,
50+
resourceName,
51+
)));
52+
const recordIdsColliding = recordsColliding.filter(Boolean) as Identifier[];
53+
return recordIdsColliding;
54+
} catch (error) {
55+
logger.error("GetIdsCollidingGetSingle", { csvIds }, error);
56+
throw translate("csv.parsing.collidingIds");
57+
}
58+
}
59+
60+
export async function IsIdColliding(
61+
dataProvider: DataProvider,
62+
id: Identifier,
63+
resourceName: string,
64+
) {
65+
return dataProvider.getOne(resourceName, {id})
66+
.then(_ => undefined)
67+
.catch(_ => id);
68+
}
69+
70+
export async function GetIdsCollidingGetMany(
71+
logging: boolean,
72+
translate: Translate,
73+
dataProvider: DataProvider,
74+
csvIds: Identifier[],
75+
resourceName: string,
76+
): Promise<Identifier[]> {
77+
const logger = makeLogger(logging);
78+
try {
2679
const recordsColliding = await dataProvider.getMany(resourceName, {
2780
ids: csvIds,
2881
});
2982
const recordIdsColliding = recordsColliding.data.map((r) => r.id);
3083
return recordIdsColliding;
3184
} catch (error) {
32-
logger.error("GetIdsColliding", { csvValues }, error);
85+
logger.error("GetIdsCollidingGetMany", { csvIds }, error);
3386
throw translate("csv.parsing.collidingIds");
3487
}
3588
}

src/main-csv-button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const MainCsvImport = (props: any) => {
2727
transformRows,
2828
disableCreateMany,
2929
disableUpdateMany,
30+
disableGetMany,
3031
disableImportNew,
3132
disableImportOverwrite,
3233
} = props as ImportConfig;
@@ -90,7 +91,8 @@ export const MainCsvImport = (props: any) => {
9091
translate,
9192
dataProvider,
9293
csvItems,
93-
resourceName
94+
resourceName,
95+
disableGetMany,
9496
);
9597
mounted && setIdsConflicting(collidingIds);
9698
const hasCollidingIds = !!collidingIds.length;

0 commit comments

Comments
 (0)