@nocobase/database 0.5.0-alpha.36 → 0.7.0-alpha.0
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,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
exports.SingleRelationRepository = void 0;
|
|
22
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
23
|
+
const update_associations_1 = require("../update-associations");
|
|
24
|
+
const relation_repository_1 = require("./relation-repository");
|
|
25
|
+
class SingleRelationRepository extends relation_repository_1.RelationRepository {
|
|
26
|
+
remove(options) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const transaction = yield this.getTransaction(options);
|
|
29
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
30
|
+
return yield sourceModel[this.accessors().set](null, {
|
|
31
|
+
transaction,
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
set(options) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
const transaction = yield this.getTransaction(options);
|
|
38
|
+
let handleKey = lodash_1.default.isPlainObject(options) ? options.tk : options;
|
|
39
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
40
|
+
return yield sourceModel[this.accessors().set](handleKey, {
|
|
41
|
+
transaction,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
find(options) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const transaction = yield this.getTransaction(options);
|
|
48
|
+
const findOptions = this.buildQueryOptions(Object.assign({}, options));
|
|
49
|
+
const getAccessor = this.accessors().get;
|
|
50
|
+
const sourceModel = yield this.getSourceModel(transaction);
|
|
51
|
+
return yield sourceModel[getAccessor](Object.assign(Object.assign({}, findOptions), { transaction }));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
findOne(options) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
return this.find(options);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
destroy(options) {
|
|
60
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61
|
+
const transaction = yield this.getTransaction(options);
|
|
62
|
+
const target = yield this.find({
|
|
63
|
+
transaction,
|
|
64
|
+
});
|
|
65
|
+
yield target.destroy({
|
|
66
|
+
transaction,
|
|
67
|
+
});
|
|
68
|
+
return true;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
update(options) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const transaction = yield this.getTransaction(options);
|
|
74
|
+
const target = yield this.find({
|
|
75
|
+
transaction,
|
|
76
|
+
});
|
|
77
|
+
yield (0, update_associations_1.updateModelByValues)(target, options === null || options === void 0 ? void 0 : options.values, Object.assign(Object.assign({}, lodash_1.default.omit(options, 'values')), { transaction }));
|
|
78
|
+
return target;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
accessors() {
|
|
82
|
+
return super.accessors();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
__decorate([
|
|
86
|
+
(0, relation_repository_1.transaction)()
|
|
87
|
+
], SingleRelationRepository.prototype, "remove", null);
|
|
88
|
+
__decorate([
|
|
89
|
+
(0, relation_repository_1.transaction)((args, transaction) => {
|
|
90
|
+
return {
|
|
91
|
+
tk: args[0],
|
|
92
|
+
transaction,
|
|
93
|
+
};
|
|
94
|
+
})
|
|
95
|
+
], SingleRelationRepository.prototype, "set", null);
|
|
96
|
+
__decorate([
|
|
97
|
+
(0, relation_repository_1.transaction)()
|
|
98
|
+
], SingleRelationRepository.prototype, "destroy", null);
|
|
99
|
+
__decorate([
|
|
100
|
+
(0, relation_repository_1.transaction)()
|
|
101
|
+
], SingleRelationRepository.prototype, "update", null);
|
|
102
|
+
exports.SingleRelationRepository = SingleRelationRepository;
|
|
103
|
+
//# sourceMappingURL=single-relation-repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"single-relation-repository.js","sourceRoot":"","sources":["../../src/relation-repository/single-relation-repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,oDAA4B;AAI5B,gEAA6D;AAC7D,+DAAwE;AAaxE,MAAsB,wBAAyB,SAAQ,wCAAkB;IAEjE,MAAM,CAAC,OAAyB;;YACpC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YACvD,MAAM,WAAW,GAAG,M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D,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC;gBAC7B,WAAW;aACZ,CAAC,CAAC;YAEH,MAAM,MAAM,CAAC,OAAO,CAAC;gBACnB,WAAW;aACZ,CAAC,CAAC;YAEH,OAAO,IAAI,CAAC;QACd,CAAC;KAAA;IAGK,MAAM,CAAC,OAAsB;;YACjC,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;YAEvD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC;gBAC7B,WAAW;aACZ,CAAC,CAAC;YAEH,MAAM,IAAA,yCAAmB,EAAC,MAAM,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,kCAC5C,gBAAM,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,KACjC,WAAW,IACX,CAAC;YAEH,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;IAED,SAAS;QACP,OAAmC,KAAK,CAAC,SAAS,EAAE,CAAC;IACvD,CAAC;CACF;AA9EC;IADC,IAAA,iCAAW,GAAE;sDAOb;AAQD;IANC,IAAA,iCAAW,EAAC,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE;QACjC,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,CAAC,CAAC;YACX,WAAW;SACZ,CAAC;IACJ,CAAC,CAAC;mDAUD;AAsBD;IADC,IAAA,iCAAW,GAAE;uDAab;AAGD;IADC,IAAA,iCAAW,GAAE;sDAcb;AA3EH,4DAgFC","sourcesContent":["import lodash from 'lodash';\nimport { SingleAssociationAccessors } from 'sequelize';\nimport { Model } from '../model';\nimport { Appends, Except, Fields, Filter, TargetKey, TransactionAble, UpdateOptions } from '../repository';\nimport { updateModelByValues } from '../update-associations';\nimport { RelationRepository, transaction } from './relation-repository';\n\nexport interface SingleRelationFindOption extends TransactionAble {\n fields?: Fields;\n except?: Except;\n appends?: Appends;\n filter?: Filter;\n}\n\ninterface SetOption extends TransactionAble {\n tk?: TargetKey;\n}\n\nexport abstract class SingleRelationRepository extends RelationRepository {\n @transaction()\n async remove(options?: TransactionAble): Promise<void> {\n const transaction = await this.getTransaction(options);\n const sourceModel = await this.getSourceModel(transaction);\n return await sourceModel[this.accessors().set](null, {\n transaction,\n });\n }\n\n @transaction((args, transaction) => {\n return {\n tk: args[0],\n transaction,\n };\n })\n async set(options: TargetKey | SetOption): Promise<void> {\n const transaction = await this.getTransaction(options);\n let handleKey = lodash.isPlainObject(options) ? (<SetOption>options).tk : options;\n\n const sourceModel = await this.getSourceModel(transaction);\n\n return await sourceModel[this.accessors().set](handleKey, {\n transaction,\n });\n }\n\n async find(options?: SingleRelationFindOption): Promise<Model<any>> {\n const transaction = await this.getTransaction(options);\n const findOptions = this.buildQueryOptions({\n ...options,\n });\n\n const getAccessor = this.accessors().get;\n const sourceModel = await this.getSourceModel(transaction);\n\n return await sourceModel[getAccessor]({\n ...findOptions,\n transaction,\n });\n }\n\n async findOne(options?: SingleRelationFindOption): Promise<Model<any>> {\n return this.find(options);\n }\n\n @transaction()\n async destroy(options?: TransactionAble): Promise<Boolean> {\n const transaction = await this.getTransaction(options);\n\n const target = await this.find({\n transaction,\n });\n\n await target.destroy({\n transaction,\n });\n\n return true;\n }\n\n @transaction()\n async update(options: UpdateOptions): Promise<any> {\n const transaction = await this.getTransaction(options);\n\n const target = await this.find({\n transaction,\n });\n\n await updateModelByValues(target, options?.values, {\n ...lodash.omit(options, 'values'),\n transaction,\n });\n\n return target;\n }\n\n accessors() {\n return <SingleAssociationAccessors>super.accessors();\n }\n}\n"]}
|
|
@@ -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,302 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
15
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
16
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
17
|
+
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;
|
|
18
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
19
|
+
};
|
|
20
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
+
if (mod && mod.__esModule) return mod;
|
|
22
|
+
var result = {};
|
|
23
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
+
__setModuleDefault(result, mod);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
28
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
30
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
31
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
32
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
33
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.Repository = void 0;
|
|
41
|
+
const lodash_1 = __importStar(require("lodash"));
|
|
42
|
+
const sequelize_1 = require("sequelize");
|
|
43
|
+
const filter_parser_1 = __importDefault(require("./filter-parser"));
|
|
44
|
+
const options_parser_1 = require("./options-parser");
|
|
45
|
+
const belongs_to_many_repository_1 = require("./relation-repository/belongs-to-many-repository");
|
|
46
|
+
const belongs_to_repository_1 = require("./relation-repository/belongs-to-repository");
|
|
47
|
+
const hasmany_repository_1 = require("./relation-repository/hasmany-repository");
|
|
48
|
+
const hasone_repository_1 = require("./relation-repository/hasone-repository");
|
|
49
|
+
const transaction_decorator_1 = require("./transaction-decorator");
|
|
50
|
+
const update_associations_1 = require("./update-associations");
|
|
51
|
+
const update_guard_1 = require("./update-guard");
|
|
52
|
+
const debug = require('debug')('noco-database');
|
|
53
|
+
const transaction = (0, transaction_decorator_1.transactionWrapperBuilder)(function () {
|
|
54
|
+
return this.collection.model.sequelize.transaction();
|
|
55
|
+
});
|
|
56
|
+
class RelationRepositoryBuilder {
|
|
57
|
+
constructor(collection, associationName) {
|
|
58
|
+
this.builderMap = {
|
|
59
|
+
HasOne: hasone_repository_1.HasOneRepository,
|
|
60
|
+
BelongsTo: belongs_to_repository_1.BelongsToRepository,
|
|
61
|
+
BelongsToMany: belongs_to_many_repository_1.BelongsToManyRepository,
|
|
62
|
+
HasMany: hasmany_repository_1.HasManyRepository,
|
|
63
|
+
};
|
|
64
|
+
this.collection = collection;
|
|
65
|
+
this.associationName = associationName;
|
|
66
|
+
this.association = this.collection.model.associations[this.associationName];
|
|
67
|
+
}
|
|
68
|
+
builder() {
|
|
69
|
+
return this.builderMap;
|
|
70
|
+
}
|
|
71
|
+
of(id) {
|
|
72
|
+
const klass = this.builder()[this.association.associationType];
|
|
73
|
+
return new klass(this.collection, this.associationName, id);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class Repository {
|
|
77
|
+
constructor(collection) {
|
|
78
|
+
this.database = collection.context.database;
|
|
79
|
+
this.collection = collection;
|
|
80
|
+
this.model = collection.model;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* return count by filter
|
|
84
|
+
*/
|
|
85
|
+
count(countOptions) {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
let options = countOptions ? lodash_1.default.clone(countOptions) : {};
|
|
88
|
+
const transaction = yield this.getTransaction(options);
|
|
89
|
+
if (countOptions === null || countOptions === void 0 ? void 0 : countOptions.filter) {
|
|
90
|
+
options = Object.assign(Object.assign({}, options), this.parseFilter(countOptions.filter, countOptions));
|
|
91
|
+
}
|
|
92
|
+
const count = yield this.collection.model.count(Object.assign(Object.assign({}, options), { distinct: true, transaction }));
|
|
93
|
+
return count;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* find
|
|
98
|
+
* @param options
|
|
99
|
+
*/
|
|
100
|
+
find(options) {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
+
const model = this.collection.model;
|
|
103
|
+
const transaction = yield this.getTransaction(options);
|
|
104
|
+
const opts = Object.assign({ subQuery: false }, this.buildQueryOptions(options));
|
|
105
|
+
if (opts.include && opts.include.length > 0) {
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
const primaryKeyField = model.primaryKeyField || model.primaryKeyAttribute;
|
|
108
|
+
const ids = (yield model.findAll(Object.assign(Object.assign({}, opts), { includeIgnoreAttributes: false, attributes: [primaryKeyField], group: `${model.name}.${primaryKeyField}`, transaction }))).map((row) => row.get(primaryKeyField));
|
|
109
|
+
const where = {
|
|
110
|
+
[primaryKeyField]: {
|
|
111
|
+
[sequelize_1.Op.in]: ids,
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
return yield model.findAll(Object.assign(Object.assign({}, (0, lodash_1.omit)(opts, ['limit', 'offset'])), { where,
|
|
115
|
+
transaction }));
|
|
116
|
+
}
|
|
117
|
+
return yield model.findAll(Object.assign(Object.assign({}, opts), { transaction }));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* find and count
|
|
122
|
+
* @param options
|
|
123
|
+
*/
|
|
124
|
+
findAndCount(options) {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
const transaction = yield this.getTransaction(options);
|
|
127
|
+
options = Object.assign(Object.assign({}, options), { transaction });
|
|
128
|
+
return [yield this.find(options), yield this.count(options)];
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Find By Id
|
|
133
|
+
*
|
|
134
|
+
*/
|
|
135
|
+
findById(id) {
|
|
136
|
+
return this.collection.model.findByPk(id);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Find one record from database
|
|
140
|
+
*
|
|
141
|
+
* @param options
|
|
142
|
+
*/
|
|
143
|
+
findOne(options) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
const transaction = yield this.getTransaction(options);
|
|
146
|
+
const rows = yield this.find(Object.assign(Object.assign({}, options), { limit: 1, transaction }));
|
|
147
|
+
return rows.length == 1 ? rows[0] : null;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Save instance to database
|
|
152
|
+
*
|
|
153
|
+
* @param values
|
|
154
|
+
* @param options
|
|
155
|
+
*/
|
|
156
|
+
create(options) {
|
|
157
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
158
|
+
const transaction = yield this.getTransaction(options);
|
|
159
|
+
const guard = update_guard_1.UpdateGuard.fromOptions(this.model, Object.assign(Object.assign({}, options), { action: 'create' }));
|
|
160
|
+
const values = guard.sanitize(options.values || {});
|
|
161
|
+
const instance = yield this.model.create(values, Object.assign(Object.assign({}, options), { transaction }));
|
|
162
|
+
if (!instance) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
yield (0, update_associations_1.updateAssociations)(instance, values, Object.assign(Object.assign({}, options), { transaction }));
|
|
166
|
+
if (options.hooks !== false) {
|
|
167
|
+
yield this.database.emitAsync(`${this.collection.name}.afterCreateWithAssociations`, instance, options);
|
|
168
|
+
yield this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);
|
|
169
|
+
}
|
|
170
|
+
return instance;
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Save Many instances to database
|
|
175
|
+
*
|
|
176
|
+
* @param records
|
|
177
|
+
* @param options
|
|
178
|
+
*/
|
|
179
|
+
createMany(options) {
|
|
180
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
181
|
+
const transaction = yield this.getTransaction(options);
|
|
182
|
+
const { records } = options;
|
|
183
|
+
const instances = [];
|
|
184
|
+
for (const values of records) {
|
|
185
|
+
const instance = yield this.create({ values, transaction });
|
|
186
|
+
instances.push(instance);
|
|
187
|
+
}
|
|
188
|
+
return instances;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Update model value
|
|
193
|
+
*
|
|
194
|
+
* @param values
|
|
195
|
+
* @param options
|
|
196
|
+
*/
|
|
197
|
+
update(options) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
const transaction = yield this.getTransaction(options);
|
|
200
|
+
const guard = update_guard_1.UpdateGuard.fromOptions(this.model, options);
|
|
201
|
+
const values = guard.sanitize(options.values);
|
|
202
|
+
const queryOptions = this.buildQueryOptions(options);
|
|
203
|
+
const instances = yield this.find(Object.assign(Object.assign({}, queryOptions), { transaction }));
|
|
204
|
+
for (const instance of instances) {
|
|
205
|
+
yield (0, update_associations_1.updateModelByValues)(instance, values, Object.assign(Object.assign({}, options), { sanitized: true, transaction }));
|
|
206
|
+
}
|
|
207
|
+
if (options.hooks !== false) {
|
|
208
|
+
for (const instance of instances) {
|
|
209
|
+
yield this.database.emitAsync(`${this.collection.name}.afterUpdateWithAssociations`, instance, options);
|
|
210
|
+
yield this.database.emitAsync(`${this.collection.name}.afterSaveWithAssociations`, instance, options);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return instances;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
destroy(options) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
218
|
+
const transaction = yield this.getTransaction(options);
|
|
219
|
+
const modelFilterKey = this.collection.filterTargetKey;
|
|
220
|
+
options = options;
|
|
221
|
+
if (options['individualHooks'] === undefined) {
|
|
222
|
+
options['individualHooks'] = true;
|
|
223
|
+
}
|
|
224
|
+
const filterByTk = options.filterByTk && !lodash_1.default.isArray(options.filterByTk)
|
|
225
|
+
? [options.filterByTk]
|
|
226
|
+
: options.filterByTk;
|
|
227
|
+
if (filterByTk && !options.filter) {
|
|
228
|
+
return yield this.model.destroy(Object.assign(Object.assign({}, options), { where: {
|
|
229
|
+
[modelFilterKey]: {
|
|
230
|
+
[sequelize_1.Op.in]: filterByTk,
|
|
231
|
+
},
|
|
232
|
+
}, transaction }));
|
|
233
|
+
}
|
|
234
|
+
if (options.filter) {
|
|
235
|
+
let pks = (yield this.find({
|
|
236
|
+
filter: options.filter,
|
|
237
|
+
transaction,
|
|
238
|
+
})).map((instance) => instance.get(modelFilterKey));
|
|
239
|
+
if (filterByTk) {
|
|
240
|
+
pks = lodash_1.default.intersection(pks.map((i) => `${i}`), filterByTk.map((i) => `${i}`));
|
|
241
|
+
}
|
|
242
|
+
return yield this.destroy(Object.assign(Object.assign({}, lodash_1.default.omit(options, 'filter')), { filterByTk: pks, transaction }));
|
|
243
|
+
}
|
|
244
|
+
if (options.truncate) {
|
|
245
|
+
return yield this.model.destroy(Object.assign(Object.assign({}, options), { truncate: true, transaction }));
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* @param association target association
|
|
251
|
+
*/
|
|
252
|
+
relation(association) {
|
|
253
|
+
return new RelationRepositoryBuilder(this.collection, association);
|
|
254
|
+
}
|
|
255
|
+
buildQueryOptions(options) {
|
|
256
|
+
const parser = new options_parser_1.OptionsParser(options, {
|
|
257
|
+
collection: this.collection,
|
|
258
|
+
});
|
|
259
|
+
const params = parser.toSequelizeParams();
|
|
260
|
+
debug('sequelize query params %o', params);
|
|
261
|
+
return Object.assign(Object.assign({ where: {} }, options), params);
|
|
262
|
+
}
|
|
263
|
+
parseFilter(filter, options) {
|
|
264
|
+
const parser = new filter_parser_1.default(filter, {
|
|
265
|
+
collection: this.collection,
|
|
266
|
+
app: {
|
|
267
|
+
ctx: options === null || options === void 0 ? void 0 : options.context,
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
return parser.toSequelizeParams();
|
|
271
|
+
}
|
|
272
|
+
getTransaction(options, autoGen = false) {
|
|
273
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
274
|
+
if (lodash_1.default.isPlainObject(options) && options.transaction) {
|
|
275
|
+
return options.transaction;
|
|
276
|
+
}
|
|
277
|
+
if (autoGen) {
|
|
278
|
+
return yield this.model.sequelize.transaction();
|
|
279
|
+
}
|
|
280
|
+
return null;
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
__decorate([
|
|
285
|
+
transaction()
|
|
286
|
+
], Repository.prototype, "create", null);
|
|
287
|
+
__decorate([
|
|
288
|
+
transaction()
|
|
289
|
+
], Repository.prototype, "createMany", null);
|
|
290
|
+
__decorate([
|
|
291
|
+
transaction()
|
|
292
|
+
], Repository.prototype, "update", null);
|
|
293
|
+
__decorate([
|
|
294
|
+
transaction((args, transaction) => {
|
|
295
|
+
return {
|
|
296
|
+
filterByTk: args[0],
|
|
297
|
+
transaction,
|
|
298
|
+
};
|
|
299
|
+
})
|
|
300
|
+
], Repository.prototype, "destroy", null);
|
|
301
|
+
exports.Repository = Repository;
|
|
302
|
+
//# sourceMappingURL=repository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository.js","sourceRoot":"","sources":["../src/repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iDAAsC;AACtC,yCAWmB;AAInB,oEAA2C;AAE3C,qDAAiD;AACjD,iGAA2F;AAC3F,uFAAkF;AAClF,iFAA6E;AAC7E,+EAA2E;AAE3E,mEAAoE;AACpE,+DAAgF;AAChF,iDAA6C;AAE7C,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,eAAe,CAAC,CAAC;AA0GhD,MAAM,WAAW,GAAG,IAAA,iDAAyB,EAAC;IAC5C,OAAoB,IAAK,CAAC,UAAU,CAAC,KAAK,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;AACrE,CAAC,CAAC,CAAC;AAEH,MAAM,yBAAyB;IAY7B,YAAY,UAAsB,EAAE,eAAuB;QAP3D,eAAU,GAAG;YACX,MAAM,EAAE,oCAAgB;YACxB,SAAS,EAAE,2CAAmB;YAC9B,aAAa,EAAE,oDAAuB;YACtC,OAAO,EAAE,sCAAiB;SAC3B,CAAC;QAGA,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,WAAW,GAAG,IA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,EAAE,MAAM,CAAC,CAAC;QAC3C,qCAAS,KAAK,EAAE,EAAE,IAAK,OAAO,GAAK,MAAM,EAAG;IAC9C,CAAC;IAES,WAAW,CAAC,MAAc,EAAE,OAAa;QACjD,MAAM,MAAM,GAAG,IAAI,uBAAY,CAAC,MAAM,EAAE;YACtC,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,GAAG,EAAE;gBACH,GAAG,EAAE,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO;aACtB;SACF,CAAC,CAAC;QACH,OAAO,MAAM,CAAC,iBAAiB,EAAE,CAAC;IACpC,CAAC;IAEe,cAAc,CAAC,OAAY,EAAE,OAAO,GAAG,KAAK;;YAC1D,IAAI,gBAAM,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;AA/QH,gCAuTC","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;
|