Created
July 20, 2021 15:43
-
-
Save guilhermeKodama/7571f61062fc35b0a385147eb14cf112 to your computer and use it in GitHub Desktop.
Mongoose Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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