const Model = require('../mongoose/model');

class User extends Model {

    static get $options() {
        return {strict: false};
    };

    static get $visible() {
        return ['_id', 'name', 'username', 'email', 'avatar'];
    }

    static get $schema() {
        return {
            username: {type: String, index: true, sparse: true},
            email: {type: String, index: true, sparse: true},
            password: {type: String},
            avatar_etag: {type: String},
            name: {type: String},
            is_banned: {type: Boolean},
            meta: {type: Object},
            roles: {type: Array},
            sessions: [
                {
                    agent: {type: String},
                    ip: {type: String}
                }
            ]
        };
    }

    async logout(session) {
        if (session) this.sessions.remove(session);
        else this.sessions = [];

        return this.save()
    }

}

module.exports = User;

@@ -325,32 +325,46 @@
 x: true, sparse:
+ true, unique:
  true%7D,%0A        
@@ -405,24 +405,38 @@
 rue, sparse:
+ true, unique:
  true%7D,%0A    

Make username & email sparse-unique