tryaii-mcp-server
Version:
TryAII MCP Server - 15+ AI models with comparison, cost tracking, and collective intelligence
80 lines • 1.99 kB
JavaScript
import mongoose, { Schema } from 'mongoose';
const UserApiKeySchema = new Schema({
userId: {
type: String,
required: true,
index: true
},
keyId: {
type: String,
required: true,
unique: true,
index: true
},
keyHash: {
type: String,
required: true,
unique: true
},
keyPrefix: {
type: String,
required: true
},
name: {
type: String,
required: true,
trim: true,
maxlength: 100
},
permissions: {
type: [String],
required: true,
default: ['chat'],
validate: {
validator: function (permissions) {
const validPermissions = ['chat', 'compare', 'models', 'usage', 'brains'];
return permissions.every(p => validPermissions.includes(p));
},
message: 'Invalid permission specified'
}
},
isActive: {
type: Boolean,
default: true,
index: true
},
lastUsed: {
type: Date
},
usageStats: {
totalRequests: {
type: Number,
default: 0,
min: 0
},
totalCost: {
type: Number,
default: 0,
min: 0,
get: (v) => parseFloat(v.toFixed(6))
},
lastResetDate: {
type: Date,
default: Date.now
}
},
expiresAt: {
type: Date
}
}, {
timestamps: true,
versionKey: false
});
// Compound indexes for performance
UserApiKeySchema.index({ userId: 1, isActive: 1 });
UserApiKeySchema.index({ userId: 1, createdAt: -1 });
// TTL index for automatic expiration
UserApiKeySchema.index({ expiresAt: 1 }, { expireAfterSeconds: 0 });
// Don't recreate the model if it already exists
export const UserApiKey = mongoose.models.UserApiKey || mongoose.model('UserApiKey', UserApiKeySchema);
//# sourceMappingURL=UserApiKey.js.map