-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.js
37 lines (33 loc) · 895 Bytes
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const mongoose = require('mongoose');
const randToken = require("rand-token")
const Schema = mongoose.Schema;
const ApiSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
apiToken: {
type: String,
unique: true,
},
status:{
type: Boolean,
default: false,
},
admin: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Admin',
},
}, { versionKey: false, timestamps: true }); // versiyon tutma ama oluşturma tarihi ve update tarihlerini tut dedik.
ApiSchema.pre('save', function (next) {
let api = this;
if (!api.apiToken) api.apiToken = randToken.generate(32); //32 karakterlik bir alfasayısal token oluşturur.
next();
})
const Api = mongoose.model('Api', ApiSchema);
module.exports = Api;