@codenameryuu/adonis-lucid-filter 2.0.0 → 2.0.1
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/index.js +2 -2
- package/build/index.js.map +1 -1
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -10,8 +10,8 @@ import {
|
|
|
10
10
|
async function configure(command) {
|
|
11
11
|
const codemods = await command.createCodemods();
|
|
12
12
|
await codemods.updateRcFile((rcFile) => {
|
|
13
|
-
rcFile.addProvider("adonis-lucid-filter/provider");
|
|
14
|
-
rcFile.addCommand("adonis-lucid-filter/commands");
|
|
13
|
+
rcFile.addProvider("@codenameryuu/adonis-lucid-filter/provider");
|
|
14
|
+
rcFile.addCommand("@codenameryuu/adonis-lucid-filter/commands");
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
17
|
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../configure.ts","../src/base_model.ts","../src/mixin.ts"],"sourcesContent":["/*\r\n * adonis-lucid-filter\r\n *\r\n * (c) Lookin Anton <alf@lookinlab.ru>\r\n *\r\n * For the full copyright and license information, please view the LICENSE\r\n * file that was distributed with this source code.\r\n */\r\n\r\nimport type Configure from '@adonisjs/core/commands/configure'\r\n\r\nexport async function configure(command: Configure) {\r\n const codemods = await command.createCodemods()\r\n\r\n await codemods.updateRcFile((rcFile) => {\r\n rcFile.addProvider('adonis-lucid-filter/provider')\r\n rcFile.addCommand('adonis-lucid-filter/commands')\r\n })\r\n}\r\n","/*\r\n * adonis-lucid-filter\r\n *\r\n * (c) Lookin Anton <alf@lookinlab.ru>\r\n *\r\n * For the full copyright and license information, please view the LICENSE\r\n * file that was distributed with this source code.\r\n */\r\n\r\nimport camelCase from 'lodash/camelCase.js'\r\nimport type { LucidFilter, LucidFilterContract } from './types/filter.js'\r\n\r\nfunction StaticImplements<T>() {\r\n return (_t: T) => {}\r\n}\r\n\r\n/**\r\n * Class to filtering AdonisJS Lucid ORM\r\n *\r\n * @class BaseModelFilter\r\n * @constructor\r\n */\r\n@StaticImplements<LucidFilterContract>()\r\nexport class BaseModelFilter implements LucidFilter {\r\n declare ['constructor']: typeof BaseModelFilter\r\n declare $blacklist: string[]\r\n\r\n static blacklist: string[] = []\r\n static dropId: boolean = true\r\n static camelCase: boolean = true\r\n\r\n setup?($query: any): void\r\n\r\n constructor(\r\n public $query: any,\r\n public $input: object\r\n ) {\r\n this.$input = BaseModelFilter.removeEmptyInput(this.$input)\r\n this.$blacklist = this.constructor.blacklist\r\n }\r\n\r\n handle(): any {\r\n if (this.setup && typeof this.setup === 'function') {\r\n this.setup(this.$query)\r\n }\r\n this.$filterByInput()\r\n\r\n return this.$query\r\n }\r\n\r\n whitelistMethod(method: string): boolean {\r\n const index = this.$blacklist.indexOf(method)\r\n\r\n const isBlacklisted = index !== -1\r\n if (isBlacklisted) this.$blacklist.splice(index, 1)\r\n\r\n return isBlacklisted\r\n }\r\n\r\n $filterByInput(): void {\r\n for (const key in this.$input) {\r\n const method = this.$getFilterMethod(key)\r\n\r\n const keyName = key as keyof typeof this.$input\r\n const value: unknown = this.$input[keyName]\r\n\r\n if (this.$methodIsCallable(method)) {\r\n ;(this[method as keyof this] as Function)(value)\r\n }\r\n }\r\n }\r\n\r\n $getFilterMethod(key: string): string {\r\n const methodName = this.constructor.dropId ? key.replace(/^(.*)(_id|Id)$/, '$1') : key\r\n return this.constructor.camelCase ? camelCase(methodName) : methodName\r\n }\r\n\r\n static removeEmptyInput(input: object): object {\r\n const filteredInput = {}\r\n\r\n for (const key in input) {\r\n const keyName = key as keyof typeof input\r\n const value = input[keyName]\r\n\r\n if (value !== '' && value !== null && value !== undefined) {\r\n filteredInput[keyName] = value\r\n }\r\n }\r\n return filteredInput\r\n }\r\n\r\n $methodIsCallable(method: string): boolean {\r\n const methodKey = method as keyof this\r\n\r\n return (\r\n !!this[methodKey] &&\r\n typeof this[methodKey] === 'function' &&\r\n !this.$methodIsBlacklisted(method)\r\n )\r\n }\r\n\r\n $methodIsBlacklisted(method: string): boolean {\r\n return this.$blacklist.includes(method)\r\n }\r\n}\r\nexport default BaseModelFilter\r\n","/*\r\n * adonis-lucid-filter\r\n *\r\n * (c) Lookin Anton <alf@lookinlab.ru>\r\n *\r\n * For the full copyright and license information, please view the LICENSE\r\n * file that was distributed with this source code.\r\n */\r\n\r\nimport type { NormalizeConstructor } from '@adonisjs/core/types/helpers'\r\nimport type { BaseModel } from '@adonisjs/lucid/orm'\r\nimport type {\r\n InputObject,\r\n LucidFilterContract,\r\n} from '@codenameryuu/adonis-lucid-filter/types/filter'\r\nimport type {\r\n ModelQueryBuilderContract,\r\n QueryScope,\r\n QueryScopeCallback,\r\n} from '@adonisjs/lucid/types/model'\r\n\r\nexport const Filterable = <Model extends NormalizeConstructor<typeof BaseModel>>(\r\n superclass: Model\r\n) => {\r\n class FilterableModel extends superclass {\r\n declare static $filter: () => LucidFilterContract\r\n\r\n /**\r\n * Filter method of filterable model\r\n */\r\n static filter<\r\n T extends typeof FilterableModel,\r\n Filter extends LucidFilterContract = ReturnType<T['$filter']>,\r\n >(\r\n this: T,\r\n input: InputObject<InstanceType<Filter>>,\r\n filter?: Filter\r\n ): ModelQueryBuilderContract<T> {\r\n const Filter = filter || this.$filter()\r\n return new Filter(this.query(), input).handle()\r\n }\r\n\r\n /**\r\n * Filtration scope of filterable model\r\n */\r\n static filtration = function (\r\n this: typeof FilterableModel,\r\n query,\r\n input,\r\n filter?: LucidFilterContract\r\n ) {\r\n const Filter = filter || this.$filter()\r\n return new Filter(query, input).handle()\r\n } as QueryScope<Model, QueryScopeCallback>\r\n }\r\n return FilterableModel\r\n}\r\n"],"mappings":";;;;;;;;;AAWA,eAAsB,UAAU,SAAoB;AAClD,QAAM,WAAW,MAAM,QAAQ,eAAe;AAE9C,QAAM,SAAS,aAAa,CAAC,WAAW;AACtC,WAAO,YAAY,
|
|
1
|
+
{"version":3,"sources":["../configure.ts","../src/base_model.ts","../src/mixin.ts"],"sourcesContent":["/*\r\n * adonis-lucid-filter\r\n *\r\n * (c) Lookin Anton <alf@lookinlab.ru>\r\n *\r\n * For the full copyright and license information, please view the LICENSE\r\n * file that was distributed with this source code.\r\n */\r\n\r\nimport type Configure from '@adonisjs/core/commands/configure'\r\n\r\nexport async function configure(command: Configure) {\r\n const codemods = await command.createCodemods()\r\n\r\n await codemods.updateRcFile((rcFile) => {\r\n rcFile.addProvider('@codenameryuu/adonis-lucid-filter/provider')\r\n rcFile.addCommand('@codenameryuu/adonis-lucid-filter/commands')\r\n })\r\n}\r\n","/*\r\n * adonis-lucid-filter\r\n *\r\n * (c) Lookin Anton <alf@lookinlab.ru>\r\n *\r\n * For the full copyright and license information, please view the LICENSE\r\n * file that was distributed with this source code.\r\n */\r\n\r\nimport camelCase from 'lodash/camelCase.js'\r\nimport type { LucidFilter, LucidFilterContract } from './types/filter.js'\r\n\r\nfunction StaticImplements<T>() {\r\n return (_t: T) => {}\r\n}\r\n\r\n/**\r\n * Class to filtering AdonisJS Lucid ORM\r\n *\r\n * @class BaseModelFilter\r\n * @constructor\r\n */\r\n@StaticImplements<LucidFilterContract>()\r\nexport class BaseModelFilter implements LucidFilter {\r\n declare ['constructor']: typeof BaseModelFilter\r\n declare $blacklist: string[]\r\n\r\n static blacklist: string[] = []\r\n static dropId: boolean = true\r\n static camelCase: boolean = true\r\n\r\n setup?($query: any): void\r\n\r\n constructor(\r\n public $query: any,\r\n public $input: object\r\n ) {\r\n this.$input = BaseModelFilter.removeEmptyInput(this.$input)\r\n this.$blacklist = this.constructor.blacklist\r\n }\r\n\r\n handle(): any {\r\n if (this.setup && typeof this.setup === 'function') {\r\n this.setup(this.$query)\r\n }\r\n this.$filterByInput()\r\n\r\n return this.$query\r\n }\r\n\r\n whitelistMethod(method: string): boolean {\r\n const index = this.$blacklist.indexOf(method)\r\n\r\n const isBlacklisted = index !== -1\r\n if (isBlacklisted) this.$blacklist.splice(index, 1)\r\n\r\n return isBlacklisted\r\n }\r\n\r\n $filterByInput(): void {\r\n for (const key in this.$input) {\r\n const method = this.$getFilterMethod(key)\r\n\r\n const keyName = key as keyof typeof this.$input\r\n const value: unknown = this.$input[keyName]\r\n\r\n if (this.$methodIsCallable(method)) {\r\n ;(this[method as keyof this] as Function)(value)\r\n }\r\n }\r\n }\r\n\r\n $getFilterMethod(key: string): string {\r\n const methodName = this.constructor.dropId ? key.replace(/^(.*)(_id|Id)$/, '$1') : key\r\n return this.constructor.camelCase ? camelCase(methodName) : methodName\r\n }\r\n\r\n static removeEmptyInput(input: object): object {\r\n const filteredInput = {}\r\n\r\n for (const key in input) {\r\n const keyName = key as keyof typeof input\r\n const value = input[keyName]\r\n\r\n if (value !== '' && value !== null && value !== undefined) {\r\n filteredInput[keyName] = value\r\n }\r\n }\r\n return filteredInput\r\n }\r\n\r\n $methodIsCallable(method: string): boolean {\r\n const methodKey = method as keyof this\r\n\r\n return (\r\n !!this[methodKey] &&\r\n typeof this[methodKey] === 'function' &&\r\n !this.$methodIsBlacklisted(method)\r\n )\r\n }\r\n\r\n $methodIsBlacklisted(method: string): boolean {\r\n return this.$blacklist.includes(method)\r\n }\r\n}\r\nexport default BaseModelFilter\r\n","/*\r\n * adonis-lucid-filter\r\n *\r\n * (c) Lookin Anton <alf@lookinlab.ru>\r\n *\r\n * For the full copyright and license information, please view the LICENSE\r\n * file that was distributed with this source code.\r\n */\r\n\r\nimport type { NormalizeConstructor } from '@adonisjs/core/types/helpers'\r\nimport type { BaseModel } from '@adonisjs/lucid/orm'\r\nimport type {\r\n InputObject,\r\n LucidFilterContract,\r\n} from '@codenameryuu/adonis-lucid-filter/types/filter'\r\nimport type {\r\n ModelQueryBuilderContract,\r\n QueryScope,\r\n QueryScopeCallback,\r\n} from '@adonisjs/lucid/types/model'\r\n\r\nexport const Filterable = <Model extends NormalizeConstructor<typeof BaseModel>>(\r\n superclass: Model\r\n) => {\r\n class FilterableModel extends superclass {\r\n declare static $filter: () => LucidFilterContract\r\n\r\n /**\r\n * Filter method of filterable model\r\n */\r\n static filter<\r\n T extends typeof FilterableModel,\r\n Filter extends LucidFilterContract = ReturnType<T['$filter']>,\r\n >(\r\n this: T,\r\n input: InputObject<InstanceType<Filter>>,\r\n filter?: Filter\r\n ): ModelQueryBuilderContract<T> {\r\n const Filter = filter || this.$filter()\r\n return new Filter(this.query(), input).handle()\r\n }\r\n\r\n /**\r\n * Filtration scope of filterable model\r\n */\r\n static filtration = function (\r\n this: typeof FilterableModel,\r\n query,\r\n input,\r\n filter?: LucidFilterContract\r\n ) {\r\n const Filter = filter || this.$filter()\r\n return new Filter(query, input).handle()\r\n } as QueryScope<Model, QueryScopeCallback>\r\n }\r\n return FilterableModel\r\n}\r\n"],"mappings":";;;;;;;;;AAWA,eAAsB,UAAU,SAAoB;AAClD,QAAM,WAAW,MAAM,QAAQ,eAAe;AAE9C,QAAM,SAAS,aAAa,CAAC,WAAW;AACtC,WAAO,YAAY,4CAA4C;AAC/D,WAAO,WAAW,4CAA4C;AAAA,EAChE,CAAC;AACH;;;ACTA,OAAO,eAAe;AAGtB,SAAS,mBAAsB;AAC7B,SAAO,CAAC,OAAU;AAAA,EAAC;AACrB;AASO,IAAM,kBAAN,MAA6C;AAAA,EAUlD,YACS,QACA,QACP;AAFO;AACA;AAEP,SAAK,SAAS,gBAAgB,iBAAiB,KAAK,MAAM;AAC1D,SAAK,aAAa,KAAK,YAAY;AAAA,EACrC;AAAA,EAEA,SAAc;AACZ,QAAI,KAAK,SAAS,OAAO,KAAK,UAAU,YAAY;AAClD,WAAK,MAAM,KAAK,MAAM;AAAA,IACxB;AACA,SAAK,eAAe;AAEpB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,gBAAgB,QAAyB;AACvC,UAAM,QAAQ,KAAK,WAAW,QAAQ,MAAM;AAE5C,UAAM,gBAAgB,UAAU;AAChC,QAAI,cAAe,MAAK,WAAW,OAAO,OAAO,CAAC;AAElD,WAAO;AAAA,EACT;AAAA,EAEA,iBAAuB;AACrB,eAA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