@fourlights/strapi-plugin-deep-populate 1.2.1 → 1.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.
@@ -65,12 +65,12 @@ const schema$1 = {
65
65
  type: "json",
66
66
  configurable: false
67
67
  },
68
- dependencies: { type: "string", configurable: false }
68
+ dependencies: { type: "text", configurable: false }
69
69
  },
70
70
  // experimental feature:
71
71
  indexes: [
72
72
  {
73
- name: "deep-populate_cache_hash_index",
73
+ name: "caches_hash_idx",
74
74
  columns: ["hash"],
75
75
  type: "unique"
76
76
  }
@@ -78,7 +78,76 @@ const schema$1 = {
78
78
  };
79
79
  const cache$1 = { schema: schema$1 };
80
80
  const contentTypes$1 = { cache: cache$1 };
81
+ async function hasDeepPopulateCacheFullTextIndex(db, tableName, columnName) {
82
+ const knex = db.connection;
83
+ const client = db.dialect.client;
84
+ if (client === "sqlite") {
85
+ return (await db.dialect.schemaInspector.getTables()).includes(`${tableName}_fts`);
86
+ }
87
+ if (client === "mysql" || client === "mysql2") {
88
+ return (await db.dialect.schemaInspector.getIndexes(tableName)).find(
89
+ ({ name }) => name === `${tableName}_${columnName}_fulltext`
90
+ ) !== void 0;
91
+ }
92
+ if (client === "pg" || client === "postgres") {
93
+ const result = await knex.raw(
94
+ `SELECT * FROM pg_indexes WHERE tablename = '${tableName}' AND indexname = '${tableName}_${columnName}_gin'`
95
+ );
96
+ return result.rows.length > 0;
97
+ }
98
+ console.log(`Full-text index not supported for this database engine (${client})`);
99
+ return false;
100
+ }
101
+ async function addDeepPopulateCacheFullTextIndex(db, tableName, columnName) {
102
+ const knex = db.connection;
103
+ const hasTable = await knex.schema.hasTable(tableName);
104
+ if (!hasTable) return;
105
+ const hasColumn = await knex.schema.hasColumn(tableName, columnName);
106
+ if (!hasColumn) return;
107
+ const client = db.dialect.client;
108
+ if (client === "sqlite") {
109
+ await knex.raw(`CREATE VIRTUAL TABLE ${tableName}_fts USING fts3(${columnName})`);
110
+ await knex.raw(`INSERT INTO ${tableName}_fts (${columnName}) SELECT ${columnName} FROM ${tableName}`);
111
+ } else if (client === "mysql" || client === "mysql2") {
112
+ await knex.raw(`ALTER TABLE ${tableName} ADD FULLTEXT INDEX ${tableName}_${columnName}_fulltext (${columnName})`);
113
+ } else if (client === "pg" || client === "postgres") {
114
+ await knex.raw(
115
+ `CREATE INDEX ${tableName}_${columnName}_gin ON ${tableName} USING GIN (to_tsvector('english', ${columnName}))`
116
+ );
117
+ } else {
118
+ console.log(`Full-text index not supported for this database engine (${client})`);
119
+ }
120
+ }
121
+ async function removeDeepPopulateCacheFullTextIndex(db, tableName, columnName) {
122
+ const knex = db.connection;
123
+ const hasTable = await knex.schema.hasTable(tableName);
124
+ if (!hasTable) return;
125
+ const hasColumn = await knex.schema.hasColumn(tableName, columnName);
126
+ if (!hasColumn) return;
127
+ const client = db.dialect.client;
128
+ if (client === "sqlite") {
129
+ await knex.raw(`DROP TABLE ${tableName}_fts`);
130
+ } else if (client === "mysql" || client === "mysql2") {
131
+ await knex.raw(`ALTER TABLE ${tableName} DROP INDEX ${tableName}_${columnName}_fulltext`);
132
+ } else if (client === "pg" || client === "postgres") {
133
+ await knex.raw(`DROP INDEX ${tableName}_${columnName}_gin`);
134
+ } else {
135
+ console.log(`Full-text index not supported for this database engine (${client})`);
136
+ }
137
+ }
81
138
  const register = async ({ strapi: strapi2 }) => {
139
+ strapi2.hook("strapi::content-types.afterSync").register(async () => {
140
+ const tableName = "caches";
141
+ const columnName = "dependencies";
142
+ const hasIndex = await hasDeepPopulateCacheFullTextIndex(strapi2.db, tableName, columnName);
143
+ const hasTable = await strapi2.db.connection.schema.hasTable(tableName);
144
+ const hasColumn = hasTable && await strapi2.db.connection.schema.hasColumn(tableName, columnName);
145
+ const cacheIsEnabled = strapi2.config.get("plugin::deep-populate").cachePopulate === true;
146
+ const shouldCreateIndex = cacheIsEnabled && hasTable && hasColumn && !hasIndex;
147
+ const shouldRemoveIndex = hasIndex && (!cacheIsEnabled || !hasTable || !hasColumn);
148
+ if (shouldCreateIndex) await addDeepPopulateCacheFullTextIndex(strapi2.db, tableName, columnName);
149
+ if (shouldRemoveIndex) await removeDeepPopulateCacheFullTextIndex(strapi2.db, tableName, columnName);
150
+ });
82
151
  strapi2.documents.use(async (context, next) => {
83
152
  const { cachePopulate, augmentPopulateStar } = strapi2.config.get("plugin::deep-populate");
84
153
  if (
@@ -51,12 +51,12 @@ const schema$1 = {
51
51
  type: "json",
52
52
  configurable: false
53
53
  },
54
- dependencies: { type: "string", configurable: false }
54
+ dependencies: { type: "text", configurable: false }
55
55
  },
56
56
  // experimental feature:
57
57
  indexes: [
58
58
  {
59
- name: "deep-populate_cache_hash_index",
59
+ name: "caches_hash_idx",
60
60
  columns: ["hash"],
61
61
  type: "unique"
62
62
  }
@@ -64,7 +64,76 @@ const schema$1 = {
64
64
  };
65
65
  const cache$1 = { schema: schema$1 };
66
66
  const contentTypes$1 = { cache: cache$1 };
67
+ async function hasDeepPopulateCacheFullTextIndex(db, tableName, columnName) {
68
+ const knex = db.connection;
69
+ const client = db.dialect.client;
70
+ if (client === "sqlite") {
71
+ return (await db.dialect.schemaInspector.getTables()).includes(`${tableName}_fts`);
72
+ }
73
+ if (client === "mysql" || client === "mysql2") {
74
+ return (await db.dialect.schemaInspector.getIndexes(tableName)).find(
75
+ ({ name }) => name === `${tableName}_${columnName}_fulltext`
76
+ ) !== void 0;
77
+ }
78
+ if (client === "pg" || client === "postgres") {
79
+ const result = await knex.raw(
80
+ `SELECT * FROM pg_indexes WHERE tablename = '${tableName}' AND indexname = '${tableName}_${columnName}_gin'`
81
+ );
82
+ return result.rows.length > 0;
83
+ }
84
+ console.log(`Full-text index not supported for this database engine (${client})`);
85
+ return false;
86
+ }
87
+ async function addDeepPopulateCacheFullTextIndex(db, tableName, columnName) {
88
+ const knex = db.connection;
89
+ const hasTable = await knex.schema.hasTable(tableName);
90
+ if (!hasTable) return;
91
+ const hasColumn = await knex.schema.hasColumn(tableName, columnName);
92
+ if (!hasColumn) return;
93
+ const client = db.dialect.client;
94
+ if (client === "sqlite") {
95
+ await knex.raw(`CREATE VIRTUAL TABLE ${tableName}_fts USING fts3(${columnName})`);
96
+ await knex.raw(`INSERT INTO ${tableName}_fts (${columnName}) SELECT ${columnName} FROM ${tableName}`);
97
+ } else if (client === "mysql" || client === "mysql2") {
98
+ await knex.raw(`ALTER TABLE ${tableName} ADD FULLTEXT INDEX ${tableName}_${columnName}_fulltext (${columnName})`);
99
+ } else if (client === "pg" || client === "postgres") {
100
+ await knex.raw(
101
+ `CREATE INDEX ${tableName}_${columnName}_gin ON ${tableName} USING GIN (to_tsvector('english', ${columnName}))`
102
+ );
103
+ } else {
104
+ console.log(`Full-text index not supported for this database engine (${client})`);
105
+ }
106
+ }
107
+ async function removeDeepPopulateCacheFullTextIndex(db, tableName, columnName) {
108
+ const knex = db.connection;
109
+ const hasTable = await knex.schema.hasTable(tableName);
110
+ if (!hasTable) return;
111
+ const hasColumn = await knex.schema.hasColumn(tableName, columnName);
112
+ if (!hasColumn) return;
113
+ const client = db.dialect.client;
114
+ if (client === "sqlite") {
115
+ await knex.raw(`DROP TABLE ${tableName}_fts`);
116
+ } else if (client === "mysql" || client === "mysql2") {
117
+ await knex.raw(`ALTER TABLE ${tableName} DROP INDEX ${tableName}_${columnName}_fulltext`);
118
+ } else if (client === "pg" || client === "postgres") {
119
+ await knex.raw(`DROP INDEX ${tableName}_${columnName}_gin`);
120
+ } else {
121
+ console.log(`Full-text index not supported for this database engine (${client})`);
122
+ }
123
+ }
67
124
  const register = async ({ strapi: strapi2 }) => {
125
+ strapi2.hook("strapi::content-types.afterSync").register(async () => {
126
+ const tableName = "caches";
127
+ const columnName = "dependencies";
128
+ const hasIndex = await hasDeepPopulateCacheFullTextIndex(strapi2.db, tableName, columnName);
129
+ const hasTable = await strapi2.db.connection.schema.hasTable(tableName);
130
+ const hasColumn = hasTable && await strapi2.db.connection.schema.hasColumn(tableName, columnName);
131
+ const cacheIsEnabled = strapi2.config.get("plugin::deep-populate").cachePopulate === true;
132
+ const shouldCreateIndex = cacheIsEnabled && hasTable && hasColumn && !hasIndex;
133
+ const shouldRemoveIndex = hasIndex && (!cacheIsEnabled || !hasTable || !hasColumn);
134
+ if (shouldCreateIndex) await addDeepPopulateCacheFullTextIndex(strapi2.db, tableName, columnName);
135
+ if (shouldRemoveIndex) await removeDeepPopulateCacheFullTextIndex(strapi2.db, tableName, columnName);
136
+ });
68
137
  strapi2.documents.use(async (context, next) => {
69
138
  const { cachePopulate, augmentPopulateStar } = strapi2.config.get("plugin::deep-populate");
70
139
  if (
@@ -0,0 +1,4 @@
1
+ import type { Database } from "@strapi/database";
2
+ export declare function hasDeepPopulateCacheFullTextIndex(db: Database, tableName: string, columnName: string): Promise<boolean>;
3
+ export declare function addDeepPopulateCacheFullTextIndex(db: Database, tableName: string, columnName: string): Promise<void>;
4
+ export declare function removeDeepPopulateCacheFullTextIndex(db: Database, tableName: string, columnName: string): Promise<void>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.2.1",
2
+ "version": "1.2.3",
3
3
  "keywords": [
4
4
  "strapi",
5
5
  "strapi-plugin",