Skip to content

Commit

Permalink
Add sort for gei crud (#5)
Browse files Browse the repository at this point in the history
* addSort

* fixTypes

* fixInputName

* versionUp
  • Loading branch information
pavelbrui committed Aug 1, 2023
1 parent bb2a871 commit 032d346
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/integrations/gei-crud/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gei-crud",
"version": "0.7.0",
"version": "0.7.1",
"description": "GraphQL Editor integration for stucco. Allows basic crud operations and relations.",
"main": "lib/index.js",
"private": false,
Expand Down
22 changes: 21 additions & 1 deletion packages/integrations/gei-crud/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ type Object{

type Query{
objects(fieldFilter: FieldFilterInput,
fieldRegexFilter: FieldFilterInput): [Object!]
fieldRegexFilter: FieldFilterInput,
sortByField: SortInput): [Object!]
oneById(
_id: String!
): Object
}



type Mutation{
create(
object: Update!
Expand All @@ -42,6 +45,23 @@ input FieldFilterInput {
customFieldName: String
}


input SortInput {
field: SortField!
"""
True for ASC, false for DESC
"""
order: Boolean
}

enum SortField {
CREATED_AT
UPDATED_AT
NAME
CUSTOM_FIELD_NAME
}


input Update{
name: String
content: String
Expand Down
17 changes: 13 additions & 4 deletions packages/integrations/gei-crud/src/Query/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { DB } from '../db/mongo.js';

export const handler = async (input: FieldResolveInput) => {
return DB().then((db) => {

const filterInput = {
...prepareSourceParameters(input),
...(input.arguments?.fieldFilter as object),
...convertObjectToRegexFormat(input.arguments?.fieldFilterReg as QueryObject),
...convertObjectToRegexFormat(input.arguments?.fieldRegexFilter as QueryObject),
};

return db.collection(prepareModel(input)).find(filterInput).toArray();
const sort = (typeof input.arguments?.sortByField === 'object') ? input.arguments?.sortByField as {field: string, order?: boolean} : undefined
const field = snakeCaseToCamelCase(sort?.field as unknown as string)

return db.collection(prepareModel(input)).find(filterInput).sort(field ? { [field]: sort?.order === false ? -1 : 1 } : { _id: 1 }).toArray();
});
};

interface QueryObject {
[key: string]: unknown;
}


// Function to convert the object to the desired format
function convertObjectToRegexFormat(obj: QueryObject): QueryObject | undefined {
const obj2: QueryObject = {};
Expand All @@ -26,4 +30,9 @@ function convertObjectToRegexFormat(obj: QueryObject): QueryObject | undefined {
if (obj[key]) obj2[key] = { $regex: obj[key], $options: 'i' };
}
return obj2 || {};
}
}


function snakeCaseToCamelCase(input: string | null | undefined) {
return input?.toLowerCase().replace(/_([a-z])/g, (match, group1) => group1.toUpperCase());
}

0 comments on commit 032d346

Please sign in to comment.