@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,240 @@
|
|
|
1
|
+
import lodash from 'lodash';
|
|
2
|
+
import { BelongsToMany, Op, Transaction } from 'sequelize';
|
|
3
|
+
import { Model } from '../model';
|
|
4
|
+
import { CreateOptions, DestroyOptions, FindOptions, TargetKey, UpdateOptions } from '../repository';
|
|
5
|
+
import { updateThroughTableValue } from '../update-associations';
|
|
6
|
+
import { FindAndCountOptions, FindOneOptions, MultipleRelationRepository } from './multiple-relation-repository';
|
|
7
|
+
import { transaction } from './relation-repository';
|
|
8
|
+
import { AssociatedOptions, PrimaryKeyWithThroughValues } from './types';
|
|
9
|
+
|
|
10
|
+
type CreateBelongsToManyOptions = CreateOptions;
|
|
11
|
+
|
|
12
|
+
interface IBelongsToManyRepository<M extends Model> {
|
|
13
|
+
find(options?: FindOptions): Promise<M[]>;
|
|
14
|
+
findAndCount(options?: FindAndCountOptions): Promise<[M[], number]>;
|
|
15
|
+
findOne(options?: FindOneOptions): Promise<M>;
|
|
16
|
+
// 新增并关联,存在中间表数据
|
|
17
|
+
create(options?: CreateBelongsToManyOptions): Promise<M>;
|
|
18
|
+
// 更新,存在中间表数据
|
|
19
|
+
update(options?: UpdateOptions): Promise<M>;
|
|
20
|
+
// 删除
|
|
21
|
+
destroy(options?: number | string | number[] | string[] | DestroyOptions): Promise<Boolean>;
|
|
22
|
+
// 建立关联
|
|
23
|
+
set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
24
|
+
// 附加关联,存在中间表数据
|
|
25
|
+
add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
26
|
+
// 移除关联
|
|
27
|
+
remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
28
|
+
toggle(options: TargetKey | { pk?: TargetKey; transaction?: Transaction }): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class BelongsToManyRepository extends MultipleRelationRepository implements IBelongsToManyRepository<any> {
|
|
32
|
+
@transaction()
|
|
33
|
+
async create(options?: CreateBelongsToManyOptions): Promise<any> {
|
|
34
|
+
const transaction = await this.getTransaction(options);
|
|
35
|
+
|
|
36
|
+
const createAccessor = this.accessors().create;
|
|
37
|
+
|
|
38
|
+
const values = options.values || {};
|
|
39
|
+
|
|
40
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
41
|
+
|
|
42
|
+
const createOptions = {
|
|
43
|
+
...options,
|
|
44
|
+
through: values[this.throughName()],
|
|
45
|
+
transaction,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return sourceModel[createAccessor](values, createOptions);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@transaction((args, transaction) => {
|
|
52
|
+
return {
|
|
53
|
+
filterByTk: args[0],
|
|
54
|
+
transaction,
|
|
55
|
+
};
|
|
56
|
+
})
|
|
57
|
+
async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean> {
|
|
58
|
+
const transaction = await this.getTransaction(options);
|
|
59
|
+
const association = <BelongsToMany>this.association;
|
|
60
|
+
|
|
61
|
+
const instancesToIds = (instances) => {
|
|
62
|
+
return instances.map((instance) => instance.get(this.targetKey()));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Through Table
|
|
66
|
+
const throughTableWhere: Array<any> = [
|
|
67
|
+
{
|
|
68
|
+
[association.foreignKey]: this.sourceKeyValue,
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
let ids;
|
|
73
|
+
|
|
74
|
+
if (options && options['filter']) {
|
|
75
|
+
const instances = await this.find({
|
|
76
|
+
filter: options['filter'],
|
|
77
|
+
transaction,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
ids = instancesToIds(instances);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (options && options['filterByTk']) {
|
|
84
|
+
const instances = (<any>this.association).toInstanceArray(options['filterByTk']);
|
|
85
|
+
ids = ids ? lodash.intersection(ids, instancesToIds(instances)) : instancesToIds(instances);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (options && !options['filterByTk'] && !options['filter']) {
|
|
89
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
90
|
+
|
|
91
|
+
const instances = await sourceModel[this.accessors().get]({
|
|
92
|
+
transaction,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
ids = instancesToIds(instances);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throughTableWhere.push({
|
|
99
|
+
[association.otherKey]: {
|
|
100
|
+
[Op.in]: ids,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// delete through table data
|
|
105
|
+
await this.throughModel().destroy({
|
|
106
|
+
where: throughTableWhere,
|
|
107
|
+
transaction,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
await this.targetModel.destroy({
|
|
111
|
+
where: {
|
|
112
|
+
[this.targetKey()]: {
|
|
113
|
+
[Op.in]: ids,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
transaction,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
protected async setTargets(
|
|
123
|
+
call: 'add' | 'set',
|
|
124
|
+
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,
|
|
125
|
+
) {
|
|
126
|
+
let handleKeys: TargetKey[] | PrimaryKeyWithThroughValues[];
|
|
127
|
+
|
|
128
|
+
const transaction = await this.getTransaction(options, false);
|
|
129
|
+
|
|
130
|
+
if (lodash.isPlainObject(options)) {
|
|
131
|
+
options = (<AssociatedOptions>options).tk || [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (lodash.isString(options) || lodash.isNumber(options)) {
|
|
135
|
+
handleKeys = [<TargetKey>options];
|
|
136
|
+
} // if it is type primaryKeyWithThroughValues
|
|
137
|
+
else if (lodash.isArray(options) && options.length == 2 && lodash.isPlainObject(options[0][1])) {
|
|
138
|
+
handleKeys = [<PrimaryKeyWithThroughValues>options];
|
|
139
|
+
} else {
|
|
140
|
+
handleKeys = <TargetKey[] | PrimaryKeyWithThroughValues[]>options;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
144
|
+
|
|
145
|
+
const setObj = (<any>handleKeys).reduce((carry, item) => {
|
|
146
|
+
if (Array.isArray(item)) {
|
|
147
|
+
carry[item[0]] = item[1];
|
|
148
|
+
} else {
|
|
149
|
+
carry[item] = true;
|
|
150
|
+
}
|
|
151
|
+
return carry;
|
|
152
|
+
}, {});
|
|
153
|
+
|
|
154
|
+
await sourceModel[this.accessors()[call]](Object.keys(setObj), {
|
|
155
|
+
transaction,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
for (const [id, throughValues] of Object.entries(setObj)) {
|
|
159
|
+
if (typeof throughValues === 'object') {
|
|
160
|
+
const instance = await this.targetModel.findByPk(id, {
|
|
161
|
+
transaction,
|
|
162
|
+
});
|
|
163
|
+
await updateThroughTableValue(instance, this.throughName(), throughValues, sourceModel, transaction);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@transaction((args, transaction) => {
|
|
169
|
+
return {
|
|
170
|
+
tk: args[0],
|
|
171
|
+
transaction,
|
|
172
|
+
};
|
|
173
|
+
})
|
|
174
|
+
async add(
|
|
175
|
+
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,
|
|
176
|
+
): Promise<void> {
|
|
177
|
+
await this.setTargets('add', options);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@transaction((args, transaction) => {
|
|
181
|
+
return {
|
|
182
|
+
tk: args[0],
|
|
183
|
+
transaction,
|
|
184
|
+
};
|
|
185
|
+
})
|
|
186
|
+
async set(
|
|
187
|
+
options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,
|
|
188
|
+
): Promise<void> {
|
|
189
|
+
await this.setTargets('set', options);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
@transaction((args, transaction) => {
|
|
193
|
+
return {
|
|
194
|
+
tk: args[0],
|
|
195
|
+
transaction,
|
|
196
|
+
};
|
|
197
|
+
})
|
|
198
|
+
async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void> {
|
|
199
|
+
const transaction = await this.getTransaction(options);
|
|
200
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
201
|
+
|
|
202
|
+
const has = await sourceModel[this.accessors().hasSingle](options['tk'], {
|
|
203
|
+
transaction,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
if (has) {
|
|
207
|
+
await this.remove({
|
|
208
|
+
...(<any>options),
|
|
209
|
+
transaction,
|
|
210
|
+
});
|
|
211
|
+
} else {
|
|
212
|
+
await this.add({
|
|
213
|
+
...(<any>options),
|
|
214
|
+
transaction,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
extendFindOptions(findOptions) {
|
|
222
|
+
let joinTableAttributes;
|
|
223
|
+
if (lodash.get(findOptions, 'fields')) {
|
|
224
|
+
joinTableAttributes = [];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
...findOptions,
|
|
229
|
+
joinTableAttributes,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
throughName() {
|
|
234
|
+
return this.throughModel().name;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
throughModel() {
|
|
238
|
+
return (<any>this.association).through.model;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Model } from '../model';
|
|
2
|
+
import { CreateOptions, UpdateOptions } from '../repository';
|
|
3
|
+
import { SingleRelationFindOption, SingleRelationRepository } from './single-relation-repository';
|
|
4
|
+
|
|
5
|
+
interface BelongsToFindOptions extends SingleRelationFindOption {}
|
|
6
|
+
|
|
7
|
+
interface IBelongsToRepository<M extends Model> {
|
|
8
|
+
// 不需要 findOne,find 就是 findOne
|
|
9
|
+
find(options?: BelongsToFindOptions): Promise<M>;
|
|
10
|
+
findOne(options?: BelongsToFindOptions): Promise<M>;
|
|
11
|
+
// 新增并关联,如果存在关联,解除之后,与新数据建立关联
|
|
12
|
+
create(options?: CreateOptions): Promise<M>;
|
|
13
|
+
// 更新
|
|
14
|
+
update(options?: UpdateOptions): Promise<M>;
|
|
15
|
+
// 删除
|
|
16
|
+
destroy(): Promise<Boolean>;
|
|
17
|
+
// 建立关联
|
|
18
|
+
set(primaryKey: any): Promise<void>;
|
|
19
|
+
// 移除关联
|
|
20
|
+
remove(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class BelongsToRepository extends SingleRelationRepository implements IBelongsToRepository<any> {}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { omit } from 'lodash';
|
|
2
|
+
import { HasMany, Op } from 'sequelize';
|
|
3
|
+
import { Model } from '../model';
|
|
4
|
+
import { CreateOptions, DestroyOptions, FindOptions, TargetKey, TK, UpdateOptions } from '../repository';
|
|
5
|
+
import {
|
|
6
|
+
AssociatedOptions,
|
|
7
|
+
FindAndCountOptions,
|
|
8
|
+
FindOneOptions,
|
|
9
|
+
MultipleRelationRepository
|
|
10
|
+
} from './multiple-relation-repository';
|
|
11
|
+
import { transaction } from './relation-repository';
|
|
12
|
+
|
|
13
|
+
interface IHasManyRepository<M extends Model> {
|
|
14
|
+
find(options?: FindOptions): Promise<M>;
|
|
15
|
+
findAndCount(options?: FindAndCountOptions): Promise<[M[], number]>;
|
|
16
|
+
findOne(options?: FindOneOptions): Promise<M>;
|
|
17
|
+
// 新增并关联
|
|
18
|
+
create(options?: CreateOptions): Promise<M>;
|
|
19
|
+
// 更新
|
|
20
|
+
update(options?: UpdateOptions): Promise<M>;
|
|
21
|
+
// 删除
|
|
22
|
+
destroy(options?: TK | DestroyOptions): Promise<Boolean>;
|
|
23
|
+
// 建立关联
|
|
24
|
+
set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
25
|
+
// 附加关联
|
|
26
|
+
add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
27
|
+
// 移除关联
|
|
28
|
+
remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class HasManyRepository extends MultipleRelationRepository implements IHasManyRepository<any> {
|
|
32
|
+
async find(options?: FindOptions): Promise<any> {
|
|
33
|
+
const targetRepository = this.targetCollection.repository;
|
|
34
|
+
|
|
35
|
+
const addFilter = {
|
|
36
|
+
[this.association.foreignKey]: this.sourceKeyValue,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (options?.filterByTk) {
|
|
40
|
+
addFilter[this.associationField.targetKey] = options.filterByTk;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return await targetRepository.find({
|
|
44
|
+
...omit(options, ['filterByTk']),
|
|
45
|
+
filter: {
|
|
46
|
+
$and: [options.filter || {}, addFilter],
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@transaction((args, transaction) => {
|
|
52
|
+
return {
|
|
53
|
+
filterByTk: args[0],
|
|
54
|
+
transaction,
|
|
55
|
+
};
|
|
56
|
+
})
|
|
57
|
+
async destroy(options?: TK | DestroyOptions): Promise<Boolean> {
|
|
58
|
+
const transaction = await this.getTransaction(options);
|
|
59
|
+
|
|
60
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
61
|
+
|
|
62
|
+
const where = [
|
|
63
|
+
{
|
|
64
|
+
[this.association.foreignKey]: sourceModel.get((this.association as any).sourceKey),
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
if (options && options['filter']) {
|
|
69
|
+
const filterResult = this.parseFilter(options['filter'], options);
|
|
70
|
+
|
|
71
|
+
if (filterResult.include && filterResult.include.length > 0) {
|
|
72
|
+
return await this.destroyByFilter(options['filter'], transaction);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
where.push(filterResult.where);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (options && options['filterByTk']) {
|
|
79
|
+
if (typeof options === 'object' && options['filterByTk']) {
|
|
80
|
+
options = options['filterByTk'];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
where.push({
|
|
84
|
+
[this.targetKey()]: options,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await this.targetModel.destroy({
|
|
89
|
+
where: {
|
|
90
|
+
[Op.and]: where,
|
|
91
|
+
},
|
|
92
|
+
individualHooks: true,
|
|
93
|
+
transaction,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
handleKeyOfAdd(options) {
|
|
100
|
+
let handleKeys;
|
|
101
|
+
|
|
102
|
+
if (typeof options !== 'object' && !Array.isArray(options)) {
|
|
103
|
+
handleKeys = [options];
|
|
104
|
+
} else {
|
|
105
|
+
handleKeys = options['pk'];
|
|
106
|
+
}
|
|
107
|
+
return handleKeys;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@transaction((args, transaction) => {
|
|
111
|
+
return {
|
|
112
|
+
pk: args[0],
|
|
113
|
+
transaction,
|
|
114
|
+
};
|
|
115
|
+
})
|
|
116
|
+
async set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void> {
|
|
117
|
+
const transaction = await this.getTransaction(options);
|
|
118
|
+
|
|
119
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
120
|
+
|
|
121
|
+
await sourceModel[this.accessors().set](this.handleKeyOfAdd(options), {
|
|
122
|
+
transaction,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@transaction((args, transaction) => {
|
|
127
|
+
return {
|
|
128
|
+
pk: args[0],
|
|
129
|
+
transaction,
|
|
130
|
+
};
|
|
131
|
+
})
|
|
132
|
+
async add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void> {
|
|
133
|
+
const transaction = await this.getTransaction(options);
|
|
134
|
+
|
|
135
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
136
|
+
|
|
137
|
+
await sourceModel[this.accessors().add](this.handleKeyOfAdd(options), {
|
|
138
|
+
transaction,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
accessors() {
|
|
143
|
+
return (<HasMany>this.association).accessors;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Model } from '../model';
|
|
2
|
+
import { CreateOptions } from '../repository';
|
|
3
|
+
import { SingleRelationFindOption, SingleRelationRepository } from './single-relation-repository';
|
|
4
|
+
|
|
5
|
+
interface HasOneFindOptions extends SingleRelationFindOption {}
|
|
6
|
+
|
|
7
|
+
interface IHasOneRepository<M extends Model> {
|
|
8
|
+
// 不需要 findOne,find 就是 findOne
|
|
9
|
+
find(options?: HasOneFindOptions): Promise<M>;
|
|
10
|
+
findOne(options?: HasOneFindOptions): Promise<M>;
|
|
11
|
+
// 新增并关联,如果存在关联,解除之后,与新数据建立关联
|
|
12
|
+
create(options?: CreateOptions): Promise<M>;
|
|
13
|
+
// 更新
|
|
14
|
+
update(options?): Promise<M>;
|
|
15
|
+
// 删除
|
|
16
|
+
destroy(): Promise<Boolean>;
|
|
17
|
+
// 建立关联
|
|
18
|
+
set(primaryKey: any): Promise<void>;
|
|
19
|
+
// 移除关联
|
|
20
|
+
remove(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class HasOneRepository extends SingleRelationRepository implements IHasOneRepository<any> {}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { omit } from 'lodash';
|
|
2
|
+
import { MultiAssociationAccessors, Op, Sequelize, Transaction } from 'sequelize';
|
|
3
|
+
import {
|
|
4
|
+
CommonFindOptions,
|
|
5
|
+
CountOptions,
|
|
6
|
+
DestroyOptions,
|
|
7
|
+
Filter,
|
|
8
|
+
FilterByTk,
|
|
9
|
+
FindOptions,
|
|
10
|
+
TargetKey,
|
|
11
|
+
TK,
|
|
12
|
+
TransactionAble,
|
|
13
|
+
UpdateOptions
|
|
14
|
+
} from '../repository';
|
|
15
|
+
import { updateModelByValues } from '../update-associations';
|
|
16
|
+
import { UpdateGuard } from '../update-guard';
|
|
17
|
+
import { RelationRepository, transaction } from './relation-repository';
|
|
18
|
+
|
|
19
|
+
export interface FindAndCountOptions extends CommonFindOptions {}
|
|
20
|
+
|
|
21
|
+
export interface FindOneOptions extends CommonFindOptions, FilterByTk {}
|
|
22
|
+
|
|
23
|
+
export interface AssociatedOptions extends TransactionAble {
|
|
24
|
+
tk?: TK;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export abstract class MultipleRelationRepository extends RelationRepository {
|
|
28
|
+
extendFindOptions(findOptions) {
|
|
29
|
+
return findOptions;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async find(options?: FindOptions): Promise<any> {
|
|
33
|
+
const transaction = await this.getTransaction(options);
|
|
34
|
+
|
|
35
|
+
const findOptions = {
|
|
36
|
+
...this.extendFindOptions(
|
|
37
|
+
this.buildQueryOptions({
|
|
38
|
+
...options,
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
subQuery: false,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const getAccessor = this.accessors().get;
|
|
45
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
46
|
+
|
|
47
|
+
if (findOptions.include && findOptions.include.length > 0) {
|
|
48
|
+
const ids = (
|
|
49
|
+
await sourceModel[getAccessor]({
|
|
50
|
+
...findOptions,
|
|
51
|
+
includeIgnoreAttributes: false,
|
|
52
|
+
attributes: [this.targetKey()],
|
|
53
|
+
group: `${this.targetModel.name}.${this.targetKey()}`,
|
|
54
|
+
transaction,
|
|
55
|
+
})
|
|
56
|
+
).map((row) => row.get(this.targetKey()));
|
|
57
|
+
|
|
58
|
+
return await sourceModel[getAccessor]({
|
|
59
|
+
...omit(findOptions, ['limit', 'offset']),
|
|
60
|
+
where: {
|
|
61
|
+
[this.targetKey()]: {
|
|
62
|
+
[Op.in]: ids,
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
transaction,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return await sourceModel[getAccessor]({
|
|
70
|
+
...findOptions,
|
|
71
|
+
transaction,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]> {
|
|
76
|
+
const transaction = await this.getTransaction(options, false);
|
|
77
|
+
return [
|
|
78
|
+
await this.find({
|
|
79
|
+
...options,
|
|
80
|
+
transaction,
|
|
81
|
+
}),
|
|
82
|
+
await this.count({
|
|
83
|
+
...options,
|
|
84
|
+
transaction,
|
|
85
|
+
}),
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async count(options?: CountOptions) {
|
|
90
|
+
const transaction = await this.getTransaction(options);
|
|
91
|
+
|
|
92
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
93
|
+
const queryOptions = this.buildQueryOptions(options);
|
|
94
|
+
|
|
95
|
+
const count = await sourceModel[this.accessors().get]({
|
|
96
|
+
where: queryOptions.where,
|
|
97
|
+
include: queryOptions.include,
|
|
98
|
+
includeIgnoreAttributes: false,
|
|
99
|
+
attributes: [
|
|
100
|
+
[
|
|
101
|
+
Sequelize.fn(
|
|
102
|
+
'COUNT',
|
|
103
|
+
Sequelize.fn('DISTINCT', Sequelize.col(`${this.targetModel.name}.${this.targetKey()}`)),
|
|
104
|
+
),
|
|
105
|
+
'count',
|
|
106
|
+
],
|
|
107
|
+
],
|
|
108
|
+
raw: true,
|
|
109
|
+
plain: true,
|
|
110
|
+
transaction,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return parseInt(count.count);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async findOne(options?: FindOneOptions): Promise<any> {
|
|
117
|
+
const transaction = await this.getTransaction(options, false);
|
|
118
|
+
const rows = await this.find({ ...options, limit: 1, transaction });
|
|
119
|
+
return rows.length == 1 ? rows[0] : null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
@transaction((args, transaction) => {
|
|
123
|
+
return {
|
|
124
|
+
tk: args[0],
|
|
125
|
+
transaction,
|
|
126
|
+
};
|
|
127
|
+
})
|
|
128
|
+
async remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void> {
|
|
129
|
+
const transaction = await this.getTransaction(options);
|
|
130
|
+
let handleKeys = options['tk'];
|
|
131
|
+
|
|
132
|
+
if (!Array.isArray(handleKeys)) {
|
|
133
|
+
handleKeys = [handleKeys];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const sourceModel = await this.getSourceModel(transaction);
|
|
137
|
+
await sourceModel[this.accessors().removeMultiple](handleKeys, {
|
|
138
|
+
transaction,
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@transaction()
|
|
144
|
+
async update(options?: UpdateOptions): Promise<any> {
|
|
145
|
+
const transaction = await this.getTransaction(options);
|
|
146
|
+
|
|
147
|
+
const guard = UpdateGuard.fromOptions(this.targetModel, options);
|
|
148
|
+
|
|
149
|
+
const values = guard.sanitize(options.values);
|
|
150
|
+
|
|
151
|
+
const queryOptions = this.buildQueryOptions(options as any);
|
|
152
|
+
|
|
153
|
+
const instances = await this.find(queryOptions);
|
|
154
|
+
|
|
155
|
+
for (const instance of instances) {
|
|
156
|
+
await updateModelByValues(instance, values, {
|
|
157
|
+
...options,
|
|
158
|
+
sanitized: true,
|
|
159
|
+
sourceModel: this.sourceInstance,
|
|
160
|
+
transaction,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
for (const instance of instances) {
|
|
165
|
+
if (options.hooks !== false) {
|
|
166
|
+
await this.db.emitAsync(`${this.targetCollection.name}.afterUpdateWithAssociations`, instance, options);
|
|
167
|
+
await this.db.emitAsync(`${this.targetCollection.name}.afterSaveWithAssociations`, instance, options);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return instances;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async destroy(options?: TK | DestroyOptions): Promise<Boolean> {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
protected async destroyByFilter(filter: Filter, transaction?: Transaction) {
|
|
179
|
+
const instances = await this.find({
|
|
180
|
+
filter: filter,
|
|
181
|
+
transaction,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
return await this.destroy({
|
|
185
|
+
filterByTk: instances.map((instance) => instance.get(this.targetCollection.filterTargetKey)),
|
|
186
|
+
transaction,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
protected filterHasInclude(filter: Filter, options?: any) {
|
|
191
|
+
const filterResult = this.parseFilter(filter, options);
|
|
192
|
+
return filterResult.include && filterResult.include.length > 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
protected accessors() {
|
|
196
|
+
return <MultiAssociationAccessors>super.accessors();
|
|
197
|
+
}
|
|
198
|
+
}
|