strapi-plugin-i-relate-to-this 0.3.2 → 0.4.0

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.
@@ -93,18 +93,37 @@ const search = async (id, status, parent) => {
93
93
  collectionName,
94
94
  modelName
95
95
  } = model;
96
- const relationIdColumn = type === "relation" ? strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(
96
+ const {
97
+ getJoinColumnAttributeIdName,
98
+ getInverseJoinColumnAttributeIdName,
99
+ getNameFromTokens,
100
+ getName
101
+ } = strapi.db.metadata.identifiers;
102
+ const joinColumnName = type === "relation" ? getJoinColumnAttributeIdName(
97
103
  snakeCase__default.default(modelName)
98
- ) : "entity_id";
99
- let parentIdColumn = type === "relation" ? strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(
104
+ ) : getNameFromTokens([
105
+ { name: "entity", compressible: false },
106
+ { name: "id", compressible: false }
107
+ ]);
108
+ let inverseJoinColumnName = type === "relation" ? getJoinColumnAttributeIdName(
100
109
  snakeCase__default.default(parent.modelName)
101
- ) : "cmp_id";
102
- if (parentIdColumn === relationIdColumn) {
103
- parentIdColumn = `inv_${parentIdColumn}`;
110
+ ) : getNameFromTokens([
111
+ { name: "component", shortName: "cmp", compressible: false },
112
+ { name: "id", compressible: false }
113
+ ]);
114
+ if (joinColumnName === inverseJoinColumnName) {
115
+ inverseJoinColumnName = getInverseJoinColumnAttributeIdName(
116
+ snakeCase__default.default(parent.modelName)
117
+ );
104
118
  }
105
- const tableName = type === "relation" ? `${snakeCase__default.default(`${collectionName} ${key} lnk`)}` : `${collectionName}_cmps`;
119
+ const tableName = type === "relation" ? getName([snakeCase__default.default(collectionName), snakeCase__default.default(key)], { suffix: "links" }) : getNameFromTokens([
120
+ { name: collectionName, compressible: true },
121
+ // not snaked cased!
122
+ { name: "components", shortName: "cmps", compressible: false }
123
+ ]);
124
+ console.log(`getting ${joinColumnName} for ${inverseJoinColumnName} ${id} from ${tableName}`);
106
125
  const relationsInTable = await strapi.db.getConnection(tableName).where({
107
- [parentIdColumn]: id,
126
+ [inverseJoinColumnName]: id,
108
127
  ...type === "relation" ? {} : {
109
128
  component_type: parent.uid
110
129
  }
@@ -118,11 +137,11 @@ const search = async (id, status, parent) => {
118
137
  return mergeWith__default.default(prev, {
119
138
  [model.uid]: {
120
139
  info: model.info,
121
- items: [item[relationIdColumn]]
140
+ items: [item[joinColumnName]]
122
141
  }
123
142
  }, customizer);
124
143
  }
125
- const next = await search(item[relationIdColumn], status, model);
144
+ const next = await search(item[joinColumnName], status, model);
126
145
  return mergeWith__default.default(prev, next, customizer);
127
146
  }, {});
128
147
  return mergeWith__default.default(await acc, await itemsForThisCollection, customizer);
@@ -89,18 +89,37 @@ const search = async (id, status, parent) => {
89
89
  collectionName,
90
90
  modelName
91
91
  } = model;
92
- const relationIdColumn = type === "relation" ? strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(
92
+ const {
93
+ getJoinColumnAttributeIdName,
94
+ getInverseJoinColumnAttributeIdName,
95
+ getNameFromTokens,
96
+ getName
97
+ } = strapi.db.metadata.identifiers;
98
+ const joinColumnName = type === "relation" ? getJoinColumnAttributeIdName(
93
99
  snakeCase(modelName)
94
- ) : "entity_id";
95
- let parentIdColumn = type === "relation" ? strapi.db.metadata.identifiers.getJoinColumnAttributeIdName(
100
+ ) : getNameFromTokens([
101
+ { name: "entity", compressible: false },
102
+ { name: "id", compressible: false }
103
+ ]);
104
+ let inverseJoinColumnName = type === "relation" ? getJoinColumnAttributeIdName(
96
105
  snakeCase(parent.modelName)
97
- ) : "cmp_id";
98
- if (parentIdColumn === relationIdColumn) {
99
- parentIdColumn = `inv_${parentIdColumn}`;
106
+ ) : getNameFromTokens([
107
+ { name: "component", shortName: "cmp", compressible: false },
108
+ { name: "id", compressible: false }
109
+ ]);
110
+ if (joinColumnName === inverseJoinColumnName) {
111
+ inverseJoinColumnName = getInverseJoinColumnAttributeIdName(
112
+ snakeCase(parent.modelName)
113
+ );
100
114
  }
101
- const tableName = type === "relation" ? `${snakeCase(`${collectionName} ${key} lnk`)}` : `${collectionName}_cmps`;
115
+ const tableName = type === "relation" ? getName([snakeCase(collectionName), snakeCase(key)], { suffix: "links" }) : getNameFromTokens([
116
+ { name: collectionName, compressible: true },
117
+ // not snaked cased!
118
+ { name: "components", shortName: "cmps", compressible: false }
119
+ ]);
120
+ console.log(`getting ${joinColumnName} for ${inverseJoinColumnName} ${id} from ${tableName}`);
102
121
  const relationsInTable = await strapi.db.getConnection(tableName).where({
103
- [parentIdColumn]: id,
122
+ [inverseJoinColumnName]: id,
104
123
  ...type === "relation" ? {} : {
105
124
  component_type: parent.uid
106
125
  }
@@ -114,11 +133,11 @@ const search = async (id, status, parent) => {
114
133
  return mergeWith(prev, {
115
134
  [model.uid]: {
116
135
  info: model.info,
117
- items: [item[relationIdColumn]]
136
+ items: [item[joinColumnName]]
118
137
  }
119
138
  }, customizer);
120
139
  }
121
- const next = await search(item[relationIdColumn], status, model);
140
+ const next = await search(item[joinColumnName], status, model);
122
141
  return mergeWith(prev, next, customizer);
123
142
  }, {});
124
143
  return mergeWith(await acc, await itemsForThisCollection, customizer);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.3.2",
2
+ "version": "0.4.0",
3
3
  "keywords": [],
4
4
  "type": "commonjs",
5
5
  "exports": {
@@ -25,7 +25,7 @@
25
25
  "watch": "strapi-plugin watch",
26
26
  "watch:link": "strapi-plugin watch:link",
27
27
  "verify": "strapi-plugin verify",
28
- "prepublish": "npm run build"
28
+ "prepublishOnly": "npm run build"
29
29
  },
30
30
  "dependencies": {
31
31
  "@strapi/design-system": "^2.0.0-rc.30",