@nocobase/database 0.5.0-alpha.38 → 0.7.0-alpha.10
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/LICENSE +201 -21
- package/esm/collection-importer.d.ts +7 -0
- package/esm/collection-importer.js +49 -0
- package/esm/collection-importer.js.map +1 -0
- package/esm/collection.d.ts +73 -0
- package/esm/collection.js +224 -0
- package/esm/collection.js.map +1 -0
- package/esm/database.d.ts +101 -0
- package/esm/database.js +275 -0
- package/esm/database.js.map +1 -0
- package/esm/fields/array-field.d.ts +11 -0
- package/esm/fields/array-field.js +26 -0
- package/esm/fields/array-field.js.map +1 -0
- package/esm/fields/belongs-to-field.d.ts +12 -0
- package/esm/fields/belongs-to-field.js +57 -0
- package/esm/fields/belongs-to-field.js.map +1 -0
- package/esm/fields/belongs-to-many-field.d.ts +11 -0
- package/esm/fields/belongs-to-many-field.js +55 -0
- package/esm/fields/belongs-to-many-field.js.map +1 -0
- package/esm/fields/boolean-field.d.ts +8 -0
- package/esm/fields/boolean-field.js +8 -0
- package/esm/fields/boolean-field.js.map +1 -0
- package/esm/fields/context-field.d.ts +13 -0
- package/esm/fields/context-field.js +43 -0
- package/esm/fields/context-field.js.map +1 -0
- package/esm/fields/date-field.d.ts +8 -0
- package/esm/fields/date-field.js +8 -0
- package/esm/fields/date-field.js.map +1 -0
- package/esm/fields/field.d.ts +37 -0
- package/esm/fields/field.js +74 -0
- package/esm/fields/field.js.map +1 -0
- package/esm/fields/has-inverse-field.d.ts +4 -0
- package/esm/fields/has-inverse-field.js +2 -0
- package/esm/fields/has-inverse-field.js.map +1 -0
- package/esm/fields/has-many-field.d.ts +64 -0
- package/esm/fields/has-many-field.js +58 -0
- package/esm/fields/has-many-field.js.map +1 -0
- package/esm/fields/has-one-field.d.ts +64 -0
- package/esm/fields/has-one-field.js +57 -0
- package/esm/fields/has-one-field.js.map +1 -0
- package/esm/fields/index.d.ts +40 -0
- package/esm/fields/index.js +21 -0
- package/esm/fields/index.js.map +1 -0
- package/esm/fields/json-field.d.ts +14 -0
- package/esm/fields/json-field.js +17 -0
- package/esm/fields/json-field.js.map +1 -0
- package/esm/fields/number-field.d.ts +32 -0
- package/esm/fields/number-field.js +28 -0
- package/esm/fields/number-field.js.map +1 -0
- package/esm/fields/password-field.d.ts +21 -0
- package/esm/fields/password-field.js +71 -0
- package/esm/fields/password-field.js.map +1 -0
- package/esm/fields/radio-field.d.ts +14 -0
- package/esm/fields/radio-field.js +49 -0
- package/esm/fields/radio-field.js.map +1 -0
- package/esm/fields/relation-field.d.ts +20 -0
- package/esm/fields/relation-field.js +27 -0
- package/esm/fields/relation-field.js.map +1 -0
- package/esm/fields/sort-field.d.ts +16 -0
- package/esm/fields/sort-field.js +90 -0
- package/esm/fields/sort-field.js.map +1 -0
- package/esm/fields/string-field.d.ts +8 -0
- package/esm/fields/string-field.js +8 -0
- package/esm/fields/string-field.js.map +1 -0
- package/esm/fields/text-field.d.ts +8 -0
- package/esm/fields/text-field.js +8 -0
- package/esm/fields/text-field.js.map +1 -0
- package/esm/fields/time-field.d.ts +8 -0
- package/esm/fields/time-field.js +8 -0
- package/esm/fields/time-field.js.map +1 -0
- package/esm/fields/uid-field.d.ts +10 -0
- package/esm/fields/uid-field.js +27 -0
- package/esm/fields/uid-field.js.map +1 -0
- package/esm/fields/virtual-field.d.ts +8 -0
- package/esm/fields/virtual-field.js +8 -0
- package/esm/fields/virtual-field.js.map +1 -0
- package/esm/filter-parser.d.ts +27 -0
- package/esm/filter-parser.js +185 -0
- package/esm/filter-parser.js.map +1 -0
- package/esm/index.d.ts +15 -0
- package/esm/index.js +16 -0
- package/esm/index.js.map +1 -0
- package/esm/magic-attribute-model.d.ts +7 -0
- package/esm/magic-attribute-model.js +70 -0
- package/esm/magic-attribute-model.js.map +1 -0
- package/esm/mock-database.d.ts +22 -0
- package/esm/mock-database.js +34 -0
- package/esm/mock-database.js.map +1 -0
- package/esm/model-hook.d.ts +12 -0
- package/esm/model-hook.js +61 -0
- package/esm/model-hook.js.map +1 -0
- package/esm/model.d.ts +15 -0
- package/esm/model.js +80 -0
- package/esm/model.js.map +1 -0
- package/esm/operators/array.d.ts +26 -0
- package/esm/operators/array.js +105 -0
- package/esm/operators/array.js.map +1 -0
- package/esm/operators/association.d.ts +10 -0
- package/esm/operators/association.js +14 -0
- package/esm/operators/association.js.map +1 -0
- package/esm/operators/date.d.ts +34 -0
- package/esm/operators/date.js +35 -0
- package/esm/operators/date.js.map +1 -0
- package/esm/operators/empty.d.ts +28 -0
- package/esm/operators/empty.js +58 -0
- package/esm/operators/empty.js.map +1 -0
- package/esm/operators/index.d.ts +2 -0
- package/esm/operators/index.js +2 -0
- package/esm/operators/index.js.map +1 -0
- package/esm/operators/ne.d.ts +10 -0
- package/esm/operators/ne.js +12 -0
- package/esm/operators/ne.js.map +1 -0
- package/esm/operators/string.d.ts +21 -0
- package/esm/operators/string.js +35 -0
- package/esm/operators/string.js.map +1 -0
- package/esm/operators/utils.d.ts +4 -0
- package/esm/operators/utils.js +11 -0
- package/esm/operators/utils.js.map +1 -0
- package/esm/options-parser.d.ts +31 -0
- package/esm/options-parser.js +225 -0
- package/esm/options-parser.js.map +1 -0
- package/esm/playground.d.ts +1 -0
- package/esm/playground.js +53 -0
- package/esm/playground.js.map +1 -0
- package/esm/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/esm/relation-repository/belongs-to-many-repository.js +199 -0
- package/esm/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/esm/relation-repository/belongs-to-repository.d.ts +17 -0
- package/esm/relation-repository/belongs-to-repository.js +4 -0
- package/esm/relation-repository/belongs-to-repository.js.map +1 -0
- package/esm/relation-repository/hasmany-repository.d.ts +23 -0
- package/esm/relation-repository/hasmany-repository.js +125 -0
- package/esm/relation-repository/hasmany-repository.js.map +1 -0
- package/esm/relation-repository/hasone-repository.d.ts +17 -0
- package/esm/relation-repository/hasone-repository.js +4 -0
- package/esm/relation-repository/hasone-repository.js.map +1 -0
- package/esm/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/esm/relation-repository/multiple-relation-repository.js +149 -0
- package/esm/relation-repository/multiple-relation-repository.js.map +1 -0
- package/esm/relation-repository/relation-repository.d.ts +32 -0
- package/esm/relation-repository/relation-repository.js +93 -0
- package/esm/relation-repository/relation-repository.js.map +1 -0
- package/esm/relation-repository/single-relation-repository.d.ts +23 -0
- package/esm/relation-repository/single-relation-repository.js +96 -0
- package/esm/relation-repository/single-relation-repository.js.map +1 -0
- package/esm/relation-repository/types.d.ts +7 -0
- package/esm/relation-repository/types.js +2 -0
- package/esm/relation-repository/types.js.map +1 -0
- package/esm/repository.d.ts +165 -0
- package/esm/repository.js +276 -0
- package/esm/repository.js.map +1 -0
- package/esm/transaction-decorator.d.ts +1 -0
- package/esm/transaction-decorator.js +63 -0
- package/esm/transaction-decorator.js.map +1 -0
- package/esm/update-associations.d.ts +60 -0
- package/esm/update-associations.js +362 -0
- package/esm/update-associations.js.map +1 -0
- package/esm/update-guard.d.ts +26 -0
- package/esm/update-guard.js +122 -0
- package/esm/update-guard.js.map +1 -0
- package/lib/collection-importer.d.ts +7 -0
- package/lib/collection-importer.js +75 -0
- package/lib/collection-importer.js.map +1 -0
- package/lib/collection.d.ts +73 -0
- package/lib/collection.js +231 -0
- package/lib/collection.js.map +1 -0
- package/lib/database.d.ts +93 -43
- package/lib/database.js +277 -471
- package/lib/database.js.map +1 -1
- package/lib/fields/array-field.d.ts +11 -0
- package/lib/fields/array-field.js +30 -0
- package/lib/fields/array-field.js.map +1 -0
- package/lib/fields/belongs-to-field.d.ts +12 -0
- package/lib/fields/belongs-to-field.js +61 -0
- package/lib/fields/belongs-to-field.js.map +1 -0
- package/lib/fields/belongs-to-many-field.d.ts +11 -0
- package/lib/fields/belongs-to-many-field.js +59 -0
- package/lib/fields/belongs-to-many-field.js.map +1 -0
- package/lib/fields/boolean-field.d.ts +8 -0
- package/lib/fields/boolean-field.js +12 -0
- package/lib/fields/boolean-field.js.map +1 -0
- package/lib/fields/context-field.d.ts +13 -0
- package/lib/fields/context-field.js +50 -0
- package/lib/fields/context-field.js.map +1 -0
- package/lib/fields/date-field.d.ts +8 -0
- package/lib/fields/date-field.js +12 -0
- package/lib/fields/date-field.js.map +1 -0
- package/lib/fields/field.d.ts +37 -0
- package/lib/fields/field.js +81 -0
- package/lib/fields/field.js.map +1 -0
- package/lib/fields/has-inverse-field.d.ts +4 -0
- package/lib/fields/has-inverse-field.js +3 -0
- package/lib/fields/has-inverse-field.js.map +1 -0
- package/lib/fields/has-many-field.d.ts +64 -0
- package/lib/fields/has-many-field.js +62 -0
- package/lib/fields/has-many-field.js.map +1 -0
- package/lib/fields/has-one-field.d.ts +64 -0
- package/lib/fields/has-one-field.js +61 -0
- package/lib/fields/has-one-field.js.map +1 -0
- package/lib/fields/index.d.ts +40 -10
- package/lib/fields/index.js +31 -138
- package/lib/fields/index.js.map +1 -1
- package/lib/fields/json-field.d.ts +14 -0
- package/lib/fields/json-field.js +22 -0
- package/lib/fields/json-field.js.map +1 -0
- package/lib/fields/number-field.d.ts +32 -0
- package/lib/fields/number-field.js +36 -0
- package/lib/fields/number-field.js.map +1 -0
- package/lib/fields/password-field.d.ts +21 -0
- package/lib/fields/password-field.js +78 -0
- package/lib/fields/password-field.js.map +1 -0
- package/lib/fields/radio-field.d.ts +14 -0
- package/lib/fields/radio-field.js +53 -0
- package/lib/fields/radio-field.js.map +1 -0
- package/lib/fields/relation-field.d.ts +20 -0
- package/lib/fields/relation-field.js +31 -0
- package/lib/fields/relation-field.js.map +1 -0
- package/lib/fields/sort-field.d.ts +16 -0
- package/lib/fields/sort-field.js +94 -0
- package/lib/fields/sort-field.js.map +1 -0
- package/lib/fields/string-field.d.ts +8 -0
- package/lib/fields/string-field.js +12 -0
- package/lib/fields/string-field.js.map +1 -0
- package/lib/fields/text-field.d.ts +8 -0
- package/lib/fields/text-field.js +12 -0
- package/lib/fields/text-field.js.map +1 -0
- package/lib/fields/time-field.d.ts +8 -0
- package/lib/fields/time-field.js +12 -0
- package/lib/fields/time-field.js.map +1 -0
- package/lib/fields/uid-field.d.ts +10 -0
- package/lib/fields/uid-field.js +31 -0
- package/lib/fields/uid-field.js.map +1 -0
- package/lib/fields/virtual-field.d.ts +8 -0
- package/lib/fields/virtual-field.js +12 -0
- package/lib/fields/virtual-field.js.map +1 -0
- package/lib/filter-parser.d.ts +27 -0
- package/lib/filter-parser.js +191 -0
- package/lib/filter-parser.js.map +1 -0
- package/lib/index.d.ts +13 -6
- package/lib/index.js +27 -60
- package/lib/index.js.map +1 -1
- package/lib/magic-attribute-model.d.ts +7 -0
- package/lib/magic-attribute-model.js +77 -0
- package/lib/magic-attribute-model.js.map +1 -0
- package/lib/mock-database.d.ts +22 -0
- package/lib/mock-database.js +40 -0
- package/lib/mock-database.js.map +1 -0
- package/lib/model-hook.d.ts +12 -0
- package/lib/model-hook.js +68 -0
- package/lib/model-hook.js.map +1 -0
- package/lib/model.d.ts +11 -45
- package/lib/model.js +76 -452
- package/lib/model.js.map +1 -1
- package/lib/operators/array.d.ts +26 -0
- package/lib/operators/array.js +107 -0
- package/lib/operators/array.js.map +1 -0
- package/lib/operators/association.d.ts +10 -0
- package/lib/operators/association.js +16 -0
- package/lib/operators/association.js.map +1 -0
- package/lib/operators/date.d.ts +34 -0
- package/lib/operators/date.js +40 -0
- package/lib/operators/date.js.map +1 -0
- package/lib/operators/empty.d.ts +28 -0
- package/lib/operators/empty.js +82 -0
- package/lib/operators/empty.js.map +1 -0
- package/lib/operators/index.d.ts +2 -0
- package/lib/operators/index.js +4 -0
- package/lib/operators/index.js.map +1 -0
- package/lib/operators/ne.d.ts +10 -0
- package/lib/operators/ne.js +14 -0
- package/lib/operators/ne.js.map +1 -0
- package/lib/operators/string.d.ts +21 -0
- package/lib/operators/string.js +37 -0
- package/lib/operators/string.js.map +1 -0
- package/lib/operators/utils.d.ts +4 -0
- package/lib/operators/utils.js +16 -0
- package/lib/operators/utils.js.map +1 -0
- package/lib/options-parser.d.ts +31 -0
- package/lib/options-parser.js +232 -0
- package/lib/options-parser.js.map +1 -0
- package/lib/playground.d.ts +1 -0
- package/lib/playground.js +55 -0
- package/lib/playground.js.map +1 -0
- package/lib/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/lib/relation-repository/belongs-to-many-repository.js +206 -0
- package/lib/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/lib/relation-repository/belongs-to-repository.d.ts +17 -0
- package/lib/relation-repository/belongs-to-repository.js +8 -0
- package/lib/relation-repository/belongs-to-repository.js.map +1 -0
- package/lib/relation-repository/hasmany-repository.d.ts +23 -0
- package/lib/relation-repository/hasmany-repository.js +129 -0
- package/lib/relation-repository/hasmany-repository.js.map +1 -0
- package/lib/relation-repository/hasone-repository.d.ts +17 -0
- package/lib/relation-repository/hasone-repository.js +8 -0
- package/lib/relation-repository/hasone-repository.js.map +1 -0
- package/lib/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/lib/relation-repository/multiple-relation-repository.js +153 -0
- package/lib/relation-repository/multiple-relation-repository.js.map +1 -0
- package/lib/relation-repository/relation-repository.d.ts +32 -0
- package/lib/relation-repository/relation-repository.js +100 -0
- package/lib/relation-repository/relation-repository.js.map +1 -0
- package/lib/relation-repository/single-relation-repository.d.ts +23 -0
- package/lib/relation-repository/single-relation-repository.js +103 -0
- package/lib/relation-repository/single-relation-repository.js.map +1 -0
- package/lib/relation-repository/types.d.ts +7 -0
- package/lib/relation-repository/types.js +3 -0
- package/lib/relation-repository/types.js.map +1 -0
- package/lib/repository.d.ts +165 -0
- package/lib/repository.js +302 -0
- package/lib/repository.js.map +1 -0
- package/lib/transaction-decorator.d.ts +1 -0
- package/lib/transaction-decorator.js +70 -0
- package/lib/transaction-decorator.js.map +1 -0
- package/lib/update-associations.d.ts +60 -0
- package/lib/update-associations.js +374 -0
- package/lib/update-associations.js.map +1 -0
- package/lib/update-guard.d.ts +26 -0
- package/lib/update-guard.js +129 -0
- package/lib/update-guard.js.map +1 -0
- package/package.json +17 -5
- package/src/__tests__/collection-importer.test.ts +21 -0
- package/src/__tests__/collection.sortable.test.ts +65 -0
- package/src/__tests__/collection.test.ts +218 -0
- package/src/__tests__/database.import.test.ts +33 -0
- package/src/__tests__/database.test.ts +229 -0
- package/src/__tests__/field-options/hidden.test.ts +302 -0
- package/src/__tests__/field-options/sort-by.test.ts +220 -0
- package/src/__tests__/fields/belongs-to-field.test.ts +162 -0
- package/src/__tests__/fields/belongs-to-many-field.test.ts +61 -0
- package/src/__tests__/fields/context-field.test.ts +140 -0
- package/src/__tests__/fields/has-many-field.test.ts +152 -0
- package/src/__tests__/fields/has-one-field.test.ts +67 -0
- package/src/__tests__/fields/password-field.test.ts +30 -0
- package/src/__tests__/fields/sort-field.test.ts +133 -0
- package/src/__tests__/fields/string-field.test.ts +77 -0
- package/src/__tests__/filter-parser.test.ts +104 -0
- package/src/__tests__/fixtures/c0/a.ts +6 -0
- package/src/__tests__/fixtures/c1/b.ts +6 -0
- package/src/__tests__/fixtures/c2/a.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/extend.ts +6 -0
- package/src/__tests__/fixtures/collections/extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/posts.ts +4 -0
- package/src/__tests__/fixtures/collections/tags.js +4 -0
- package/src/__tests__/fixtures/collections/test.jpg +0 -0
- package/src/__tests__/fixtures/collections/user.json +9 -0
- package/src/__tests__/index.ts +1 -0
- package/src/__tests__/magic-attribute-model.test.ts +103 -0
- package/src/__tests__/model.test.ts +60 -0
- package/src/__tests__/operator/array-operator.test.ts +268 -0
- package/src/__tests__/operator/association-operator.test.ts +263 -0
- package/src/__tests__/operator/date-operator.test.ts +165 -0
- package/src/__tests__/operator/empty-operator.test.ts +77 -0
- package/src/__tests__/operator/ne.test.ts +33 -0
- package/src/__tests__/operator/string-operator.test.ts +62 -0
- package/src/__tests__/option-parser.test.ts +185 -0
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +697 -0
- package/src/__tests__/relation-repository/has-many-repository.test.ts +414 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +77 -0
- package/src/__tests__/repository/count.test.ts +180 -0
- package/src/__tests__/repository/create.test.ts +163 -0
- package/src/__tests__/repository/destroy.test.ts +196 -0
- package/src/__tests__/repository/find.test.ts +247 -0
- package/src/__tests__/repository/update.test.ts +60 -0
- package/src/__tests__/repository.test.ts +438 -0
- package/src/__tests__/update-associations.test.ts +412 -0
- package/src/__tests__/update-guard.test.ts +376 -0
- package/src/collection-importer.ts +49 -0
- package/src/collection.ts +282 -0
- package/src/database.ts +340 -0
- package/src/fields/array-field.ts +35 -0
- package/src/fields/belongs-to-field.ts +76 -0
- package/src/fields/belongs-to-many-field.ts +77 -0
- package/src/fields/boolean-field.ts +12 -0
- package/src/fields/context-field.ts +45 -0
- package/src/fields/date-field.ts +12 -0
- package/src/fields/field.ts +105 -0
- package/src/fields/has-inverse-field.ts +5 -0
- package/src/fields/has-many-field.ts +143 -0
- package/src/fields/has-one-field.ts +136 -0
- package/src/fields/index.ts +72 -0
- package/src/fields/json-field.ts +25 -0
- package/src/fields/number-field.ts +52 -0
- package/src/fields/password-field.ts +72 -0
- package/src/fields/radio-field.ts +50 -0
- package/src/fields/relation-field.ts +37 -0
- package/src/fields/sort-field.ts +96 -0
- package/src/fields/string-field.ts +12 -0
- package/src/fields/text-field.ts +12 -0
- package/src/fields/time-field.ts +12 -0
- package/src/fields/uid-field.ts +24 -0
- package/src/fields/virtual-field.ts +12 -0
- package/src/filter-parser.ts +243 -0
- package/src/index.ts +16 -0
- package/src/magic-attribute-model.ts +62 -0
- package/src/mock-database.ts +42 -0
- package/src/model-hook.ts +69 -0
- package/src/model.ts +114 -0
- package/src/operators/array.ts +145 -0
- package/src/operators/association.ts +14 -0
- package/src/operators/date.ts +41 -0
- package/src/operators/empty.ts +75 -0
- package/src/operators/index.ts +8 -0
- package/src/operators/ne.ts +12 -0
- package/src/operators/string.ts +40 -0
- package/src/operators/utils.ts +13 -0
- package/src/options-parser.ts +285 -0
- package/src/playground.ts +52 -0
- package/src/relation-repository/belongs-to-many-repository.ts +240 -0
- package/src/relation-repository/belongs-to-repository.ts +23 -0
- package/src/relation-repository/hasmany-repository.ts +145 -0
- package/src/relation-repository/hasone-repository.ts +23 -0
- package/src/relation-repository/multiple-relation-repository.ts +198 -0
- package/src/relation-repository/relation-repository.ts +114 -0
- package/src/relation-repository/single-relation-repository.ts +99 -0
- package/src/relation-repository/types.ts +15 -0
- package/src/repository.ts +478 -0
- package/src/transaction-decorator.ts +58 -0
- package/src/update-associations.ts +478 -0
- package/src/update-guard.ts +167 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +5 -0
- package/examples/index.ts +0 -125
- package/examples/plugins/db-driven/index.ts +0 -25
- package/examples/plugins/db-driven/tables/fields.ts +0 -78
- package/examples/plugins/db-driven/tables/tables.ts +0 -53
- package/examples/tables/bar.js +0 -26
- package/examples/tables/comments.ts +0 -19
- package/examples/tables/foo.json +0 -3
- package/examples/tables/posts.ts +0 -28
- package/examples/tables/profiles.ts +0 -23
- package/examples/tables/tags.ts +0 -15
- package/examples/tables/users.ts +0 -34
- package/lib/database.d.ts.map +0 -1
- package/lib/fields/field-types.d.ts +0 -419
- package/lib/fields/field-types.d.ts.map +0 -1
- package/lib/fields/field-types.js +0 -1222
- package/lib/fields/field-types.js.map +0 -1
- package/lib/fields/index.d.ts.map +0 -1
- package/lib/fields/option-types.d.ts +0 -105
- package/lib/fields/option-types.d.ts.map +0 -1
- package/lib/fields/option-types.js +0 -18
- package/lib/fields/option-types.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/model.d.ts.map +0 -1
- package/lib/op.d.ts +0 -45
- package/lib/op.d.ts.map +0 -1
- package/lib/op.js +0 -225
- package/lib/op.js.map +0 -1
- package/lib/table.d.ts +0 -56
- package/lib/table.d.ts.map +0 -1
- package/lib/table.js +0 -456
- package/lib/table.js.map +0 -1
- package/lib/utils.d.ts +0 -26
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -438
- package/lib/utils.js.map +0 -1
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { Op, Sequelize } from 'sequelize';
|
|
2
|
+
import { isPg, isMySQL } from './utils';
|
|
3
|
+
|
|
4
|
+
const getFieldName = (ctx) => {
|
|
5
|
+
const fieldName = ctx.fieldName;
|
|
6
|
+
return fieldName;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const escape = (value, ctx) => {
|
|
10
|
+
const sequelize: Sequelize = ctx.db.sequelize;
|
|
11
|
+
return sequelize.escape(value);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const sqliteExistQuery = (value, ctx) => {
|
|
15
|
+
const fieldName = getFieldName(ctx);
|
|
16
|
+
|
|
17
|
+
const sqlArray = `(${value.map((v) => `'${v}'`).join(', ')})`;
|
|
18
|
+
|
|
19
|
+
const subQuery = `exists (select * from json_each(${fieldName}) where json_each.value in ${sqlArray})`;
|
|
20
|
+
|
|
21
|
+
return subQuery;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const emptyQuery = (ctx, operator: '=' | '>') => {
|
|
25
|
+
const fieldName = getFieldName(ctx);
|
|
26
|
+
|
|
27
|
+
let funcName = 'json_array_length';
|
|
28
|
+
let ifNull = 'IFNULL';
|
|
29
|
+
|
|
30
|
+
if (isPg(ctx)) {
|
|
31
|
+
funcName = 'jsonb_array_length';
|
|
32
|
+
ifNull = 'coalesce';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (isMySQL(ctx)) {
|
|
36
|
+
funcName = 'json_length';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return `(select ${ifNull}(${funcName}(${fieldName}), 0) ${operator} 0)`;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default {
|
|
43
|
+
$match(value, ctx) {
|
|
44
|
+
const fieldName = getFieldName(ctx);
|
|
45
|
+
|
|
46
|
+
if (isPg(ctx)) {
|
|
47
|
+
return {
|
|
48
|
+
[Op.contained]: value,
|
|
49
|
+
[Op.contains]: value,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
value = escape(JSON.stringify(value.sort()), ctx);
|
|
54
|
+
|
|
55
|
+
if (isMySQL(ctx)) {
|
|
56
|
+
return Sequelize.literal(`JSON_CONTAINS(${fieldName}, ${value}) AND JSON_CONTAINS(${value}, ${fieldName})`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
[Op.eq]: Sequelize.literal(`json(${value})`),
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
$notMatch(value, ctx) {
|
|
65
|
+
const fieldName = getFieldName(ctx);
|
|
66
|
+
value = escape(JSON.stringify(value), ctx);
|
|
67
|
+
|
|
68
|
+
if (isPg(ctx)) {
|
|
69
|
+
return Sequelize.literal(`not (${fieldName} <@ ${value}::JSONB and ${fieldName} @> ${value}::JSONB)`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (isMySQL(ctx)) {
|
|
73
|
+
return Sequelize.literal(`not (JSON_CONTAINS(${fieldName}, ${value}) AND JSON_CONTAINS(${value}, ${fieldName}))`);
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
[Op.ne]: Sequelize.literal(`json(${value})`),
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
$anyOf(value, ctx) {
|
|
81
|
+
const fieldName = getFieldName(ctx);
|
|
82
|
+
|
|
83
|
+
if (isPg(ctx)) {
|
|
84
|
+
return Sequelize.literal(
|
|
85
|
+
`${fieldName} ?| ${escape(
|
|
86
|
+
value.map((i) => `${i}`),
|
|
87
|
+
ctx,
|
|
88
|
+
)}`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (isMySQL(ctx)) {
|
|
93
|
+
value = escape(JSON.stringify(value), ctx);
|
|
94
|
+
|
|
95
|
+
return Sequelize.literal(`JSON_OVERLAPS(${fieldName}, ${value})`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const subQuery = sqliteExistQuery(value, ctx);
|
|
99
|
+
|
|
100
|
+
return Sequelize.literal(subQuery);
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
$noneOf(value, ctx) {
|
|
104
|
+
let where;
|
|
105
|
+
|
|
106
|
+
if (isPg(ctx)) {
|
|
107
|
+
const fieldName = getFieldName(ctx);
|
|
108
|
+
// pg single quote
|
|
109
|
+
where = Sequelize.literal(
|
|
110
|
+
`not (${fieldName} ?| ${escape(
|
|
111
|
+
value.map((i) => `${i}`),
|
|
112
|
+
ctx,
|
|
113
|
+
)})`,
|
|
114
|
+
);
|
|
115
|
+
} else if (isMySQL(ctx)) {
|
|
116
|
+
const fieldName = getFieldName(ctx);
|
|
117
|
+
value = escape(JSON.stringify(value), ctx);
|
|
118
|
+
where = Sequelize.literal(`NOT JSON_OVERLAPS(${fieldName}, ${value})`);
|
|
119
|
+
} else {
|
|
120
|
+
const subQuery = sqliteExistQuery(value, ctx);
|
|
121
|
+
|
|
122
|
+
where = Sequelize.literal(`not ${subQuery}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
[Op.or]: [where, { [Op.is]: null }],
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
$arrayEmpty(value, ctx) {
|
|
131
|
+
const subQuery = emptyQuery(ctx, '=');
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
[Op.and]: [Sequelize.literal(`${subQuery}`)],
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
$arrayNotEmpty(value, ctx) {
|
|
139
|
+
const subQuery = emptyQuery(ctx, '>');
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
[Op.and]: [Sequelize.literal(`${subQuery}`)],
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Op } from 'sequelize';
|
|
2
|
+
import moment, { MomentInput } from 'moment';
|
|
3
|
+
function stringToDate(value: string): Date {
|
|
4
|
+
return moment(value).toDate();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function getNextDay(value: MomentInput): Date {
|
|
8
|
+
return moment(value).add(1, 'd').toDate();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
$dateOn(value) {
|
|
13
|
+
return {
|
|
14
|
+
[Op.and]: [{ [Op.gte]: stringToDate(value) }, { [Op.lt]: getNextDay(value) }],
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
$dateNotOn(value) {
|
|
19
|
+
return {
|
|
20
|
+
[Op.or]: [{ [Op.lt]: stringToDate(value) }, { [Op.gte]: getNextDay(value) }],
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
$dateBefore(value) {
|
|
25
|
+
return { [Op.lt]: stringToDate(value) };
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
$dateNotBefore(value) {
|
|
29
|
+
return {
|
|
30
|
+
[Op.gte]: stringToDate(value),
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
$dateAfter(value) {
|
|
35
|
+
return { [Op.gte]: getNextDay(value) };
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
$dateNotAfter(value) {
|
|
39
|
+
return { [Op.lt]: getNextDay(value) };
|
|
40
|
+
},
|
|
41
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { DataTypes, Op } from 'sequelize';
|
|
2
|
+
import { ArrayField, StringField } from '../fields';
|
|
3
|
+
import arrayOperators from './array';
|
|
4
|
+
import lodash, { parseInt } from 'lodash';
|
|
5
|
+
|
|
6
|
+
const findFilterFieldType = (ctx) => {
|
|
7
|
+
const db = ctx.db;
|
|
8
|
+
|
|
9
|
+
let path = ctx.path.split('.');
|
|
10
|
+
|
|
11
|
+
// remove operators
|
|
12
|
+
path.pop();
|
|
13
|
+
|
|
14
|
+
const fieldName = path.pop();
|
|
15
|
+
|
|
16
|
+
let model = ctx.model;
|
|
17
|
+
|
|
18
|
+
const associationPath = path;
|
|
19
|
+
|
|
20
|
+
for (const association of associationPath) {
|
|
21
|
+
if (lodash.isNumber(parseInt(association)) || association.startsWith('$')) {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
model = model.associations[association].target;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const collection = db.modelCollection.get(model);
|
|
29
|
+
|
|
30
|
+
return collection.getField(fieldName);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export default {
|
|
34
|
+
$empty(_, ctx) {
|
|
35
|
+
const field = findFilterFieldType(ctx);
|
|
36
|
+
|
|
37
|
+
if (field instanceof StringField) {
|
|
38
|
+
return {
|
|
39
|
+
[Op.or]: {
|
|
40
|
+
[Op.is]: null,
|
|
41
|
+
[Op.eq]: '',
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (field instanceof ArrayField) {
|
|
47
|
+
return arrayOperators.$arrayEmpty(_, ctx);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
[Op.is]: null,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
$notEmpty(_, ctx) {
|
|
56
|
+
const field = findFilterFieldType(ctx);
|
|
57
|
+
|
|
58
|
+
if (field instanceof StringField) {
|
|
59
|
+
return {
|
|
60
|
+
[Op.and]: {
|
|
61
|
+
[Op.not]: null,
|
|
62
|
+
[Op.ne]: '',
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (field instanceof ArrayField) {
|
|
68
|
+
return arrayOperators.$arrayNotEmpty(_, ctx);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
[Op.not]: null,
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isPg } from './utils';
|
|
2
|
+
import { Op } from 'sequelize';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
$includes(value, ctx) {
|
|
6
|
+
return {
|
|
7
|
+
[isPg(ctx) ? Op.iLike : Op.like]: `%${value}%`,
|
|
8
|
+
};
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
$notIncludes(value, ctx) {
|
|
12
|
+
return {
|
|
13
|
+
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}%`,
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
$startsWith(value, ctx) {
|
|
18
|
+
return {
|
|
19
|
+
[isPg(ctx) ? Op.iLike : Op.like]: `${value}%`,
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
$notStartsWith(value, ctx) {
|
|
24
|
+
return {
|
|
25
|
+
[isPg(ctx) ? Op.notILike : Op.notLike]: `${value}%`,
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
$endWith(value, ctx) {
|
|
30
|
+
return {
|
|
31
|
+
[isPg(ctx) ? Op.iLike : Op.like]: `%${value}`,
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
$notEndWith(value, ctx) {
|
|
36
|
+
return {
|
|
37
|
+
[isPg(ctx) ? Op.notILike : Op.notLike]: `%${value}`,
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const getDialect = (ctx) => {
|
|
2
|
+
return ctx.db.sequelize.getDialect();
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
const isPg = (ctx) => {
|
|
6
|
+
return getDialect(ctx) === 'postgres';
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const isMySQL = (ctx) => {
|
|
10
|
+
return getDialect(ctx) === 'mysql';
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { getDialect, isPg, isMySQL };
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { FindAttributeOptions, ModelCtor, Op } from 'sequelize';
|
|
2
|
+
import { Collection } from './collection';
|
|
3
|
+
import { Database } from './database';
|
|
4
|
+
import FilterParser from './filter-parser';
|
|
5
|
+
import { Appends, Except, FindOptions } from './repository';
|
|
6
|
+
|
|
7
|
+
const debug = require('debug')('noco-database');
|
|
8
|
+
|
|
9
|
+
interface OptionsParserContext {
|
|
10
|
+
collection: Collection;
|
|
11
|
+
targetKey?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class OptionsParser {
|
|
15
|
+
options: FindOptions;
|
|
16
|
+
database: Database;
|
|
17
|
+
collection: Collection;
|
|
18
|
+
model: ModelCtor<any>;
|
|
19
|
+
filterParser: FilterParser;
|
|
20
|
+
context: OptionsParserContext;
|
|
21
|
+
|
|
22
|
+
constructor(options: FindOptions, context: OptionsParserContext) {
|
|
23
|
+
const { collection } = context;
|
|
24
|
+
|
|
25
|
+
this.collection = collection;
|
|
26
|
+
this.model = collection.model;
|
|
27
|
+
this.options = options;
|
|
28
|
+
this.database = collection.context.database;
|
|
29
|
+
this.filterParser = new FilterParser(options?.filter, {
|
|
30
|
+
collection,
|
|
31
|
+
app: {
|
|
32
|
+
ctx: options?.context,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
this.context = context;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
isAssociation(key: string) {
|
|
39
|
+
return this.model.associations[key] !== undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
isAssociationPath(path: string) {
|
|
43
|
+
return this.isAssociation(path.split('.')[0]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
toSequelizeParams() {
|
|
47
|
+
const queryParams = this.filterParser.toSequelizeParams();
|
|
48
|
+
|
|
49
|
+
if (this.options?.filterByTk) {
|
|
50
|
+
queryParams.where = {
|
|
51
|
+
[Op.and]: [
|
|
52
|
+
queryParams.where,
|
|
53
|
+
{
|
|
54
|
+
[this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return this.parseSort(this.parseFields(queryParams));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* parser sort options
|
|
65
|
+
* @param filterParams
|
|
66
|
+
* @protected
|
|
67
|
+
*/
|
|
68
|
+
protected parseSort(filterParams) {
|
|
69
|
+
let sort = this.options?.sort || [];
|
|
70
|
+
if (typeof sort === 'string') {
|
|
71
|
+
sort = sort.split(',');
|
|
72
|
+
}
|
|
73
|
+
const orderParams = sort.map((sortKey: string) => {
|
|
74
|
+
const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';
|
|
75
|
+
const sortField: Array<any> = sortKey.replace('-', '').split('.');
|
|
76
|
+
|
|
77
|
+
// handle sort by association
|
|
78
|
+
if (sortField.length > 1) {
|
|
79
|
+
let associationModel = this.model;
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < sortField.length - 1; i++) {
|
|
82
|
+
const associationKey = sortField[i];
|
|
83
|
+
sortField[i] = associationModel.associations[associationKey].target;
|
|
84
|
+
associationModel = sortField[i];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
sortField.push(direction);
|
|
89
|
+
return sortField;
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (orderParams.length > 0) {
|
|
93
|
+
return {
|
|
94
|
+
order: orderParams,
|
|
95
|
+
...filterParams,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return filterParams;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected parseFields(filterParams: any) {
|
|
103
|
+
const appends = this.options?.appends || [];
|
|
104
|
+
const except = [];
|
|
105
|
+
|
|
106
|
+
let attributes: FindAttributeOptions = {
|
|
107
|
+
include: [],
|
|
108
|
+
exclude: [],
|
|
109
|
+
}; // out put all fields by default
|
|
110
|
+
|
|
111
|
+
if (this.options?.fields) {
|
|
112
|
+
// 将fields拆分为 attributes 和 appends
|
|
113
|
+
for (const field of this.options.fields) {
|
|
114
|
+
if (this.isAssociationPath(field)) {
|
|
115
|
+
// field is association field
|
|
116
|
+
appends.push(field);
|
|
117
|
+
} else {
|
|
118
|
+
// field is model attribute, change attributes to array type
|
|
119
|
+
if (!Array.isArray(attributes)) attributes = [];
|
|
120
|
+
|
|
121
|
+
attributes.push(field);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (this.options?.except) {
|
|
127
|
+
for (const exceptKey of this.options.except) {
|
|
128
|
+
if (this.isAssociationPath(exceptKey)) {
|
|
129
|
+
// except association field
|
|
130
|
+
except.push(exceptKey);
|
|
131
|
+
} else {
|
|
132
|
+
// if attributes is array form, ignore except
|
|
133
|
+
if (Array.isArray(attributes)) continue;
|
|
134
|
+
attributes.exclude.push(exceptKey);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
attributes,
|
|
141
|
+
...this.parseExcept(except, this.parseAppends(appends, filterParams)),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
protected parseExcept(except: Except, filterParams: any) {
|
|
146
|
+
if (!except) return filterParams;
|
|
147
|
+
const setExcept = (queryParams: any, except: string) => {
|
|
148
|
+
// split exceptKey to path form
|
|
149
|
+
// posts.comments.content => ['posts', 'comments', 'content']
|
|
150
|
+
// then set except on include attributes
|
|
151
|
+
const exceptPath = except.split('.');
|
|
152
|
+
const association = exceptPath[0];
|
|
153
|
+
const lastLevel = exceptPath.length <= 2;
|
|
154
|
+
|
|
155
|
+
let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == association);
|
|
156
|
+
|
|
157
|
+
if (existIncludeIndex == -1) {
|
|
158
|
+
// if include not exists, ignore this except
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (lastLevel) {
|
|
163
|
+
// if it not have exclude form
|
|
164
|
+
if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {
|
|
165
|
+
return;
|
|
166
|
+
} else {
|
|
167
|
+
if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {
|
|
168
|
+
queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
for (const exceptKey of except) {
|
|
179
|
+
setExcept(filterParams, exceptKey);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return filterParams;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
protected parseAppends(appends: Appends, filterParams: any) {
|
|
186
|
+
if (!appends) return filterParams;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* set include params
|
|
190
|
+
* @param includeRoot
|
|
191
|
+
* @param appends
|
|
192
|
+
*/
|
|
193
|
+
const setInclude = (model: ModelCtor<any>, queryParams: any, append: string) => {
|
|
194
|
+
const appendFields = append.split('.');
|
|
195
|
+
const appendAssociation = appendFields[0];
|
|
196
|
+
|
|
197
|
+
const associations = model.associations;
|
|
198
|
+
|
|
199
|
+
// if append length less or equal 2
|
|
200
|
+
// example:
|
|
201
|
+
// appends: ['posts']
|
|
202
|
+
// appends: ['posts.title']
|
|
203
|
+
// All of these can be seen as last level
|
|
204
|
+
let lastLevel: boolean = false;
|
|
205
|
+
|
|
206
|
+
if (appendFields.length == 1) {
|
|
207
|
+
lastLevel = true;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (appendFields.length == 2) {
|
|
211
|
+
const associationModel = associations[appendFields[0]].target;
|
|
212
|
+
if (associationModel.rawAttributes[appendFields[1]]) {
|
|
213
|
+
lastLevel = true;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// find association index
|
|
218
|
+
if (queryParams['include'] == undefined) {
|
|
219
|
+
queryParams['include'] = [];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
let existIncludeIndex = queryParams['include'].findIndex(
|
|
223
|
+
(include) => include['association'] == appendAssociation,
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
// if association not exist, create it
|
|
227
|
+
if (existIncludeIndex == -1) {
|
|
228
|
+
// association not exists
|
|
229
|
+
queryParams['include'].push({
|
|
230
|
+
association: appendAssociation,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
existIncludeIndex = 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// end appends
|
|
237
|
+
// without nests association
|
|
238
|
+
if (lastLevel) {
|
|
239
|
+
// get exist association attributes
|
|
240
|
+
let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
|
|
241
|
+
include: [], // all fields are output by default
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// if need set attribute
|
|
245
|
+
if (appendFields.length == 2) {
|
|
246
|
+
if (!Array.isArray(attributes)) {
|
|
247
|
+
attributes = [];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const attributeName = appendFields[1];
|
|
251
|
+
|
|
252
|
+
// push field to it
|
|
253
|
+
attributes.push(attributeName);
|
|
254
|
+
} else {
|
|
255
|
+
// if attributes is empty array, change it to object
|
|
256
|
+
if (Array.isArray(attributes) && attributes.length == 0) {
|
|
257
|
+
attributes = {
|
|
258
|
+
include: [],
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// set new attributes
|
|
264
|
+
queryParams['include'][existIncludeIndex] = {
|
|
265
|
+
...queryParams['include'][existIncludeIndex],
|
|
266
|
+
attributes,
|
|
267
|
+
};
|
|
268
|
+
} else {
|
|
269
|
+
setInclude(
|
|
270
|
+
model.associations[queryParams['include'][existIncludeIndex].association].target,
|
|
271
|
+
queryParams['include'][existIncludeIndex],
|
|
272
|
+
appendFields.filter((_, index) => index !== 0).join('.'),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// handle every appends
|
|
278
|
+
for (const append of appends) {
|
|
279
|
+
setInclude(this.model, filterParams, append);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
debug('filter params: %o', filterParams);
|
|
283
|
+
return filterParams;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Database } from './database';
|
|
2
|
+
import FilterParser from './filter-parser';
|
|
3
|
+
|
|
4
|
+
const db = new Database({
|
|
5
|
+
dialect: 'sqlite',
|
|
6
|
+
dialectModule: require('sqlite3'),
|
|
7
|
+
storage: ':memory:',
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
(async () => {
|
|
11
|
+
const User = db.collection({
|
|
12
|
+
name: 'users',
|
|
13
|
+
fields: [{ type: 'string', name: 'name' }],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const Post = db.collection({
|
|
17
|
+
name: 'posts',
|
|
18
|
+
fields: [
|
|
19
|
+
{ type: 'string', name: 'title' },
|
|
20
|
+
{
|
|
21
|
+
type: 'belongsTo',
|
|
22
|
+
name: 'user',
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await db.sync();
|
|
28
|
+
|
|
29
|
+
const repository = User.repository;
|
|
30
|
+
|
|
31
|
+
await repository.createMany({
|
|
32
|
+
records: [
|
|
33
|
+
{ name: 'u1', posts: [{ title: 'u1t1' }] },
|
|
34
|
+
{ name: 'u2', posts: [{ title: 'u2t1' }] },
|
|
35
|
+
{ name: 'u3', posts: [{ title: 'u3t1' }] },
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const Model = User.model;
|
|
40
|
+
const user = await Model.findOne({
|
|
41
|
+
subQuery: false,
|
|
42
|
+
where: {
|
|
43
|
+
'$posts.title$': 'u1t1',
|
|
44
|
+
},
|
|
45
|
+
include: { association: 'posts', attributes: [] },
|
|
46
|
+
attributes: {
|
|
47
|
+
include: [],
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log(user.toJSON());
|
|
52
|
+
})();
|