Skip to content

Instantly share code, notes, and snippets.

@Mifrill
Last active October 18, 2023 19:18
Show Gist options
  • Save Mifrill/90de68b449dda780dcac271bb6f12f9c to your computer and use it in GitHub Desktop.
Save Mifrill/90de68b449dda780dcac271bb6f12f9c to your computer and use it in GitHub Desktop.
mongoose-schema-type_encrypted-string
import mongoose from 'mongoose'
import { Cryptography } from './cryptography'
export class EncryptedString extends mongoose.SchemaType {
constructor(key: string, options: Record<string, any>) {
options.get = (value: any): string => new Cryptography().decrypt(value)
super(key, options, 'EncryptedString')
}
cast(value: any): any {
return new Cryptography().encrypt(value)
}
}
mongoose.Schema.Types.EncryptedString = EncryptedString
@Mifrill
Copy link
Author

Mifrill commented Oct 18, 2023

import { Schema, model } from 'mongoose'

import { EncryptedString } from 'db/types/EncryptedString'

const clientConfigSchema: Schema = new Schema(
  {
    secretProperty: EncryptedString,
  }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment