1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-08 07:00:04 +00:00

fix: use custom logic for handling sequence ids

This commit is contained in:
2023-05-01 19:28:51 +05:00
parent d2f011e8a9
commit dba53de646
9 changed files with 76 additions and 93 deletions

View File

@@ -1,7 +1,6 @@
import mongoose, { Schema, model, Document, Model } from 'mongoose'
const AutoIncrement = require('mongoose-sequence')(mongoose)
import { Schema, model, Document, Model } from 'mongoose'
import bcrypt from 'bcryptjs'
import { AuthProviderType } from '../utils'
import { AuthProviderType, getSequenceNextValue } from '../utils'
export interface UserPayload {
/**
@@ -66,6 +65,10 @@ const userSchema = new Schema<IUserDocument>({
required: true,
unique: true
},
id: {
type: Number,
unique: true
},
password: {
type: String,
required: true
@@ -107,7 +110,16 @@ const userSchema = new Schema<IUserDocument>({
}
]
})
userSchema.plugin(AutoIncrement, { inc_field: 'id' })
// userSchema.plugin(AutoIncrement, { inc_field: 'id' })
// Hooks
userSchema.pre('save', async function (next) {
if (this.isNew) {
this.id = await getSequenceNextValue('id')
}
next()
})
// Static Methods
userSchema.static('hashPassword', (password: string): string => {