首页 > 编程语言 >[Node.js] mongoose schema

[Node.js] mongoose schema

时间:2022-08-22 19:35:30浏览次数:77  
标签:Node String true required mongoose password type schema

Example 1:

import mongoose from 'mongoose'

const itemSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      trim: true,
      maxlength: 50
    },
    status: {
      type: String,
      required: true,
      enum: ['active', 'complete', 'pastdue'],
      default: 'active'
    },
    notes: String,
    due: Date,
    createdBy: {
      type: mongoose.SchemaTypes.ObjectId,
      ref: 'user',
      required: true
    },
    list: {
      type: mongoose.SchemaTypes.ObjectId,
      ref: 'list',
      required: true
    }
  },
  { timestamps: true }
)

itemSchema.index({ list: 1, name: 1 }, { unique: true })

export const Item = mongoose.model('item', itemSchema)

 

Example 2:

import mongoose from 'mongoose'

const listSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      trim: true,
      maxlength: 50
    },
    description: String,
    createdBy: {
      type: mongoose.SchemaTypes.ObjectId,
      ref: 'user',
      required: true
    }
  },
  { timestamps: true }
)

listSchema.index({ user: 1, name: 1 }, { unique: true })

export const List = mongoose.model('list', listSchema)

 

Example 3:

import mongoose from 'mongoose'
import bcrypt from 'bcrypt'

const userSchema = new mongoose.Schema(
  {
    email: {
      type: String,
      required: true,
      unique: true,
      trim: true
    },

    password: {
      type: String,
      required: true
    },
    settings: {
      theme: {
        type: String,
        required: true,
        default: 'dark'
      },
      notifications: {
        type: Boolean,
        required: true,
        default: true
      },
      compactMode: {
        type: Boolean,
        required: true,
        default: false
      }
    }
  },
  { timestamps: true }
)

userSchema.pre('save', function(next) {
  if (!this.isModified('password')) {
    return next()
  }

  bcrypt.hash(this.password, 8, (err, hash) => {
    if (err) {
      return next(err)
    }

    this.password = hash
    next()
  })
})

userSchema.methods.checkPassword = function(password) {
  const passwordHash = this.password
  return new Promise((resolve, reject) => {
    bcrypt.compare(password, passwordHash, (err, same) => {
      if (err) {
        return reject(err)
      }

      resolve(same)
    })
  })
}

export const User = mongoose.model('user', userSchema)

 

标签:Node,String,true,required,mongoose,password,type,schema
From: https://www.cnblogs.com/Answer1215/p/16613984.html

相关文章

  • node的模块化导入导出
    node的模块化语法:通过module.exports导出模块,require引入模块module.exports=addletadd=require('./add') ES5的模块化语法: import引入外部模块export暴......
  • Vue3+Vite+Vant报错Uncaught SyntaxError: The requested module '/node_modules/.vit
    原因在开发过程中Vue3的依赖版本有变更,直接使用的npminstall下载新的版本,会导致node_modules下存在旧版本的缓存,从而影响了本地项目的启动编译。解决方案删除项目的......
  • node与node-sass对应版本
    (122条消息)三、第一个ElementUI登录页_PkyShare的博客-CSDN博客(122条消息)Modulebuildfailed:TypeError:this.getOptionsisnotafunction报错解决方案_YOGi......
  • 解决yarn安装node-sass失败
    第一步:更改镜像源yarnconfigsetregistryhttps://registry.npm.taobao.org-g第二步:配置node-sass的二进制包镜像地址yarnconfigsetsass_binary_sitehttps://np......
  • 使用pnpm的patch命令打补丁(正确修改源码,在外部修改node_modules代码 )
    在开发时,有时碰到依赖的类库有bug或者不满足要求时让作者改,一般不太现实和及时使用patch-package打补丁,安装依赖后自动打上修改的内容在pnpm7.4(pnpm高效npm版本管理工......
  • nodejs 读取博客园自己博客列表 生成全部标题列表
    需求自己的博文有823篇了,但是不能在一页里面显示,本来想通过MetaWeblog读取,后来发现失败了。没办法,自己写个脚本读取吧。之前MetaWeblog读取失败的文章:https://www.c......
  • WIN7支持Node14
     按照流程安装https://blog.csdn.net/qq_34235767/article/details/124465946安装好后,本机运行setNODE_SKIP_PLATFORM_CHECK=1 开启React项目 在项目文件夹......
  • babel运行nodejs
    babel运行nodejs安装依赖yarnadd@babel/core@babel/cli@babel/node@babel/preset-env-D或者全局安装yarnglobaladd@babel/core@babel/cli@babel/node@bab......
  • 在node.js中使用multer实现文件的上传
    在node.js中使用multer实现文件的上传上传图片的思路客户端--点击上传--服务器上(物理位置)数据库中的字段banner_img存放的是图片在服务器上的路径场景在使用node.js(ex......
  • monodepth学习4-训练讲解
    训练学习monodepth2的训练过程由于存在多个训练模式和网络结构导致部分比较难以理解,这里我们结合网上的资料和自己对代码的理解进行简要地介绍,个人能力有限,对计算机视觉接......