|
1 | 1 | import { Prisma } from "@/generated/prisma/client"; |
2 | | -import { Config, getInvalidConfigReason, normalize, override } from "@stackframe/stack-shared/dist/config/format"; |
| 2 | +import { Config, getInvalidConfigReason, normalize, override, removeKeysFromConfig } from "@stackframe/stack-shared/dist/config/format"; |
3 | 3 | import { BranchConfigOverride, BranchConfigOverrideOverride, BranchIncompleteConfig, BranchRenderedConfig, CompleteConfig, EnvironmentConfigOverride, EnvironmentConfigOverrideOverride, EnvironmentIncompleteConfig, EnvironmentRenderedConfig, OrganizationConfigOverride, OrganizationConfigOverrideOverride, OrganizationIncompleteConfig, ProjectConfigOverride, ProjectConfigOverrideOverride, ProjectIncompleteConfig, ProjectRenderedConfig, applyBranchDefaults, applyEnvironmentDefaults, applyOrganizationDefaults, applyProjectDefaults, assertNoConfigOverrideErrors, branchConfigSchema, environmentConfigSchema, getConfigOverrideErrors, getIncompleteConfigWarnings, migrateConfigOverride, organizationConfigSchema, projectConfigSchema, sanitizeBranchConfig, sanitizeEnvironmentConfig, sanitizeOrganizationConfig, sanitizeProjectConfig } from "@stackframe/stack-shared/dist/config/schema"; |
4 | 4 | import { ProjectsCrud } from "@stackframe/stack-shared/dist/interface/crud/projects"; |
5 | 5 | import { branchConfigSourceSchema, yupBoolean, yupMixed, yupObject, yupRecord, yupString, yupUnion } from "@stackframe/stack-shared/dist/schema-fields"; |
@@ -457,6 +457,75 @@ export function overrideOrganizationConfigOverride(options: { |
457 | 457 | } |
458 | 458 |
|
459 | 459 |
|
| 460 | +// --------------------------------------------------------------------------------------------------------------------- |
| 461 | +// reset functions (remove specific keys from config override) |
| 462 | +// --------------------------------------------------------------------------------------------------------------------- |
| 463 | +// Uses the same nested key logic as the `override` function: resetting key "a.b" also resets "a.b.c". |
| 464 | + |
| 465 | +export async function resetProjectConfigOverrideKeys(options: { |
| 466 | + projectId: string, |
| 467 | + keysToReset: string[], |
| 468 | +}): Promise<void> { |
| 469 | + // TODO put this in a serializable transaction (or a single SQL query) to prevent race conditions |
| 470 | + const oldConfig = await rawQuery(globalPrismaClient, getProjectConfigOverrideQuery(options)); |
| 471 | + const newConfig = removeKeysFromConfig(oldConfig, options.keysToReset); |
| 472 | + |
| 473 | + await setProjectConfigOverride({ |
| 474 | + projectId: options.projectId, |
| 475 | + projectConfigOverride: newConfig as ProjectConfigOverride, |
| 476 | + }); |
| 477 | +} |
| 478 | + |
| 479 | +export async function resetBranchConfigOverrideKeys(options: { |
| 480 | + projectId: string, |
| 481 | + branchId: string, |
| 482 | + keysToReset: string[], |
| 483 | +}): Promise<void> { |
| 484 | + // TODO put this in a serializable transaction (or a single SQL query) to prevent race conditions |
| 485 | + const oldConfig = await rawQuery(globalPrismaClient, getBranchConfigOverrideQuery(options)); |
| 486 | + const newConfig = removeKeysFromConfig(oldConfig, options.keysToReset); |
| 487 | + |
| 488 | + await setBranchConfigOverride({ |
| 489 | + projectId: options.projectId, |
| 490 | + branchId: options.branchId, |
| 491 | + branchConfigOverride: newConfig as BranchConfigOverride, |
| 492 | + }); |
| 493 | +} |
| 494 | + |
| 495 | +export async function resetEnvironmentConfigOverrideKeys(options: { |
| 496 | + projectId: string, |
| 497 | + branchId: string, |
| 498 | + keysToReset: string[], |
| 499 | +}): Promise<void> { |
| 500 | + // TODO put this in a serializable transaction (or a single SQL query) to prevent race conditions |
| 501 | + const oldConfig = await rawQuery(globalPrismaClient, getEnvironmentConfigOverrideQuery(options)); |
| 502 | + const newConfig = removeKeysFromConfig(oldConfig, options.keysToReset); |
| 503 | + |
| 504 | + await setEnvironmentConfigOverride({ |
| 505 | + projectId: options.projectId, |
| 506 | + branchId: options.branchId, |
| 507 | + environmentConfigOverride: newConfig as EnvironmentConfigOverride, |
| 508 | + }); |
| 509 | +} |
| 510 | + |
| 511 | +export async function resetOrganizationConfigOverrideKeys(options: { |
| 512 | + projectId: string, |
| 513 | + branchId: string, |
| 514 | + organizationId: string | null, |
| 515 | + keysToReset: string[], |
| 516 | +}): Promise<void> { |
| 517 | + // TODO put this in a serializable transaction (or a single SQL query) to prevent race conditions |
| 518 | + const oldConfig = await rawQuery(globalPrismaClient, getOrganizationConfigOverrideQuery(options)); |
| 519 | + const newConfig = removeKeysFromConfig(oldConfig, options.keysToReset); |
| 520 | + |
| 521 | + await setOrganizationConfigOverride({ |
| 522 | + projectId: options.projectId, |
| 523 | + branchId: options.branchId, |
| 524 | + organizationId: options.organizationId, |
| 525 | + organizationConfigOverride: newConfig as OrganizationConfigOverride, |
| 526 | + }); |
| 527 | +} |
| 528 | + |
460 | 529 | // --------------------------------------------------------------------------------------------------------------------- |
461 | 530 | // internal functions |
462 | 531 | // --------------------------------------------------------------------------------------------------------------------- |
|
0 commit comments