@codenameryuu/adonis-lucid-filter 2.8.0 → 2.9.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/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 +7 -13
|
@@ -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": "2.
|
|
4
|
+
"version": "2.9.0",
|
|
5
5
|
"author": "codenameryuu",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/codenameryuu/adonis-lucid-filter#readme",
|
|
@@ -26,21 +26,15 @@
|
|
|
26
26
|
"./provider": "./build/providers/lucid_filter_provider.js"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
|
-
"lint": "eslint .",
|
|
30
|
-
"format": "prettier --write .",
|
|
31
29
|
"clean": "del-cli build",
|
|
32
|
-
"precompile": "npm run lint && npm run clean",
|
|
33
|
-
"compile": "tsc --emitDeclarationOnly --declaration",
|
|
34
|
-
"build": "npm run compile",
|
|
35
|
-
"pretest": "npm run lint",
|
|
36
|
-
"test": "c8 npm run quick:test",
|
|
37
30
|
"typecheck": "tsc --noEmit",
|
|
31
|
+
"lint": "eslint . --ext=.ts",
|
|
32
|
+
"format": "prettier --write .",
|
|
33
|
+
"prebuild": "npm run lint && npm run clean",
|
|
34
|
+
"build": "npm run clean && tsc",
|
|
35
|
+
"release": "release-it",
|
|
38
36
|
"version": "npm run build",
|
|
39
|
-
"prepublishOnly": "npm run build"
|
|
40
|
-
"release": "np",
|
|
41
|
-
"quick:test": "NODE_DEBUG=\"adonis-lucid-filter\" node --enable-source-maps --loader=ts-node/esm bin/test.ts",
|
|
42
|
-
"copy:stubs": "copyfiles \"stubs/**/**/*.stub\" --up=\"1\" build",
|
|
43
|
-
"index:commands": "adonis-kit index build/commands"
|
|
37
|
+
"prepublishOnly": "npm run build"
|
|
44
38
|
},
|
|
45
39
|
"devDependencies": {
|
|
46
40
|
"@adonisjs/assembler": "^7.7.0",
|