@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,23 @@
|
|
|
1
|
+
import { Model } from '../model';
|
|
2
|
+
import { CreateOptions, DestroyOptions, FindOptions, TargetKey, TK, UpdateOptions } from '../repository';
|
|
3
|
+
import { AssociatedOptions, FindAndCountOptions, FindOneOptions, MultipleRelationRepository } from './multiple-relation-repository';
|
|
4
|
+
interface IHasManyRepository<M extends Model> {
|
|
5
|
+
find(options?: FindOptions): Promise<M>;
|
|
6
|
+
findAndCount(options?: FindAndCountOptions): Promise<[M[], number]>;
|
|
7
|
+
findOne(options?: FindOneOptions): Promise<M>;
|
|
8
|
+
create(options?: CreateOptions): Promise<M>;
|
|
9
|
+
update(options?: UpdateOptions): Promise<M>;
|
|
10
|
+
destroy(options?: TK | DestroyOptions): Promise<Boolean>;
|
|
11
|
+
set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
12
|
+
add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
13
|
+
remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare class HasManyRepository extends MultipleRelationRepository implements IHasManyRepository<any> {
|
|
16
|
+
find(options?: FindOptions): Promise<any>;
|
|
17
|
+
destroy(options?: TK | DestroyOptions): Promise<Boolean>;
|
|
18
|
+
handleKeyOfAdd(options: any): any;
|
|
19
|
+
set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
20
|
+
add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
21
|
+
accessors(): import("sequelize").MultiAssociationAccessors;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.HasManyRepository = void 0;
|
|
19
|
+
const lodash_1 = require("lodash");
|
|
20
|
+
const sequelize_1 = require("sequelize");
|
|
21
|
+
const multiple_relation_repository_1 = require("./multiple-relation-repository");
|
|
22
|
+
const relation_repository_1 = require("./relation-repository");
|
|
23
|
+
class HasManyRepository extends multiple_relation_repository_1.MultipleRelationRepository {
|
|
24
|
+
find(options) {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const targetRepository = this.targetCollection.repository;
|
|
27
|
+
const addFilter = {
|
|
28
|
+
[this.association.foreignKey]: this.sourceKeyValue,
|
|
29
|
+
};
|
|
30
|
+
if (options === null || options === void 0 ? void 0 : options.filterByTk) {
|
|
31
|
+
addFilter[this.associationField.targetKey] = options.filterByTk;
|
|
32
|
+
}
|
|
33
|
+
return yield targetRepository.find(Object.assign(Object.assign({}, (0, lodash_1.omit)(options, ['filterByTk'])), { filter: {
|
|
34
|
+
$and: [options.filter || {}, addFilter],
|
|
35
|
+
} }));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
destroy(options) {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const transaction = yield this.getTransaction(options);
|
|
41
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
42
|
+
const where = [
|
|
43
|
+
{
|
|
44
|
+
[this.association.foreignKey]: sourceModel.get(this.association.sourceKey),
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
if (options && options['filter']) {
|
|
48
|
+
const filterResult = this.parseFilter(options['filter'], options);
|
|
49
|
+
if (filterResult.include && filterResult.include.length > 0) {
|
|
50
|
+
return yield this.destroyByFilter(options['filter'], transaction);
|
|
51
|
+
}
|
|
52
|
+
where.push(filterResult.where);
|
|
53
|
+
}
|
|
54
|
+
if (options && options['filterByTk']) {
|
|
55
|
+
if (typeof options === 'object' && options['filterByTk']) {
|
|
56
|
+
options = options['filterByTk'];
|
|
57
|
+
}
|
|
58
|
+
where.push({
|
|
59
|
+
[this.targetKey()]: options,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
yield this.targetModel.destroy({
|
|
63
|
+
where: {
|
|
64
|
+
[sequelize_1.Op.and]: where,
|
|
65
|
+
},
|
|
66
|
+
individualHooks: true,
|
|
67
|
+
transaction,
|
|
68
|
+
});
|
|
69
|
+
return true;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
handleKeyOfAdd(options) {
|
|
73
|
+
let handleKeys;
|
|
74
|
+
if (typeof options !== 'object' && !Array.isArray(options)) {
|
|
75
|
+
handleKeys = [options];
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
handleKeys = options['pk'];
|
|
79
|
+
}
|
|
80
|
+
return handleKeys;
|
|
81
|
+
}
|
|
82
|
+
set(options) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const transaction = yield this.getTransaction(options);
|
|
85
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
86
|
+
yield sourceModel[this.accessors().set](this.handleKeyOfAdd(options), {
|
|
87
|
+
transaction,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
add(options) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const transaction = yield this.getTransaction(options);
|
|
94
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
95
|
+
yield sourceModel[this.accessors().add](this.handleKeyOfAdd(options), {
|
|
96
|
+
transaction,
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
accessors() {
|
|
101
|
+
return this.association.accessors;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
__decorate([
|
|
105
|
+
(0, relation_repository_1.transaction)((args, transaction) => {
|
|
106
|
+
return {
|
|
107
|
+
filterByTk: args[0],
|
|
108
|
+
transaction,
|
|
109
|
+
};
|
|
110
|
+
})
|
|
111
|
+
], HasManyRepository.prototype, "destroy", null);
|
|
112
|
+
__decorate([
|
|
113
|
+
(0, relation_repository_1.transaction)((args, transaction) => {
|
|
114
|
+
return {
|
|
115
|
+
pk: args[0],
|
|
116
|
+
transaction,
|
|
117
|
+
};
|
|
118
|
+
})
|
|
119
|
+
], HasManyRepository.prototype, "set", null);
|
|
120
|
+
__decorate([
|
|
121
|
+
(0, relation_repository_1.transaction)((args, transaction) => {
|
|
122
|
+
return {
|
|
123
|
+
pk: args[0],
|
|
124
|
+
transaction,
|
|
125
|
+
};
|
|
126
|
+
})
|
|
127
|
+
], HasManyRepository.prototype, "add", null);
|
|
128
|
+
exports.HasManyRepository = HasManyRepository;
|
|
129
|
+
//# sourceMappingURL=hasmany-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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C,WAAW,CAAC,CAAC;YAE3D,MAAM,WAAW,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE;gBACpE,WAAW;aACZ,CAAC,CAAC;QACL,CAAC;KAAA;IAQK,GAAG,CAAC,OAAoD;;YAC5D,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YAEvD,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;YAE3D,MAAM,WAAW,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE;gBACpE,WAAW;aACZ,CAAC,CAAC;QACL,CAAC;KAAA;IAED,SAAS;QACP,OAAiB,IAAI,CAAC,WAAY,CAAC,SAAS,CAAC;IAC/C,CAAC;CACF;AAxFC;IANC,IAAA,iCAAW,EAAC,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE;QACjC,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC;YACnB,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;gDAyCD;AAmBD;IANC,IAAA,iCAAW,EAAC,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE;QACjC,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;YACX,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;4CASD;AAQD;IANC,IAAA,iCAAW,EAAC,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE;QACjC,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;YACX,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;4CASD;AA7GH,8CAkHC","sourcesContent":["import { omit } from 'lodash';\nimport { HasMany, Op } from 'sequelize';\nimport { Model } from '../model';\nimport { CreateOptions, DestroyOptions, FindOptions, TargetKey, TK, UpdateOptions } from '../repository';\nimport {\n AssociatedOptions,\n FindAndCountOptions,\n FindOneOptions,\n MultipleRelationRepository\n} from './multiple-relation-repository';\nimport { transaction } from './relation-repository';\n\ninterface IHasManyRepository<M extends Model> {\n find(options?: FindOptions): Promise<M>;\n findAndCount(options?: FindAndCountOptions): Promise<[M[], number]>;\n findOne(options?: FindOneOptions): Promise<M>;\n // 新增并关联\n create(options?: CreateOptions): Promise<M>;\n // 更新\n update(options?: UpdateOptions): Promise<M>;\n // 删除\n destroy(options?: TK | DestroyOptions): Promise<Boolean>;\n // 建立关联\n set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;\n // 附加关联\n add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;\n // 移除关联\n remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;\n}\n\nexport class HasManyRepository extends MultipleRelationRepository implements IHasManyRepository<any> {\n async find(options?: FindOptions): Promise<any> {\n const targetRepository = this.targetCollection.repository;\n\n const addFilter = {\n [this.association.foreignKey]: this.sourceKeyValue,\n };\n\n if (options?.filterByTk) {\n addFilter[this.associationField.targetKey] = options.filterByTk;\n }\n\n return await targetRepository.find({\n ...omit(options, ['filterByTk']),\n filter: {\n $and: [options.filter || {}, addFilter],\n },\n });\n }\n\n @transaction((args, transaction) => {\n return {\n filterByTk: args[0],\n transaction,\n };\n })\n async destroy(options?: TK | DestroyOptions): Promise<Boolean> {\n const transaction = await this.getTransaction(options);\n\n const sourceModel = await this.getSourceModel(transaction);\n\n const where = [\n {\n [this.association.foreignKey]: sourceModel.get((this.association as any).sourceKey),\n },\n ];\n\n if (options && options['filter']) {\n const filterResult = this.parseFilter(options['filter'], options);\n\n if (filterResult.include && filterResult.include.length > 0) {\n return await this.destroyByFilter(options['filter'], transaction);\n }\n\n where.push(filterResult.where);\n }\n\n if (options && options['filterByTk']) {\n if (typeof options === 'object' && options['filterByTk']) {\n options = options['filterByTk'];\n }\n\n where.push({\n [this.targetKey()]: options,\n });\n }\n\n await this.targetModel.destroy({\n where: {\n [Op.and]: where,\n },\n individualHooks: true,\n transaction,\n });\n\n return true;\n }\n\n handleKeyOfAdd(options) {\n let handleKeys;\n\n if (typeof options !== 'object' && !Array.isArray(options)) {\n handleKeys = [options];\n } else {\n handleKeys = options['pk'];\n }\n return handleKeys;\n }\n\n @transaction((args, transaction) => {\n return {\n pk: args[0],\n transaction,\n };\n })\n async set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void> {\n const transaction = await this.getTransaction(options);\n\n const sourceModel = await this.getSourceModel(transaction);\n\n await sourceModel[this.accessors().set](this.handleKeyOfAdd(options), {\n transaction,\n });\n }\n\n @transaction((args, transaction) => {\n return {\n pk: args[0],\n transaction,\n };\n })\n async add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void> {\n const transaction = await this.getTransaction(options);\n\n const sourceModel = await this.getSourceModel(transaction);\n\n await sourceModel[this.accessors().add](this.handleKeyOfAdd(options), {\n transaction,\n });\n }\n\n accessors() {\n return (<HasMany>this.association).accessors;\n }\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Model } from '../model';
|
|
2
|
+
import { CreateOptions } from '../repository';
|
|
3
|
+
import { SingleRelationFindOption, SingleRelationRepository } from './single-relation-repository';
|
|
4
|
+
interface HasOneFindOptions extends SingleRelationFindOption {
|
|
5
|
+
}
|
|
6
|
+
interface IHasOneRepository<M extends Model> {
|
|
7
|
+
find(options?: HasOneFindOptions): Promise<M>;
|
|
8
|
+
findOne(options?: HasOneFindOptions): Promise<M>;
|
|
9
|
+
create(options?: CreateOptions): Promise<M>;
|
|
10
|
+
update(options?: any): Promise<M>;
|
|
11
|
+
destroy(): Promise<Boolean>;
|
|
12
|
+
set(primaryKey: any): Promise<void>;
|
|
13
|
+
remove(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare class HasOneRepository extends SingleRelationRepository implements IHasOneRepository<any> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HasOneRepository = void 0;
|
|
4
|
+
const single_relation_repository_1 = require("./single-relation-repository");
|
|
5
|
+
class HasOneRepository extends single_relation_repository_1.SingleRelationRepository {
|
|
6
|
+
}
|
|
7
|
+
exports.HasOneRepository = HasOneRepository;
|
|
8
|
+
//# sourceMappingURL=hasone-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasone-repository.js","sourceRoot":"","sources":["../../src/relation-repository/hasone-repository.ts"],"names":[],"mappings":";;;AAEA,6EAAkG;AAoBlG,MAAa,gBAAiB,SAAQ,qDAAwB;CAAqC;AAAnG,4CAAmG","sourcesContent":["import { Model } from '../model';\nimport { CreateOptions } from '../repository';\nimport { SingleRelationFindOption, SingleRelationRepository } from './single-relation-repository';\n\ninterface HasOneFindOptions extends SingleRelationFindOption {}\n\ninterface IHasOneRepository<M extends Model> {\n // 不需要 findOne,find 就是 findOne\n find(options?: HasOneFindOptions): Promise<M>;\n findOne(options?: HasOneFindOptions): Promise<M>;\n // 新增并关联,如果存在关联,解除之后,与新数据建立关联\n create(options?: CreateOptions): Promise<M>;\n // 更新\n update(options?): Promise<M>;\n // 删除\n destroy(): Promise<Boolean>;\n // 建立关联\n set(primaryKey: any): Promise<void>;\n // 移除关联\n remove(): Promise<void>;\n}\n\nexport class HasOneRepository extends SingleRelationRepository implements IHasOneRepository<any> {}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MultiAssociationAccessors, Transaction } from 'sequelize';
|
|
2
|
+
import { CommonFindOptions, CountOptions, DestroyOptions, Filter, FilterByTk, FindOptions, TargetKey, TK, TransactionAble, UpdateOptions } from '../repository';
|
|
3
|
+
import { RelationRepository } from './relation-repository';
|
|
4
|
+
export interface FindAndCountOptions extends CommonFindOptions {
|
|
5
|
+
}
|
|
6
|
+
export interface FindOneOptions extends CommonFindOptions, FilterByTk {
|
|
7
|
+
}
|
|
8
|
+
export interface AssociatedOptions extends TransactionAble {
|
|
9
|
+
tk?: TK;
|
|
10
|
+
}
|
|
11
|
+
export declare abstract class MultipleRelationRepository extends RelationRepository {
|
|
12
|
+
extendFindOptions(findOptions: any): any;
|
|
13
|
+
find(options?: FindOptions): Promise<any>;
|
|
14
|
+
findAndCount(options?: FindAndCountOptions): Promise<[any[], number]>;
|
|
15
|
+
count(options?: CountOptions): Promise<number>;
|
|
16
|
+
findOne(options?: FindOneOptions): Promise<any>;
|
|
17
|
+
remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
18
|
+
update(options?: UpdateOptions): Promise<any>;
|
|
19
|
+
destroy(options?: TK | DestroyOptions): Promise<Boolean>;
|
|
20
|
+
protected destroyByFilter(filter: Filter, transaction?: Transaction): Promise<Boolean>;
|
|
21
|
+
protected filterHasInclude(filter: Filter, options?: any): boolean;
|
|
22
|
+
protected accessors(): MultiAssociationAccessors;
|
|
23
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.MultipleRelationRepository = void 0;
|
|
19
|
+
const lodash_1 = require("lodash");
|
|
20
|
+
const sequelize_1 = require("sequelize");
|
|
21
|
+
const update_associations_1 = require("../update-associations");
|
|
22
|
+
const update_guard_1 = require("../update-guard");
|
|
23
|
+
const relation_repository_1 = require("./relation-repository");
|
|
24
|
+
class MultipleRelationRepository extends relation_repository_1.RelationRepository {
|
|
25
|
+
extendFindOptions(findOptions) {
|
|
26
|
+
return findOptions;
|
|
27
|
+
}
|
|
28
|
+
find(options) {
|
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
const transaction = yield this.getTransaction(options);
|
|
31
|
+
const findOptions = Object.assign(Object.assign({}, this.extendFindOptions(this.buildQueryOptions(Object.assign({}, options)))), { subQuery: false });
|
|
32
|
+
const getAccessor = this.accessors().get;
|
|
33
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
34
|
+
if (findOptions.include && findOptions.include.length > 0) {
|
|
35
|
+
const ids = (yield sourceModel[getAccessor](Object.assign(Object.assign({}, findOptions), { includeIgnoreAttributes: false, attributes: [this.targetKey()], group: `${this.targetModel.name}.${this.targetKey()}`, transaction }))).map((row) => row.get(this.targetKey()));
|
|
36
|
+
return yield sourceModel[getAccessor](Object.assign(Object.assign({}, (0, lodash_1.omit)(findOptions, ['limit', 'offset'])), { where: {
|
|
37
|
+
[this.targetKey()]: {
|
|
38
|
+
[sequelize_1.Op.in]: ids,
|
|
39
|
+
},
|
|
40
|
+
}, transaction }));
|
|
41
|
+
}
|
|
42
|
+
return yield sourceModel[getAccessor](Object.assign(Object.assign({}, findOptions), { transaction }));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
findAndCount(options) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const transaction = yield this.getTransaction(options, false);
|
|
48
|
+
return [
|
|
49
|
+
yield this.find(Object.assign(Object.assign({}, options), { transaction })),
|
|
50
|
+
yield this.count(Object.assign(Object.assign({}, options), { transaction })),
|
|
51
|
+
];
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
count(options) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const transaction = yield this.getTransaction(options);
|
|
57
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
58
|
+
const queryOptions = this.buildQueryOptions(options);
|
|
59
|
+
const count = yield sourceModel[this.accessors().get]({
|
|
60
|
+
where: queryOptions.where,
|
|
61
|
+
include: queryOptions.include,
|
|
62
|
+
includeIgnoreAttributes: false,
|
|
63
|
+
attributes: [
|
|
64
|
+
[
|
|
65
|
+
sequelize_1.Sequelize.fn('COUNT', sequelize_1.Sequelize.fn('DISTINCT', sequelize_1.Sequelize.col(`${this.targetModel.name}.${this.targetKey()}`))),
|
|
66
|
+
'count',
|
|
67
|
+
],
|
|
68
|
+
],
|
|
69
|
+
raw: true,
|
|
70
|
+
plain: true,
|
|
71
|
+
transaction,
|
|
72
|
+
});
|
|
73
|
+
return parseInt(count.count);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
findOne(options) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const transaction = yield this.getTransaction(options, false);
|
|
79
|
+
const rows = yield this.find(Object.assign(Object.assign({}, options), { limit: 1, transaction }));
|
|
80
|
+
return rows.length == 1 ? rows[0] : null;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
remove(options) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
const transaction = yield this.getTransaction(options);
|
|
86
|
+
let handleKeys = options['tk'];
|
|
87
|
+
if (!Array.isArray(handleKeys)) {
|
|
88
|
+
handleKeys = [handleKeys];
|
|
89
|
+
}
|
|
90
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
91
|
+
yield sourceModel[this.accessors().removeMultiple](handleKeys, {
|
|
92
|
+
transaction,
|
|
93
|
+
});
|
|
94
|
+
return;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
update(options) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const transaction = yield this.getTransaction(options);
|
|
100
|
+
const guard = update_guard_1.UpdateGuard.fromOptions(this.targetModel, options);
|
|
101
|
+
const values = guard.sanitize(options.values);
|
|
102
|
+
const queryOptions = this.buildQueryOptions(options);
|
|
103
|
+
const instances = yield this.find(queryOptions);
|
|
104
|
+
for (const instance of instances) {
|
|
105
|
+
yield (0, update_associations_1.updateModelByValues)(instance, values, Object.assign(Object.assign({}, options), { sanitized: true, sourceModel: this.sourceInstance, transaction }));
|
|
106
|
+
}
|
|
107
|
+
for (const instance of instances) {
|
|
108
|
+
if (options.hooks !== false) {
|
|
109
|
+
yield this.db.emitAsync(`${this.targetCollection.name}.afterUpdateWithAssociations`, instance, options);
|
|
110
|
+
yield this.db.emitAsync(`${this.targetCollection.name}.afterSaveWithAssociations`, instance, options);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return instances;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
destroy(options) {
|
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
return false;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
destroyByFilter(filter, transaction) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
const instances = yield this.find({
|
|
124
|
+
filter: filter,
|
|
125
|
+
transaction,
|
|
126
|
+
});
|
|
127
|
+
return yield this.destroy({
|
|
128
|
+
filterByTk: instances.map((instance) => instance.get(this.targetCollection.filterTargetKey)),
|
|
129
|
+
transaction,
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
filterHasInclude(filter, options) {
|
|
134
|
+
const filterResult = this.parseFilter(filter, options);
|
|
135
|
+
return filterResult.include && filterResult.include.length > 0;
|
|
136
|
+
}
|
|
137
|
+
accessors() {
|
|
138
|
+
return super.accessors();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
__decorate([
|
|
142
|
+
(0, relation_repository_1.transaction)((args, transaction) => {
|
|
143
|
+
return {
|
|
144
|
+
tk: args[0],
|
|
145
|
+
transaction,
|
|
146
|
+
};
|
|
147
|
+
})
|
|
148
|
+
], MultipleRelationRepository.prototype, "remove", null);
|
|
149
|
+
__decorate([
|
|
150
|
+
(0, relation_repository_1.transaction)()
|
|
151
|
+
], MultipleRelationRepository.prototype, "update", null);
|
|
152
|
+
exports.MultipleRelationRepository = MultipleRelationRepository;
|
|
153
|
+
//# sourceMappingURL=multiple-relation-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multiple-relation-repository.js","sourceRoot":"","sources":["../../src/relation-repository/multiple-relation-repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,mCAA8B;AAC9B,yCAAkF;AAalF,gEAA6D;AAC7D,kDAA8C;AAC9C,+DAAwE;AAUxE,MAAsB,0BAA2B,SAAQ,wCAAkB;IACzE,iBAAiB,CAAC,WAAW;QAC3B,OAAO,WAAW,CAAC;IACrB,CAAC;IAEK,IAAI,CAAC,OAAqB;;YAC9B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YAEvD,MAAM,WAAW,mCACZ,IAAI,CAAC,iBAAiB,CACvB,IAAI,CAAC,iBAAiB,mBACjB,OAAO,EACV,CACH,KACD,QAAQ,EAAE,KAAK,GAChB,CAAC;YAEF,MAAM,WAAW,GAAG,IAAI,CAAC,SAAS,EA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\n CommonFindOptions,\n CountOptions,\n DestroyOptions,\n Filter,\n FilterByTk,\n FindOptions,\n TargetKey,\n TK,\n TransactionAble,\n UpdateOptions\n} from '../repository';\nimport { updateModelByValues } from '../update-associations';\nimport { UpdateGuard } from '../update-guard';\nimport { RelationRepository, transaction } from './relation-repository';\n\nexport interface FindAndCountOptions extends CommonFindOptions {}\n\nexport interface FindOneOptions extends CommonFindOptions, FilterByTk {}\n\nexport interface AssociatedOptions extends TransactionAble {\n tk?: TK;\n}\n\nexport abstract class MultipleRelationRepository extends RelationRepository {\n extendFindOptions(findOptions) {\n return findOptions;\n }\n\n async find(options?: FindOptions): Promise<any> {\n const transaction = await this.getTransaction(options);\n\n const findOptions = {\n ...this.extendFindOptions(\n this.buildQueryOptions({\n ...options,\n }),\n ),\n subQuery: false,\n };\n\n const getAccessor = this.accessors().get;\n const sourceModel = await this.getSourceModel(transaction);\n\n if (findOptions.include && findOptions.include.length > 0) {\n const ids = (\n await sourceModel[getAccessor]({\n ...findOptions,\n includeIgnoreAttributes: false,\n attributes: [this.targetKey()],\n group: `${this.targetModel.name}.${this.targetKey()}`,\n transaction,\n })\n ).map((row) => row.get(this.targetKey()));\n\n return await sourceModel[getAccessor]({\n ...omit(findOptions, ['limit', 'offset']),\n where: {\n [this.targetKey()]: {\n [Op.in]: ids,\n },\n },\n transaction,\n });\n }\n\n return await sourceModel[getAccessor]({\n ...findOptions,\n transaction,\n });\n }\n\n async findAndCount(options?: FindAndCountOptions): Promise<[any[], number]> {\n const transaction = await this.getTransaction(options, false);\n return [\n await this.find({\n ...options,\n transaction,\n }),\n await this.count({\n ...options,\n transaction,\n }),\n ];\n }\n\n async count(options?: CountOptions) {\n const transaction = await this.getTransaction(options);\n\n const sourceModel = await this.getSourceModel(transaction);\n const queryOptions = this.buildQueryOptions(options);\n\n const count = await sourceModel[this.accessors().get]({\n where: queryOptions.where,\n include: queryOptions.include,\n includeIgnoreAttributes: false,\n attributes: [\n [\n Sequelize.fn(\n 'COUNT',\n Sequelize.fn('DISTINCT', Sequelize.col(`${this.targetModel.name}.${this.targetKey()}`)),\n ),\n 'count',\n ],\n ],\n raw: true,\n plain: true,\n transaction,\n });\n\n return parseInt(count.count);\n }\n\n async findOne(options?: FindOneOptions): Promise<any> {\n const transaction = await this.getTransaction(options, false);\n const rows = await this.find({ ...options, limit: 1, transaction });\n return rows.length == 1 ? rows[0] : null;\n }\n\n @transaction((args, transaction) => {\n return {\n tk: args[0],\n transaction,\n };\n })\n async remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void> {\n const transaction = await this.getTransaction(options);\n let handleKeys = options['tk'];\n\n if (!Array.isArray(handleKeys)) {\n handleKeys = [handleKeys];\n }\n\n const sourceModel = await this.getSourceModel(transaction);\n await sourceModel[this.accessors().removeMultiple](handleKeys, {\n transaction,\n });\n return;\n }\n\n @transaction()\n async update(options?: UpdateOptions): Promise<any> {\n const transaction = await this.getTransaction(options);\n\n const guard = UpdateGuard.fromOptions(this.targetModel, options);\n\n const values = guard.sanitize(options.values);\n\n const queryOptions = this.buildQueryOptions(options as any);\n\n const instances = await this.find(queryOptions);\n\n for (const instance of instances) {\n await updateModelByValues(instance, values, {\n ...options,\n sanitized: true,\n sourceModel: this.sourceInstance,\n transaction,\n });\n }\n\n for (const instance of instances) {\n if (options.hooks !== false) {\n await this.db.emitAsync(`${this.targetCollection.name}.afterUpdateWithAssociations`, instance, options);\n await this.db.emitAsync(`${this.targetCollection.name}.afterSaveWithAssociations`, instance, options);\n }\n }\n\n return instances;\n }\n\n async destroy(options?: TK | DestroyOptions): Promise<Boolean> {\n return false;\n }\n\n protected async destroyByFilter(filter: Filter, transaction?: Transaction) {\n const instances = await this.find({\n filter: filter,\n transaction,\n });\n\n return await this.destroy({\n filterByTk: instances.map((instance) => instance.get(this.targetCollection.filterTargetKey)),\n transaction,\n });\n }\n\n protected filterHasInclude(filter: Filter, options?: any) {\n const filterResult = this.parseFilter(filter, options);\n return filterResult.include && filterResult.include.length > 0;\n }\n\n protected accessors() {\n return <MultiAssociationAccessors>super.accessors();\n }\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Association, ModelCtor, Transaction } from 'sequelize';
|
|
2
|
+
import { Collection } from '../collection';
|
|
3
|
+
import Database from '../database';
|
|
4
|
+
import { RelationField } from '../fields/relation-field';
|
|
5
|
+
import { Model } from '../model';
|
|
6
|
+
import { CreateOptions, Filter, FindOptions } from '../repository';
|
|
7
|
+
export declare const transaction: (transactionInjector?: any) => (target: any, name: any, descriptor: any) => any;
|
|
8
|
+
export declare abstract class RelationRepository {
|
|
9
|
+
sourceCollection: Collection;
|
|
10
|
+
association: Association;
|
|
11
|
+
targetModel: ModelCtor<any>;
|
|
12
|
+
targetCollection: Collection;
|
|
13
|
+
associationName: string;
|
|
14
|
+
associationField: RelationField;
|
|
15
|
+
sourceKeyValue: string | number;
|
|
16
|
+
sourceInstance: Model;
|
|
17
|
+
db: Database;
|
|
18
|
+
constructor(sourceCollection: Collection, association: string, sourceKeyValue: string | number);
|
|
19
|
+
targetKey(): any;
|
|
20
|
+
protected accessors(): import("sequelize").SingleAssociationAccessors | import("sequelize").MultiAssociationAccessors;
|
|
21
|
+
create(options?: CreateOptions): Promise<any>;
|
|
22
|
+
getSourceModel(transaction?: any): Promise<Model<any, any>>;
|
|
23
|
+
protected buildQueryOptions(options: FindOptions): any;
|
|
24
|
+
protected parseFilter(filter: Filter, options?: any): {
|
|
25
|
+
where?: undefined;
|
|
26
|
+
include?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
where: {};
|
|
29
|
+
include: any[];
|
|
30
|
+
};
|
|
31
|
+
protected getTransaction(options: any, autoGen?: boolean): Promise<Transaction | null>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.RelationRepository = exports.transaction = void 0;
|
|
16
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
17
|
+
const filter_parser_1 = __importDefault(require("../filter-parser"));
|
|
18
|
+
const options_parser_1 = require("../options-parser");
|
|
19
|
+
const transaction_decorator_1 = require("../transaction-decorator");
|
|
20
|
+
const update_associations_1 = require("../update-associations");
|
|
21
|
+
const update_guard_1 = require("../update-guard");
|
|
22
|
+
exports.transaction = (0, transaction_decorator_1.transactionWrapperBuilder)(function () {
|
|
23
|
+
return this.sourceCollection.model.sequelize.transaction();
|
|
24
|
+
});
|
|
25
|
+
class RelationRepository {
|
|
26
|
+
constructor(sourceCollection, association, sourceKeyValue) {
|
|
27
|
+
this.db = sourceCollection.context.database;
|
|
28
|
+
this.sourceCollection = sourceCollection;
|
|
29
|
+
this.sourceKeyValue = sourceKeyValue;
|
|
30
|
+
this.associationName = association;
|
|
31
|
+
this.association = this.sourceCollection.model.associations[association];
|
|
32
|
+
this.associationField = this.sourceCollection.getField(association);
|
|
33
|
+
this.targetModel = this.association.target;
|
|
34
|
+
this.targetCollection = this.sourceCollection.context.database.modelCollection.get(this.targetModel);
|
|
35
|
+
}
|
|
36
|
+
targetKey() {
|
|
37
|
+
return this.associationField.targetKey;
|
|
38
|
+
}
|
|
39
|
+
accessors() {
|
|
40
|
+
return this.association.accessors;
|
|
41
|
+
}
|
|
42
|
+
create(options) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const createAccessor = this.accessors().create;
|
|
45
|
+
const guard = update_guard_1.UpdateGuard.fromOptions(this.targetModel, options);
|
|
46
|
+
const values = options.values;
|
|
47
|
+
const sourceModel = yield this.getSourceModel();
|
|
48
|
+
const instance = yield sourceModel[createAccessor](guard.sanitize(options.values), options);
|
|
49
|
+
yield (0, update_associations_1.updateAssociations)(instance, values, options);
|
|
50
|
+
if (options.hooks !== false) {
|
|
51
|
+
yield this.db.emitAsync(`${this.targetCollection.name}.afterCreateWithAssociations`, instance, options);
|
|
52
|
+
const eventName = `${this.targetCollection.name}.afterSaveWithAssociations`;
|
|
53
|
+
yield this.db.emitAsync(eventName, instance, options);
|
|
54
|
+
}
|
|
55
|
+
return instance;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
getSourceModel(transaction) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
if (!this.sourceInstance) {
|
|
61
|
+
this.sourceInstance = yield this.sourceCollection.model.findOne({
|
|
62
|
+
where: {
|
|
63
|
+
[this.associationField.sourceKey]: this.sourceKeyValue,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return this.sourceInstance;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
buildQueryOptions(options) {
|
|
71
|
+
const parser = new options_parser_1.OptionsParser(options, {
|
|
72
|
+
collection: this.targetCollection,
|
|
73
|
+
targetKey: this.targetKey(),
|
|
74
|
+
});
|
|
75
|
+
const params = parser.toSequelizeParams();
|
|
76
|
+
return Object.assign(Object.assign({}, options), params);
|
|
77
|
+
}
|
|
78
|
+
parseFilter(filter, options) {
|
|
79
|
+
const parser = new filter_parser_1.default(filter, {
|
|
80
|
+
collection: this.targetCollection,
|
|
81
|
+
app: {
|
|
82
|
+
ctx: options === null || options === void 0 ? void 0 : options.context,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
return parser.toSequelizeParams();
|
|
86
|
+
}
|
|
87
|
+
getTransaction(options, autoGen = false) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
if (lodash_1.default.isPlainObject(options) && options.transaction) {
|
|
90
|
+
return options.transaction;
|
|
91
|
+
}
|
|
92
|
+
if (autoGen) {
|
|
93
|
+
return yield this.sourceCollection.model.sequelize.transaction();
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.RelationRepository = RelationRepository;
|
|
100
|
+
//# sourceMappingURL=relation-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relation-repository.js","sourceRoot":"","sources":["../../src/relation-repository/relation-repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oDAA4B;AAK5B,qEAA4C;AAE5C,sDAAkD;AAElD,oEAAqE;AACrE,gEAA4D;AAC5D,kDAA8C;AAEjC,QAAA,WAAW,GAAG,IAAA,iDAAyB,EAAC;IACnD,OAAO,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;AAC7D,CAAC,CAAC,CAAC;AAEH,MAAsB,kBAAkB;IAWtC,YAAY,gBAA4B,EAAE,WAAmB,EAAE,cAA+B;QAC5F,IAAI,CAAC,EAAE,GAAG,gBAAgB,CAAC,OAAO,CAAC,QAAQ,CAAC;QAE5C,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,eAAe,GAAG,WAAW,CAAC;QACnC,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,YAAY,CAAC,WAAW,CAAC,CAAC;QAEzE,IAAI,CAAC,gBAAgB,GA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IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;aAClE;YAED,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;CACF;AAhGD,gDAgGC","sourcesContent":["import lodash from 'lodash';\nimport { Association, BelongsTo, BelongsToMany, HasMany, HasOne, ModelCtor, Transaction } from 'sequelize';\nimport { Collection } from '../collection';\nimport Database from '../database';\nimport { RelationField } from '../fields/relation-field';\nimport FilterParser from '../filter-parser';\nimport { Model } from '../model';\nimport { OptionsParser } from '../options-parser';\nimport { CreateOptions, Filter, FindOptions } from '../repository';\nimport { transactionWrapperBuilder } from '../transaction-decorator';\nimport { updateAssociations } from '../update-associations';\nimport { UpdateGuard } from '../update-guard';\n\nexport const transaction = transactionWrapperBuilder(function () {\n return this.sourceCollection.model.sequelize.transaction();\n});\n\nexport abstract class RelationRepository {\n sourceCollection: Collection;\n association: Association;\n targetModel: ModelCtor<any>;\n targetCollection: Collection;\n associationName: string;\n associationField: RelationField;\n sourceKeyValue: string | number;\n sourceInstance: Model;\n db: Database;\n\n constructor(sourceCollection: Collection, association: string, sourceKeyValue: string | number) {\n this.db = sourceCollection.context.database;\n\n this.sourceCollection = sourceCollection;\n this.sourceKeyValue = sourceKeyValue;\n this.associationName = association;\n this.association = this.sourceCollection.model.associations[association];\n\n this.associationField = this.sourceCollection.getField(association);\n\n this.targetModel = this.association.target;\n this.targetCollection = this.sourceCollection.context.database.modelCollection.get(this.targetModel);\n }\n\n targetKey() {\n return this.associationField.targetKey;\n }\n\n protected accessors() {\n return (<BelongsTo | HasOne | HasMany | BelongsToMany>this.association).accessors;\n }\n\n async create(options?: CreateOptions): Promise<any> {\n const createAccessor = this.accessors().create;\n\n const guard = UpdateGuard.fromOptions(this.targetModel, options);\n const values = options.values;\n\n const sourceModel = await this.getSourceModel();\n\n const instance = await sourceModel[createAccessor](guard.sanitize(options.values), options);\n\n await updateAssociations(instance, values, options);\n\n if (options.hooks !== false) {\n await this.db.emitAsync(`${this.targetCollection.name}.afterCreateWithAssociations`, instance, options);\n const eventName = `${this.targetCollection.name}.afterSaveWithAssociations`;\n await this.db.emitAsync(eventName, instance, options);\n }\n\n return instance;\n }\n\n async getSourceModel(transaction?: any) {\n if (!this.sourceInstance) {\n this.sourceInstance = await this.sourceCollection.model.findOne({\n where: {\n [this.associationField.sourceKey]: this.sourceKeyValue,\n },\n });\n }\n\n return this.sourceInstance;\n }\n\n protected buildQueryOptions(options: FindOptions) {\n const parser = new OptionsParser(options, {\n collection: this.targetCollection,\n targetKey: this.targetKey(),\n });\n const params = parser.toSequelizeParams();\n return { ...options, ...params };\n }\n\n protected parseFilter(filter: Filter, options?: any) {\n const parser = new FilterParser(filter, {\n collection: this.targetCollection,\n app: {\n ctx: options?.context,\n },\n });\n return parser.toSequelizeParams();\n }\n\n protected async getTransaction(options: any, autoGen = false): Promise<Transaction | null> {\n if (lodash.isPlainObject(options) && options.transaction) {\n return options.transaction;\n }\n\n if (autoGen) {\n return await this.sourceCollection.model.sequelize.transaction();\n }\n\n return null;\n }\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SingleAssociationAccessors } from 'sequelize';
|
|
2
|
+
import { Model } from '../model';
|
|
3
|
+
import { Appends, Except, Fields, Filter, TargetKey, TransactionAble, UpdateOptions } from '../repository';
|
|
4
|
+
import { RelationRepository } from './relation-repository';
|
|
5
|
+
export interface SingleRelationFindOption extends TransactionAble {
|
|
6
|
+
fields?: Fields;
|
|
7
|
+
except?: Except;
|
|
8
|
+
appends?: Appends;
|
|
9
|
+
filter?: Filter;
|
|
10
|
+
}
|
|
11
|
+
interface SetOption extends TransactionAble {
|
|
12
|
+
tk?: TargetKey;
|
|
13
|
+
}
|
|
14
|
+
export declare abstract class SingleRelationRepository extends RelationRepository {
|
|
15
|
+
remove(options?: TransactionAble): Promise<void>;
|
|
16
|
+
set(options: TargetKey | SetOption): Promise<void>;
|
|
17
|
+
find(options?: SingleRelationFindOption): Promise<Model<any>>;
|
|
18
|
+
findOne(options?: SingleRelationFindOption): Promise<Model<any>>;
|
|
19
|
+
destroy(options?: TransactionAble): Promise<Boolean>;
|
|
20
|
+
update(options: UpdateOptions): Promise<any>;
|
|
21
|
+
accessors(): SingleAssociationAccessors;
|
|
22
|
+
}
|
|
23
|
+
export {};
|