Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Port Core to Deno #2531

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
22 changes: 22 additions & 0 deletions main/adapter-commons/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2021 Feathers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

22 changes: 22 additions & 0 deletions main/adapter-commons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Feathers Adapter Commons

[![CI](https://github.com/feathersjs/feathers/workflows/Node.js%20CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3A%22Node.js+CI%22)
[![Dependency Status](https://img.shields.io/david/feathersjs/feathers.svg?style=flat-square&path=packages/adapter-commons)](https://david-dm.org/feathersjs/feathers?path=packages/adapter-commons)
[![Download Status](https://img.shields.io/npm/dm/@feathersjs/adapter-commons.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/adapter-commons)

> Shared utility functions for Feathers adatabase adapters

## About

This is a repository for handling Feathers common database syntax. See the [API documentation](https://docs.feathersjs.com/api/databases/common.html) for more information.


## Authors

[Feathers contributors](https://github.com/feathersjs/adapter-commons/graphs/contributors)

## License

Copyright (c) 2021 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)

Licensed under the [MIT license](LICENSE).
116 changes: 116 additions & 0 deletions main/adapter-commons/src/filter-query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { _ } from '../../commons/src/index.ts';
import { BadRequest } from '../../errors/src/index.ts';

function parse (number: any) {
if (typeof number !== 'undefined') {
return Math.abs(parseInt(number, 10));
}

return undefined;
}

// Returns the pagination limit and will take into account the
// default and max pagination settings
function getLimit (limit: any, paginate: any) {
if (paginate && (paginate.default || paginate.max)) {
const base = paginate.default || 0;
const lower = typeof limit === 'number' && !isNaN(limit) ? limit : base;
const upper = typeof paginate.max === 'number' ? paginate.max : Number.MAX_VALUE;

return Math.min(lower, upper);
}

return limit;
}

// Makes sure that $sort order is always converted to an actual number
function convertSort (sort: any) {
if (typeof sort !== 'object' || Array.isArray(sort)) {
return sort;
}

return Object.keys(sort).reduce((result, key) => {
result[key] = typeof sort[key] === 'object'
? sort[key] : parseInt(sort[key], 10);

return result;
}, {} as { [key: string]: number });
}

function cleanQuery (query: any, operators: any, filters: any): any {
if (Array.isArray(query)) {
return query.map(value => cleanQuery(value, operators, filters));
} else if (_.isObject(query) && query.constructor === {}.constructor) {
const result: { [key: string]: any } = {};

_.each(query, (value, key) => {
if (key[0] === '$') {
if (filters[key] !== undefined) {
return;
}

if (!operators.includes(key)) {
throw new BadRequest(`Invalid query parameter ${key}`, query);
}
}

result[key] = cleanQuery(value, operators, filters);
});

Object.getOwnPropertySymbols(query).forEach(symbol => {
// @ts-ignore
result[symbol] = query[symbol];
});

return result;
}

return query;
}

function assignFilters (object: any, query: any, filters: any, options: any) {
if (Array.isArray(filters)) {
_.each(filters, (key) => {
if (query[key] !== undefined) {
object[key] = query[key];
}
});
} else {
_.each(filters, (converter, key) => {
const converted = converter(query[key], options);

if (converted !== undefined) {
object[key] = converted;
}
});
}

return object;
}

export const FILTERS = {
$sort: (value: any) => convertSort(value),
$limit: (value: any, options: any) => getLimit(parse(value), options.paginate),
$skip: (value: any) => parse(value),
$select: (value: any) => value
};

export const OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or'];

// Converts Feathers special query parameters and pagination settings
// and returns them separately a `filters` and the rest of the query
// as `query`
export function filterQuery (query: any, options: any = {}) {
const {
filters: additionalFilters = {},
operators: additionalOperators = []
} = options;
const result: { [key: string]: any } = {};

result.filters = assignFilters({}, query, FILTERS, options);
result.filters = assignFilters(result.filters, query, additionalFilters, options);

result.query = cleanQuery(query, OPERATORS.concat(additionalOperators), result.filters);

return result;
}
33 changes: 33 additions & 0 deletions main/adapter-commons/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { _ } from '../../commons/src/index.ts';

export { AdapterService } from './service.ts';
export type { InternalServiceMethods, ServiceOptions, AdapterParams } from './service.ts';
export { filterQuery, FILTERS, OPERATORS } from './filter-query.ts';
export * from './sort.ts';

// Return a function that filters a result object or array
// and picks only the fields passed as `params.query.$select`
// and additional `otherFields`
export function select (params: any, ...otherFields: any[]) {
const fields = params && params.query && params.query.$select;

if (Array.isArray(fields) && otherFields.length) {
fields.push(...otherFields);
}

const convert = (result: any) => {
if (!Array.isArray(fields)) {
return result;
}

return _.pick(result, ...fields);
};

return (result: any) => {
if (Array.isArray(result)) {
return result.map(convert);
}

return convert(result);
};
}
Loading