@nocobase/database 0.5.0-alpha.37 → 0.7.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +47 -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,7 @@
|
|
|
1
|
+
import { TargetKey, Values } from '../repository';
|
|
2
|
+
import { Transactionable } from 'sequelize';
|
|
3
|
+
export declare type PrimaryKeyWithThroughValues = [TargetKey, Values];
|
|
4
|
+
export interface AssociatedOptions extends Transactionable {
|
|
5
|
+
tk?: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[];
|
|
6
|
+
}
|
|
7
|
+
export declare type setAssociationOptions = TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/relation-repository/types.ts"],"names":[],"mappings":"","sourcesContent":["import { TargetKey, Values } from '../repository';\nimport { Transactionable } from 'sequelize';\n\nexport type PrimaryKeyWithThroughValues = [TargetKey, Values];\n\nexport interface AssociatedOptions extends Transactionable {\n tk?: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[];\n}\n\nexport type setAssociationOptions =\n | TargetKey\n | TargetKey[]\n | PrimaryKeyWithThroughValues\n | PrimaryKeyWithThroughValues[]\n | AssociatedOptions;\n"]}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Association, BulkCreateOptions, CreateOptions as SequelizeCreateOptions, DestroyOptions as SequelizeDestroyOptions, FindAndCountOptions as SequelizeAndCountOptions, FindOptions as SequelizeFindOptions, ModelCtor, Transaction, UpdateOptions as SequelizeUpdateOptions } from 'sequelize';
|
|
2
|
+
import { Collection } from './collection';
|
|
3
|
+
import { Database } from './database';
|
|
4
|
+
import { Model } from './model';
|
|
5
|
+
import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
|
|
6
|
+
import { BelongsToRepository } from './relation-repository/belongs-to-repository';
|
|
7
|
+
import { HasManyRepository } from './relation-repository/hasmany-repository';
|
|
8
|
+
import { HasOneRepository } from './relation-repository/hasone-repository';
|
|
9
|
+
import { RelationRepository } from './relation-repository/relation-repository';
|
|
10
|
+
export interface IRepository {
|
|
11
|
+
}
|
|
12
|
+
interface CreateManyOptions extends BulkCreateOptions {
|
|
13
|
+
records: Values[];
|
|
14
|
+
}
|
|
15
|
+
export interface TransactionAble {
|
|
16
|
+
transaction?: Transaction;
|
|
17
|
+
}
|
|
18
|
+
export interface FilterAble {
|
|
19
|
+
filter: Filter;
|
|
20
|
+
}
|
|
21
|
+
export declare type TargetKey = string | number;
|
|
22
|
+
export declare type TK = TargetKey | TargetKey[];
|
|
23
|
+
export declare type Filter = any;
|
|
24
|
+
export declare type Appends = string[];
|
|
25
|
+
export declare type Except = string[];
|
|
26
|
+
export declare type Fields = string[];
|
|
27
|
+
export declare type Sort = string[] | string;
|
|
28
|
+
export declare type WhiteList = string[];
|
|
29
|
+
export declare type BlackList = string[];
|
|
30
|
+
export declare type AssociationKeysToBeUpdate = string[];
|
|
31
|
+
export declare type Values = any;
|
|
32
|
+
export interface CountOptions extends Omit<SequelizeCreateOptions, 'distinct' | 'where' | 'include'>, TransactionAble {
|
|
33
|
+
fields?: Fields;
|
|
34
|
+
filter?: Filter;
|
|
35
|
+
}
|
|
36
|
+
export interface FilterByTk {
|
|
37
|
+
filterByTk?: TargetKey;
|
|
38
|
+
}
|
|
39
|
+
export interface FindOptions extends SequelizeFindOptions, CommonFindOptions, FilterByTk {
|
|
40
|
+
}
|
|
41
|
+
export interface CommonFindOptions {
|
|
42
|
+
filter?: Filter;
|
|
43
|
+
fields?: Fields;
|
|
44
|
+
appends?: Appends;
|
|
45
|
+
except?: Except;
|
|
46
|
+
sort?: Sort;
|
|
47
|
+
context?: any;
|
|
48
|
+
}
|
|
49
|
+
interface FindOneOptions extends FindOptions, CommonFindOptions {
|
|
50
|
+
}
|
|
51
|
+
export interface DestroyOptions extends SequelizeDestroyOptions {
|
|
52
|
+
filter?: Filter;
|
|
53
|
+
filterByTk?: TargetKey | TargetKey[];
|
|
54
|
+
truncate?: boolean;
|
|
55
|
+
context?: any;
|
|
56
|
+
}
|
|
57
|
+
interface FindAndCountOptions extends Omit<SequelizeAndCountOptions, 'where' | 'include' | 'order'> {
|
|
58
|
+
filter?: Filter;
|
|
59
|
+
fields?: Fields;
|
|
60
|
+
except?: Except;
|
|
61
|
+
appends?: Appends;
|
|
62
|
+
sort?: Sort;
|
|
63
|
+
}
|
|
64
|
+
export interface CreateOptions extends SequelizeCreateOptions {
|
|
65
|
+
values?: Values;
|
|
66
|
+
whitelist?: WhiteList;
|
|
67
|
+
blacklist?: BlackList;
|
|
68
|
+
updateAssociationValues?: AssociationKeysToBeUpdate;
|
|
69
|
+
context?: any;
|
|
70
|
+
}
|
|
71
|
+
export interface UpdateOptions extends Omit<SequelizeUpdateOptions, 'where'> {
|
|
72
|
+
values: Values;
|
|
73
|
+
filter?: Filter;
|
|
74
|
+
filterByTk?: TargetKey;
|
|
75
|
+
whitelist?: WhiteList;
|
|
76
|
+
blacklist?: BlackList;
|
|
77
|
+
updateAssociationValues?: AssociationKeysToBeUpdate;
|
|
78
|
+
context?: any;
|
|
79
|
+
}
|
|
80
|
+
declare class RelationRepositoryBuilder<R extends RelationRepository> {
|
|
81
|
+
collection: Collection;
|
|
82
|
+
associationName: string;
|
|
83
|
+
association: Association;
|
|
84
|
+
builderMap: {
|
|
85
|
+
HasOne: typeof HasOneRepository;
|
|
86
|
+
BelongsTo: typeof BelongsToRepository;
|
|
87
|
+
BelongsToMany: typeof BelongsToManyRepository;
|
|
88
|
+
HasMany: typeof HasManyRepository;
|
|
89
|
+
};
|
|
90
|
+
constructor(collection: Collection, associationName: string);
|
|
91
|
+
protected builder(): {
|
|
92
|
+
HasOne: typeof HasOneRepository;
|
|
93
|
+
BelongsTo: typeof BelongsToRepository;
|
|
94
|
+
BelongsToMany: typeof BelongsToManyRepository;
|
|
95
|
+
HasMany: typeof HasManyRepository;
|
|
96
|
+
};
|
|
97
|
+
of(id: string | number): R;
|
|
98
|
+
}
|
|
99
|
+
export declare class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> implements IRepository {
|
|
100
|
+
database: Database;
|
|
101
|
+
collection: Collection;
|
|
102
|
+
model: ModelCtor<Model>;
|
|
103
|
+
constructor(collection: Collection);
|
|
104
|
+
/**
|
|
105
|
+
* return count by filter
|
|
106
|
+
*/
|
|
107
|
+
count(countOptions?: CountOptions): Promise<number>;
|
|
108
|
+
/**
|
|
109
|
+
* find
|
|
110
|
+
* @param options
|
|
111
|
+
*/
|
|
112
|
+
find(options?: FindOptions): Promise<Model<any, any>[]>;
|
|
113
|
+
/**
|
|
114
|
+
* find and count
|
|
115
|
+
* @param options
|
|
116
|
+
*/
|
|
117
|
+
findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]>;
|
|
118
|
+
/**
|
|
119
|
+
* Find By Id
|
|
120
|
+
*
|
|
121
|
+
*/
|
|
122
|
+
findById(id: string | number): Promise<Model<any, any>>;
|
|
123
|
+
/**
|
|
124
|
+
* Find one record from database
|
|
125
|
+
*
|
|
126
|
+
* @param options
|
|
127
|
+
*/
|
|
128
|
+
findOne(options?: FindOneOptions): Promise<Model<any, any>>;
|
|
129
|
+
/**
|
|
130
|
+
* Save instance to database
|
|
131
|
+
*
|
|
132
|
+
* @param values
|
|
133
|
+
* @param options
|
|
134
|
+
*/
|
|
135
|
+
create<M extends Model>(options: CreateOptions): Promise<M>;
|
|
136
|
+
/**
|
|
137
|
+
* Save Many instances to database
|
|
138
|
+
*
|
|
139
|
+
* @param records
|
|
140
|
+
* @param options
|
|
141
|
+
*/
|
|
142
|
+
createMany(options: CreateManyOptions): Promise<any[]>;
|
|
143
|
+
/**
|
|
144
|
+
* Update model value
|
|
145
|
+
*
|
|
146
|
+
* @param values
|
|
147
|
+
* @param options
|
|
148
|
+
*/
|
|
149
|
+
update(options: UpdateOptions): Promise<Model<any, any>[]>;
|
|
150
|
+
destroy(options?: TargetKey | TargetKey[] | DestroyOptions): any;
|
|
151
|
+
/**
|
|
152
|
+
* @param association target association
|
|
153
|
+
*/
|
|
154
|
+
relation<R extends RelationRepository>(association: string): RelationRepositoryBuilder<R>;
|
|
155
|
+
protected buildQueryOptions(options: any): any;
|
|
156
|
+
protected parseFilter(filter: Filter, options?: any): {
|
|
157
|
+
where?: undefined;
|
|
158
|
+
include?: undefined;
|
|
159
|
+
} | {
|
|
160
|
+
where: {};
|
|
161
|
+
include: any[];
|
|
162
|
+
};
|
|
163
|
+
protected getTransaction(options: any, autoGen?: boolean): Promise<any>;
|
|
164
|
+
}
|
|
165
|
+
export {};
|
|
@@ -0,0 +1,276 @@
|
|
|
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, { omit } from 'lodash';
|
|
17
|
+
import { Op } from 'sequelize';
|
|
18
|
+
import FilterParser from './filter-parser';
|
|
19
|
+
import { OptionsParser } from './options-parser';
|
|
20
|
+
import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
|
|
21
|
+
import { BelongsToRepository } from './relation-repository/belongs-to-repository';
|
|
22
|
+
import { HasManyRepository } from './relation-repository/hasmany-repository';
|
|
23
|
+
import { HasOneRepository } from './relation-repository/hasone-repository';
|
|
24
|
+
import { transactionWrapperBuilder } from './transaction-decorator';
|
|
25
|
+
import { updateAssociations, updateModelByValues } from './update-associations';
|
|
26
|
+
import { UpdateGuard } from './update-guard';
|
|
27
|
+
const debug = require('debug')('noco-database');
|
|
28
|
+
const transaction = transactionWrapperBuilder(function () {
|
|
29
|
+
return this.collection.model.sequelize.transaction();
|
|
30
|
+
});
|
|
31
|
+
class RelationRepositoryBuilder {
|
|
32
|
+
constructor(collection, associationName) {
|
|
33
|
+
this.builderMap = {
|
|
34
|
+
HasOne: HasOneRepository,
|
|
35
|
+
BelongsTo: BelongsToRepository,
|
|
36
|
+
BelongsToMany: BelongsToManyRepository,
|
|
37
|
+
HasMany: HasManyRepository,
|
|
38
|
+
};
|
|
39
|
+
this.collection = collection;
|
|
40
|
+
this.associationName = associationName;
|
|
41
|
+
this.association = this.collection.model.associations[this.associationName];
|
|
42
|
+
}
|
|
43
|
+
builder() {
|
|
44
|
+
return this.builderMap;
|
|
45
|
+
}
|
|
46
|
+
of(id) {
|
|
47
|
+
const klass = this.builder()[this.association.associationType];
|
|
48
|
+
return new klass(this.collection, this.associationName, id);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export class Repository {
|
|
52
|
+
constructor(collection) {
|
|
53
|
+
this.database = collection.context.database;
|
|
54
|
+
this.collection = collection;
|
|
55
|
+
this.model = collection.model;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* return count by filter
|
|
59
|
+
*/
|
|
60
|
+
count(countOptions) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
let options = countOptions ? lodash.clone(countOptions) : {};
|
|
63
|
+
const transaction = yield this.getTransaction(options);
|
|
64
|
+
if (countOptions === null || countOptions === void 0 ? void 0 : countOptions.filter) {
|
|
65
|
+
options = Object.assign(Object.assign({}, options), this.parseFilter(countOptions.filter, countOptions));
|
|
66
|
+
}
|
|
67
|
+
const count = yield this.collection.model.count(Object.assign(Object.assign({}, options), { distinct: true, transaction }));
|
|
68
|
+
return count;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* find
|
|
73
|
+
* @param options
|
|
74
|
+
*/
|
|
75
|
+
find(options) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
const model = this.collection.model;
|
|
78
|
+
const transaction = yield this.getTransaction(options);
|
|
79
|
+
const opts = Object.assign({ subQuery: false }, this.buildQueryOptions(options));
|
|
80
|
+
if (opts.include && opts.include.length > 0) {
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
const primaryKeyField = model.primaryKeyField || model.primaryKeyAttribute;
|
|
83
|
+
const ids = (yield model.findAll(Object.assign(Object.assign({}, opts), { includeIgnoreAttributes: false, attributes: [primaryKeyField], group: `${model.name}.${primaryKeyField}`, transaction }))).map((row) => row.get(primaryKeyField));
|
|
84
|
+
const where = {
|
|
85
|
+
[primaryKeyField]: {
|
|
86
|
+
[Op.in]: ids,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
return yield model.findAll(Object.assign(Object.assign({}, omit(opts, ['limit', 'offset'])), { where,
|
|
90
|
+
transaction }));
|
|
91
|
+
}
|
|
92
|
+
return yield model.findAll(Object.assign(Object.assign({}, opts), { transaction }));
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* find and count
|
|
97
|
+
* @param options
|
|
98
|
+
*/
|
|
99
|
+
findAndCount(options) {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
const transaction = yield this.getTransaction(options);
|
|
102
|
+
options = Object.assign(Object.assign({}, options), { transaction });
|
|
103
|
+
return [yield this.find(options), yield this.count(options)];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Find By Id
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
110
|
+
findById(id) {
|
|
111
|
+
return this.collection.model.findByPk(id);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Find one record from database
|
|
115
|
+
*
|
|
116
|
+
* @param options
|
|
117
|
+
*/
|
|
118
|
+
findOne(options) {
|
|
119
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
120
|
+
const transaction = yield this.getTransaction(options);
|
|
121
|
+
const rows = yield this.find(Object.assign(Object.assign({}, options), { limit: 1, transaction }));
|
|
122
|
+
return rows.length == 1 ? rows[0] : null;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Save instance to database
|
|
127
|
+
*
|
|
128
|
+
* @param values
|
|
129
|
+
* @param options
|
|
130
|
+
*/
|
|
131
|
+
create(options) {
|
|
132
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
133
|
+
const transaction = yield this.getTransaction(options);
|
|
134
|
+
const guard = UpdateGuard.fromOptions(this.model, Object.assign(Object.assign({}, options), { action: 'create' }));
|
|
135
|
+
const values = guard.sanitize(options.values || {});
|
|
136
|
+
const instance = yield this.model.create(values, Object.assign(Object.assign({}, options), { transaction }));
|
|
137
|
+
if (!instance) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
yield updateAssociations(instance, values, Object.assign(Object.assign({}, options), { transaction }));
|
|
141
|
+
if (options.hooks !== false) {
|
|
142
|
+
yield this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, instance, options);
|
|
143
|
+
yield this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);
|
|
144
|
+
}
|
|
145
|
+
return instance;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Save Many instances to database
|
|
150
|
+
*
|
|
151
|
+
* @param records
|
|
152
|
+
* @param options
|
|
153
|
+
*/
|
|
154
|
+
createMany(options) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
const transaction = yield this.getTransaction(options);
|
|
157
|
+
const { records } = options;
|
|
158
|
+
const instances = [];
|
|
159
|
+
for (const values of records) {
|
|
160
|
+
const instance = yield this.create({ values, transaction });
|
|
161
|
+
instances.push(instance);
|
|
162
|
+
}
|
|
163
|
+
return instances;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Update model value
|
|
168
|
+
*
|
|
169
|
+
* @param values
|
|
170
|
+
* @param options
|
|
171
|
+
*/
|
|
172
|
+
update(options) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
const transaction = yield this.getTransaction(options);
|
|
175
|
+
const guard = UpdateGuard.fromOptions(this.model, options);
|
|
176
|
+
const values = guard.sanitize(options.values);
|
|
177
|
+
const queryOptions = this.buildQueryOptions(options);
|
|
178
|
+
const instances = yield this.find(Object.assign(Object.assign({}, queryOptions), { transaction }));
|
|
179
|
+
for (const instance of instances) {
|
|
180
|
+
yield updateModelByValues(instance, values, Object.assign(Object.assign({}, options), { sanitized: true, transaction }));
|
|
181
|
+
}
|
|
182
|
+
if (options.hooks !== false) {
|
|
183
|
+
for (const instance of instances) {
|
|
184
|
+
yield this.database.emitAsync(`${this.collection.name}.afterUpdateWithAssociations`, instance, options);
|
|
185
|
+
yield this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return instances;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
destroy(options) {
|
|
192
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
193
|
+
const transaction = yield this.getTransaction(options);
|
|
194
|
+
const modelFilterKey = this.collection.filterTargetKey;
|
|
195
|
+
options = options;
|
|
196
|
+
if (options['individualHooks'] === undefined) {
|
|
197
|
+
options['individualHooks'] = true;
|
|
198
|
+
}
|
|
199
|
+
const filterByTk = options.filterByTk && !lodash.isArray(options.filterByTk)
|
|
200
|
+
? [options.filterByTk]
|
|
201
|
+
: options.filterByTk;
|
|
202
|
+
if (filterByTk && !options.filter) {
|
|
203
|
+
return yield this.model.destroy(Object.assign(Object.assign({}, options), { where: {
|
|
204
|
+
[modelFilterKey]: {
|
|
205
|
+
[Op.in]: filterByTk,
|
|
206
|
+
},
|
|
207
|
+
}, transaction }));
|
|
208
|
+
}
|
|
209
|
+
if (options.filter) {
|
|
210
|
+
let pks = (yield this.find({
|
|
211
|
+
filter: options.filter,
|
|
212
|
+
transaction,
|
|
213
|
+
})).map((instance) => instance.get(modelFilterKey));
|
|
214
|
+
if (filterByTk) {
|
|
215
|
+
pks = lodash.intersection(pks.map((i) => `${i}`), filterByTk.map((i) => `${i}`));
|
|
216
|
+
}
|
|
217
|
+
return yield this.destroy(Object.assign(Object.assign({}, lodash.omit(options, 'filter')), { filterByTk: pks, transaction }));
|
|
218
|
+
}
|
|
219
|
+
if (options.truncate) {
|
|
220
|
+
return yield this.model.destroy(Object.assign(Object.assign({}, options), { truncate: true, transaction }));
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @param association target association
|
|
226
|
+
*/
|
|
227
|
+
relation(association) {
|
|
228
|
+
return new RelationRepositoryBuilder(this.collection, association);
|
|
229
|
+
}
|
|
230
|
+
buildQueryOptions(options) {
|
|
231
|
+
const parser = new OptionsParser(options, {
|
|
232
|
+
collection: this.collection,
|
|
233
|
+
});
|
|
234
|
+
const params = parser.toSequelizeParams();
|
|
235
|
+
debug('sequelize query params %o', params);
|
|
236
|
+
return Object.assign(Object.assign({ where: {} }, options), params);
|
|
237
|
+
}
|
|
238
|
+
parseFilter(filter, options) {
|
|
239
|
+
const parser = new FilterParser(filter, {
|
|
240
|
+
collection: this.collection,
|
|
241
|
+
app: {
|
|
242
|
+
ctx: options === null || options === void 0 ? void 0 : options.context,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
return parser.toSequelizeParams();
|
|
246
|
+
}
|
|
247
|
+
getTransaction(options, autoGen = false) {
|
|
248
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
+
if (lodash.isPlainObject(options) && options.transaction) {
|
|
250
|
+
return options.transaction;
|
|
251
|
+
}
|
|
252
|
+
if (autoGen) {
|
|
253
|
+
return yield this.model.sequelize.transaction();
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
__decorate([
|
|
260
|
+
transaction()
|
|
261
|
+
], Repository.prototype, "create", null);
|
|
262
|
+
__decorate([
|
|
263
|
+
transaction()
|
|
264
|
+
], Repository.prototype, "createMany", null);
|
|
265
|
+
__decorate([
|
|
266
|
+
transaction()
|
|
267
|
+
], Repository.prototype, "update", null);
|
|
268
|
+
__decorate([
|
|
269
|
+
transaction((args, transaction) => {
|
|
270
|
+
return {
|
|
271
|
+
filterByTk: args[0],
|
|
272
|
+
transaction,
|
|
273
|
+
};
|
|
274
|
+
})
|
|
275
|
+
], Repository.prototype, "destroy", null);
|
|
276
|
+
//# sourceMappingURL=repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../src/repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,OAAO,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAQL,EAAE,EAGH,MAAM,WAAW,CAAC;AAInB,OAAO,YAAY,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,uBAAuB,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AE,CAAC;IACpC,CAAC;IAEe,cAAc,CAAC,OAAY,EAAE,OAAO,GAAG,KAAK;;YAC1D,IAAI,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,WAAW,EAAE;gBACxD,OAAO,OAAO,CAAC,WAAW,CAAC;aAC5B;YAED,IAAI,OAAO,EAAE;gBACX,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;aACjD;YAED,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;CACF;AA3LC;IADC,WAAW,EAAE;wCA2Bb;AASD;IADC,WAAW,EAAE;4CAUb;AASD;IADC,WAAW,EAAE;wCA8Bb;AAQD;IANC,WAAW,CAAC,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE;QACjC,OAAO;YACL,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC;YACnB,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;yCA0DD","sourcesContent":["import lodash, { omit } from 'lodash';\nimport {\n Association,\n BulkCreateOptions,\n CreateOptions as SequelizeCreateOptions,\n DestroyOptions as SequelizeDestroyOptions,\n FindAndCountOptions as SequelizeAndCountOptions,\n FindOptions as SequelizeFindOptions,\n ModelCtor,\n Op,\n Transaction,\n UpdateOptions as SequelizeUpdateOptions\n} from 'sequelize';\nimport { Collection } from './collection';\nimport { Database } from './database';\nimport { RelationField } from './fields';\nimport FilterParser from './filter-parser';\nimport { Model } from './model';\nimport { OptionsParser } from './options-parser';\nimport { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';\nimport { BelongsToRepository } from './relation-repository/belongs-to-repository';\nimport { HasManyRepository } from './relation-repository/hasmany-repository';\nimport { HasOneRepository } from './relation-repository/hasone-repository';\nimport { RelationRepository } from './relation-repository/relation-repository';\nimport { transactionWrapperBuilder } from './transaction-decorator';\nimport { updateAssociations, updateModelByValues } from './update-associations';\nimport { UpdateGuard } from './update-guard';\n\nconst debug = require('debug')('noco-database');\n\nexport interface IRepository {}\n\ninterface CreateManyOptions extends BulkCreateOptions {\n records: Values[];\n}\n\nexport interface TransactionAble {\n transaction?: Transaction;\n}\n\nexport interface FilterAble {\n filter: Filter;\n}\n\nexport type TargetKey = string | number;\nexport type TK = TargetKey | TargetKey[];\n\nexport type Filter = any;\nexport type Appends = string[];\nexport type Except = string[];\nexport type Fields = string[];\nexport type Sort = string[] | string;\n\nexport type WhiteList = string[];\nexport type BlackList = string[];\nexport type AssociationKeysToBeUpdate = string[];\n\nexport type Values = any;\n\nexport interface CountOptions extends Omit<SequelizeCreateOptions, 'distinct' | 'where' | 'include'>, TransactionAble {\n fields?: Fields;\n filter?: Filter;\n}\n\nexport interface FilterByTk {\n filterByTk?: TargetKey;\n}\n\nexport interface FindOptions extends SequelizeFindOptions, CommonFindOptions, FilterByTk {}\n\nexport interface CommonFindOptions {\n filter?: Filter;\n fields?: Fields;\n appends?: Appends;\n except?: Except;\n sort?: Sort;\n context?: any;\n}\n\ninterface FindOneOptions extends FindOptions, CommonFindOptions {}\n\nexport interface DestroyOptions extends SequelizeDestroyOptions {\n filter?: Filter;\n filterByTk?: TargetKey | TargetKey[];\n truncate?: boolean;\n context?: any;\n}\n\ninterface FindAndCountOptions extends Omit<SequelizeAndCountOptions, 'where' | 'include' | 'order'> {\n // 数据过滤\n filter?: Filter;\n // 输出结果显示哪些字段\n fields?: Fields;\n // 输出结果不显示哪些字段\n except?: Except;\n // 附加字段,用于控制关系字段的输出\n appends?: Appends;\n // 排序,字段前面加上 “-” 表示降序\n sort?: Sort;\n}\n\nexport interface CreateOptions extends SequelizeCreateOptions {\n values?: Values;\n whitelist?: WhiteList;\n blacklist?: BlackList;\n updateAssociationValues?: AssociationKeysToBeUpdate;\n context?: any;\n}\n\nexport interface UpdateOptions extends Omit<SequelizeUpdateOptions, 'where'> {\n values: Values;\n filter?: Filter;\n filterByTk?: TargetKey;\n whitelist?: WhiteList;\n blacklist?: BlackList;\n updateAssociationValues?: AssociationKeysToBeUpdate;\n context?: any;\n}\n\ninterface RelatedQueryOptions {\n database: Database;\n field: RelationField;\n source: {\n idOrInstance: any;\n collection: Collection;\n };\n target: {\n association: Association & {\n accessors: any;\n };\n collection: Collection;\n };\n}\n\nconst transaction = transactionWrapperBuilder(function () {\n return (<Repository>this).collection.model.sequelize.transaction();\n});\n\nclass RelationRepositoryBuilder<R extends RelationRepository> {\n collection: Collection;\n associationName: string;\n association: Association;\n\n builderMap = {\n HasOne: HasOneRepository,\n BelongsTo: BelongsToRepository,\n BelongsToMany: BelongsToManyRepository,\n HasMany: HasManyRepository,\n };\n\n constructor(collection: Collection, associationName: string) {\n this.collection = collection;\n this.associationName = associationName;\n this.association = this.collection.model.associations[this.associationName];\n }\n\n protected builder() {\n return this.builderMap;\n }\n\n of(id: string | number): R {\n const klass = this.builder()[this.association.associationType];\n return new klass(this.collection, this.associationName, id);\n }\n}\n\nexport class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>\n implements IRepository\n{\n database: Database;\n collection: Collection;\n model: ModelCtor<Model>;\n\n constructor(collection: Collection) {\n this.database = collection.context.database;\n this.collection = collection;\n this.model = collection.model;\n }\n\n /**\n * return count by filter\n */\n async count(countOptions?: CountOptions): Promise<number> {\n let options = countOptions ? lodash.clone(countOptions) : {};\n\n const transaction = await this.getTransaction(options);\n\n if (countOptions?.filter) {\n options = {\n ...options,\n ...this.parseFilter(countOptions.filter, countOptions),\n };\n }\n\n const count = await this.collection.model.count({\n ...options,\n distinct: true,\n transaction,\n });\n\n return count;\n }\n\n /**\n * find\n * @param options\n */\n async find(options?: FindOptions) {\n const model = this.collection.model;\n const transaction = await this.getTransaction(options);\n\n const opts = {\n subQuery: false,\n ...this.buildQueryOptions(options),\n };\n\n if (opts.include && opts.include.length > 0) {\n // @ts-ignore\n const primaryKeyField = model.primaryKeyField || model.primaryKeyAttribute;\n\n const ids = (\n await model.findAll({\n ...opts,\n includeIgnoreAttributes: false,\n attributes: [primaryKeyField],\n group: `${model.name}.${primaryKeyField}`,\n transaction,\n })\n ).map((row) => row.get(primaryKeyField));\n\n const where = {\n [primaryKeyField]: {\n [Op.in]: ids,\n },\n };\n\n return await model.findAll({\n ...omit(opts, ['limit', 'offset']),\n where,\n transaction,\n });\n }\n\n return await model.findAll({\n ...opts,\n transaction,\n });\n }\n\n /**\n * find and count\n * @param options\n */\n async findAndCount(options?: FindAndCountOptions): Promise<[Model[], number]> {\n const transaction = await this.getTransaction(options);\n options = {\n ...options,\n transaction,\n };\n\n return [await this.find(options), await this.count(options)];\n }\n\n /**\n * Find By Id\n *\n */\n findById(id: string | number) {\n return this.collection.model.findByPk(id);\n }\n\n /**\n * Find one record from database\n *\n * @param options\n */\n async findOne(options?: FindOneOptions) {\n const transaction = await this.getTransaction(options);\n\n const rows = await this.find({ ...options, limit: 1, transaction });\n return rows.length == 1 ? rows[0] : null;\n }\n\n /**\n * Save instance to database\n *\n * @param values\n * @param options\n */\n @transaction()\n async create<M extends Model>(options: CreateOptions): Promise<M> {\n const transaction = await this.getTransaction(options);\n\n const guard = UpdateGuard.fromOptions(this.model, { ...options, action: 'create' });\n const values = guard.sanitize(options.values || {});\n\n const instance = await this.model.create<any>(values, {\n ...options,\n transaction,\n });\n\n if (!instance) {\n return;\n }\n\n await updateAssociations(instance, values, {\n ...options,\n transaction,\n });\n\n if (options.hooks !== false) {\n await this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, instance, options);\n await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);\n }\n\n return instance;\n }\n\n /**\n * Save Many instances to database\n *\n * @param records\n * @param options\n */\n @transaction()\n async createMany(options: CreateManyOptions) {\n const transaction = await this.getTransaction(options);\n const { records } = options;\n const instances = [];\n for (const values of records) {\n const instance = await this.create({ values, transaction });\n instances.push(instance);\n }\n return instances;\n }\n\n /**\n * Update model value\n *\n * @param values\n * @param options\n */\n @transaction()\n async update(options: UpdateOptions) {\n const transaction = await this.getTransaction(options);\n const guard = UpdateGuard.fromOptions(this.model, options);\n\n const values = guard.sanitize(options.values);\n\n const queryOptions = this.buildQueryOptions(options);\n\n const instances = await this.find({\n ...queryOptions,\n transaction,\n });\n\n for (const instance of instances) {\n await updateModelByValues(instance, values, {\n ...options,\n sanitized: true,\n transaction,\n });\n }\n\n if (options.hooks !== false) {\n for (const instance of instances) {\n await this.database.emitAsync(`${this.collection.name}.afterUpdateWithAssociations`, instance, options);\n await this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);\n }\n }\n\n return instances;\n }\n\n @transaction((args, transaction) => {\n return {\n filterByTk: args[0],\n transaction,\n };\n })\n async destroy(options?: TargetKey | TargetKey[] | DestroyOptions) {\n const transaction = await this.getTransaction(options);\n\n const modelFilterKey = this.collection.filterTargetKey;\n\n options = <DestroyOptions>options;\n\n if (options['individualHooks'] === undefined) {\n options['individualHooks'] = true;\n }\n\n const filterByTk: TargetKey[] | undefined =\n options.filterByTk && !lodash.isArray(options.filterByTk)\n ? [options.filterByTk]\n : (options.filterByTk as TargetKey[] | undefined);\n\n if (filterByTk && !options.filter) {\n return await this.model.destroy({\n ...options,\n where: {\n [modelFilterKey]: {\n [Op.in]: filterByTk,\n },\n },\n transaction,\n });\n }\n\n if (options.filter) {\n let pks = (\n await this.find({\n filter: options.filter,\n transaction,\n })\n ).map((instance) => instance.get(modelFilterKey) as TargetKey);\n\n if (filterByTk) {\n pks = lodash.intersection(\n pks.map((i) => `${i}`),\n filterByTk.map((i) => `${i}`),\n );\n }\n\n return await this.destroy({\n ...lodash.omit(options, 'filter'),\n filterByTk: pks,\n transaction,\n });\n }\n\n if (options.truncate) {\n return await this.model.destroy({\n ...options,\n truncate: true,\n transaction,\n });\n }\n }\n\n /**\n * @param association target association\n */\n relation<R extends RelationRepository>(association: string): RelationRepositoryBuilder<R> {\n return new RelationRepositoryBuilder<R>(this.collection, association);\n }\n\n protected buildQueryOptions(options: any) {\n const parser = new OptionsParser(options, {\n collection: this.collection,\n });\n\n const params = parser.toSequelizeParams();\n debug('sequelize query params %o', params);\n return { where: {}, ...options, ...params };\n }\n\n protected parseFilter(filter: Filter, options?: any) {\n const parser = new FilterParser(filter, {\n collection: this.collection,\n app: {\n ctx: options?.context,\n },\n });\n return parser.toSequelizeParams();\n }\n\n protected async getTransaction(options: any, autoGen = false) {\n if (lodash.isPlainObject(options) && options.transaction) {\n return options.transaction;\n }\n\n if (autoGen) {\n return await this.model.sequelize.transaction();\n }\n\n return null;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function transactionWrapperBuilder(transactionGenerator: any): (transactionInjector?: any) => (target: any, name: any, descriptor: any) => any;
|
|
@@ -0,0 +1,63 @@
|
|
|
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 lodash from 'lodash';
|
|
11
|
+
export function transactionWrapperBuilder(transactionGenerator) {
|
|
12
|
+
return function transaction(transactionInjector) {
|
|
13
|
+
return (target, name, descriptor) => {
|
|
14
|
+
const oldValue = descriptor.value;
|
|
15
|
+
descriptor.value = function () {
|
|
16
|
+
return __awaiter(this, arguments, void 0, function* () {
|
|
17
|
+
let transaction;
|
|
18
|
+
let newTransaction = false;
|
|
19
|
+
if (arguments.length > 0 && typeof arguments[0] === 'object') {
|
|
20
|
+
transaction = arguments[0]['transaction'];
|
|
21
|
+
}
|
|
22
|
+
if (!transaction) {
|
|
23
|
+
transaction = yield transactionGenerator.apply(this);
|
|
24
|
+
newTransaction = true;
|
|
25
|
+
}
|
|
26
|
+
// 需要将 newTransaction 注入到被装饰函数参数内
|
|
27
|
+
if (newTransaction) {
|
|
28
|
+
try {
|
|
29
|
+
let callArguments;
|
|
30
|
+
if (lodash.isPlainObject(arguments[0])) {
|
|
31
|
+
callArguments = Object.assign(Object.assign({}, arguments[0]), { transaction });
|
|
32
|
+
}
|
|
33
|
+
else if (transactionInjector) {
|
|
34
|
+
callArguments = transactionInjector(arguments, transaction);
|
|
35
|
+
}
|
|
36
|
+
else if (lodash.isNull(arguments[0]) || lodash.isUndefined(arguments[0])) {
|
|
37
|
+
callArguments = {
|
|
38
|
+
transaction,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
throw new Error(`please provide transactionInjector for ${name} call`);
|
|
43
|
+
}
|
|
44
|
+
const results = yield oldValue.apply(this, [callArguments]);
|
|
45
|
+
yield transaction.commit();
|
|
46
|
+
return results;
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
console.error({ err });
|
|
50
|
+
yield transaction.rollback();
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return oldValue.apply(this, arguments);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
return descriptor;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=transaction-decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-decorator.js","sourceRoot":"","sources":["../src/transaction-decorator.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,UAAU,yBAAyB,CAAC,oBAAoB;IAC5D,OAAO,SAAS,WAAW,CAAC,mBAAoB;QAC9C,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE;YAClC,MAA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AE;4BACZ,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC;4BACvB,MAAM,WAAW,CAAC,QAAQ,EAAE,CAAC;4BAC7B,MAAM,GAAG,CAAC;yBACX;qBACF;yBAAM;wBACL,OAAO,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;qBACxC;gBACH,CAAC;aAAA,CAAC;YAEF,OAAO,UAAU,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import lodash from 'lodash';\n\nexport function transactionWrapperBuilder(transactionGenerator) {\n return function transaction(transactionInjector?) {\n return (target, name, descriptor) => {\n const oldValue = descriptor.value;\n\n descriptor.value = async function () {\n let transaction;\n let newTransaction = false;\n\n if (arguments.length > 0 && typeof arguments[0] === 'object') {\n transaction = arguments[0]['transaction'];\n }\n\n if (!transaction) {\n transaction = await transactionGenerator.apply(this);\n newTransaction = true;\n }\n\n // 需要将 newTransaction 注入到被装饰函数参数内\n if (newTransaction) {\n try {\n let callArguments;\n if (lodash.isPlainObject(arguments[0])) {\n callArguments = {\n ...arguments[0],\n transaction,\n };\n } else if (transactionInjector) {\n callArguments = transactionInjector(arguments, transaction);\n } else if (lodash.isNull(arguments[0]) || lodash.isUndefined(arguments[0])) {\n callArguments = {\n transaction,\n };\n } else {\n throw new Error(`please provide transactionInjector for ${name} call`);\n }\n\n const results = await oldValue.apply(this, [callArguments]);\n\n await transaction.commit();\n\n return results;\n } catch (err) {\n console.error({ err });\n await transaction.rollback();\n throw err;\n }\n } else {\n return oldValue.apply(this, arguments);\n }\n };\n\n return descriptor;\n };\n };\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Association, BelongsToMany, Hookable, Transactionable } from 'sequelize';
|
|
2
|
+
import { Model } from './model';
|
|
3
|
+
import { TransactionAble } from './repository';
|
|
4
|
+
export declare function modelAssociations(instance: Model): {
|
|
5
|
+
[key: string]: Association<import("sequelize").Model<any, any>, import("sequelize").Model<any, any>>;
|
|
6
|
+
};
|
|
7
|
+
export declare function belongsToManyAssociations(instance: Model): Array<BelongsToMany>;
|
|
8
|
+
export declare function modelAssociationByKey(instance: Model, key: string): Association;
|
|
9
|
+
declare type UpdateValue = {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
};
|
|
12
|
+
interface UpdateOptions extends TransactionAble {
|
|
13
|
+
filter?: any;
|
|
14
|
+
filterByTk?: number | string;
|
|
15
|
+
whitelist?: string[];
|
|
16
|
+
blacklist?: string[];
|
|
17
|
+
updateAssociationValues?: string[];
|
|
18
|
+
sanitized?: boolean;
|
|
19
|
+
sourceModel?: Model;
|
|
20
|
+
}
|
|
21
|
+
interface UpdateAssociationOptions extends Transactionable, Hookable {
|
|
22
|
+
updateAssociationValues?: string[];
|
|
23
|
+
sourceModel?: Model;
|
|
24
|
+
context?: any;
|
|
25
|
+
associationContext?: any;
|
|
26
|
+
}
|
|
27
|
+
export declare function updateModelByValues(instance: Model, values: UpdateValue, options?: UpdateOptions): Promise<void>;
|
|
28
|
+
export declare function updateThroughTableValue(instance: Model, throughName: string, throughValues: any, source: Model, transaction?: any): Promise<any>;
|
|
29
|
+
/**
|
|
30
|
+
* update association of instance by values
|
|
31
|
+
* @param instance
|
|
32
|
+
* @param values
|
|
33
|
+
* @param options
|
|
34
|
+
*/
|
|
35
|
+
export declare function updateAssociations(instance: Model, values: any, options?: UpdateAssociationOptions): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* update model association by key
|
|
38
|
+
* @param instance
|
|
39
|
+
* @param key
|
|
40
|
+
* @param value
|
|
41
|
+
* @param options
|
|
42
|
+
*/
|
|
43
|
+
export declare function updateAssociation(instance: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* update belongsTo and HasOne
|
|
46
|
+
* @param model
|
|
47
|
+
* @param key
|
|
48
|
+
* @param value
|
|
49
|
+
* @param options
|
|
50
|
+
*/
|
|
51
|
+
export declare function updateSingleAssociation(model: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* update multiple association of model by value
|
|
54
|
+
* @param model
|
|
55
|
+
* @param key
|
|
56
|
+
* @param value
|
|
57
|
+
* @param options
|
|
58
|
+
*/
|
|
59
|
+
export declare function updateMultipleAssociation(model: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
|
|
60
|
+
export {};
|