summary refs log tree commit diff
path: root/src/db/schemas/user.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/schemas/user.js')
-rw-r--r--src/db/schemas/user.js152
1 files changed, 79 insertions, 73 deletions
diff --git a/src/db/schemas/user.js b/src/db/schemas/user.js

index 69ebb02..55e2e67 100644 --- a/src/db/schemas/user.js +++ b/src/db/schemas/user.js
@@ -11,23 +11,26 @@ export const AlarmType = Object.freeze({ TOILET: 'toilet' }); -export const deviceSchema = new Schema({ - name: { - type: String, - required: true, - trim: true - }, - createdAt: { - type: Date, - default: Date.now, - immutable: true +export const deviceSchema = new Schema( + { + name: { + type: String, + required: true, + trim: true + }, + lastSeen: { + type: Date, + default: Date.now, + required: true + } }, - lastSeen: { - type: Date, - default: Date.now, - required: true + { + timestamps: { + createdAt: true, + updatedAt: false + } } -}); +); export const alarmSchema = new Schema( { @@ -49,66 +52,69 @@ export const alarmSchema = new Schema( * User schema for MongoDB. * @type {module:mongoose.Schema} */ -export const userSchema = new Schema({ - username: { - type: String, - required: true, - unique: true, - trim: true - }, - passwordHash: { - type: String, - required: true - }, - email: { - type: String, - required: true, - unique: true, - trim: true - }, - type: { - type: String, - enum: Object.values(UserType), - default: 'user' - }, - createdAt: { - type: Date, - default: Date.now, - immutable: true - }, - devices: { - type: [deviceSchema], - default: [] - }, - alarm: { - type: alarmSchema - }, - monitoredUsers: { - type: [ObjectId], - ref: 'users' - }, - balance: { - type: Number, - default: 0 - }, - spendHistory: { - type: [ObjectId], - ref: 'spendHistory' - }, - emergencyContacts: { - type: String - }, - medicalInfo: { - type: String +export const userSchema = new Schema( + { + username: { + type: String, + required: true, + unique: true, + trim: true + }, + passwordHash: { + type: String, + required: true + }, + email: { + type: String, + required: true, + unique: true, + trim: true + }, + type: { + type: String, + enum: Object.values(UserType), + default: 'user' + }, + devices: { + type: [deviceSchema], + default: [] + }, + alarm: { + type: alarmSchema + }, + monitoredUsers: { + type: [ObjectId], + ref: 'users' + }, + balance: { + type: Number, + default: 0 + }, + spendHistory: { + type: [ObjectId], + ref: 'spendHistory' + }, + emergencyContacts: { + type: String + }, + medicalInfo: { + type: String + }, + location: { + // https://stackoverflow.com/a/27218808 + // Longtitute, Latitude info + type: [Number], + index: { type: '2dsphere', sparse: true }, + count: 2 + } }, - location: { - // https://stackoverflow.com/a/27218808 - // Longtitute, Latitude info - type: [Number], - index: { type: '2dsphere', sparse: true }, - count: 2 + { + timestamps: { + createdAt: true, + updatedAt: false + } } -}); +); export const DbUser = model('user', userSchema);