<commit_before>'use strict';

/**
 * ButtressJS - Realtime datastore for software
 *
 * @file schemaRoutes.js
 * @description A list of default routes for schema
 * @module routes
 * @author <NAME>
 *
 */

// const Logging = require('../logging');
const Route = require('./route');
const Model = require('../model');
const Helpers = require('../helpers');
const Schema = require('../schema');
const SchemaModel = require('../model/schemaModel');
const ObjectId = require('mongodb').ObjectId;

const routes = [];

/**
 * @class GetList
 */
class GetList extends Route {
	constructor(schema, appShort) {
		super(`${schema.name}`, `GET ${schema.name} LIST`);
		this.verb = Route.Constants.Verbs.GET;
		this.auth = Route.Constants.Auth.USER;
		this.permissions = Route.Constants.Permissions.LIST;

		this.activityDescription = `GET ${schema.name} LIST`;
		this.activityBroadcast = false;

		let schemaCollection = schema.collection;
		if (appShort) {
			schemaCollection = `${appShort}-${schema.collection}`;
		}

		this.slowLogging = false;

		// Fetch model
		this.schema = new Schema(schema);
		this.model = Model[schemaCollection];

		if (!this.model) {
			throw new Error(`GetList Route missing model ${schemaCollection}`);
		}
	}

	_validate(req, res, token) {
		let query = Promise.resolve({});
		if (token.authLevel < 3) {
			query = this.model.generateRoleFilterQuery(token, req.roles, Model);
		}

		return query;
	}

	_exec(req, res, query) {
		return this.model.find(query, {}, true);
	}
}
routes.push(GetList);

/**
 * @class SearchList
 */
class SearchList extends Route {
	constructor(schema, appShort) {
		super(`${schema.name}`, `SEARCH ${schema.name} LIST`);
		this.verb = Route.Constants.Verbs.SEARCH;
		this.auth = Route.Constants.Auth.USER;
		this.permissions = Route.Constants.Permissions.LIST;

		this.activityDescription = `SEARCH ${schema.name} LIST`;
		this.activityBroadcast = false;

		let schemaCollection = schema.collection;
		if (appShort) {
			schemaCollection = `${appShort}-${schema.collection}`;
		}

		// Fetch model
		this.schema = new Schema(schema);
		this.model = Model[schemaCollection];

		if (!this.model) {
			throw new Error(`SearchList Route missing model ${schemaCollection}`);
		}
	}

	_validate(req, res, token) {
		let generateQuery = Promise.resolve({});
		if (token.authLevel < 3) {
			generateQuery = this.model.generateRoleFilterQuery(token, req.roles, Model);
		}

		return generateQuery
			.then((query) => {
				if (!query.$and) {
					query.$and = [];
				}

				// TODO: Vaildate this input against the schema, schema properties should be tagged with what can be queried
				if (req.body) {
					query.$and.push(req.body);
				}

				return query;
			})
			.then((query) => SchemaModel.parseQuery(query, {}, this.model.flatSchemaData));
	}

	_exec(req, res, query) {
		return this.model.find(query, {}, true);
	}
}
routes.push(SearchList);
<commit_msg>REFACTOR: Search now expects the body to be a obj containing query, limit, skip<commit_after>'use strict';

/**
 * ButtressJS - Realtime datastore for software
 *
 * @file schemaRoutes.js
 * @description A list of default routes for schema
 * @module routes
 * @author <NAME>
 *
 */

// const Logging = require('../logging');
const Route = require('./route');
const Model = require('../model');
const Helpers = require('../helpers');
const Schema = require('../schema');
const SchemaModel = require('../model/schemaModel');
const ObjectId = require('mongodb').ObjectId;

const routes = [];

/**
 * @class GetList
 */
class GetList extends Route {
	constructor(schema, appShort) {
		super(`${schema.name}`, `GET ${schema.name} LIST`);
		this.verb = Route.Constants.Verbs.GET;
		this.auth = Route.Constants.Auth.USER;
		this.permissions = Route.Constants.Permissions.LIST;

		this.activityDescription = `GET ${schema.name} LIST`;
		this.activityBroadcast = false;

		let schemaCollection = schema.collection;
		if (appShort) {
			schemaCollection = `${appShort}-${schema.collection}`;
		}

		this.slowLogging = false;

		// Fetch model
		this.schema = new Schema(schema);
		this.model = Model[schemaCollection];

		if (!this.model) {
			throw new Error(`GetList Route missing model ${schemaCollection}`);
		}
	}

	_validate(req, res, token) {
		let query = Promise.resolve({});
		if (token.authLevel < 3) {
			query = this.model.generateRoleFilterQuery(token, req.roles, Model);
		}

		return query;
	}

	_exec(req, res, query) {
		return this.model.find(query, {}, true);
	}
}
routes.push(GetList);

/**
 * @class SearchList
 */
class SearchList extends Route {
	constructor(schema, appShort) {
		super(`${schema.name}`, `SEARCH ${schema.name} LIST`);
		this.verb = Route.Constants.Verbs.SEARCH;
		this.auth = Route.Constants.Auth.USER;
		this.permissions = Route.Constants.Permissions.LIST;

		this.activityDescription = `SEARCH ${schema.name} LIST`;
		this.activityBroadcast = false;

		let schemaCollection = schema.collection;
		if (appShort) {
			schemaCollection = `${appShort}-${schema.collection}`;
		}

		// Fetch model
		this.schema = new Schema(schema);
		this.model = Model[schemaCollection];

		if (!this.model) {
			throw new Error(`SearchList Route missing model ${schemaCollection}`);
		}
	}

	_validate(req, res, token) {
		let generateQuery = Promise.resolve({});
		if (token.authLevel < 3) {
			generateQuery = this.model.generateRoleFilterQuery(token, req.roles, Model);
		}

		const result = {
			query: null,
			skip: (req.body && req.body.skip) ? parseInt(req.body.skip) : 0,
			limit: (req.body && req.body.limit) ? parseInt(req.body.limit) : 0,
		};

		return generateQuery
			.then((query) => {
				if (!query.$and) {
					query.$and = [];
				}

				// TODO: Vaildate this input against the schema, schema properties should be tagged with what can be queried
				if (req.body) {
					query.$and.push(req.body);
				}

				if (req.body && req.body.query) {
					query.$and.push(req.body.query);
				} else if (req.body && !req.body.query) {
					query.$and.push(req.body);
				}

				return SchemaModel.parseQuery(query, {}, this.model.flatSchemaData);
			})
			.then((query) => {
				result.query = query;
				return result;
			});
	}

	_exec(req, res, validateResult) {
		return this.model.find(validateResult.query, {}, true, validateResult.limit, validateResult.skip);
	}
}
routes.push(SearchList);

{'fraction_non_alphanumeric': 0.15751262626262627, 'fraction_numerical': 0.000946969696969697, 'mean_word_length': 3.732636295743092, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2854392', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2034, 'n_words': 467}