The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
import type { BucketVersioningFormValues } from '../FilesBucket.schema'
import type { ExpirationMode } from '@/components/interfaces/Storage/StorageVersioning.constants'
import type { BucketLifecycle } from '@/data/storage/bucket-lifecycle-query'
import type { BucketLifecycleRuleInput } from '@/data/storage/bucket-lifecycle-update-mutation'
/** Lets a round trip recognize the rules the dashboard wrote, rather than guess from shape. */
export const AGE_RULE_ID = 'supabase-noncurrent-age'
export const COUNT_RULE_ID = 'supabase-noncurrent-count'
/**
* S3 honors `newer_noncurrent_versions` only alongside `noncurrent_days`, so "either
* condition" becomes two rules, the second pinned at the shortest age the API accepts.
*/
export const MIN_NONCURRENT_DAYS = 1
export interface LifecycleFormPolicy {
versionExpiryDays: number | null
maxNoncurrentVersions: number | null
expirationMode: ExpirationMode
}
export const toLifecycleRules = (
values: Pick<
BucketVersioningFormValues,
'enable_versioning' | 'version_expiry_days' | 'max_noncurrent_versions' | 'expiration_mode'
>
): BucketLifecycleRuleInput[] => {
const days = values.version_expiry_days === '' ? null : values.version_expiry_days
const versions = values.max_noncurrent_versions === '' ? null : values.max_noncurrent_versions
if (days === null && versions === null) return []
if (days !== null && versions !== null && values.expiration_mode === 'or') {
return [
{
id: AGE_RULE_ID,
status: 'Enabled',
filter: {},
noncurrent_version_expiration: { noncurrent_days: days },
},
{
id: COUNT_RULE_ID,
status: 'Enabled',
filter: {},
noncurrent_version_expiration: {
noncurrent_days: MIN_NONCURRENT_DAYS,
newer_noncurrent_versions: versions,
},
},
]
}
// The form's validation already rejects a count with no age.
if (days === null) return []
return [
{
id: AGE_RULE_ID,
status: 'Enabled',
filter: {},
noncurrent_version_expiration: {
noncurrent_days: days,
...(versions === null ? {} : { newer_noncurrent_versions: versions }),
},
},
]
}
type LifecycleRule = BucketLifecycle['rules'][number]
const isModeledRule = (rule: LifecycleRule) =>
rule.status === 'Enabled' && rule.noncurrent_version_expiration !== undefined
const isCappedRule = (rule: LifecycleRule) =>
rule.noncurrent_version_expiration?.newer_noncurrent_versions !== undefined
/** Keeps a save that changed nothing from rewriting the policy. */
export const hasLifecyclePolicyChanged = (
stored: LifecycleFormPolicy,
values: Pick<
BucketVersioningFormValues,
'version_expiry_days' | 'max_noncurrent_versions' | 'expiration_mode'
>
) => {
const days = values.version_expiry_days === '' ? null : values.version_expiry_days
const versions = values.max_noncurrent_versions === '' ? null : values.max_noncurrent_versions
if (stored.versionExpiryDays !== days) return true
if (stored.maxNoncurrentVersions !== versions) return true
// The mode only means anything when both conditions are set.
return days !== null && versions !== null && stored.expirationMode !== values.expiration_mode
}
/**
* The update endpoint replaces the whole policy, so anything `fromLifecycleRules` had to
* skip would be dropped by a save. Splitting an age rule from a capped one is the only
* multi-rule shape the form writes; a policy set elsewhere can hold more.
*/
export const hasUnsupportedLifecycleRules = (lifecycle?: BucketLifecycle | null) => {
const rules = lifecycle?.rules ?? []
if (!rules.every(isModeledRule)) return true
if (rules.length <= 1) return false
return rules.length > 2 || rules.filter(isCappedRule).length !== 1
}
/** Rules this can't model — extra, disabled, or non-expiration — are ignored, not guessed at. */
export const fromLifecycleRules = (lifecycle?: BucketLifecycle | null): LifecycleFormPolicy => {
const enabledRules = (lifecycle?.rules ?? []).filter(isModeledRule)
if (enabledRules.length === 0) {
return { versionExpiryDays: null, maxNoncurrentVersions: null, expirationMode: 'and' }
}
const ageOnlyRule = enabledRules.find((rule) => !isCappedRule(rule))
const cappedRule = enabledRules.find(isCappedRule)
if (ageOnlyRule !== undefined && cappedRule !== undefined) {
return {
versionExpiryDays: ageOnlyRule.noncurrent_version_expiration?.noncurrent_days ?? null,
maxNoncurrentVersions:
cappedRule.noncurrent_version_expiration?.newer_noncurrent_versions ?? null,
expirationMode: 'or',
}
}
const rule = cappedRule ?? ageOnlyRule
return {
versionExpiryDays: rule?.noncurrent_version_expiration?.noncurrent_days ?? null,
maxNoncurrentVersions: rule?.noncurrent_version_expiration?.newer_noncurrent_versions ?? null,
expirationMode: 'and',
}
}