strapi-plugin-i-relate-to-this 0.3.3 → 0.4.1

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,36 @@ 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
+ ]);
106
124
  const relationsInTable = await strapi.db.getConnection(tableName).where({
107
- [parentIdColumn]: id,
125
+ [inverseJoinColumnName]: id,
108
126
  ...type === "relation" ? {} : {
109
127
  component_type: parent.uid
110
128
  }
@@ -118,11 +136,11 @@ const search = async (id, status, parent) => {
118
136
  return mergeWith__default.default(prev, {
119
137
  [model.uid]: {
120
138
  info: model.info,
121
- items: [item[relationIdColumn]]
139
+ items: [item[joinColumnName]]
122
140
  }
123
141
  }, customizer);
124
142
  }
125
- const next = await search(item[relationIdColumn], status, model);
143
+ const next = await search(item[joinColumnName], status, model);
126
144
  return mergeWith__default.default(prev, next, customizer);
127
145
  }, {});
128
146
  return mergeWith__default.default(await acc, await itemsForThisCollection, customizer);
@@ -89,18 +89,36 @@ 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
+ ]);
102
120
  const relationsInTable = await strapi.db.getConnection(tableName).where({
103
- [parentIdColumn]: id,
121
+ [inverseJoinColumnName]: id,
104
122
  ...type === "relation" ? {} : {
105
123
  component_type: parent.uid
106
124
  }
@@ -114,11 +132,11 @@ const search = async (id, status, parent) => {
114
132
  return mergeWith(prev, {
115
133
  [model.uid]: {
116
134
  info: model.info,
117
- items: [item[relationIdColumn]]
135
+ items: [item[joinColumnName]]
118
136
  }
119
137
  }, customizer);
120
138
  }
121
- const next = await search(item[relationIdColumn], status, model);
139
+ const next = await search(item[joinColumnName], status, model);
122
140
  return mergeWith(prev, next, customizer);
123
141
  }, {});
124
142
  return mergeWith(await acc, await itemsForThisCollection, customizer);
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.3.3",
2
+ "version": "0.4.1",
3
3
  "keywords": [],
4
4
  "type": "commonjs",
5
5
  "exports": {