@codenameryuu/adonis-lucid-filter 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/build/commands/commands.json +1 -1
- package/build/commands/make_filter.js +32 -0
- package/build/configure.js +15 -0
- package/build/index.js +12 -0
- package/build/providers/lucid_filter_provider.js +19 -0
- package/build/src/base_model.js +93 -0
- package/build/src/bindings/model_query_builder.js +17 -0
- package/build/src/mixin.js +27 -0
- package/build/src/types/filter.js +9 -0
- package/build/src/types/querybuilder.js +9 -0
- package/build/stubs/main.js +11 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,24 +4,24 @@ This addon adds the functionality to filter Lucid Models Adonis JS 7. Inspired b
|
|
|
4
4
|
|
|
5
5
|
## Requirement
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
* Adonis Js 7
|
|
8
|
+
* Lucid 22 or higher
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
* Install the package
|
|
13
13
|
|
|
14
14
|
```bash
|
|
15
15
|
yarn add @codenameryuu/adonis-lucid-filter
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
* Configure the package
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
21
|
node ace configure @codenameryuu/adonis-lucid-filter
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
* Make sure to register the provider inside `adonisrc.ts` file.
|
|
25
25
|
|
|
26
26
|
```typescript
|
|
27
27
|
providers: [
|
|
@@ -48,13 +48,13 @@ Where `product` is the Lucid Model you are creating the filter for. This will cr
|
|
|
48
48
|
|
|
49
49
|
Define the filter logic based on the camel cased input key passed to the `filter()` method.
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
51
|
+
* Empty strings are ignored
|
|
52
|
+
* `setup()` will be called regardless of input
|
|
53
|
+
* `_id` is dropped from the end of the input to define the method so filtering `product_id` would use the `product()` method
|
|
54
|
+
* Input without a corresponding filter method are ignored
|
|
55
|
+
* The value of the key is injected into the method
|
|
56
|
+
* All values are accessible through the `this.$input` a property
|
|
57
|
+
* All QueryBuilder methods are accessible in `this.$query` object in the model filter class.
|
|
58
58
|
|
|
59
59
|
To define methods for the following input:
|
|
60
60
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commands":[],"version":1}
|
|
1
|
+
{"commands":[{"commandName":"make:filter","description":"Make a new Lucid filter","help":"","namespace":"make","aliases":[],"flags":[],"args":[{"name":"name","argumentName":"name","required":true,"description":"Name of the model file","type":"string"}],"options":{},"filePath":"make_filter.js"}],"version":1}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
10
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
11
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
12
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
13
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
14
|
+
};
|
|
15
|
+
import { BaseCommand, args } from '@adonisjs/core/ace';
|
|
16
|
+
import { stubsRoot } from '../stubs/main.js';
|
|
17
|
+
export default class MakeFilter extends BaseCommand {
|
|
18
|
+
static commandName = 'make:filter';
|
|
19
|
+
static description = 'Make a new Lucid filter';
|
|
20
|
+
/**
|
|
21
|
+
* Run command
|
|
22
|
+
*/
|
|
23
|
+
async run() {
|
|
24
|
+
const codemods = await this.createCodemods();
|
|
25
|
+
await codemods.makeUsingStub(stubsRoot, 'make/filter/main.stub', {
|
|
26
|
+
name: this.name,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
__decorate([
|
|
31
|
+
args.string({ description: 'Name of the model file' })
|
|
32
|
+
], MakeFilter.prototype, "name", void 0);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
export async function configure(command) {
|
|
10
|
+
const codemods = await command.createCodemods();
|
|
11
|
+
await codemods.updateRcFile((rcFile) => {
|
|
12
|
+
rcFile.addProvider('@codenameryuu/adonis-lucid-filter/provider');
|
|
13
|
+
rcFile.addCommand('@codenameryuu/adonis-lucid-filter/commands');
|
|
14
|
+
});
|
|
15
|
+
}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
export { configure } from './configure.js';
|
|
10
|
+
export { stubsRoot } from './stubs/main.js';
|
|
11
|
+
export { BaseModelFilter } from './src/base_model.js';
|
|
12
|
+
export { Filterable } from './src/mixin.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { extendModelQueryBuilder } from '../src/bindings/model_query_builder.js';
|
|
10
|
+
export default class LucidFilterProvider {
|
|
11
|
+
app;
|
|
12
|
+
constructor(app) {
|
|
13
|
+
this.app = app;
|
|
14
|
+
}
|
|
15
|
+
async boot() {
|
|
16
|
+
const { ModelQueryBuilder } = await this.app.import('@adonisjs/lucid/orm');
|
|
17
|
+
extendModelQueryBuilder(ModelQueryBuilder);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
10
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
11
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
12
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
13
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
14
|
+
};
|
|
15
|
+
var BaseModelFilter_1;
|
|
16
|
+
import camelCase from 'lodash/camelCase.js';
|
|
17
|
+
function StaticImplements() {
|
|
18
|
+
return (_t) => { };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Class to filtering AdonisJS Lucid ORM
|
|
22
|
+
*
|
|
23
|
+
* @class BaseModelFilter
|
|
24
|
+
* @constructor
|
|
25
|
+
*/
|
|
26
|
+
let BaseModelFilter = class BaseModelFilter {
|
|
27
|
+
static { BaseModelFilter_1 = this; }
|
|
28
|
+
$query;
|
|
29
|
+
$input;
|
|
30
|
+
static blacklist = [];
|
|
31
|
+
static dropId = true;
|
|
32
|
+
static camelCase = true;
|
|
33
|
+
constructor($query, $input) {
|
|
34
|
+
this.$query = $query;
|
|
35
|
+
this.$input = $input;
|
|
36
|
+
this.$input = BaseModelFilter_1.removeEmptyInput(this.$input);
|
|
37
|
+
this.$blacklist = this.constructor.blacklist;
|
|
38
|
+
}
|
|
39
|
+
handle() {
|
|
40
|
+
if (this.setup && typeof this.setup === 'function') {
|
|
41
|
+
this.setup(this.$query);
|
|
42
|
+
}
|
|
43
|
+
this.$filterByInput();
|
|
44
|
+
return this.$query;
|
|
45
|
+
}
|
|
46
|
+
whitelistMethod(method) {
|
|
47
|
+
const index = this.$blacklist.indexOf(method);
|
|
48
|
+
const isBlacklisted = index !== -1;
|
|
49
|
+
if (isBlacklisted)
|
|
50
|
+
this.$blacklist.splice(index, 1);
|
|
51
|
+
return isBlacklisted;
|
|
52
|
+
}
|
|
53
|
+
$filterByInput() {
|
|
54
|
+
for (const key in this.$input) {
|
|
55
|
+
const method = this.$getFilterMethod(key);
|
|
56
|
+
const keyName = key;
|
|
57
|
+
const value = this.$input[keyName];
|
|
58
|
+
if (this.$methodIsCallable(method)) {
|
|
59
|
+
;
|
|
60
|
+
this[method](value);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
$getFilterMethod(key) {
|
|
65
|
+
const methodName = this.constructor.dropId ? key.replace(/^(.*)(_id|Id)$/, '$1') : key;
|
|
66
|
+
return this.constructor.camelCase ? camelCase(methodName) : methodName;
|
|
67
|
+
}
|
|
68
|
+
static removeEmptyInput(input) {
|
|
69
|
+
const filteredInput = {};
|
|
70
|
+
for (const key in input) {
|
|
71
|
+
const keyName = key;
|
|
72
|
+
const value = input[keyName];
|
|
73
|
+
if (value !== '' && value !== null && value !== undefined) {
|
|
74
|
+
filteredInput[keyName] = value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return filteredInput;
|
|
78
|
+
}
|
|
79
|
+
$methodIsCallable(method) {
|
|
80
|
+
const methodKey = method;
|
|
81
|
+
return (!!this[methodKey] &&
|
|
82
|
+
typeof this[methodKey] === 'function' &&
|
|
83
|
+
!this.$methodIsBlacklisted(method));
|
|
84
|
+
}
|
|
85
|
+
$methodIsBlacklisted(method) {
|
|
86
|
+
return this.$blacklist.includes(method);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
BaseModelFilter = BaseModelFilter_1 = __decorate([
|
|
90
|
+
StaticImplements()
|
|
91
|
+
], BaseModelFilter);
|
|
92
|
+
export { BaseModelFilter };
|
|
93
|
+
export default BaseModelFilter;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Define filter method to ModelQueryBuilder
|
|
11
|
+
*/
|
|
12
|
+
export function extendModelQueryBuilder(builder) {
|
|
13
|
+
builder.macro('filter', function (input, filter) {
|
|
14
|
+
const Filter = filter || this.model.$filter();
|
|
15
|
+
return new Filter(this, input).handle();
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
export const Filterable = (superclass) => {
|
|
10
|
+
class FilterableModel extends superclass {
|
|
11
|
+
/**
|
|
12
|
+
* Filter method of filterable model
|
|
13
|
+
*/
|
|
14
|
+
static filter(input, filter) {
|
|
15
|
+
const Filter = filter || this.$filter();
|
|
16
|
+
return new Filter(this.query(), input).handle();
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Filtration scope of filterable model
|
|
20
|
+
*/
|
|
21
|
+
static filtration = function (query, input, filter) {
|
|
22
|
+
const Filter = filter || this.$filter();
|
|
23
|
+
return new Filter(query, input).handle();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return FilterableModel;
|
|
27
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* adonis-lucid-filter
|
|
3
|
+
*
|
|
4
|
+
* (c) Lookin Anton <alf@lookinlab.ru>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import { dirname } from 'node:path';
|
|
11
|
+
export const stubsRoot = dirname(fileURLToPath(import.meta.url));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codenameryuu/adonis-lucid-filter",
|
|
3
3
|
"description": "Addon for filtering Adonis JS 7 Lucid ORM",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.2.0",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/codenameryuu/adonis-lucid-filter#readme",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"clean": "del-cli build",
|
|
32
32
|
"precompile": "npm run lint && npm run clean",
|
|
33
33
|
"postcompile": "npm run copy:stubs && npm run index:commands",
|
|
34
|
-
"compile": "tsc
|
|
34
|
+
"compile": "tsc",
|
|
35
35
|
"build": "npm run compile",
|
|
36
36
|
"copy:stubs": "copyfiles \"stubs/**/**/*.stub\" --up=\"1\" build",
|
|
37
37
|
"index:commands": "adonis-kit index build/commands",
|