@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,225 @@
|
|
|
1
|
+
import { Op } from 'sequelize';
|
|
2
|
+
import FilterParser from './filter-parser';
|
|
3
|
+
const debug = require('debug')('noco-database');
|
|
4
|
+
export class OptionsParser {
|
|
5
|
+
constructor(options, context) {
|
|
6
|
+
const { collection } = context;
|
|
7
|
+
this.collection = collection;
|
|
8
|
+
this.model = collection.model;
|
|
9
|
+
this.options = options;
|
|
10
|
+
this.database = collection.context.database;
|
|
11
|
+
this.filterParser = new FilterParser(options === null || options === void 0 ? void 0 : options.filter, {
|
|
12
|
+
collection,
|
|
13
|
+
app: {
|
|
14
|
+
ctx: options === null || options === void 0 ? void 0 : options.context,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
this.context = context;
|
|
18
|
+
}
|
|
19
|
+
isAssociation(key) {
|
|
20
|
+
return this.model.associations[key] !== undefined;
|
|
21
|
+
}
|
|
22
|
+
isAssociationPath(path) {
|
|
23
|
+
return this.isAssociation(path.split('.')[0]);
|
|
24
|
+
}
|
|
25
|
+
toSequelizeParams() {
|
|
26
|
+
var _a;
|
|
27
|
+
const queryParams = this.filterParser.toSequelizeParams();
|
|
28
|
+
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.filterByTk) {
|
|
29
|
+
queryParams.where = {
|
|
30
|
+
[Op.and]: [
|
|
31
|
+
queryParams.where,
|
|
32
|
+
{
|
|
33
|
+
[this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return this.parseSort(this.parseFields(queryParams));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* parser sort options
|
|
42
|
+
* @param filterParams
|
|
43
|
+
* @protected
|
|
44
|
+
*/
|
|
45
|
+
parseSort(filterParams) {
|
|
46
|
+
var _a;
|
|
47
|
+
let sort = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.sort) || [];
|
|
48
|
+
if (typeof sort === 'string') {
|
|
49
|
+
sort = sort.split(',');
|
|
50
|
+
}
|
|
51
|
+
const orderParams = sort.map((sortKey) => {
|
|
52
|
+
const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';
|
|
53
|
+
const sortField = sortKey.replace('-', '').split('.');
|
|
54
|
+
// handle sort by association
|
|
55
|
+
if (sortField.length > 1) {
|
|
56
|
+
let associationModel = this.model;
|
|
57
|
+
for (let i = 0; i < sortField.length - 1; i++) {
|
|
58
|
+
const associationKey = sortField[i];
|
|
59
|
+
sortField[i] = associationModel.associations[associationKey].target;
|
|
60
|
+
associationModel = sortField[i];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
sortField.push(direction);
|
|
64
|
+
return sortField;
|
|
65
|
+
});
|
|
66
|
+
if (orderParams.length > 0) {
|
|
67
|
+
return Object.assign({ order: orderParams }, filterParams);
|
|
68
|
+
}
|
|
69
|
+
return filterParams;
|
|
70
|
+
}
|
|
71
|
+
parseFields(filterParams) {
|
|
72
|
+
var _a, _b, _c;
|
|
73
|
+
const appends = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.appends) || [];
|
|
74
|
+
const except = [];
|
|
75
|
+
let attributes = {
|
|
76
|
+
include: [],
|
|
77
|
+
exclude: [],
|
|
78
|
+
}; // out put all fields by default
|
|
79
|
+
if ((_b = this.options) === null || _b === void 0 ? void 0 : _b.fields) {
|
|
80
|
+
// 将fields拆分为 attributes 和 appends
|
|
81
|
+
for (const field of this.options.fields) {
|
|
82
|
+
if (this.isAssociationPath(field)) {
|
|
83
|
+
// field is association field
|
|
84
|
+
appends.push(field);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
// field is model attribute, change attributes to array type
|
|
88
|
+
if (!Array.isArray(attributes))
|
|
89
|
+
attributes = [];
|
|
90
|
+
attributes.push(field);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if ((_c = this.options) === null || _c === void 0 ? void 0 : _c.except) {
|
|
95
|
+
for (const exceptKey of this.options.except) {
|
|
96
|
+
if (this.isAssociationPath(exceptKey)) {
|
|
97
|
+
// except association field
|
|
98
|
+
except.push(exceptKey);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// if attributes is array form, ignore except
|
|
102
|
+
if (Array.isArray(attributes))
|
|
103
|
+
continue;
|
|
104
|
+
attributes.exclude.push(exceptKey);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return Object.assign({ attributes }, this.parseExcept(except, this.parseAppends(appends, filterParams)));
|
|
109
|
+
}
|
|
110
|
+
parseExcept(except, filterParams) {
|
|
111
|
+
if (!except)
|
|
112
|
+
return filterParams;
|
|
113
|
+
const setExcept = (queryParams, except) => {
|
|
114
|
+
// split exceptKey to path form
|
|
115
|
+
// posts.comments.content => ['posts', 'comments', 'content']
|
|
116
|
+
// then set except on include attributes
|
|
117
|
+
const exceptPath = except.split('.');
|
|
118
|
+
const association = exceptPath[0];
|
|
119
|
+
const lastLevel = exceptPath.length <= 2;
|
|
120
|
+
let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == association);
|
|
121
|
+
if (existIncludeIndex == -1) {
|
|
122
|
+
// if include not exists, ignore this except
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (lastLevel) {
|
|
126
|
+
// if it not have exclude form
|
|
127
|
+
if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {
|
|
132
|
+
queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];
|
|
133
|
+
}
|
|
134
|
+
queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
for (const exceptKey of except) {
|
|
142
|
+
setExcept(filterParams, exceptKey);
|
|
143
|
+
}
|
|
144
|
+
return filterParams;
|
|
145
|
+
}
|
|
146
|
+
parseAppends(appends, filterParams) {
|
|
147
|
+
if (!appends)
|
|
148
|
+
return filterParams;
|
|
149
|
+
/**
|
|
150
|
+
* set include params
|
|
151
|
+
* @param includeRoot
|
|
152
|
+
* @param appends
|
|
153
|
+
*/
|
|
154
|
+
const setInclude = (model, queryParams, append) => {
|
|
155
|
+
const appendFields = append.split('.');
|
|
156
|
+
const appendAssociation = appendFields[0];
|
|
157
|
+
const associations = model.associations;
|
|
158
|
+
// if append length less or equal 2
|
|
159
|
+
// example:
|
|
160
|
+
// appends: ['posts']
|
|
161
|
+
// appends: ['posts.title']
|
|
162
|
+
// All of these can be seen as last level
|
|
163
|
+
let lastLevel = false;
|
|
164
|
+
if (appendFields.length == 1) {
|
|
165
|
+
lastLevel = true;
|
|
166
|
+
}
|
|
167
|
+
if (appendFields.length == 2) {
|
|
168
|
+
const associationModel = associations[appendFields[0]].target;
|
|
169
|
+
if (associationModel.rawAttributes[appendFields[1]]) {
|
|
170
|
+
lastLevel = true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
// find association index
|
|
174
|
+
if (queryParams['include'] == undefined) {
|
|
175
|
+
queryParams['include'] = [];
|
|
176
|
+
}
|
|
177
|
+
let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == appendAssociation);
|
|
178
|
+
// if association not exist, create it
|
|
179
|
+
if (existIncludeIndex == -1) {
|
|
180
|
+
// association not exists
|
|
181
|
+
queryParams['include'].push({
|
|
182
|
+
association: appendAssociation,
|
|
183
|
+
});
|
|
184
|
+
existIncludeIndex = 0;
|
|
185
|
+
}
|
|
186
|
+
// end appends
|
|
187
|
+
// without nests association
|
|
188
|
+
if (lastLevel) {
|
|
189
|
+
// get exist association attributes
|
|
190
|
+
let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {
|
|
191
|
+
include: [], // all fields are output by default
|
|
192
|
+
};
|
|
193
|
+
// if need set attribute
|
|
194
|
+
if (appendFields.length == 2) {
|
|
195
|
+
if (!Array.isArray(attributes)) {
|
|
196
|
+
attributes = [];
|
|
197
|
+
}
|
|
198
|
+
const attributeName = appendFields[1];
|
|
199
|
+
// push field to it
|
|
200
|
+
attributes.push(attributeName);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
// if attributes is empty array, change it to object
|
|
204
|
+
if (Array.isArray(attributes) && attributes.length == 0) {
|
|
205
|
+
attributes = {
|
|
206
|
+
include: [],
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
// set new attributes
|
|
211
|
+
queryParams['include'][existIncludeIndex] = Object.assign(Object.assign({}, queryParams['include'][existIncludeIndex]), { attributes });
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
setInclude(model.associations[queryParams['include'][existIncludeIndex].association].target, queryParams['include'][existIncludeIndex], appendFields.filter((_, index) => index !== 0).join('.'));
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
// handle every appends
|
|
218
|
+
for (const append of appends) {
|
|
219
|
+
setInclude(this.model, filterParams, append);
|
|
220
|
+
}
|
|
221
|
+
debug('filter params: %o', filterParams);
|
|
222
|
+
return filterParams;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=options-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options-parser.js","sourceRoot":"","sources":["../src/options-parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,EAAE,EAAE,MAAM,WAAW,CAAC;AAGhE,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAG3C,MAAM,KAAK,GAAG,OAAO,CAA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AC,iBAAiB,CAAC,KAC5C,UAAU,GACX,CAAC;aACH;iBAAM;gBACL,UAAU,CACR,KAAK,CAAC,YAAY,CAAC,WAAW,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,CAAC,WAAW,CAAC,CAAC,MAAM,EAChF,WAAW,CAAC,SAAS,CAAC,CAAC,iBAAiB,CAAC,EACzC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CACzD,CAAC;aACH;QACH,CAAC,CAAC;QAEF,uBAAuB;QACvB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC5B,UAAU,CAAC,IAAI,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;SAC9C;QAED,KAAK,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;QACzC,OAAO,YAAY,CAAC;IACtB,CAAC;CACF","sourcesContent":["import { FindAttributeOptions, ModelCtor, Op } from 'sequelize';\nimport { Collection } from './collection';\nimport { Database } from './database';\nimport FilterParser from './filter-parser';\nimport { Appends, Except, FindOptions } from './repository';\n\nconst debug = require('debug')('noco-database');\n\ninterface OptionsParserContext {\n collection: Collection;\n targetKey?: string;\n}\n\nexport class OptionsParser {\n options: FindOptions;\n database: Database;\n collection: Collection;\n model: ModelCtor<any>;\n filterParser: FilterParser;\n context: OptionsParserContext;\n\n constructor(options: FindOptions, context: OptionsParserContext) {\n const { collection } = context;\n\n this.collection = collection;\n this.model = collection.model;\n this.options = options;\n this.database = collection.context.database;\n this.filterParser = new FilterParser(options?.filter, {\n collection,\n app: {\n ctx: options?.context,\n },\n });\n this.context = context;\n }\n\n isAssociation(key: string) {\n return this.model.associations[key] !== undefined;\n }\n\n isAssociationPath(path: string) {\n return this.isAssociation(path.split('.')[0]);\n }\n\n toSequelizeParams() {\n const queryParams = this.filterParser.toSequelizeParams();\n\n if (this.options?.filterByTk) {\n queryParams.where = {\n [Op.and]: [\n queryParams.where,\n {\n [this.context.targetKey || this.collection.filterTargetKey]: this.options.filterByTk,\n },\n ],\n };\n }\n\n return this.parseSort(this.parseFields(queryParams));\n }\n\n /**\n * parser sort options\n * @param filterParams\n * @protected\n */\n protected parseSort(filterParams) {\n let sort = this.options?.sort || [];\n if (typeof sort === 'string') {\n sort = sort.split(',');\n }\n const orderParams = sort.map((sortKey: string) => {\n const direction = sortKey.startsWith('-') ? 'DESC' : 'ASC';\n const sortField: Array<any> = sortKey.replace('-', '').split('.');\n\n // handle sort by association\n if (sortField.length > 1) {\n let associationModel = this.model;\n\n for (let i = 0; i < sortField.length - 1; i++) {\n const associationKey = sortField[i];\n sortField[i] = associationModel.associations[associationKey].target;\n associationModel = sortField[i];\n }\n }\n\n sortField.push(direction);\n return sortField;\n });\n\n if (orderParams.length > 0) {\n return {\n order: orderParams,\n ...filterParams,\n };\n }\n\n return filterParams;\n }\n\n protected parseFields(filterParams: any) {\n const appends = this.options?.appends || [];\n const except = [];\n\n let attributes: FindAttributeOptions = {\n include: [],\n exclude: [],\n }; // out put all fields by default\n\n if (this.options?.fields) {\n // 将fields拆分为 attributes 和 appends\n for (const field of this.options.fields) {\n if (this.isAssociationPath(field)) {\n // field is association field\n appends.push(field);\n } else {\n // field is model attribute, change attributes to array type\n if (!Array.isArray(attributes)) attributes = [];\n\n attributes.push(field);\n }\n }\n }\n\n if (this.options?.except) {\n for (const exceptKey of this.options.except) {\n if (this.isAssociationPath(exceptKey)) {\n // except association field\n except.push(exceptKey);\n } else {\n // if attributes is array form, ignore except\n if (Array.isArray(attributes)) continue;\n attributes.exclude.push(exceptKey);\n }\n }\n }\n\n return {\n attributes,\n ...this.parseExcept(except, this.parseAppends(appends, filterParams)),\n };\n }\n\n protected parseExcept(except: Except, filterParams: any) {\n if (!except) return filterParams;\n const setExcept = (queryParams: any, except: string) => {\n // split exceptKey to path form\n // posts.comments.content => ['posts', 'comments', 'content']\n // then set except on include attributes\n const exceptPath = except.split('.');\n const association = exceptPath[0];\n const lastLevel = exceptPath.length <= 2;\n\n let existIncludeIndex = queryParams['include'].findIndex((include) => include['association'] == association);\n\n if (existIncludeIndex == -1) {\n // if include not exists, ignore this except\n return;\n }\n\n if (lastLevel) {\n // if it not have exclude form\n if (Array.isArray(queryParams['include'][existIncludeIndex]['attributes'])) {\n return;\n } else {\n if (!queryParams['include'][existIncludeIndex]['attributes']['exclude']) {\n queryParams['include'][existIncludeIndex]['attributes']['exclude'] = [];\n }\n\n queryParams['include'][existIncludeIndex]['attributes']['exclude'].push(exceptPath[1]);\n }\n } else {\n setExcept(queryParams['include'][existIncludeIndex], exceptPath.filter((_, index) => index !== 0).join('.'));\n }\n };\n\n for (const exceptKey of except) {\n setExcept(filterParams, exceptKey);\n }\n\n return filterParams;\n }\n\n protected parseAppends(appends: Appends, filterParams: any) {\n if (!appends) return filterParams;\n\n /**\n * set include params\n * @param includeRoot\n * @param appends\n */\n const setInclude = (model: ModelCtor<any>, queryParams: any, append: string) => {\n const appendFields = append.split('.');\n const appendAssociation = appendFields[0];\n\n const associations = model.associations;\n\n // if append length less or equal 2\n // example:\n // appends: ['posts']\n // appends: ['posts.title']\n // All of these can be seen as last level\n let lastLevel: boolean = false;\n\n if (appendFields.length == 1) {\n lastLevel = true;\n }\n\n if (appendFields.length == 2) {\n const associationModel = associations[appendFields[0]].target;\n if (associationModel.rawAttributes[appendFields[1]]) {\n lastLevel = true;\n }\n }\n\n // find association index\n if (queryParams['include'] == undefined) {\n queryParams['include'] = [];\n }\n\n let existIncludeIndex = queryParams['include'].findIndex(\n (include) => include['association'] == appendAssociation,\n );\n\n // if association not exist, create it\n if (existIncludeIndex == -1) {\n // association not exists\n queryParams['include'].push({\n association: appendAssociation,\n });\n\n existIncludeIndex = 0;\n }\n\n // end appends\n // without nests association\n if (lastLevel) {\n // get exist association attributes\n let attributes = queryParams['include'][existIncludeIndex]['attributes'] || {\n include: [], // all fields are output by default\n };\n\n // if need set attribute\n if (appendFields.length == 2) {\n if (!Array.isArray(attributes)) {\n attributes = [];\n }\n\n const attributeName = appendFields[1];\n\n // push field to it\n attributes.push(attributeName);\n } else {\n // if attributes is empty array, change it to object\n if (Array.isArray(attributes) && attributes.length == 0) {\n attributes = {\n include: [],\n };\n }\n }\n\n // set new attributes\n queryParams['include'][existIncludeIndex] = {\n ...queryParams['include'][existIncludeIndex],\n attributes,\n };\n } else {\n setInclude(\n model.associations[queryParams['include'][existIncludeIndex].association].target,\n queryParams['include'][existIncludeIndex],\n appendFields.filter((_, index) => index !== 0).join('.'),\n );\n }\n };\n\n // handle every appends\n for (const append of appends) {\n setInclude(this.model, filterParams, append);\n }\n\n debug('filter params: %o', filterParams);\n return filterParams;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { Database } from './database';
|
|
11
|
+
const db = new Database({
|
|
12
|
+
dialect: 'sqlite',
|
|
13
|
+
dialectModule: require('sqlite3'),
|
|
14
|
+
storage: ':memory:',
|
|
15
|
+
});
|
|
16
|
+
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
17
|
+
const User = db.collection({
|
|
18
|
+
name: 'users',
|
|
19
|
+
fields: [{ type: 'string', name: 'name' }],
|
|
20
|
+
});
|
|
21
|
+
const Post = db.collection({
|
|
22
|
+
name: 'posts',
|
|
23
|
+
fields: [
|
|
24
|
+
{ type: 'string', name: 'title' },
|
|
25
|
+
{
|
|
26
|
+
type: 'belongsTo',
|
|
27
|
+
name: 'user',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
yield db.sync();
|
|
32
|
+
const repository = User.repository;
|
|
33
|
+
yield repository.createMany({
|
|
34
|
+
records: [
|
|
35
|
+
{ name: 'u1', posts: [{ title: 'u1t1' }] },
|
|
36
|
+
{ name: 'u2', posts: [{ title: 'u2t1' }] },
|
|
37
|
+
{ name: 'u3', posts: [{ title: 'u3t1' }] },
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
const Model = User.model;
|
|
41
|
+
const user = yield Model.findOne({
|
|
42
|
+
subQuery: false,
|
|
43
|
+
where: {
|
|
44
|
+
'$posts.title$': 'u1t1',
|
|
45
|
+
},
|
|
46
|
+
include: { association: 'posts', attributes: [] },
|
|
47
|
+
attributes: {
|
|
48
|
+
include: [],
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
console.log(user.toJSON());
|
|
52
|
+
}))();
|
|
53
|
+
//# sourceMappingURL=playground.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"playground.js","sourceRoot":"","sources":["../src/playground.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC;IACtB,OAAO,EAAE,QAAQ;IACjB,aAAa,EAAE,OAAO,CAAC,SAAS,CAAC;IACjC,OAAO,EAAE,UAAU;CACpB,CAAC,CAAC;AAEH,CAAC,GAAS,EAAE;IACV,MAAM,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC;QACzB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;KAC3C,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,EAAE,CAAC,UAAU,CAAC;QACzB,IAAI,EAAE,OAAO;QACb,MAAM,EAAE;YACN,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE;YACjC;gBACE,IAAI,EAAE,WAAW;gBACjB,IAAI,EAAE,MAAM;aACb;SACF;KACF,CAAC,CAAC;IAEH,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC;IAEhB,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;IAEnC,MAAM,UAAU,CAAC,UAAU,CAAC;QAC1B,OAAO,EAAE;YACP,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE;YAC1C,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE;YAC1C,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE;SAC3C;KACF,CAAC,CAAC;IAEH,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;IACzB,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC;QAC/B,QAAQ,EAAE,KAAK;QACf,KAAK,EAAE;YACL,eAAe,EAAE,MAAM;SACxB;QACD,OAAO,EAAE,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE;QACjD,UAAU,EAAE;YACV,OAAO,EAAE,EAAE;SACZ;KACF,CAAC,CAAC;IAEH,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;AAC7B,CAAC,CAAA,CAAC,EAAE,CAAC","sourcesContent":["import { Database } from './database';\nimport FilterParser from './filter-parser';\n\nconst db = new Database({\n dialect: 'sqlite',\n dialectModule: require('sqlite3'),\n storage: ':memory:',\n});\n\n(async () => {\n const User = db.collection({\n name: 'users',\n fields: [{ type: 'string', name: 'name' }],\n });\n\n const Post = db.collection({\n name: 'posts',\n fields: [\n { type: 'string', name: 'title' },\n {\n type: 'belongsTo',\n name: 'user',\n },\n ],\n });\n\n await db.sync();\n\n const repository = User.repository;\n\n await repository.createMany({\n records: [\n { name: 'u1', posts: [{ title: 'u1t1' }] },\n { name: 'u2', posts: [{ title: 'u2t1' }] },\n { name: 'u3', posts: [{ title: 'u3t1' }] },\n ],\n });\n\n const Model = User.model;\n const user = await Model.findOne({\n subQuery: false,\n where: {\n '$posts.title$': 'u1t1',\n },\n include: { association: 'posts', attributes: [] },\n attributes: {\n include: [],\n },\n });\n\n console.log(user.toJSON());\n})();\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Transaction } from 'sequelize';
|
|
2
|
+
import { Model } from '../model';
|
|
3
|
+
import { CreateOptions, DestroyOptions, FindOptions, TargetKey, UpdateOptions } from '../repository';
|
|
4
|
+
import { FindAndCountOptions, FindOneOptions, MultipleRelationRepository } from './multiple-relation-repository';
|
|
5
|
+
import { AssociatedOptions, PrimaryKeyWithThroughValues } from './types';
|
|
6
|
+
declare type CreateBelongsToManyOptions = CreateOptions;
|
|
7
|
+
interface IBelongsToManyRepository<M extends Model> {
|
|
8
|
+
find(options?: FindOptions): Promise<M[]>;
|
|
9
|
+
findAndCount(options?: FindAndCountOptions): Promise<[M[], number]>;
|
|
10
|
+
findOne(options?: FindOneOptions): Promise<M>;
|
|
11
|
+
create(options?: CreateBelongsToManyOptions): Promise<M>;
|
|
12
|
+
update(options?: UpdateOptions): Promise<M>;
|
|
13
|
+
destroy(options?: number | string | number[] | string[] | DestroyOptions): Promise<Boolean>;
|
|
14
|
+
set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
15
|
+
add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
16
|
+
remove(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
|
|
17
|
+
toggle(options: TargetKey | {
|
|
18
|
+
pk?: TargetKey;
|
|
19
|
+
transaction?: Transaction;
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export declare class BelongsToManyRepository extends MultipleRelationRepository implements IBelongsToManyRepository<any> {
|
|
23
|
+
create(options?: CreateBelongsToManyOptions): Promise<any>;
|
|
24
|
+
destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean>;
|
|
25
|
+
protected setTargets(call: 'add' | 'set', options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|
|
26
|
+
add(options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|
|
27
|
+
set(options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|
|
28
|
+
toggle(options: TargetKey | {
|
|
29
|
+
tk?: TargetKey;
|
|
30
|
+
transaction?: Transaction;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
extendFindOptions(findOptions: any): any;
|
|
33
|
+
throughName(): any;
|
|
34
|
+
throughModel(): any;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
8
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
9
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
10
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
11
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
12
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
13
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
import lodash from 'lodash';
|
|
17
|
+
import { Op } from 'sequelize';
|
|
18
|
+
import { updateThroughTableValue } from '../update-associations';
|
|
19
|
+
import { MultipleRelationRepository } from './multiple-relation-repository';
|
|
20
|
+
import { transaction } from './relation-repository';
|
|
21
|
+
export class BelongsToManyRepository extends MultipleRelationRepository {
|
|
22
|
+
create(options) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
const transaction = yield this.getTransaction(options);
|
|
25
|
+
const createAccessor = this.accessors().create;
|
|
26
|
+
const values = options.values || {};
|
|
27
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
28
|
+
const createOptions = Object.assign(Object.assign({}, options), { through: values[this.throughName()], transaction });
|
|
29
|
+
return sourceModel[createAccessor](values, createOptions);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
destroy(options) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const transaction = yield this.getTransaction(options);
|
|
35
|
+
const association = this.association;
|
|
36
|
+
const instancesToIds = (instances) => {
|
|
37
|
+
return instances.map((instance) => instance.get(this.targetKey()));
|
|
38
|
+
};
|
|
39
|
+
// Through Table
|
|
40
|
+
const throughTableWhere = [
|
|
41
|
+
{
|
|
42
|
+
[association.foreignKey]: this.sourceKeyValue,
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
let ids;
|
|
46
|
+
if (options && options['filter']) {
|
|
47
|
+
const instances = yield this.find({
|
|
48
|
+
filter: options['filter'],
|
|
49
|
+
transaction,
|
|
50
|
+
});
|
|
51
|
+
ids = instancesToIds(instances);
|
|
52
|
+
}
|
|
53
|
+
if (options && options['filterByTk']) {
|
|
54
|
+
const instances = this.association.toInstanceArray(options['filterByTk']);
|
|
55
|
+
ids = ids ? lodash.intersection(ids, instancesToIds(instances)) : instancesToIds(instances);
|
|
56
|
+
}
|
|
57
|
+
if (options && !options['filterByTk'] && !options['filter']) {
|
|
58
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
59
|
+
const instances = yield sourceModel[this.accessors().get]({
|
|
60
|
+
transaction,
|
|
61
|
+
});
|
|
62
|
+
ids = instancesToIds(instances);
|
|
63
|
+
}
|
|
64
|
+
throughTableWhere.push({
|
|
65
|
+
[association.otherKey]: {
|
|
66
|
+
[Op.in]: ids,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
// delete through table data
|
|
70
|
+
yield this.throughModel().destroy({
|
|
71
|
+
where: throughTableWhere,
|
|
72
|
+
transaction,
|
|
73
|
+
});
|
|
74
|
+
yield this.targetModel.destroy({
|
|
75
|
+
where: {
|
|
76
|
+
[this.targetKey()]: {
|
|
77
|
+
[Op.in]: ids,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
transaction,
|
|
81
|
+
});
|
|
82
|
+
return true;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
setTargets(call, options) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
let handleKeys;
|
|
88
|
+
const transaction = yield this.getTransaction(options, false);
|
|
89
|
+
if (lodash.isPlainObject(options)) {
|
|
90
|
+
options = options.tk || [];
|
|
91
|
+
}
|
|
92
|
+
if (lodash.isString(options) || lodash.isNumber(options)) {
|
|
93
|
+
handleKeys = [options];
|
|
94
|
+
} // if it is type primaryKeyWithThroughValues
|
|
95
|
+
else if (lodash.isArray(options) && options.length == 2 && lodash.isPlainObject(options[0][1])) {
|
|
96
|
+
handleKeys = [options];
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
handleKeys = options;
|
|
100
|
+
}
|
|
101
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
102
|
+
const setObj = handleKeys.reduce((carry, item) => {
|
|
103
|
+
if (Array.isArray(item)) {
|
|
104
|
+
carry[item[0]] = item[1];
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
carry[item] = true;
|
|
108
|
+
}
|
|
109
|
+
return carry;
|
|
110
|
+
}, {});
|
|
111
|
+
yield sourceModel[this.accessors()[call]](Object.keys(setObj), {
|
|
112
|
+
transaction,
|
|
113
|
+
});
|
|
114
|
+
for (const [id, throughValues] of Object.entries(setObj)) {
|
|
115
|
+
if (typeof throughValues === 'object') {
|
|
116
|
+
const instance = yield this.targetModel.findByPk(id, {
|
|
117
|
+
transaction,
|
|
118
|
+
});
|
|
119
|
+
yield updateThroughTableValue(instance, this.throughName(), throughValues, sourceModel, transaction);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
add(options) {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
yield this.setTargets('add', options);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
set(options) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
yield this.setTargets('set', options);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
toggle(options) {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
const transaction = yield this.getTransaction(options);
|
|
137
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
138
|
+
const has = yield sourceModel[this.accessors().hasSingle](options['tk'], {
|
|
139
|
+
transaction,
|
|
140
|
+
});
|
|
141
|
+
if (has) {
|
|
142
|
+
yield this.remove(Object.assign(Object.assign({}, options), { transaction }));
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
yield this.add(Object.assign(Object.assign({}, options), { transaction }));
|
|
146
|
+
}
|
|
147
|
+
return;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
extendFindOptions(findOptions) {
|
|
151
|
+
let joinTableAttributes;
|
|
152
|
+
if (lodash.get(findOptions, 'fields')) {
|
|
153
|
+
joinTableAttributes = [];
|
|
154
|
+
}
|
|
155
|
+
return Object.assign(Object.assign({}, findOptions), { joinTableAttributes });
|
|
156
|
+
}
|
|
157
|
+
throughName() {
|
|
158
|
+
return this.throughModel().name;
|
|
159
|
+
}
|
|
160
|
+
throughModel() {
|
|
161
|
+
return this.association.through.model;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
__decorate([
|
|
165
|
+
transaction()
|
|
166
|
+
], BelongsToManyRepository.prototype, "create", null);
|
|
167
|
+
__decorate([
|
|
168
|
+
transaction((args, transaction) => {
|
|
169
|
+
return {
|
|
170
|
+
filterByTk: args[0],
|
|
171
|
+
transaction,
|
|
172
|
+
};
|
|
173
|
+
})
|
|
174
|
+
], BelongsToManyRepository.prototype, "destroy", null);
|
|
175
|
+
__decorate([
|
|
176
|
+
transaction((args, transaction) => {
|
|
177
|
+
return {
|
|
178
|
+
tk: args[0],
|
|
179
|
+
transaction,
|
|
180
|
+
};
|
|
181
|
+
})
|
|
182
|
+
], BelongsToManyRepository.prototype, "add", null);
|
|
183
|
+
__decorate([
|
|
184
|
+
transaction((args, transaction) => {
|
|
185
|
+
return {
|
|
186
|
+
tk: args[0],
|
|
187
|
+
transaction,
|
|
188
|
+
};
|
|
189
|
+
})
|
|
190
|
+
], BelongsToManyRepository.prototype, "set", null);
|
|
191
|
+
__decorate([
|
|
192
|
+
transaction((args, transaction) => {
|
|
193
|
+
return {
|
|
194
|
+
tk: args[0],
|
|
195
|
+
transaction,
|
|
196
|
+
};
|
|
197
|
+
})
|
|
198
|
+
], BelongsToManyRepository.prototype, "toggle", null);
|
|
199
|
+
//# sourceMappingURL=belongs-to-many-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"belongs-to-many-repository.js","sourceRoot":"","sources":["../../src/relation-repository/belongs-to-many-repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAiB,EAAE,EAAe,MAAM,WAAW,CAAC;AAG3D,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAuC,0BAA0B,EAAE,MAAM,gCAAgC,CAAC;AACjH,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAwBpD,MAAM,OAAO,uBAAwB,SAAQ,0BAA0B;IAE/D,MAAM,CAAC,OAAoC;;YAC/C,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YAEvD,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC,MAAM,CAAC;YAE/C,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC;YAEpC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,CAAC;YAE3D,MAAM,aAAa,mCACd,OAAO,KACV,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,EACnC,WAAW,GACZ,CAAC;YAEF,OAAO,WAAW,CAAC,cAAc,CAAC,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;QAC5D,CAAC;KAAA;IAQK,OAAO,CAAC,OAAkD;;YAC9D,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YACvD,MAAM,WAAW,GAAkB,IAAI,CAAC,WAAW,CAAC;YAEpD,MAAM,cAAc,GAAG,CAAC,SAAS,EAAE,EAAE;gBACnC,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAA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AAI,CAAC,CAAC,CAAC;YACX,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;kDAKD;AAQD;IANC,WAAW,CAAC,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE;QACjC,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;YACX,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;qDAsBD","sourcesContent":["import lodash from 'lodash';\nimport { BelongsToMany, Op, Transaction } from 'sequelize';\nimport { Model } from '../model';\nimport { CreateOptions, DestroyOptions, FindOptions, TargetKey, UpdateOptions } from '../repository';\nimport { updateThroughTableValue } from '../update-associations';\nimport { FindAndCountOptions, FindOneOptions, MultipleRelationRepository } from './multiple-relation-repository';\nimport { transaction } from './relation-repository';\nimport { AssociatedOptions, PrimaryKeyWithThroughValues } from './types';\n\ntype CreateBelongsToManyOptions = CreateOptions;\n\ninterface IBelongsToManyRepository<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?: CreateBelongsToManyOptions): Promise<M>;\n // 更新,存在中间表数据\n update(options?: UpdateOptions): Promise<M>;\n // 删除\n destroy(options?: number | string | number[] | string[] | 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 toggle(options: TargetKey | { pk?: TargetKey; transaction?: Transaction }): Promise<void>;\n}\n\nexport class BelongsToManyRepository extends MultipleRelationRepository implements IBelongsToManyRepository<any> {\n @transaction()\n async create(options?: CreateBelongsToManyOptions): Promise<any> {\n const transaction = await this.getTransaction(options);\n\n const createAccessor = this.accessors().create;\n\n const values = options.values || {};\n\n const sourceModel = await this.getSourceModel(transaction);\n\n const createOptions = {\n ...options,\n through: values[this.throughName()],\n transaction,\n };\n\n return sourceModel[createAccessor](values, createOptions);\n }\n\n @transaction((args, transaction) => {\n return {\n filterByTk: args[0],\n transaction,\n };\n })\n async destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<Boolean> {\n const transaction = await this.getTransaction(options);\n const association = <BelongsToMany>this.association;\n\n const instancesToIds = (instances) => {\n return instances.map((instance) => instance.get(this.targetKey()));\n };\n\n // Through Table\n const throughTableWhere: Array<any> = [\n {\n [association.foreignKey]: this.sourceKeyValue,\n },\n ];\n\n let ids;\n\n if (options && options['filter']) {\n const instances = await this.find({\n filter: options['filter'],\n transaction,\n });\n\n ids = instancesToIds(instances);\n }\n\n if (options && options['filterByTk']) {\n const instances = (<any>this.association).toInstanceArray(options['filterByTk']);\n ids = ids ? lodash.intersection(ids, instancesToIds(instances)) : instancesToIds(instances);\n }\n\n if (options && !options['filterByTk'] && !options['filter']) {\n const sourceModel = await this.getSourceModel(transaction);\n\n const instances = await sourceModel[this.accessors().get]({\n transaction,\n });\n\n ids = instancesToIds(instances);\n }\n\n throughTableWhere.push({\n [association.otherKey]: {\n [Op.in]: ids,\n },\n });\n\n // delete through table data\n await this.throughModel().destroy({\n where: throughTableWhere,\n transaction,\n });\n\n await this.targetModel.destroy({\n where: {\n [this.targetKey()]: {\n [Op.in]: ids,\n },\n },\n transaction,\n });\n\n return true;\n }\n\n protected async setTargets(\n call: 'add' | 'set',\n options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,\n ) {\n let handleKeys: TargetKey[] | PrimaryKeyWithThroughValues[];\n\n const transaction = await this.getTransaction(options, false);\n\n if (lodash.isPlainObject(options)) {\n options = (<AssociatedOptions>options).tk || [];\n }\n\n if (lodash.isString(options) || lodash.isNumber(options)) {\n handleKeys = [<TargetKey>options];\n } // if it is type primaryKeyWithThroughValues\n else if (lodash.isArray(options) && options.length == 2 && lodash.isPlainObject(options[0][1])) {\n handleKeys = [<PrimaryKeyWithThroughValues>options];\n } else {\n handleKeys = <TargetKey[] | PrimaryKeyWithThroughValues[]>options;\n }\n\n const sourceModel = await this.getSourceModel(transaction);\n\n const setObj = (<any>handleKeys).reduce((carry, item) => {\n if (Array.isArray(item)) {\n carry[item[0]] = item[1];\n } else {\n carry[item] = true;\n }\n return carry;\n }, {});\n\n await sourceModel[this.accessors()[call]](Object.keys(setObj), {\n transaction,\n });\n\n for (const [id, throughValues] of Object.entries(setObj)) {\n if (typeof throughValues === 'object') {\n const instance = await this.targetModel.findByPk(id, {\n transaction,\n });\n await updateThroughTableValue(instance, this.throughName(), throughValues, sourceModel, transaction);\n }\n }\n }\n\n @transaction((args, transaction) => {\n return {\n tk: args[0],\n transaction,\n };\n })\n async add(\n options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,\n ): Promise<void> {\n await this.setTargets('add', options);\n }\n\n @transaction((args, transaction) => {\n return {\n tk: args[0],\n transaction,\n };\n })\n async set(\n options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions,\n ): Promise<void> {\n await this.setTargets('set', options);\n }\n\n @transaction((args, transaction) => {\n return {\n tk: args[0],\n transaction,\n };\n })\n async toggle(options: TargetKey | { tk?: TargetKey; transaction?: Transaction }): Promise<void> {\n const transaction = await this.getTransaction(options);\n const sourceModel = await this.getSourceModel(transaction);\n\n const has = await sourceModel[this.accessors().hasSingle](options['tk'], {\n transaction,\n });\n\n if (has) {\n await this.remove({\n ...(<any>options),\n transaction,\n });\n } else {\n await this.add({\n ...(<any>options),\n transaction,\n });\n }\n\n return;\n }\n\n extendFindOptions(findOptions) {\n let joinTableAttributes;\n if (lodash.get(findOptions, 'fields')) {\n joinTableAttributes = [];\n }\n\n return {\n ...findOptions,\n joinTableAttributes,\n };\n }\n\n throughName() {\n return this.throughModel().name;\n }\n\n throughModel() {\n return (<any>this.association).through.model;\n }\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Model } from '../model';
|
|
2
|
+
import { CreateOptions, UpdateOptions } from '../repository';
|
|
3
|
+
import { SingleRelationFindOption, SingleRelationRepository } from './single-relation-repository';
|
|
4
|
+
interface BelongsToFindOptions extends SingleRelationFindOption {
|
|
5
|
+
}
|
|
6
|
+
interface IBelongsToRepository<M extends Model> {
|
|
7
|
+
find(options?: BelongsToFindOptions): Promise<M>;
|
|
8
|
+
findOne(options?: BelongsToFindOptions): Promise<M>;
|
|
9
|
+
create(options?: CreateOptions): Promise<M>;
|
|
10
|
+
update(options?: UpdateOptions): Promise<M>;
|
|
11
|
+
destroy(): Promise<Boolean>;
|
|
12
|
+
set(primaryKey: any): Promise<void>;
|
|
13
|
+
remove(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare class BelongsToRepository extends SingleRelationRepository implements IBelongsToRepository<any> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"belongs-to-repository.js","sourceRoot":"","sources":["../../src/relation-repository/belongs-to-repository.ts"],"names":[],"mappings":"AAEA,OAAO,EAA4B,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAoBlG,MAAM,OAAO,mBAAoB,SAAQ,wBAAwB;CAAwC","sourcesContent":["import { Model } from '../model';\nimport { CreateOptions, UpdateOptions } from '../repository';\nimport { SingleRelationFindOption, SingleRelationRepository } from './single-relation-repository';\n\ninterface BelongsToFindOptions extends SingleRelationFindOption {}\n\ninterface IBelongsToRepository<M extends Model> {\n // 不需要 findOne,find 就是 findOne\n find(options?: BelongsToFindOptions): Promise<M>;\n findOne(options?: BelongsToFindOptions): Promise<M>;\n // 新增并关联,如果存在关联,解除之后,与新数据建立关联\n create(options?: CreateOptions): Promise<M>;\n // 更新\n update(options?: UpdateOptions): Promise<M>;\n // 删除\n destroy(): Promise<Boolean>;\n // 建立关联\n set(primaryKey: any): Promise<void>;\n // 移除关联\n remove(): Promise<void>;\n}\n\nexport class BelongsToRepository extends SingleRelationRepository implements IBelongsToRepository<any> {}\n"]}
|
|
@@ -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 {};
|