<commit_before>var Joi = require('joi'),
    P = require('bluebird'),
    _ = require('lodash');

module.exports = function(server, config, log) {
    "use strict";

    var dbName = config.get('plugins:restmod:dbname','MAIN');
    var random = server.plugins['covistra-system'].random;

    /**
     * key[><=]value;
     */
    var _parseFilter = P.method(function(model, options) {

        if(options.filter) {
            return options.filter;
        }
        else if(options.params && options.params.filter) {
            return options.params.filter;
        }
    });

    class BaseModel {

        constructor(data) {
            _.merge(this, data);
        }

        static get validation() {
            var params = {};
            params.id = Joi.string().required();
            return {
                create: {
                    payload: this.constructor.Schema
                },
                update: {
                    payload: this.constructor.Schema
                },
                upsert: {
                    payload: this.constructor.Schema
                },
                remove: {
                    params: params
                },
                show: {
                    params: params
                },
                list: {
                    query: {
                        filter: Joi.string(),
                        offset: Joi.number().precision(0),
                        size: Joi.number().precision(0)
                    }
                }
            }
        }

        static get auth() {
            return "token";
        }

        static get db() {
            return server.plugins['covistra-mongodb'][dbName];
        }

        // THESE METHODS MUST BE OVERRIDEN BY SUBCLASSES

        static get idField() {
            return "id";
        }

        static get collection() {
            throw new Error("collection must be overriden for model "+this);
        }

        static get endpoint() {
            throw new Error("endpoint must be overriden for model "+this);
        }

        static get name() {
            throw new Error("name must be overriden for model "+this);
        }

        static get handlers() {
            return {}
        }

        static get Schema() {
            return Joi.object();
        }

        static list(options) {
            log.debug("list %s", this.constructor.name);
            options = options || {};
            var coll = this.db.collection(this.collection);

            return _parseFilter(this, options).then(this.callStaticHandler('pre-list', options)).then((query) => {

                return coll.find(query).toArray().then((results) => {
                    if(options.wrap) {
                        return P.map(results, (r) => { return this.wrap(r)});
                    }
                    else {
                        return results;
                    }
                }).then(this.callStaticHandler('post-list', options));
            });

        }

        static show(id, options) {
            log.debug("show %s ", this.name, id);
            options = options || {};
            var _this = this;
            var coll = this.db.collection(this.collection);
            var q = {};
            q[this.idField] = id;
            return this.callStaticHandler('pre-show', options)(q).then(function(q) {
                return coll.findOne(q);
            }).then(function(data) {
                if(options.wrap) {
                    return _this.wrap(data);
                }
                else {
                    return data;<commit_msg>Make sure we can show model instances using filter instead of just the id<commit_after>var Joi = require('joi'),
    P = require('bluebird'),
    _ = require('lodash');

module.exports = function(server, config, log) {
    "use strict";

    var dbName = config.get('plugins:restmod:dbname','MAIN');
    var random = server.plugins['covistra-system'].random;

    /**
     * key[><=]value;
     */
    var _parseFilter = P.method(function(model, options) {

        if(options.filter) {
            return options.filter;
        }
        else if(options.params && options.params.filter) {
            return options.params.filter;
        }
    });

    class BaseModel {

        constructor(data) {
            _.merge(this, data);
        }

        static get validation() {
            var params = {};
            params.id = Joi.string().required();
            return {
                create: {
                    payload: this.constructor.Schema
                },
                update: {
                    payload: this.constructor.Schema
                },
                upsert: {
                    payload: this.constructor.Schema
                },
                remove: {
                    params: params
                },
                show: {
                    params: params
                },
                list: {
                    query: {
                        filter: Joi.string(),
                        offset: Joi.number().precision(0),
                        size: Joi.number().precision(0)
                    }
                }
            }
        }

        static get auth() {
            return "token";
        }

        static get db() {
            return server.plugins['covistra-mongodb'][dbName];
        }

        // THESE METHODS MUST BE OVERRIDEN BY SUBCLASSES

        static get idField() {
            return "id";
        }

        static get collection() {
            throw new Error("collection must be overriden for model "+this);
        }

        static get endpoint() {
            throw new Error("endpoint must be overriden for model "+this);
        }

        static get name() {
            throw new Error("name must be overriden for model "+this);
        }

        static get handlers() {
            return {}
        }

        static get Schema() {
            return Joi.object();
        }

        static list(options) {
            log.debug("list %s", this.constructor.name);
            options = options || {};
            var coll = this.db.collection(this.collection);

            return _parseFilter(this, options).then(this.callStaticHandler('pre-list', options)).then((query) => {

                return coll.find(query).toArray().then((results) => {
                    if(options.wrap) {
                        return P.map(results, (r) => { return this.wrap(r)});
                    }
                    else {
                        return results;
                    }
                }).then(this.callStaticHandler('post-list', options));
            });

        }

        static show(id, options) {
            log.debug("show %s ", this.name, id);
            options = options || {};
            var _this = this;
            var coll = this.db.collection(this.collection);
            var q = {};

            if(_.isObject(id)) {
                q = id;
            }
            else {
                q[this.idField] = id;
            }

            return this.callStaticHandler('pre-show', options)(q).then(function(q) {
                return coll.findOne(q);
            }).then(function(data) {
                if(options.wrap) {
                    return _this.wrap(data);
                }
                else {
                    return data;
{'fraction_non_alphanumeric': 0.11604971629289382, 'fraction_numerical': 0.0005403944879762226, 'mean_word_length': 1.1863555818074425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2437484', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1779, 'n_words': 400}