All files / libs/kernel/bootstrap/database/src/lib partitions.config.ts

14.03% Statements 48/342
100% Branches 0/0
0% Functions 0/7
14.03% Lines 48/342

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 3431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                         1x 1x 1x                                                                                           1x 1x 1x                                                                                                       1x 1x 1x                                         1x 1x 1x                                                                                             1x 1x 1x                                                                                                         1x 1x 1x                                         1x 1x  
import { MetricMetadataType } from '@amalia/core/models';
import { FormatsEnum } from '@amalia/data-capture/fields/types';
import { CurrencySymbolsEnum } from '@amalia/ext/iso-4217';
import { RuleType, RuleTypeWithAdjustment } from '@amalia/payout-definition/plans/types';
 
interface PartitionConfiguration {
  createMasterTableSql: (tableName: string) => string;
}
 
export const dataEnums = {
  rule_metric_ruletype_enum: { ...RuleType, ...RuleTypeWithAdjustment },
  rule_metric_currency_enum: CurrencySymbolsEnum,
  record_metric_ruletype_enum: { ...RuleType, ...RuleTypeWithAdjustment },
  record_metric_currency_enum: CurrencySymbolsEnum,
  metric_metadata_metadatatype_enum: MetricMetadataType,
  metric_metadata_format_enum: FormatsEnum,
};
 
/**
 * Partition definition for each table.
 *
 * We have to write it by hand because it's not supported by typeorm.
 *
 * Follow https://github.com/typeorm/typeorm/issues/9620
 */
export const partitionsConfig: Record<string, PartitionConfiguration> = {
  custom_object: {
    createMasterTableSql: (tableName: string) => `
      CREATE TABLE public.${tableName} (
          id             uuid      default uuid_generate_v4()                         not null,
          content        jsonb                                                        not null,
          "createdAt"    timestamp default ('now'::text)::timestamp without time zone not null,
          "updatedAt"    timestamp default ('now'::text)::timestamp without time zone not null,
          "definitionId" uuid,
          "companyId"    uuid,
          "externalId"   varchar                                                      not null,
          name           varchar(500),
          url            varchar,
          CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId"),
          constraint "UQ_${tableName}_EXTERNALID"
              unique ("companyId", "definitionId", "externalId")
      ) PARTITION BY LIST ("companyId");

      CREATE INDEX "${tableName}-externalId"
        ON public.${tableName} ("externalId");

      CREATE INDEX ${tableName}_company
        ON public.${tableName} ("companyId");

      CREATE INDEX ${tableName}_content_accountid
        ON public.${tableName} ((content ->> 'accountId'::text));

      CREATE INDEX ${tableName}_content_closedate
        ON public.${tableName} ((content ->> 'closedate'::text));

      CREATE INDEX ${tableName}_content_entity
        ON public.${tableName} ((content ->> 'entity'::text));

      CREATE INDEX ${tableName}_content_hs_object_id
        ON public.${tableName} ((content ->> 'hsObjectId'::text));

      CREATE INDEX ${tableName}_content_hsownerid
        ON public.${tableName} ((content ->> 'hubspotOwnerId'::text));

      CREATE INDEX ${tableName}_content_id
        ON public.${tableName} ((content ->> 'id'::text));

      CREATE INDEX ${tableName}_content_opportunityid
        ON public.${tableName} ((content ->> 'opportunityId'::text));

      CREATE INDEX ${tableName}_content_ownerid
        ON public.${tableName} ((content ->> 'ownerId'::text));

      CREATE INDEX ${tableName}_content_trandisplayname
        ON public.${tableName} ((content ->> 'trandisplayname'::text));

      CREATE INDEX ${tableName}_definition
        ON public.${tableName} ("definitionId");

      CREATE INDEX ${tableName}_content_name
        ON public.${tableName} ((content ->> 'name'::text));

      CREATE INDEX "${tableName}_companyOrAccount"
        ON public.${tableName} ((content ->> 'companyOrAccount'::text));

      CREATE INDEX idx_updated_at_${tableName}
        ON public.${tableName} ("updatedAt");
    `,
  },
  rule_metric: {
    createMasterTableSql: (tableName: string) => `
      create table public.${tableName} (
        id uuid not null default uuid_generate_v4(),
        "createdAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "updatedAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "companyId" uuid not null,
        "statementId" uuid not null,
        "statementReviewed" boolean not null,
        "periodId" uuid not null,
        "periodName" character varying not null,
        "periodStartDate" timestamp without time zone not null,
        "periodEndDate" timestamp without time zone not null,
        "planId" uuid not null,
        "planName" character varying not null,
        "userId" uuid not null,
        "teamId" character varying,
        "teamName" character varying,
        "ruleId" uuid,
        "ruleName" character varying,
        "ruleType" rule_metric_ruletype_enum not null,
        "ruleDescription" character varying,
        data jsonb not null,
        currency rule_metric_currency_enum not null,
        rate double precision not null,
        "adjustmentId" uuid,
        total double precision not null,
        "userEmail" character varying not null,
        "userExternalId" character varying,
        "userFirstName" character varying,
        "userLastName" character varying,
        "userHrisId" character varying,
        "parentTeamAssignments" jsonb,
        "directTeamAssignments" jsonb,
        CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId")
      ) PARTITION BY LIST ("companyId");

      create index "IDX_${tableName}_companyId" on ${tableName} ("companyId");
      create index "IDX_${tableName}_statementId" on ${tableName} ("statementId");
      create index "IDX_${tableName}_statementReviewed" on ${tableName} ("statementReviewed");
      create index "IDX_${tableName}_periodId" on ${tableName} ("periodId");
      create index "IDX_${tableName}_planId" on ${tableName} ("planId");
      create index "IDX_${tableName}_userId" on ${tableName} ("userId");
      create index "IDX_${tableName}_userExternalId" on ${tableName} ("userExternalId");
      create index "IDX_${tableName}_ruleId" on ${tableName} ("ruleId");
      create index idx_updated_at_${tableName} on ${tableName} ("updatedAt");
    `,
  },
  record_metric: {
    createMasterTableSql: (tableName) => `
      create table public.${tableName} (
        id uuid not null default uuid_generate_v4(),
        "createdAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "updatedAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "companyId" uuid not null,
        "statementId" uuid not null,
        "statementReviewed" boolean not null,
        "periodId" uuid not null,
        "periodName" character varying not null,
        "periodStartDate" timestamp without time zone not null,
        "periodEndDate" timestamp without time zone not null,
        "planId" uuid not null,
        "planName" character varying not null,
        "userId" uuid not null,
        "teamId" character varying,
        "teamName" character varying,
        "ruleId" uuid,
        "ruleName" character varying,
        "ruleType" record_metric_ruletype_enum not null,
        "ruleDescription" character varying,
        data jsonb not null,
        currency record_metric_currency_enum not null,
        rate double precision not null,
        commission double precision not null,
        "rowExternalId" character varying,
        "rowName" character varying,
        "customObjectDefinitionMachineName" character varying,
        "userEmail" character varying not null,
        "userExternalId" character varying,
        "userFirstName" character varying,
        "userLastName" character varying,
        "userHrisId" character varying,
        "parentTeamAssignments" jsonb,
        "directTeamAssignments" jsonb,
        "filterId" character varying,
        "filterMachineName" character varying,
        "filterName" character varying,
        CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId")
      ) PARTITION BY LIST ("companyId");

      create index "IDX_${tableName}_companyId" on ${tableName} ("companyId");
      create index "IDX_${tableName}_statementId" on ${tableName} ("statementId");
      create index "IDX_${tableName}_statementReviewed" on ${tableName} ("statementReviewed");
      create index "IDX_${tableName}_periodId" on ${tableName} ("periodId");
      create index "IDX_${tableName}_planId" on ${tableName} ("planId");
      create index "IDX_${tableName}_userId" on ${tableName} ("userId");
      create index "IDX_${tableName}_userExternalId" on ${tableName} ("userExternalId");
      create index "IDX_${tableName}_ruleId" on ${tableName} ("ruleId");
      create index "IDX_${tableName}_customObjectDefinitionMachineName" on ${tableName} ("customObjectDefinitionMachineName");
      create index idx_updated_at_${tableName} on ${tableName} ("updatedAt");
    `,
  },
  metric_metadata: {
    createMasterTableSql: (tableName) => `
      create table public.${tableName} (
        id uuid not null default uuid_generate_v4(),
        "createdAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "updatedAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "companyId" uuid not null,
        "metadataType" metric_metadata_metadatatype_enum not null,
        "customObjectDefinitionMachineName" character varying not null,
        key character varying not null,
        label character varying not null,
        format metric_metadata_format_enum not null,
        "planIds" jsonb NOT NULL DEFAULT '[]',
        "teamIds" jsonb NOT NULL DEFAULT '[]',
        "periodIds" jsonb NOT NULL DEFAULT '[]',
        CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId")
      ) PARTITION BY LIST ("companyId");

      create index idx_updated_at_${tableName} on ${tableName} ("updatedAt");
      create unique index "UQ_${tableName}_KEY_PER_COMPANY"
          on ${tableName} ("companyId", key, "customObjectDefinitionMachineName", format);
      `,
  },
  rule_metric_forecast: {
    createMasterTableSql: (tableName: string) => `
      create table public.${tableName} (
        id uuid not null default uuid_generate_v4(),
        "createdAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "updatedAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "companyId" uuid not null,
        "statementId" uuid not null,
        "statementReviewed" boolean not null,
        "periodId" uuid not null,
        "periodName" character varying not null,
        "periodStartDate" timestamp without time zone not null,
        "periodEndDate" timestamp without time zone not null,
        "planId" uuid not null,
        "planName" character varying not null,
        "userId" uuid not null,
        "teamId" character varying,
        "teamName" character varying,
        "ruleId" uuid,
        "ruleName" character varying,
        "ruleType" rule_metric_ruletype_enum not null,
        "ruleDescription" character varying,
        data jsonb not null,
        currency rule_metric_currency_enum not null,
        rate double precision not null,
        "adjustmentId" uuid,
        total double precision not null,
        "userEmail" character varying not null,
        "userExternalId" character varying,
        "userFirstName" character varying,
        "userLastName" character varying,
        "userHrisId" character varying,
        "statementForecastId" uuid not null,
        "parentTeamAssignments" jsonb,
        "directTeamAssignments" jsonb,
        CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId")
      ) PARTITION BY LIST ("companyId");

      create index "IDX_${tableName}_companyId" on ${tableName} ("companyId");
      create index "IDX_${tableName}_statementId" on ${tableName} ("statementId");
      create index "IDX_${tableName}_statementReviewed" on ${tableName} ("statementReviewed");
      create index "IDX_${tableName}_periodId" on ${tableName} ("periodId");
      create index "IDX_${tableName}_planId" on ${tableName} ("planId");
      create index "IDX_${tableName}_userId" on ${tableName} ("userId");
      create index "IDX_${tableName}_userExternalId" on ${tableName} ("userExternalId");
      create index "IDX_${tableName}_ruleId" on ${tableName} ("ruleId");
      create index idx_updated_at_${tableName} on ${tableName} ("updatedAt");
    `,
  },
  record_metric_forecast: {
    createMasterTableSql: (tableName) => `
      create table public.${tableName} (
        id uuid not null default uuid_generate_v4(),
        "createdAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "updatedAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "companyId" uuid not null,
        "statementId" uuid not null,
        "statementReviewed" boolean not null,
        "periodId" uuid not null,
        "periodName" character varying not null,
        "periodStartDate" timestamp without time zone not null,
        "periodEndDate" timestamp without time zone not null,
        "planId" uuid not null,
        "planName" character varying not null,
        "userId" uuid not null,
        "teamId" character varying,
        "teamName" character varying,
        "ruleId" uuid,
        "ruleName" character varying,
        "ruleType" record_metric_ruletype_enum not null,
        "ruleDescription" character varying,
        data jsonb not null,
        currency record_metric_currency_enum not null,
        rate double precision not null,
        commission double precision not null,
        "rowExternalId" character varying,
        "rowName" character varying,
        "customObjectDefinitionMachineName" character varying,
        "userEmail" character varying not null,
        "userExternalId" character varying,
        "userFirstName" character varying,
        "userLastName" character varying,
        "userHrisId" character varying,
        "statementForecastId" uuid not null,
        "parentTeamAssignments" jsonb,
        "directTeamAssignments" jsonb,
        "filterId" character varying,
        "filterMachineName" character varying,
        "filterName" character varying,
        CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId")
      ) PARTITION BY LIST ("companyId");

      create index "IDX_${tableName}_companyId" on ${tableName} ("companyId");
      create index "IDX_${tableName}_statementId" on ${tableName} ("statementId");
      create index "IDX_${tableName}_statementReviewed" on ${tableName} ("statementReviewed");
      create index "IDX_${tableName}_periodId" on ${tableName} ("periodId");
      create index "IDX_${tableName}_planId" on ${tableName} ("planId");
      create index "IDX_${tableName}_userId" on ${tableName} ("userId");
      create index "IDX_${tableName}_userExternalId" on ${tableName} ("userExternalId");
      create index "IDX_${tableName}_ruleId" on ${tableName} ("ruleId");
      create index "IDX_${tableName}_customObjectDefinitionMachineName" on ${tableName} ("customObjectDefinitionMachineName");
      create index idx_updated_at_${tableName} on ${tableName} ("updatedAt");
    `,
  },
  metric_metadata_forecast: {
    createMasterTableSql: (tableName) => `
      create table public.${tableName} (
        id uuid not null default uuid_generate_v4(),
        "createdAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "updatedAt" timestamp without time zone not null default ('now'::text)::timestamp without time zone,
        "companyId" uuid not null,
        "metadataType" metric_metadata_metadatatype_enum not null,
        "customObjectDefinitionMachineName" character varying not null,
        key character varying not null,
        label character varying not null,
        format metric_metadata_format_enum not null,
        "planIds" jsonb NOT NULL DEFAULT '[]',
        "teamIds" jsonb NOT NULL DEFAULT '[]',
        "periodIds" jsonb NOT NULL DEFAULT '[]',
        CONSTRAINT "PK_${tableName}" PRIMARY KEY ("id", "companyId")
      ) PARTITION BY LIST ("companyId");

      create index idx_updated_at_${tableName} on ${tableName} ("updatedAt");
      create unique index "UQ_${tableName}_KEY_PER_COMPANY"
          on ${tableName} ("companyId", key, "customObjectDefinitionMachineName", format);
      `,
  },
};