@takaro/db 0.1.2 → 0.1.4

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.
@@ -0,0 +1,4 @@
1
+ import { Knex } from 'knex';
2
+ export declare function up(knex: Knex): Promise<void>;
3
+ export declare function down(knex: Knex): Promise<void>;
4
+ //# sourceMappingURL=20250701-command-query-performance-indices.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"20250701-command-query-performance-indices.d.ts","sourceRoot":"","sources":["../../../src/migrations/sql/20250701-command-query-performance-indices.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,wBAAsB,EAAE,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAiClD;AAED,wBAAsB,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAepD"}
@@ -0,0 +1,42 @@
1
+ export async function up(knex) {
2
+ // Optimize the getTriggeredCommands query performance
3
+ // The query joins multiple tables and performs complex JSONB operations
4
+ // 1. Case-insensitive index on commands.trigger for efficient trigger matching
5
+ // This allows the LOWER(commands.trigger) = ? condition to use an index
6
+ await knex.raw('CREATE INDEX idx_commands_trigger_lower ON commands (LOWER(trigger))');
7
+ // 2. Index on moduleInstallations.gameserverId for efficient filtering
8
+ // The query filters by gameserverId through the join chain
9
+ await knex.schema.alterTable('moduleInstallations', (table) => {
10
+ table.index(['gameserverId'], 'idx_moduleinstallations_gameserverid');
11
+ });
12
+ // 3. Index on moduleInstallations.versionId for join optimization
13
+ // This helps the join between moduleInstallations and moduleVersions
14
+ await knex.schema.alterTable('moduleInstallations', (table) => {
15
+ table.index(['versionId'], 'idx_moduleinstallations_versionid');
16
+ });
17
+ // 4. GIN index on moduleInstallations.systemConfig for JSONB queries
18
+ // This optimizes the complex EXISTS subquery that searches command aliases
19
+ await knex.raw(`
20
+ CREATE INDEX idx_moduleinstallations_systemconfig_commands_gin
21
+ ON "moduleInstallations"
22
+ USING gin (("systemConfig" -> 'commands'))
23
+ `);
24
+ // 5. Composite index on commands for better join performance
25
+ // This helps when joining from moduleVersions to commands
26
+ await knex.schema.alterTable('commands', (table) => {
27
+ table.index(['versionId', 'domain'], 'idx_commands_versionid_domain');
28
+ });
29
+ }
30
+ export async function down(knex) {
31
+ // Drop all indices created in the up function
32
+ await knex.raw('DROP INDEX IF EXISTS idx_commands_trigger_lower');
33
+ await knex.schema.alterTable('moduleInstallations', (table) => {
34
+ table.dropIndex([], 'idx_moduleinstallations_gameserverid');
35
+ table.dropIndex([], 'idx_moduleinstallations_versionid');
36
+ });
37
+ await knex.raw('DROP INDEX IF EXISTS idx_moduleinstallations_systemconfig_commands_gin');
38
+ await knex.schema.alterTable('commands', (table) => {
39
+ table.dropIndex([], 'idx_commands_versionid_domain');
40
+ });
41
+ }
42
+ //# sourceMappingURL=20250701-command-query-performance-indices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"20250701-command-query-performance-indices.js","sourceRoot":"","sources":["../../../src/migrations/sql/20250701-command-query-performance-indices.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,KAAK,UAAU,EAAE,CAAC,IAAU;IACjC,sDAAsD;IACtD,wEAAwE;IAExE,+EAA+E;IAC/E,wEAAwE;IACxE,MAAM,IAAI,CAAC,GAAG,CAAC,sEAAsE,CAAC,CAAC;IAEvF,uEAAuE;IACvE,2DAA2D;IAC3D,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,qBAAqB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC5D,KAAK,CAAC,KAAK,CAAC,CAAC,cAAc,CAAC,EAAE,sCAAsC,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;IAEH,kEAAkE;IAClE,qEAAqE;IACrE,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,qBAAqB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC5D,KAAK,CAAC,KAAK,CAAC,CAAC,WAAW,CAAC,EAAE,mCAAmC,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;IAEH,qEAAqE;IACrE,2EAA2E;IAC3E,MAAM,IAAI,CAAC,GAAG,CAAC;;;;GAId,CAAC,CAAC;IAEH,6DAA6D;IAC7D,0DAA0D;IAC1D,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE;QACjD,KAAK,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,+BAA+B,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,IAAU;IACnC,8CAA8C;IAE9C,MAAM,IAAI,CAAC,GAAG,CAAC,iDAAiD,CAAC,CAAC;IAElE,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,qBAAqB,EAAE,CAAC,KAAK,EAAE,EAAE;QAC5D,KAAK,CAAC,SAAS,CAAC,EAAE,EAAE,sCAAsC,CAAC,CAAC;QAC5D,KAAK,CAAC,SAAS,CAAC,EAAE,EAAE,mCAAmC,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IAEH,MAAM,IAAI,CAAC,GAAG,CAAC,wEAAwE,CAAC,CAAC;IAEzF,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE;QACjD,KAAK,CAAC,SAAS,CAAC,EAAE,EAAE,+BAA+B,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@takaro/db",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "An opinionated data layer",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
@@ -0,0 +1,53 @@
1
+ import { Knex } from 'knex';
2
+
3
+ export async function up(knex: Knex): Promise<void> {
4
+ // Optimize the getTriggeredCommands query performance
5
+ // The query joins multiple tables and performs complex JSONB operations
6
+
7
+ // 1. Case-insensitive index on commands.trigger for efficient trigger matching
8
+ // This allows the LOWER(commands.trigger) = ? condition to use an index
9
+ await knex.raw('CREATE INDEX idx_commands_trigger_lower ON commands (LOWER(trigger))');
10
+
11
+ // 2. Index on moduleInstallations.gameserverId for efficient filtering
12
+ // The query filters by gameserverId through the join chain
13
+ await knex.schema.alterTable('moduleInstallations', (table) => {
14
+ table.index(['gameserverId'], 'idx_moduleinstallations_gameserverid');
15
+ });
16
+
17
+ // 3. Index on moduleInstallations.versionId for join optimization
18
+ // This helps the join between moduleInstallations and moduleVersions
19
+ await knex.schema.alterTable('moduleInstallations', (table) => {
20
+ table.index(['versionId'], 'idx_moduleinstallations_versionid');
21
+ });
22
+
23
+ // 4. GIN index on moduleInstallations.systemConfig for JSONB queries
24
+ // This optimizes the complex EXISTS subquery that searches command aliases
25
+ await knex.raw(`
26
+ CREATE INDEX idx_moduleinstallations_systemconfig_commands_gin
27
+ ON "moduleInstallations"
28
+ USING gin (("systemConfig" -> 'commands'))
29
+ `);
30
+
31
+ // 5. Composite index on commands for better join performance
32
+ // This helps when joining from moduleVersions to commands
33
+ await knex.schema.alterTable('commands', (table) => {
34
+ table.index(['versionId', 'domain'], 'idx_commands_versionid_domain');
35
+ });
36
+ }
37
+
38
+ export async function down(knex: Knex): Promise<void> {
39
+ // Drop all indices created in the up function
40
+
41
+ await knex.raw('DROP INDEX IF EXISTS idx_commands_trigger_lower');
42
+
43
+ await knex.schema.alterTable('moduleInstallations', (table) => {
44
+ table.dropIndex([], 'idx_moduleinstallations_gameserverid');
45
+ table.dropIndex([], 'idx_moduleinstallations_versionid');
46
+ });
47
+
48
+ await knex.raw('DROP INDEX IF EXISTS idx_moduleinstallations_systemconfig_commands_gin');
49
+
50
+ await knex.schema.alterTable('commands', (table) => {
51
+ table.dropIndex([], 'idx_commands_versionid_domain');
52
+ });
53
+ }