Skip to content

feat: update Filters methods to accept variable arguments for improve… #215

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

Merged
merged 1 commit into from
May 23, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,13 @@ You can mitigate this by adding couple of lines into edit hook:
//diff-add
const oldStrings = await adminforth.resource('translations').list([
//diff-add
Filters.AND([
Filters.AND(
//diff-add
Filters.EQ('category', 'seo_page_config'),
//diff-add
Filters.IN('en_string', Object.keys(updates).map((key: string) => oldRecord[key]))
//diff-add
])
)
//diff-add
]);
//diff-add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ hooks: {
// replace apartment_type filter with complex one
if (filter.field === 'apartment_type') {
if (filter.value === 'luxury') {
return Filters.OR([Filters.GTE('square_meter', 80), Filters.GTE('price', 100000)]);
return Filters.OR(Filters.GTE('square_meter', 80), Filters.GTE('price', 100000));
}

// filter for "base" apartment as default
return Filters.AND([Filters.LT('square_meter', 80), Filters.LT('price', 100000)]);
return Filters.AND(Filters.LT('square_meter', 80), Filters.LT('price', 100000));
}

return filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const admin = new AdminForth({
});

// get the resource object
await admin.resource('adminuser').get([Filters.EQ('id', '1234')]);
await admin.resource('adminuser').get(Filters.EQ('id', '1234'));
```

Here we will show you how to use the Data API with simple examples.
Expand All @@ -37,7 +37,7 @@ Signature:

```ts
.get(
filters: [],
filters: <AdminForthFilterObject>,
): Promise<any>
```

Expand All @@ -62,7 +62,8 @@ Get user with name 'John' and role not 'SuperAdmin'

```ts
const user = await admin.resource('adminuser').get(
[Filters.EQ('name', 'John'), Filters.NEQ('role', 'SuperAdmin')]
Filters.EQ('name', 'John'),
Filters.NEQ('role', 'SuperAdmin')
);
```

Expand All @@ -73,7 +74,7 @@ Signature:

```ts
.list(
filters: [],
filters: <AdminForthFilterObject>,
limit: number | null
offset: number | null
sort: []
Expand Down Expand Up @@ -112,13 +113,13 @@ Get all users that have gmail address AND the ones created not in 2024

```ts
const users = await admin.resource('adminuser').list(
Filters.AND([
Filters.AND(
Filters.LIKE('email', '@gmail.com'),
Filters.OR([
Filters.OR(
Filters.LT('createdAt', '2024-01-01T00:00:00.000Z'),
Filters.GTE('createdAt', '2025-01-01T00:00:00.000Z'),
]),
])
),
)
);
```

Expand Down Expand Up @@ -150,16 +151,16 @@ Signature:

```ts
.count(
filters: [],
): Promise<number>
filters: <AdminForthFilterObject>,
): Promise<any>
```

Returns number of items in database which match the filters.

Count number of schools with rating above 4:

```ts
const schoolsCount = await admin.resource('schools').count([Filters.GT('rating', 4)]);
const schoolsCount = await admin.resource('schools').count(Filters.GT('rating', 4));
```

Create data for daily report with number of users signed up daily for last 7 days:
Expand Down
28 changes: 24 additions & 4 deletions adminforth/types/Back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,11 +1100,31 @@ export class Filters {
static LIKE(field: string, value: any): IAdminForthSingleFilter {
return { field, operator: AdminForthFilterOperators.LIKE, value };
}
static AND(subFilters: Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>): IAdminForthAndOrFilter {
return { operator: AdminForthFilterOperators.AND, subFilters };
static AND(
...args: (IAdminForthSingleFilter | IAdminForthAndOrFilter | Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>)[]
): IAdminForthAndOrFilter {
const subFilters =
args.length === 1 && Array.isArray(args[0])
? args[0]
: args as Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>;

return {
operator: AdminForthFilterOperators.AND,
subFilters,
};
}
static OR(subFilters: Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>): IAdminForthAndOrFilter {
return { operator: AdminForthFilterOperators.OR, subFilters };
static OR(
...args: (IAdminForthSingleFilter | IAdminForthAndOrFilter | Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>)[]
): IAdminForthAndOrFilter {
const subFilters =
args.length === 1 && Array.isArray(args[0])
? args[0]
: args as Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>;

return {
operator: AdminForthFilterOperators.OR,
subFilters,
};
}
}

Expand Down