diff --git a/.gitignore b/.gitignore index ad46b30..e4f46fd 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,5 @@ typings/ # next.js build output .next + +dist diff --git a/dist/builders/filter.d.ts b/dist/builders/filter.d.ts deleted file mode 100644 index a2b3b37..0000000 --- a/dist/builders/filter.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const filter: (model: any, filters: any) => any; -export default filter; diff --git a/dist/builders/filter.js b/dist/builders/filter.js deleted file mode 100644 index c8ce5fa..0000000 --- a/dist/builders/filter.js +++ /dev/null @@ -1,53 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const BASIC_OPERATORS = { - eq: "=", - gt: ">", - gte: ">=", - lt: "<", - lte: "<=", - ne: "<>", -}; -const operate = (query, field, operator, value) => { - switch (operator) { - case "lt": - case "lte": - case "eq": - case "ne": - case "gte": - case "gt": - return query.where(field, BASIC_OPERATORS[operator], value); - case "ex": - if (value === true) - return query.whereNotNull(field); - return query.whereNull(field); - case "in": - return query.whereIn(field, value); - case "nin": - return query.whereNotIn(field, value); - case "bet": - return query.whereBetween(field, value[0], value[1]); - case "nbe": - return query.whereNotBetween(field, value[0], value[1]); - default: - throw new TypeError("Unknown operator"); - } -}; -const and = (query, filters) => filters - .reduce((prev, curr) => prev.where((q) => filter(q, curr)), query); -const or = (query, filters) => filters.reduce((prev, curr, index) => { - if (index === 0) - return prev.where((q) => filter(q, curr)); - return prev.orWhere((q) => filter(q, curr)); -}, query); -const filter = (model, filters) => { - if (filters.and) { - if (filters.or) - throw new TypeError("filter can only have one of [\"and\", \"or\"]"); - return and(model, filters.and); - } - if (filters.or) - return or(model, filters.or); - return operate(model, filters.field, filters.operator, filters.value); -}; -exports.default = filter; diff --git a/dist/builders/include.d.ts b/dist/builders/include.d.ts deleted file mode 100644 index ed2a74d..0000000 --- a/dist/builders/include.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default, relations: string[]) => import("@foxify/odin/dist/base/Query").default<{}, any>; -export default _default; diff --git a/dist/builders/include.js b/dist/builders/include.js deleted file mode 100644 index 9b32de7..0000000 --- a/dist/builders/include.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, relations) => model.with(...relations); diff --git a/dist/builders/limit.d.ts b/dist/builders/limit.d.ts deleted file mode 100644 index a14f7ed..0000000 --- a/dist/builders/limit.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import * as Odin from "@foxify/odin"; -declare const _default: (model: Odin<{}> | (import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default), limit: number) => any; -export default _default; diff --git a/dist/builders/limit.js b/dist/builders/limit.js deleted file mode 100644 index 7edd9a4..0000000 --- a/dist/builders/limit.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, limit) => model.limit(limit); diff --git a/dist/builders/skip.d.ts b/dist/builders/skip.d.ts deleted file mode 100644 index a6aeb95..0000000 --- a/dist/builders/skip.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import * as Odin from "@foxify/odin"; -declare const _default: (model: Odin<{}> | (import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default), skip: number) => any; -export default _default; diff --git a/dist/builders/skip.js b/dist/builders/skip.js deleted file mode 100644 index 8645a07..0000000 --- a/dist/builders/skip.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, skip) => model.skip(skip); diff --git a/dist/builders/sort.d.ts b/dist/builders/sort.d.ts deleted file mode 100644 index abae351..0000000 --- a/dist/builders/sort.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: any, sorts: string[]) => any; -export default _default; diff --git a/dist/builders/sort.js b/dist/builders/sort.js deleted file mode 100644 index f137351..0000000 --- a/dist/builders/sort.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, sorts) => sorts.reduce((prev, sort) => prev.orderBy(sort.replace(/^(\+|\-)/, ""), /^\-/.test(sort) ? "desc" : "asc"), model); diff --git a/dist/decoder.d.ts b/dist/decoder.d.ts deleted file mode 100644 index 1ea8eca..0000000 --- a/dist/decoder.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -declare const decoder: (query: { - [key: string]: any; -}) => { - [key: string]: any; -}; -export default decoder; diff --git a/dist/decoder.js b/dist/decoder.js deleted file mode 100644 index bca2a73..0000000 --- a/dist/decoder.js +++ /dev/null @@ -1,33 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const methods_1 = require("prototyped.js/es6/methods"); -const { forEach: objectForEach, isPlainObject: isObject } = methods_1.object; -const isArray = (value) => Array.isArray(value); -const isBoolean = (value) => value === "false" || value === "true"; -const isNumber = (value) => !isNaN(parseFloat(value)) && isFinite(value); -const parseArray = (arr) => arr.map(value => parseValue(value)); -const parseBoolean = (str) => str === "true"; -const parseNumber = (str) => Number(str); -const parseValue = (val) => { - if (typeof val === "undefined" || val === "") - return null; - if (isBoolean(val)) - return parseBoolean(val); - if (isArray(val)) - return parseArray(val); - if (isObject(val)) - return decoder(val); - if (isNumber(val)) - return parseNumber(val); - return val; -}; -const decoder = (query) => { - const result = {}; - objectForEach(query, (value, key) => { - value = parseValue(value); - if (value !== null) - result[key] = value; - }); - return result; -}; -exports.default = decoder; diff --git a/dist/filter.1.d.ts b/dist/filter.1.d.ts deleted file mode 100644 index e69de29..0000000 diff --git a/dist/filter.1.js b/dist/filter.1.js deleted file mode 100644 index 3918c74..0000000 --- a/dist/filter.1.js +++ /dev/null @@ -1 +0,0 @@ -"use strict"; diff --git a/dist/filter.d.ts b/dist/filter.d.ts deleted file mode 100644 index a2b3b37..0000000 --- a/dist/filter.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const filter: (model: any, filters: any) => any; -export default filter; diff --git a/dist/filter.js b/dist/filter.js deleted file mode 100644 index 02e0291..0000000 --- a/dist/filter.js +++ /dev/null @@ -1,48 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const BASIC_OPERATORS = { - eq: "=", - gt: ">", - gte: ">=", - lt: "<", - lte: "<=", - ne: "<>", -}; -const operate = (query, field, operator, value) => { - switch (operator) { - case "lt": - case "lte": - case "eq": - case "ne": - case "gte": - case "gt": - return query.where(field, BASIC_OPERATORS[operator], value); - case "ex": - if (value === true) - return query.whereNotNull(field); - return query.whereNull(field); - case "in": - return query.whereIn(field, value); - case "nin": - return query.whereNotIn(field, value); - case "bet": - return query.whereBetween(field, value[0], value[1]); - case "nbe": - return query.whereNotBetween(field, value[0], value[1]); - default: - throw new TypeError("Unknown operator"); - } -}; -const and = (query, filters) => query.where((q) => filters.reduce((prev, curr) => filter(prev, curr), q)); -const or = (query, filters) => query.orWhere((q) => filters.reduce((prev, curr) => filter(prev, curr), q)); -const filter = (model, filters) => { - if (filters.and) { - if (filters.or) - throw new TypeError("filter can only have one of [\"and\", \"or\"]"); - return and(model, filters.and); - } - if (filters.or) - return or(model, filters.or); - return operate(model, filters.field, filters.operator, filters.value); -}; -exports.default = filter; diff --git a/dist/include.1.d.ts b/dist/include.1.d.ts deleted file mode 100644 index ed2a74d..0000000 --- a/dist/include.1.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default, relations: string[]) => import("@foxify/odin/dist/base/Query").default<{}, any>; -export default _default; diff --git a/dist/include.1.js b/dist/include.1.js deleted file mode 100644 index 9b32de7..0000000 --- a/dist/include.1.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, relations) => model.with(...relations); diff --git a/dist/include.d.ts b/dist/include.d.ts deleted file mode 100644 index ed2a74d..0000000 --- a/dist/include.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default, relations: string[]) => import("@foxify/odin/dist/base/Query").default<{}, any>; -export default _default; diff --git a/dist/include.js b/dist/include.js deleted file mode 100644 index 9b32de7..0000000 --- a/dist/include.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, relations) => model.with(...relations); diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index 82522be..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -declare namespace restify { - type Operator = "lt" | "lte" | "eq" | "ne" | "gte" | "gt" | "ex" | "in" | "nin" | "bet" | "nbe"; - interface FilterObject { - field: string; - operator: Operator; - value: string | number | boolean | any[] | object | Date; - } - interface Filter { - and?: Array; - or?: Array; - } - interface Query { - filter: Filter | FilterObject; - include: string[]; - sort: string[]; - skip: number; - limit: number; - } -} -declare const restify: (model: import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default) => import("foxify/framework/routing/Layer").Handler; -export = restify; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index 3781e11..0000000 --- a/dist/index.js +++ /dev/null @@ -1,19 +0,0 @@ -"use strict"; -const Odin = require("@foxify/odin"); -const qs_1 = require("qs"); -const decoder_1 = require("./decoder"); -const query_1 = require("./query"); -const restify = (model) => { - if (!(typeof model === typeof Odin)) { - throw new TypeError("Expected model to be a Odin database model"); - } - // tslint:disable-next-line:variable-name - const foxify_restify_odin = (req, res, next) => { - const parsed = qs_1.parse(req.url.replace(/^.*\?/, "")); - const decoded = decoder_1.default(parsed); - req.fro = Object.assign({ decoded }, query_1.default(model, decoded)); - next(); - }; - return foxify_restify_odin; -}; -module.exports = restify; diff --git a/dist/limit.d.ts b/dist/limit.d.ts deleted file mode 100644 index 746ebba..0000000 --- a/dist/limit.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import * as Odin from "@foxify/odin"; -declare const _default: (model: (import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default) | Odin<{}>, limit: number) => any; -export default _default; diff --git a/dist/limit.js b/dist/limit.js deleted file mode 100644 index 7edd9a4..0000000 --- a/dist/limit.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, limit) => model.limit(limit); diff --git a/dist/query.d.ts b/dist/query.d.ts deleted file mode 100644 index 9e2dbf0..0000000 --- a/dist/query.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Query } from "."; -declare const _default: (model: any, decoded: Query) => { - counter: any; - query: any; -}; -export default _default; diff --git a/dist/query.js b/dist/query.js deleted file mode 100644 index bcdb860..0000000 --- a/dist/query.js +++ /dev/null @@ -1,21 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const filter_1 = require("./builders/filter"); -const include_1 = require("./builders/include"); -const limit_1 = require("./builders/limit"); -const skip_1 = require("./builders/skip"); -const sort_1 = require("./builders/sort"); -exports.default = (model, decoded) => { - if (decoded.include) - model = include_1.default(model, decoded.include); - if (decoded.filter) - model = filter_1.default(model, decoded.filter); - const counter = model; - if (decoded.sort) - model = sort_1.default(model, decoded.sort); - if (decoded.skip) - model = skip_1.default(model, decoded.skip); - if (decoded.limit) - model = limit_1.default(model, decoded.limit); - return { counter, query: model }; -}; diff --git a/dist/skip.1.d.ts b/dist/skip.1.d.ts deleted file mode 100644 index 3c361bc..0000000 --- a/dist/skip.1.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default, skip: number) => import("@foxify/odin/dist/base/Query").default<{}, any>; -export default _default; diff --git a/dist/skip.1.js b/dist/skip.1.js deleted file mode 100644 index 8645a07..0000000 --- a/dist/skip.1.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, skip) => model.skip(skip); diff --git a/dist/skip.d.ts b/dist/skip.d.ts deleted file mode 100644 index c74df43..0000000 --- a/dist/skip.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -import * as Odin from "@foxify/odin"; -declare const _default: (model: (import("@foxify/odin/dist/utils").ClassInterface & typeof import("@foxify/odin/dist/Model").default & typeof import("@foxify/odin/dist/base/QueryBuilder").default & typeof import("@foxify/odin/dist/base/Relational").default & typeof import("@foxify/odin/dist/GraphQL/Model").default) | Odin<{}>, skip: number) => any; -export default _default; diff --git a/dist/skip.js b/dist/skip.js deleted file mode 100644 index 8645a07..0000000 --- a/dist/skip.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, skip) => model.skip(skip); diff --git a/dist/sort.d.ts b/dist/sort.d.ts deleted file mode 100644 index abae351..0000000 --- a/dist/sort.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: any, sorts: string[]) => any; -export default _default; diff --git a/dist/sort.js b/dist/sort.js deleted file mode 100644 index f137351..0000000 --- a/dist/sort.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, sorts) => sorts.reduce((prev, sort) => prev.orderBy(sort.replace(/^(\+|\-)/, ""), /^\-/.test(sort) ? "desc" : "asc"), model); diff --git a/dist/src/builders/filter.d.ts b/dist/src/builders/filter.d.ts deleted file mode 100644 index a2b3b37..0000000 --- a/dist/src/builders/filter.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const filter: (model: any, filters: any) => any; -export default filter; diff --git a/dist/src/builders/filter.js b/dist/src/builders/filter.js deleted file mode 100644 index 02e0291..0000000 --- a/dist/src/builders/filter.js +++ /dev/null @@ -1,48 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const BASIC_OPERATORS = { - eq: "=", - gt: ">", - gte: ">=", - lt: "<", - lte: "<=", - ne: "<>", -}; -const operate = (query, field, operator, value) => { - switch (operator) { - case "lt": - case "lte": - case "eq": - case "ne": - case "gte": - case "gt": - return query.where(field, BASIC_OPERATORS[operator], value); - case "ex": - if (value === true) - return query.whereNotNull(field); - return query.whereNull(field); - case "in": - return query.whereIn(field, value); - case "nin": - return query.whereNotIn(field, value); - case "bet": - return query.whereBetween(field, value[0], value[1]); - case "nbe": - return query.whereNotBetween(field, value[0], value[1]); - default: - throw new TypeError("Unknown operator"); - } -}; -const and = (query, filters) => query.where((q) => filters.reduce((prev, curr) => filter(prev, curr), q)); -const or = (query, filters) => query.orWhere((q) => filters.reduce((prev, curr) => filter(prev, curr), q)); -const filter = (model, filters) => { - if (filters.and) { - if (filters.or) - throw new TypeError("filter can only have one of [\"and\", \"or\"]"); - return and(model, filters.and); - } - if (filters.or) - return or(model, filters.or); - return operate(model, filters.field, filters.operator, filters.value); -}; -exports.default = filter; diff --git a/dist/src/builders/include.d.ts b/dist/src/builders/include.d.ts deleted file mode 100644 index 4c1594f..0000000 --- a/dist/src/builders/include.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: any, relations: string[]) => any; -export default _default; diff --git a/dist/src/builders/include.js b/dist/src/builders/include.js deleted file mode 100644 index 9b32de7..0000000 --- a/dist/src/builders/include.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, relations) => model.with(...relations); diff --git a/dist/src/builders/limit.d.ts b/dist/src/builders/limit.d.ts deleted file mode 100644 index f10c219..0000000 --- a/dist/src/builders/limit.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: any, limit: number) => any; -export default _default; diff --git a/dist/src/builders/limit.js b/dist/src/builders/limit.js deleted file mode 100644 index 7edd9a4..0000000 --- a/dist/src/builders/limit.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, limit) => model.limit(limit); diff --git a/dist/src/builders/skip.d.ts b/dist/src/builders/skip.d.ts deleted file mode 100644 index e78fbeb..0000000 --- a/dist/src/builders/skip.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: any, skip: number) => any; -export default _default; diff --git a/dist/src/builders/skip.js b/dist/src/builders/skip.js deleted file mode 100644 index 8645a07..0000000 --- a/dist/src/builders/skip.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, skip) => model.skip(skip); diff --git a/dist/src/builders/sort.d.ts b/dist/src/builders/sort.d.ts deleted file mode 100644 index abae351..0000000 --- a/dist/src/builders/sort.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _default: (model: any, sorts: string[]) => any; -export default _default; diff --git a/dist/src/builders/sort.js b/dist/src/builders/sort.js deleted file mode 100644 index f137351..0000000 --- a/dist/src/builders/sort.js +++ /dev/null @@ -1,3 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = (model, sorts) => sorts.reduce((prev, sort) => prev.orderBy(sort.replace(/^(\+|\-)/, ""), /^\-/.test(sort) ? "desc" : "asc"), model); diff --git a/dist/src/decoder.d.ts b/dist/src/decoder.d.ts deleted file mode 100644 index 1ea8eca..0000000 --- a/dist/src/decoder.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -declare const decoder: (query: { - [key: string]: any; -}) => { - [key: string]: any; -}; -export default decoder; diff --git a/dist/src/decoder.js b/dist/src/decoder.js deleted file mode 100644 index bca2a73..0000000 --- a/dist/src/decoder.js +++ /dev/null @@ -1,33 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const methods_1 = require("prototyped.js/es6/methods"); -const { forEach: objectForEach, isPlainObject: isObject } = methods_1.object; -const isArray = (value) => Array.isArray(value); -const isBoolean = (value) => value === "false" || value === "true"; -const isNumber = (value) => !isNaN(parseFloat(value)) && isFinite(value); -const parseArray = (arr) => arr.map(value => parseValue(value)); -const parseBoolean = (str) => str === "true"; -const parseNumber = (str) => Number(str); -const parseValue = (val) => { - if (typeof val === "undefined" || val === "") - return null; - if (isBoolean(val)) - return parseBoolean(val); - if (isArray(val)) - return parseArray(val); - if (isObject(val)) - return decoder(val); - if (isNumber(val)) - return parseNumber(val); - return val; -}; -const decoder = (query) => { - const result = {}; - objectForEach(query, (value, key) => { - value = parseValue(value); - if (value !== null) - result[key] = value; - }); - return result; -}; -exports.default = decoder; diff --git a/dist/src/index.d.ts b/dist/src/index.d.ts deleted file mode 100644 index 586b3e2..0000000 --- a/dist/src/index.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -declare namespace restify { - type Operator = "lt" | "lte" | "eq" | "ne" | "gte" | "gt" | "ex" | "in" | "nin" | "bet" | "nbe"; - interface FilterObject { - field: string; - operator: Operator; - value: string | number | boolean | any[] | object | Date; - } - interface Filter { - and?: Array; - or?: Array; - } - interface Query { - filter: Filter | FilterObject; - include: string[]; - sort: string[]; - skip: number; - limit: number; - } -} -declare const restify: (model: any) => any; -export = restify; diff --git a/dist/src/index.js b/dist/src/index.js deleted file mode 100644 index 265f28b..0000000 --- a/dist/src/index.js +++ /dev/null @@ -1,15 +0,0 @@ -"use strict"; -const Odin = require("@foxify/odin"); -const query_1 = require("./query"); -const restify = (model) => { - if (!(typeof model === typeof Odin)) { - throw new TypeError("Expected model to be a Odin database model"); - } - // tslint:disable-next-line:variable-name - const foxify_restify_odin = (req, res, next) => { - req.fro = query_1.default(model, req.url); - next(); - }; - return foxify_restify_odin; -}; -module.exports = restify; diff --git a/dist/src/query.d.ts b/dist/src/query.d.ts deleted file mode 100644 index 9689686..0000000 --- a/dist/src/query.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const query: (model: any, url: string) => any; -export default query; diff --git a/dist/src/query.js b/dist/src/query.js deleted file mode 100644 index 4f74ee0..0000000 --- a/dist/src/query.js +++ /dev/null @@ -1,66 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const qs_1 = require("qs"); -const filter_1 = require("./builders/filter"); -const include_1 = require("./builders/include"); -const limit_1 = require("./builders/limit"); -const skip_1 = require("./builders/skip"); -const sort_1 = require("./builders/sort"); -const decoder_1 = require("./decoder"); -const query = (model, url) => { - url = url.replace(/^.*\?/, ""); - const parsed = qs_1.parse(url); - const decoded = decoder_1.default(parsed); - if (decoded.include) - model = include_1.default(model, decoded.include); - if (decoded.filter) - model = filter_1.default(model, decoded.filter); - if (decoded.sort) - model = sort_1.default(model, decoded.sort); - if (decoded.skip) - model = skip_1.default(model, decoded.skip); - if (decoded.limit) - model = limit_1.default(model, decoded.limit); - return model; -}; -exports.default = query; -const test = `/test?${qs_1.stringify({ - filter: { - and: [ - { - field: "age", - operator: ">=", - value: 18, - }, - { - field: "type", - operator: "?", - value: true, - }, - { - or: [ - { - field: "username", - operator: "=", - value: "ardalanamini", - }, - { - field: "email", - operator: "!=", - value: "ardalanamini22@gmail.com", - }, - ], - }, - ], - }, - include: [ - "jobs", - "offers", - ], - limit: 10, - skip: 10, - sort: [ - "age", - "-created_at", - ], -})}`; diff --git a/dist/test/Model.d.ts b/dist/test/Model.d.ts deleted file mode 100644 index 96962e3..0000000 --- a/dist/test/Model.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -declare global { - namespace NodeJS { - interface Global { - __MONGO_DB_NAME__: string; - __MONGO_CONNECTION__: any; - } - } -} -export {}; diff --git a/dist/test/Model.js b/dist/test/Model.js deleted file mode 100644 index a119038..0000000 --- a/dist/test/Model.js +++ /dev/null @@ -1,121 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const Odin = require("../src"); -const utils = require("../src/utils"); -const TABLE = "users"; -const ITEMS = [ - { - username: "ardalanamini", - email: "ardalanamini22@gmail.com", - name: { - first: "Ardalan", - last: "Amini", - }, - }, - { - username: "john", - email: "johndue@example.com", - name: { - first: "John", - last: "Due", - }, - }, -]; -Odin.connections({ - default: { - driver: "MongoDB", - database: global.__MONGO_DB_NAME__, - connection: global.__MONGO_CONNECTION__, - }, -}); -beforeAll((done) => { - Odin.DB.table(TABLE).insert(ITEMS, (err) => { - if (err) - throw err; - Odin.DB.table(TABLE).get((err, items) => { - if (err) - throw err; - ITEMS.length = 0; - ITEMS.push(...items); - done(); - }); - }); -}); -afterEach((done) => { - Odin.DB.table(TABLE).delete((err) => { - if (err) - throw err; - Odin.DB.table(TABLE).insert(ITEMS, (err) => { - if (err) - throw err; - done(); - }); - }); -}); -afterAll((done) => { - Odin.DB.table(TABLE).delete((err) => { - if (err) - throw err; - done(); - }); -}); -class User extends Odin { -} -User.schema = { - username: User.Types.String.alphanum.min(3).required, - email: User.Types.String.email.required, - name: { - first: User.Types.String.min(3).required, - last: User.Types.String.min(3), - }, -}; -describe("Testing Model", () => { - test("Model.create (async/await style)", () => __awaiter(this, void 0, void 0, function* () { - expect.assertions(2); - const item = utils.object.omit(ITEMS[0], ["id"]); - const result = yield User.create(item); - expect(result).toBeInstanceOf(Odin); - expect(utils.object.omit(result.toJSON(), ["id", "created_at"])).toEqual(item); - })); - test("Model.create (callback style)", (done) => { - const item = utils.object.omit(ITEMS[0], ["id"]); - User.create(item, (err, res) => { - expect(err).toBe(null); - expect(res).toBeInstanceOf(Odin); - expect(utils.object.omit(res.toJSON(), ["id", "created_at"])).toEqual(item); - done(); - }); - }); - test("Model.insert (async/await style)", () => __awaiter(this, void 0, void 0, function* () { - expect.assertions(1); - const items = ITEMS.map((item) => utils.object.omit(item, ["id"])); - const result = yield User.insert(items); - expect(result).toBe(items.length); - })); - test("Model.insert (callback style)", (done) => { - const items = ITEMS.map((item) => utils.object.omit(item, ["id"])); - User.insert(items, (err, res) => { - expect(err).toBe(null); - expect(res).toBe(items.length); - done(); - }); - }); - test("Model.on 'create'", () => __awaiter(this, void 0, void 0, function* () { - expect.assertions(3); - User.on("created", (result) => { - expect(utils.object.omit(result.toJSON(), ["id", "created_at"])).toEqual(item); - }); - const item = utils.object.omit(ITEMS[0], ["id"]); - const result = yield User.create(item); - expect(result).toBeInstanceOf(Odin); - expect(utils.object.omit(result.toJSON(), ["id", "created_at"])).toEqual(item); - })); -}); diff --git a/dist/test/sort.d.ts b/dist/test/sort.d.ts deleted file mode 100644 index db91f45..0000000 --- a/dist/test/sort.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -import "prototyped.js"; -declare global { - namespace NodeJS { - interface Global { - __MONGO_DB_NAME__: string; - __MONGO_CONNECTION__: any; - } - } -} diff --git a/dist/test/sort.js b/dist/test/sort.js deleted file mode 100644 index 89655d6..0000000 --- a/dist/test/sort.js +++ /dev/null @@ -1,113 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const Odin = require("@foxify/odin"); -const Foxify = require("foxify"); -require("prototyped.js"); -const qs_1 = require("qs"); -const restify = require("../src"); -const TABLE = "users"; -const ITEMS = [ - { - email: "ardalanamini22@gmail.com", - username: "ardalanamini", - age: 22, - name: { - first: "Ardalan", - last: "Amini", - }, - }, - { - email: "johndue@example.com", - username: "john", - age: 45, - name: { - first: "John", - last: "Due", - }, - }, -]; -Odin.connections({ - default: { - driver: "MongoDB", - database: global.__MONGO_DB_NAME__, - connection: global.__MONGO_CONNECTION__, - }, -}); -beforeAll((done) => { - Odin.DB.table(TABLE).insert(ITEMS, (err) => { - if (err) - throw err; - Odin.DB.table(TABLE).get((err, items) => { - if (err) - throw err; - ITEMS.length = 0; - ITEMS.push(...items); - done(); - }); - }); -}); -afterEach((done) => { - Odin.DB.table(TABLE).delete((err) => { - if (err) - throw err; - Odin.DB.table(TABLE).insert(ITEMS, (err) => { - if (err) - throw err; - done(); - }); - }); -}); -afterAll((done) => { - Odin.DB.table(TABLE).delete((err) => { - if (err) - throw err; - done(); - }); -}); -class User extends Odin { -} -User.schema = { - email: User.Types.String.email.required, - username: User.Types.String.alphanum.min(3).required, - age: User.Types.Number.min(18).required, - name: { - first: User.Types.String.min(3).required, - last: User.Types.String.min(3), - }, -}; -it("Should sort by age [asc]", () => __awaiter(this, void 0, void 0, function* () { - expect.assertions(2); - const app = new Foxify(); - app.get("/users", restify(User), (req, res) => __awaiter(this, void 0, void 0, function* () { - expect(req.fro).toBeDefined(); - res.json(yield req.fro.get()); - })); - const result = yield app.inject(`/users?${qs_1.stringify({ - sort: [ - "age", - ], - })}`); - expect(JSON.parse(result.body)).toEqual(ITEMS.orderBy("age")); -})); -it("Should sort by age [desc]", () => __awaiter(this, void 0, void 0, function* () { - expect.assertions(2); - const app = new Foxify(); - app.get("/users", restify(User), (req, res) => __awaiter(this, void 0, void 0, function* () { - expect(req.fro).toBeDefined(); - res.json(yield req.fro.get()); - })); - const result = yield app.inject(`/users?${qs_1.stringify({ - sort: [ - "-age", - ], - })}`); - expect(JSON.parse(result.body)).toEqual(ITEMS.orderBy("age", "desc")); -}));