Skip to content

Instantly share code, notes, and snippets.

@guilhermeKodama
Created July 20, 2021 15:43
Show Gist options
  • Save guilhermeKodama/7571f61062fc35b0a385147eb14cf112 to your computer and use it in GitHub Desktop.
Save guilhermeKodama/7571f61062fc35b0a385147eb14cf112 to your computer and use it in GitHub Desktop.
Mongoose Example
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable func-names */
import mongoose from 'mongoose'
mongoose.Promise = global.Promise
const MONGO_URL = '{{uri}}'
const USER = 'user'
const PASSWORD = 'pass'
const options = {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
promiseLibrary: global.Promise,
auth: {
user: USER,
password: PASSWORD
}
}
mongoose.connection.on('connected', () => {
console.debug('[Mongoose] connected!')
})
mongoose.connection.on('error', (error) => {
console.error('[Mongoose] error: ', error)
})
mongoose.connection.on('disconnected', () => {
console.debug('[Mongoose] disconnected!')
})
// const { ObjectId } = mongoose.Types
// const { Mixed } = mongoose.Schema.Types
const Schema = new mongoose.Schema(
{
patientId: String,
fap: String,
password: String
},
{
strict: false,
timestamps: true
}
)
Schema.pre('save', async function(next) {
const data = this
data.set('patientId', `${data.get('fap') + data.get('password')}`)
next()
})
const Model = mongoose.model('CacheSisluFap', Schema, 'cacheSisluFap')
beforeAll(async () => {
await mongoose.connect(MONGO_URL, options)
})
test('insert item', async () => {
await Model.create({ fap: '796100918370', password: '123', teste: 'sou um teste' })
const data = await Model.find({ })
console.log('data:', data)
expect(true).toBe(true)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment