Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions gateway/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,42 +147,48 @@ RETURNING entityType, scope, spend > spendingLimit as ex;`,
}

async updateProjectLimits(projectId: number, { daily, weekly, monthly }: LimitUpdate) {
const stmts = []
if (daily) {
await this.updateSpend(daily, 'project', projectId, 'daily')
stmts.push(this.updateSpend(daily, 'project', projectId, 'daily'))
}
if (weekly) {
await this.updateSpend(weekly, 'project', projectId, 'weekly')
stmts.push(this.updateSpend(weekly, 'project', projectId, 'weekly'))
}
if (monthly) {
await this.updateSpend(monthly, 'project', projectId, 'monthly')
stmts.push(this.updateSpend(monthly, 'project', projectId, 'monthly'))
}
await this.db.batch(stmts)
}

async updateUserLimits(userId: number, { daily, weekly, monthly }: LimitUpdate) {
const stmts = []
if (daily) {
await this.updateSpend(daily, 'user', userId, 'daily')
stmts.push(this.updateSpend(daily, 'user', userId, 'daily'))
}
if (weekly) {
await this.updateSpend(weekly, 'user', userId, 'weekly')
stmts.push(this.updateSpend(weekly, 'user', userId, 'weekly'))
}
if (monthly) {
await this.updateSpend(monthly, 'user', userId, 'monthly')
stmts.push(this.updateSpend(monthly, 'user', userId, 'monthly'))
}
await this.db.batch(stmts)
}

async updateKeyLimits(keyId: number, { daily, weekly, monthly, total }: KeyLimitUpdate) {
const stmts = []
if (daily) {
await this.updateSpend(daily, 'key', keyId, 'daily')
stmts.push(this.updateSpend(daily, 'key', keyId, 'daily'))
}
if (weekly) {
await this.updateSpend(weekly, 'key', keyId, 'weekly')
stmts.push(this.updateSpend(weekly, 'key', keyId, 'weekly'))
}
if (monthly) {
await this.updateSpend(monthly, 'key', keyId, 'monthly')
stmts.push(this.updateSpend(monthly, 'key', keyId, 'monthly'))
}
if (total) {
await this.updateSpend(total, 'key', keyId, 'total')
stmts.push(this.updateSpend(total, 'key', keyId, 'total'))
}
await this.db.batch(stmts)
}

async spendStatus(entityType: EntityType, entityId?: number): Promise<SpendStatus[]> {
Expand Down Expand Up @@ -218,11 +224,10 @@ WHERE entityType = ? ${entityIdClause}
}))
}

protected async updateSpend(limit: number, entityType: EntityType, entityId: number, scope: Scope) {
await this.db
protected updateSpend(limit: number, entityType: EntityType, entityId: number, scope: Scope): D1PreparedStatement {
return this.db
.prepare(`UPDATE spend SET spendingLimit = ? WHERE entityType = ? AND entityId = ? and scope = ?`)
.bind(limit, entityTypeLookup[entityType], entityId, scopeLookup[scope])
.run()
}
}

Expand Down