1- import { Schema } from 'mongoose' ;
1+ import mongoose , { Schema , Model , InferSchemaType } from 'mongoose' ;
22
3- export const apiKeySchema = new Schema (
3+ interface IApiKey {
4+ label : string ;
5+ lastUsedAt : Date ;
6+ hashedKey : string ;
7+ }
8+
9+ interface ApiKeyVirtuals {
10+ id : string ;
11+ }
12+
13+ type ApiKeyModelType = Model < IApiKey , { } , { } , ApiKeyVirtuals > ;
14+
15+ interface MongooseTimestamps {
16+ createdAt : Date ;
17+ updatedAt : Date ;
18+ }
19+
20+ interface SanitisedApiKey
21+ extends Omit < IApiKey , 'hashedKey' > ,
22+ ApiKeyVirtuals ,
23+ Pick < MongooseTimestamps , 'createdAt' > { }
24+
25+ export const apiKeySchema = new Schema <
26+ IApiKey ,
27+ ApiKeyModelType ,
28+ { } ,
29+ { } ,
30+ ApiKeyVirtuals
31+ > (
432 {
533 label : { type : String , default : 'API Key' } ,
634 lastUsedAt : { type : Date } ,
@@ -18,7 +46,7 @@ apiKeySchema.virtual('id').get(function getApiKeyId() {
1846 * should never be exposed to the client. So we only return
1947 * a safe list of fields when toObject and toJSON are called.
2048 */
21- function apiKeyMetadata ( doc , ret , options ) {
49+ function apiKeyMetadata ( doc , ret , options ) : SanitisedApiKey {
2250 return {
2351 id : doc . id ,
2452 label : doc . label ,
@@ -35,3 +63,9 @@ apiKeySchema.set('toJSON', {
3563 virtuals : true ,
3664 transform : apiKeyMetadata
3765} ) ;
66+
67+ // Derived type for schema fields (including timestamps)
68+ type ApiKeySchemaType = InferSchemaType < typeof apiKeySchema > ;
69+
70+ // The actual document type
71+ // export type ApiKeyDocument = Document & ApiKeySchemaFields & ApiKeyVirtuals;
0 commit comments