strapi-plugin-meilisearch-plus 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -863,46 +863,56 @@ const meilisearch = ({ strapi }) => ({
863
863
  }
864
864
  let indexWasJustCreated = false;
865
865
  try {
866
- const indexes = await client.getIndexes();
867
- const existingIndex = indexes.results.find((idx) => idx.uid === finalIndexName);
868
- if (!existingIndex) {
866
+ try {
867
+ strapi.log.info(`[meilisearch-plus] Creating index ${finalIndexName}`);
868
+ await client.createIndex(finalIndexName);
869
+ indexWasJustCreated = true;
870
+ await client.index(finalIndexName).updateSettings({
871
+ primaryKey: "id"
872
+ });
869
873
  strapi.log.info(
870
- `[meilisearch-plus] Index ${finalIndexName} does not exist, creating it...`
874
+ `[meilisearch-plus] Index ${finalIndexName} created with primaryKey: 'id'`
871
875
  );
872
- await client.createIndex(finalIndexName, { primaryKey: "id" });
873
- indexWasJustCreated = true;
874
- strapi.log.info(`[meilisearch-plus] ✓ Index ${finalIndexName} created with primaryKey: 'id'`);
875
- } else {
876
- const indexSettings2 = await client.index(finalIndexName).getSettings();
877
- const currentPrimaryKey = indexSettings2.primaryKey;
878
- if (currentPrimaryKey !== "id") {
879
- strapi.log.warn(
880
- `[meilisearch-plus] Index ${finalIndexName} has primaryKey: '${currentPrimaryKey}' but expected 'id'. Updating...`
876
+ } catch (createError) {
877
+ if (createError.code === "index_already_exists" || createError.message?.includes("already exists")) {
878
+ strapi.log.info(
879
+ `[meilisearch-plus] Index ${finalIndexName} already exists, checking primary key...`
881
880
  );
882
- try {
883
- await client.index(finalIndexName).updateSettings({
884
- primaryKey: "id"
885
- });
886
- strapi.log.info(
887
- `[meilisearch-plus] ✓ Updated index ${finalIndexName} primaryKey to 'id'`
888
- );
889
- } catch (updateError) {
890
- strapi.log.error(
891
- `[meilisearch-plus] Could not update primaryKey for ${finalIndexName}:`,
892
- updateError.message
893
- );
881
+ const indexSettings2 = await client.index(finalIndexName).getSettings();
882
+ const currentPrimaryKey = indexSettings2.primaryKey;
883
+ if (currentPrimaryKey !== "id") {
894
884
  strapi.log.warn(
895
- `[meilisearch-plus] Index ${finalIndexName} may need to be recreated to use primaryKey: 'id'`
885
+ `[meilisearch-plus] Index ${finalIndexName} has primaryKey: '${currentPrimaryKey}' but expected 'id'. Deleting and recreating...`
886
+ );
887
+ try {
888
+ await client.deleteIndex(finalIndexName);
889
+ strapi.log.info(`[meilisearch-plus] ✓ Deleted index ${finalIndexName}`);
890
+ await client.createIndex(finalIndexName);
891
+ await client.index(finalIndexName).updateSettings({
892
+ primaryKey: "id"
893
+ });
894
+ indexWasJustCreated = true;
895
+ strapi.log.info(
896
+ `[meilisearch-plus] ✓ Recreated index ${finalIndexName} with primaryKey: 'id'`
897
+ );
898
+ } catch (recreateError) {
899
+ strapi.log.error(
900
+ `[meilisearch-plus] Could not recreate index ${finalIndexName}:`,
901
+ recreateError.message
902
+ );
903
+ throw recreateError;
904
+ }
905
+ } else {
906
+ strapi.log.info(
907
+ `[meilisearch-plus] Index ${finalIndexName} exists with correct primaryKey: 'id'`
896
908
  );
897
909
  }
898
910
  } else {
899
- strapi.log.info(
900
- `[meilisearch-plus] Index ${finalIndexName} exists with correct primaryKey: 'id'`
901
- );
911
+ throw createError;
902
912
  }
903
913
  }
904
914
  } catch (error) {
905
- strapi.log.error(`[meilisearch-plus] Error checking/creating index:`, error);
915
+ strapi.log.error(`[meilisearch-plus] Error ensuring index ${finalIndexName}:`, error);
906
916
  throw error;
907
917
  }
908
918
  const index2 = client.index(finalIndexName);
@@ -923,8 +933,7 @@ const meilisearch = ({ strapi }) => ({
923
933
  let taskUids = [];
924
934
  for (let i = 0; i < sanitized.length; i += this.BATCH_SIZE) {
925
935
  const batch = sanitized.slice(i, i + this.BATCH_SIZE);
926
- const addOptions = indexWasJustCreated ? { primaryKey: "id" } : {};
927
- const response = await index2.addDocuments(batch, addOptions);
936
+ const response = await index2.addDocuments(batch);
928
937
  strapi.log.info(
929
938
  `[meilisearch-plus] Added batch of ${batch.length} documents for ${contentType} (Task uid: ${response.taskUid})`
930
939
  );
@@ -862,46 +862,56 @@ const meilisearch = ({ strapi }) => ({
862
862
  }
863
863
  let indexWasJustCreated = false;
864
864
  try {
865
- const indexes = await client.getIndexes();
866
- const existingIndex = indexes.results.find((idx) => idx.uid === finalIndexName);
867
- if (!existingIndex) {
865
+ try {
866
+ strapi.log.info(`[meilisearch-plus] Creating index ${finalIndexName}`);
867
+ await client.createIndex(finalIndexName);
868
+ indexWasJustCreated = true;
869
+ await client.index(finalIndexName).updateSettings({
870
+ primaryKey: "id"
871
+ });
868
872
  strapi.log.info(
869
- `[meilisearch-plus] Index ${finalIndexName} does not exist, creating it...`
873
+ `[meilisearch-plus] Index ${finalIndexName} created with primaryKey: 'id'`
870
874
  );
871
- await client.createIndex(finalIndexName, { primaryKey: "id" });
872
- indexWasJustCreated = true;
873
- strapi.log.info(`[meilisearch-plus] ✓ Index ${finalIndexName} created with primaryKey: 'id'`);
874
- } else {
875
- const indexSettings2 = await client.index(finalIndexName).getSettings();
876
- const currentPrimaryKey = indexSettings2.primaryKey;
877
- if (currentPrimaryKey !== "id") {
878
- strapi.log.warn(
879
- `[meilisearch-plus] Index ${finalIndexName} has primaryKey: '${currentPrimaryKey}' but expected 'id'. Updating...`
875
+ } catch (createError) {
876
+ if (createError.code === "index_already_exists" || createError.message?.includes("already exists")) {
877
+ strapi.log.info(
878
+ `[meilisearch-plus] Index ${finalIndexName} already exists, checking primary key...`
880
879
  );
881
- try {
882
- await client.index(finalIndexName).updateSettings({
883
- primaryKey: "id"
884
- });
885
- strapi.log.info(
886
- `[meilisearch-plus] ✓ Updated index ${finalIndexName} primaryKey to 'id'`
887
- );
888
- } catch (updateError) {
889
- strapi.log.error(
890
- `[meilisearch-plus] Could not update primaryKey for ${finalIndexName}:`,
891
- updateError.message
892
- );
880
+ const indexSettings2 = await client.index(finalIndexName).getSettings();
881
+ const currentPrimaryKey = indexSettings2.primaryKey;
882
+ if (currentPrimaryKey !== "id") {
893
883
  strapi.log.warn(
894
- `[meilisearch-plus] Index ${finalIndexName} may need to be recreated to use primaryKey: 'id'`
884
+ `[meilisearch-plus] Index ${finalIndexName} has primaryKey: '${currentPrimaryKey}' but expected 'id'. Deleting and recreating...`
885
+ );
886
+ try {
887
+ await client.deleteIndex(finalIndexName);
888
+ strapi.log.info(`[meilisearch-plus] ✓ Deleted index ${finalIndexName}`);
889
+ await client.createIndex(finalIndexName);
890
+ await client.index(finalIndexName).updateSettings({
891
+ primaryKey: "id"
892
+ });
893
+ indexWasJustCreated = true;
894
+ strapi.log.info(
895
+ `[meilisearch-plus] ✓ Recreated index ${finalIndexName} with primaryKey: 'id'`
896
+ );
897
+ } catch (recreateError) {
898
+ strapi.log.error(
899
+ `[meilisearch-plus] Could not recreate index ${finalIndexName}:`,
900
+ recreateError.message
901
+ );
902
+ throw recreateError;
903
+ }
904
+ } else {
905
+ strapi.log.info(
906
+ `[meilisearch-plus] Index ${finalIndexName} exists with correct primaryKey: 'id'`
895
907
  );
896
908
  }
897
909
  } else {
898
- strapi.log.info(
899
- `[meilisearch-plus] Index ${finalIndexName} exists with correct primaryKey: 'id'`
900
- );
910
+ throw createError;
901
911
  }
902
912
  }
903
913
  } catch (error) {
904
- strapi.log.error(`[meilisearch-plus] Error checking/creating index:`, error);
914
+ strapi.log.error(`[meilisearch-plus] Error ensuring index ${finalIndexName}:`, error);
905
915
  throw error;
906
916
  }
907
917
  const index2 = client.index(finalIndexName);
@@ -922,8 +932,7 @@ const meilisearch = ({ strapi }) => ({
922
932
  let taskUids = [];
923
933
  for (let i = 0; i < sanitized.length; i += this.BATCH_SIZE) {
924
934
  const batch = sanitized.slice(i, i + this.BATCH_SIZE);
925
- const addOptions = indexWasJustCreated ? { primaryKey: "id" } : {};
926
- const response = await index2.addDocuments(batch, addOptions);
935
+ const response = await index2.addDocuments(batch);
927
936
  strapi.log.info(
928
937
  `[meilisearch-plus] Added batch of ${batch.length} documents for ${contentType} (Task uid: ${response.taskUid})`
929
938
  );
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.1",
2
+ "version": "0.2.3",
3
3
  "keywords": [],
4
4
  "type": "commonjs",
5
5
  "exports": {