@nocobase/database 0.5.0-alpha.38 → 0.7.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/esm/collection-importer.d.ts +7 -0
- package/esm/collection-importer.js +49 -0
- package/esm/collection-importer.js.map +1 -0
- package/esm/collection.d.ts +73 -0
- package/esm/collection.js +224 -0
- package/esm/collection.js.map +1 -0
- package/esm/database.d.ts +101 -0
- package/esm/database.js +275 -0
- package/esm/database.js.map +1 -0
- package/esm/fields/array-field.d.ts +11 -0
- package/esm/fields/array-field.js +26 -0
- package/esm/fields/array-field.js.map +1 -0
- package/esm/fields/belongs-to-field.d.ts +12 -0
- package/esm/fields/belongs-to-field.js +57 -0
- package/esm/fields/belongs-to-field.js.map +1 -0
- package/esm/fields/belongs-to-many-field.d.ts +11 -0
- package/esm/fields/belongs-to-many-field.js +55 -0
- package/esm/fields/belongs-to-many-field.js.map +1 -0
- package/esm/fields/boolean-field.d.ts +8 -0
- package/esm/fields/boolean-field.js +8 -0
- package/esm/fields/boolean-field.js.map +1 -0
- package/esm/fields/context-field.d.ts +13 -0
- package/esm/fields/context-field.js +43 -0
- package/esm/fields/context-field.js.map +1 -0
- package/esm/fields/date-field.d.ts +8 -0
- package/esm/fields/date-field.js +8 -0
- package/esm/fields/date-field.js.map +1 -0
- package/esm/fields/field.d.ts +37 -0
- package/esm/fields/field.js +74 -0
- package/esm/fields/field.js.map +1 -0
- package/esm/fields/has-inverse-field.d.ts +4 -0
- package/esm/fields/has-inverse-field.js +2 -0
- package/esm/fields/has-inverse-field.js.map +1 -0
- package/esm/fields/has-many-field.d.ts +64 -0
- package/esm/fields/has-many-field.js +58 -0
- package/esm/fields/has-many-field.js.map +1 -0
- package/esm/fields/has-one-field.d.ts +64 -0
- package/esm/fields/has-one-field.js +57 -0
- package/esm/fields/has-one-field.js.map +1 -0
- package/esm/fields/index.d.ts +40 -0
- package/esm/fields/index.js +21 -0
- package/esm/fields/index.js.map +1 -0
- package/esm/fields/json-field.d.ts +14 -0
- package/esm/fields/json-field.js +17 -0
- package/esm/fields/json-field.js.map +1 -0
- package/esm/fields/number-field.d.ts +32 -0
- package/esm/fields/number-field.js +28 -0
- package/esm/fields/number-field.js.map +1 -0
- package/esm/fields/password-field.d.ts +21 -0
- package/esm/fields/password-field.js +71 -0
- package/esm/fields/password-field.js.map +1 -0
- package/esm/fields/radio-field.d.ts +14 -0
- package/esm/fields/radio-field.js +49 -0
- package/esm/fields/radio-field.js.map +1 -0
- package/esm/fields/relation-field.d.ts +20 -0
- package/esm/fields/relation-field.js +27 -0
- package/esm/fields/relation-field.js.map +1 -0
- package/esm/fields/sort-field.d.ts +16 -0
- package/esm/fields/sort-field.js +90 -0
- package/esm/fields/sort-field.js.map +1 -0
- package/esm/fields/string-field.d.ts +8 -0
- package/esm/fields/string-field.js +8 -0
- package/esm/fields/string-field.js.map +1 -0
- package/esm/fields/text-field.d.ts +8 -0
- package/esm/fields/text-field.js +8 -0
- package/esm/fields/text-field.js.map +1 -0
- package/esm/fields/time-field.d.ts +8 -0
- package/esm/fields/time-field.js +8 -0
- package/esm/fields/time-field.js.map +1 -0
- package/esm/fields/uid-field.d.ts +10 -0
- package/esm/fields/uid-field.js +27 -0
- package/esm/fields/uid-field.js.map +1 -0
- package/esm/fields/virtual-field.d.ts +8 -0
- package/esm/fields/virtual-field.js +8 -0
- package/esm/fields/virtual-field.js.map +1 -0
- package/esm/filter-parser.d.ts +27 -0
- package/esm/filter-parser.js +185 -0
- package/esm/filter-parser.js.map +1 -0
- package/esm/index.d.ts +15 -0
- package/esm/index.js +16 -0
- package/esm/index.js.map +1 -0
- package/esm/magic-attribute-model.d.ts +7 -0
- package/esm/magic-attribute-model.js +70 -0
- package/esm/magic-attribute-model.js.map +1 -0
- package/esm/mock-database.d.ts +22 -0
- package/esm/mock-database.js +34 -0
- package/esm/mock-database.js.map +1 -0
- package/esm/model-hook.d.ts +12 -0
- package/esm/model-hook.js +61 -0
- package/esm/model-hook.js.map +1 -0
- package/esm/model.d.ts +15 -0
- package/esm/model.js +80 -0
- package/esm/model.js.map +1 -0
- package/esm/operators/array.d.ts +26 -0
- package/esm/operators/array.js +105 -0
- package/esm/operators/array.js.map +1 -0
- package/esm/operators/association.d.ts +10 -0
- package/esm/operators/association.js +14 -0
- package/esm/operators/association.js.map +1 -0
- package/esm/operators/date.d.ts +34 -0
- package/esm/operators/date.js +35 -0
- package/esm/operators/date.js.map +1 -0
- package/esm/operators/empty.d.ts +28 -0
- package/esm/operators/empty.js +58 -0
- package/esm/operators/empty.js.map +1 -0
- package/esm/operators/index.d.ts +2 -0
- package/esm/operators/index.js +2 -0
- package/esm/operators/index.js.map +1 -0
- package/esm/operators/ne.d.ts +10 -0
- package/esm/operators/ne.js +12 -0
- package/esm/operators/ne.js.map +1 -0
- package/esm/operators/string.d.ts +21 -0
- package/esm/operators/string.js +35 -0
- package/esm/operators/string.js.map +1 -0
- package/esm/operators/utils.d.ts +4 -0
- package/esm/operators/utils.js +11 -0
- package/esm/operators/utils.js.map +1 -0
- package/esm/options-parser.d.ts +31 -0
- package/esm/options-parser.js +225 -0
- package/esm/options-parser.js.map +1 -0
- package/esm/playground.d.ts +1 -0
- package/esm/playground.js +53 -0
- package/esm/playground.js.map +1 -0
- package/esm/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/esm/relation-repository/belongs-to-many-repository.js +199 -0
- package/esm/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/esm/relation-repository/belongs-to-repository.d.ts +17 -0
- package/esm/relation-repository/belongs-to-repository.js +4 -0
- package/esm/relation-repository/belongs-to-repository.js.map +1 -0
- package/esm/relation-repository/hasmany-repository.d.ts +23 -0
- package/esm/relation-repository/hasmany-repository.js +125 -0
- package/esm/relation-repository/hasmany-repository.js.map +1 -0
- package/esm/relation-repository/hasone-repository.d.ts +17 -0
- package/esm/relation-repository/hasone-repository.js +4 -0
- package/esm/relation-repository/hasone-repository.js.map +1 -0
- package/esm/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/esm/relation-repository/multiple-relation-repository.js +149 -0
- package/esm/relation-repository/multiple-relation-repository.js.map +1 -0
- package/esm/relation-repository/relation-repository.d.ts +32 -0
- package/esm/relation-repository/relation-repository.js +93 -0
- package/esm/relation-repository/relation-repository.js.map +1 -0
- package/esm/relation-repository/single-relation-repository.d.ts +23 -0
- package/esm/relation-repository/single-relation-repository.js +96 -0
- package/esm/relation-repository/single-relation-repository.js.map +1 -0
- package/esm/relation-repository/types.d.ts +7 -0
- package/esm/relation-repository/types.js +2 -0
- package/esm/relation-repository/types.js.map +1 -0
- package/esm/repository.d.ts +165 -0
- package/esm/repository.js +276 -0
- package/esm/repository.js.map +1 -0
- package/esm/transaction-decorator.d.ts +1 -0
- package/esm/transaction-decorator.js +63 -0
- package/esm/transaction-decorator.js.map +1 -0
- package/esm/update-associations.d.ts +60 -0
- package/esm/update-associations.js +362 -0
- package/esm/update-associations.js.map +1 -0
- package/esm/update-guard.d.ts +26 -0
- package/esm/update-guard.js +122 -0
- package/esm/update-guard.js.map +1 -0
- package/lib/collection-importer.d.ts +7 -0
- package/lib/collection-importer.js +75 -0
- package/lib/collection-importer.js.map +1 -0
- package/lib/collection.d.ts +73 -0
- package/lib/collection.js +231 -0
- package/lib/collection.js.map +1 -0
- package/lib/database.d.ts +93 -43
- package/lib/database.js +277 -471
- package/lib/database.js.map +1 -1
- package/lib/fields/array-field.d.ts +11 -0
- package/lib/fields/array-field.js +30 -0
- package/lib/fields/array-field.js.map +1 -0
- package/lib/fields/belongs-to-field.d.ts +12 -0
- package/lib/fields/belongs-to-field.js +61 -0
- package/lib/fields/belongs-to-field.js.map +1 -0
- package/lib/fields/belongs-to-many-field.d.ts +11 -0
- package/lib/fields/belongs-to-many-field.js +59 -0
- package/lib/fields/belongs-to-many-field.js.map +1 -0
- package/lib/fields/boolean-field.d.ts +8 -0
- package/lib/fields/boolean-field.js +12 -0
- package/lib/fields/boolean-field.js.map +1 -0
- package/lib/fields/context-field.d.ts +13 -0
- package/lib/fields/context-field.js +50 -0
- package/lib/fields/context-field.js.map +1 -0
- package/lib/fields/date-field.d.ts +8 -0
- package/lib/fields/date-field.js +12 -0
- package/lib/fields/date-field.js.map +1 -0
- package/lib/fields/field.d.ts +37 -0
- package/lib/fields/field.js +81 -0
- package/lib/fields/field.js.map +1 -0
- package/lib/fields/has-inverse-field.d.ts +4 -0
- package/lib/fields/has-inverse-field.js +3 -0
- package/lib/fields/has-inverse-field.js.map +1 -0
- package/lib/fields/has-many-field.d.ts +64 -0
- package/lib/fields/has-many-field.js +62 -0
- package/lib/fields/has-many-field.js.map +1 -0
- package/lib/fields/has-one-field.d.ts +64 -0
- package/lib/fields/has-one-field.js +61 -0
- package/lib/fields/has-one-field.js.map +1 -0
- package/lib/fields/index.d.ts +40 -10
- package/lib/fields/index.js +31 -138
- package/lib/fields/index.js.map +1 -1
- package/lib/fields/json-field.d.ts +14 -0
- package/lib/fields/json-field.js +22 -0
- package/lib/fields/json-field.js.map +1 -0
- package/lib/fields/number-field.d.ts +32 -0
- package/lib/fields/number-field.js +36 -0
- package/lib/fields/number-field.js.map +1 -0
- package/lib/fields/password-field.d.ts +21 -0
- package/lib/fields/password-field.js +78 -0
- package/lib/fields/password-field.js.map +1 -0
- package/lib/fields/radio-field.d.ts +14 -0
- package/lib/fields/radio-field.js +53 -0
- package/lib/fields/radio-field.js.map +1 -0
- package/lib/fields/relation-field.d.ts +20 -0
- package/lib/fields/relation-field.js +31 -0
- package/lib/fields/relation-field.js.map +1 -0
- package/lib/fields/sort-field.d.ts +16 -0
- package/lib/fields/sort-field.js +94 -0
- package/lib/fields/sort-field.js.map +1 -0
- package/lib/fields/string-field.d.ts +8 -0
- package/lib/fields/string-field.js +12 -0
- package/lib/fields/string-field.js.map +1 -0
- package/lib/fields/text-field.d.ts +8 -0
- package/lib/fields/text-field.js +12 -0
- package/lib/fields/text-field.js.map +1 -0
- package/lib/fields/time-field.d.ts +8 -0
- package/lib/fields/time-field.js +12 -0
- package/lib/fields/time-field.js.map +1 -0
- package/lib/fields/uid-field.d.ts +10 -0
- package/lib/fields/uid-field.js +31 -0
- package/lib/fields/uid-field.js.map +1 -0
- package/lib/fields/virtual-field.d.ts +8 -0
- package/lib/fields/virtual-field.js +12 -0
- package/lib/fields/virtual-field.js.map +1 -0
- package/lib/filter-parser.d.ts +27 -0
- package/lib/filter-parser.js +191 -0
- package/lib/filter-parser.js.map +1 -0
- package/lib/index.d.ts +13 -6
- package/lib/index.js +27 -60
- package/lib/index.js.map +1 -1
- package/lib/magic-attribute-model.d.ts +7 -0
- package/lib/magic-attribute-model.js +77 -0
- package/lib/magic-attribute-model.js.map +1 -0
- package/lib/mock-database.d.ts +22 -0
- package/lib/mock-database.js +40 -0
- package/lib/mock-database.js.map +1 -0
- package/lib/model-hook.d.ts +12 -0
- package/lib/model-hook.js +68 -0
- package/lib/model-hook.js.map +1 -0
- package/lib/model.d.ts +11 -45
- package/lib/model.js +76 -452
- package/lib/model.js.map +1 -1
- package/lib/operators/array.d.ts +26 -0
- package/lib/operators/array.js +107 -0
- package/lib/operators/array.js.map +1 -0
- package/lib/operators/association.d.ts +10 -0
- package/lib/operators/association.js +16 -0
- package/lib/operators/association.js.map +1 -0
- package/lib/operators/date.d.ts +34 -0
- package/lib/operators/date.js +40 -0
- package/lib/operators/date.js.map +1 -0
- package/lib/operators/empty.d.ts +28 -0
- package/lib/operators/empty.js +82 -0
- package/lib/operators/empty.js.map +1 -0
- package/lib/operators/index.d.ts +2 -0
- package/lib/operators/index.js +4 -0
- package/lib/operators/index.js.map +1 -0
- package/lib/operators/ne.d.ts +10 -0
- package/lib/operators/ne.js +14 -0
- package/lib/operators/ne.js.map +1 -0
- package/lib/operators/string.d.ts +21 -0
- package/lib/operators/string.js +37 -0
- package/lib/operators/string.js.map +1 -0
- package/lib/operators/utils.d.ts +4 -0
- package/lib/operators/utils.js +16 -0
- package/lib/operators/utils.js.map +1 -0
- package/lib/options-parser.d.ts +31 -0
- package/lib/options-parser.js +232 -0
- package/lib/options-parser.js.map +1 -0
- package/lib/playground.d.ts +1 -0
- package/lib/playground.js +55 -0
- package/lib/playground.js.map +1 -0
- package/lib/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/lib/relation-repository/belongs-to-many-repository.js +206 -0
- package/lib/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/lib/relation-repository/belongs-to-repository.d.ts +17 -0
- package/lib/relation-repository/belongs-to-repository.js +8 -0
- package/lib/relation-repository/belongs-to-repository.js.map +1 -0
- package/lib/relation-repository/hasmany-repository.d.ts +23 -0
- package/lib/relation-repository/hasmany-repository.js +129 -0
- package/lib/relation-repository/hasmany-repository.js.map +1 -0
- package/lib/relation-repository/hasone-repository.d.ts +17 -0
- package/lib/relation-repository/hasone-repository.js +8 -0
- package/lib/relation-repository/hasone-repository.js.map +1 -0
- package/lib/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/lib/relation-repository/multiple-relation-repository.js +153 -0
- package/lib/relation-repository/multiple-relation-repository.js.map +1 -0
- package/lib/relation-repository/relation-repository.d.ts +32 -0
- package/lib/relation-repository/relation-repository.js +100 -0
- package/lib/relation-repository/relation-repository.js.map +1 -0
- package/lib/relation-repository/single-relation-repository.d.ts +23 -0
- package/lib/relation-repository/single-relation-repository.js +103 -0
- package/lib/relation-repository/single-relation-repository.js.map +1 -0
- package/lib/relation-repository/types.d.ts +7 -0
- package/lib/relation-repository/types.js +3 -0
- package/lib/relation-repository/types.js.map +1 -0
- package/lib/repository.d.ts +165 -0
- package/lib/repository.js +302 -0
- package/lib/repository.js.map +1 -0
- package/lib/transaction-decorator.d.ts +1 -0
- package/lib/transaction-decorator.js +70 -0
- package/lib/transaction-decorator.js.map +1 -0
- package/lib/update-associations.d.ts +60 -0
- package/lib/update-associations.js +374 -0
- package/lib/update-associations.js.map +1 -0
- package/lib/update-guard.d.ts +26 -0
- package/lib/update-guard.js +129 -0
- package/lib/update-guard.js.map +1 -0
- package/package.json +17 -5
- package/src/__tests__/collection-importer.test.ts +21 -0
- package/src/__tests__/collection.sortable.test.ts +65 -0
- package/src/__tests__/collection.test.ts +218 -0
- package/src/__tests__/database.import.test.ts +33 -0
- package/src/__tests__/database.test.ts +229 -0
- package/src/__tests__/field-options/hidden.test.ts +302 -0
- package/src/__tests__/field-options/sort-by.test.ts +220 -0
- package/src/__tests__/fields/belongs-to-field.test.ts +162 -0
- package/src/__tests__/fields/belongs-to-many-field.test.ts +61 -0
- package/src/__tests__/fields/context-field.test.ts +140 -0
- package/src/__tests__/fields/has-many-field.test.ts +152 -0
- package/src/__tests__/fields/has-one-field.test.ts +67 -0
- package/src/__tests__/fields/password-field.test.ts +30 -0
- package/src/__tests__/fields/sort-field.test.ts +133 -0
- package/src/__tests__/fields/string-field.test.ts +77 -0
- package/src/__tests__/filter-parser.test.ts +104 -0
- package/src/__tests__/fixtures/c0/a.ts +6 -0
- package/src/__tests__/fixtures/c1/b.ts +6 -0
- package/src/__tests__/fixtures/c2/a.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/extend.ts +6 -0
- package/src/__tests__/fixtures/collections/extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/posts.ts +4 -0
- package/src/__tests__/fixtures/collections/tags.js +4 -0
- package/src/__tests__/fixtures/collections/test.jpg +0 -0
- package/src/__tests__/fixtures/collections/user.json +9 -0
- package/src/__tests__/index.ts +1 -0
- package/src/__tests__/magic-attribute-model.test.ts +103 -0
- package/src/__tests__/model.test.ts +60 -0
- package/src/__tests__/operator/array-operator.test.ts +268 -0
- package/src/__tests__/operator/association-operator.test.ts +263 -0
- package/src/__tests__/operator/date-operator.test.ts +165 -0
- package/src/__tests__/operator/empty-operator.test.ts +77 -0
- package/src/__tests__/operator/ne.test.ts +33 -0
- package/src/__tests__/operator/string-operator.test.ts +62 -0
- package/src/__tests__/option-parser.test.ts +185 -0
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +697 -0
- package/src/__tests__/relation-repository/has-many-repository.test.ts +414 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +77 -0
- package/src/__tests__/repository/count.test.ts +180 -0
- package/src/__tests__/repository/create.test.ts +163 -0
- package/src/__tests__/repository/destroy.test.ts +196 -0
- package/src/__tests__/repository/find.test.ts +247 -0
- package/src/__tests__/repository/update.test.ts +60 -0
- package/src/__tests__/repository.test.ts +438 -0
- package/src/__tests__/update-associations.test.ts +412 -0
- package/src/__tests__/update-guard.test.ts +376 -0
- package/src/collection-importer.ts +49 -0
- package/src/collection.ts +282 -0
- package/src/database.ts +340 -0
- package/src/fields/array-field.ts +35 -0
- package/src/fields/belongs-to-field.ts +76 -0
- package/src/fields/belongs-to-many-field.ts +77 -0
- package/src/fields/boolean-field.ts +12 -0
- package/src/fields/context-field.ts +45 -0
- package/src/fields/date-field.ts +12 -0
- package/src/fields/field.ts +105 -0
- package/src/fields/has-inverse-field.ts +5 -0
- package/src/fields/has-many-field.ts +143 -0
- package/src/fields/has-one-field.ts +136 -0
- package/src/fields/index.ts +72 -0
- package/src/fields/json-field.ts +25 -0
- package/src/fields/number-field.ts +52 -0
- package/src/fields/password-field.ts +72 -0
- package/src/fields/radio-field.ts +50 -0
- package/src/fields/relation-field.ts +37 -0
- package/src/fields/sort-field.ts +96 -0
- package/src/fields/string-field.ts +12 -0
- package/src/fields/text-field.ts +12 -0
- package/src/fields/time-field.ts +12 -0
- package/src/fields/uid-field.ts +24 -0
- package/src/fields/virtual-field.ts +12 -0
- package/src/filter-parser.ts +243 -0
- package/src/index.ts +16 -0
- package/src/magic-attribute-model.ts +62 -0
- package/src/mock-database.ts +42 -0
- package/src/model-hook.ts +69 -0
- package/src/model.ts +114 -0
- package/src/operators/array.ts +145 -0
- package/src/operators/association.ts +14 -0
- package/src/operators/date.ts +41 -0
- package/src/operators/empty.ts +75 -0
- package/src/operators/index.ts +8 -0
- package/src/operators/ne.ts +12 -0
- package/src/operators/string.ts +40 -0
- package/src/operators/utils.ts +13 -0
- package/src/options-parser.ts +285 -0
- package/src/playground.ts +52 -0
- package/src/relation-repository/belongs-to-many-repository.ts +240 -0
- package/src/relation-repository/belongs-to-repository.ts +23 -0
- package/src/relation-repository/hasmany-repository.ts +145 -0
- package/src/relation-repository/hasone-repository.ts +23 -0
- package/src/relation-repository/multiple-relation-repository.ts +198 -0
- package/src/relation-repository/relation-repository.ts +114 -0
- package/src/relation-repository/single-relation-repository.ts +99 -0
- package/src/relation-repository/types.ts +15 -0
- package/src/repository.ts +478 -0
- package/src/transaction-decorator.ts +58 -0
- package/src/update-associations.ts +478 -0
- package/src/update-guard.ts +167 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +5 -0
- package/examples/index.ts +0 -125
- package/examples/plugins/db-driven/index.ts +0 -25
- package/examples/plugins/db-driven/tables/fields.ts +0 -78
- package/examples/plugins/db-driven/tables/tables.ts +0 -53
- package/examples/tables/bar.js +0 -26
- package/examples/tables/comments.ts +0 -19
- package/examples/tables/foo.json +0 -3
- package/examples/tables/posts.ts +0 -28
- package/examples/tables/profiles.ts +0 -23
- package/examples/tables/tags.ts +0 -15
- package/examples/tables/users.ts +0 -34
- package/lib/database.d.ts.map +0 -1
- package/lib/fields/field-types.d.ts +0 -419
- package/lib/fields/field-types.d.ts.map +0 -1
- package/lib/fields/field-types.js +0 -1222
- package/lib/fields/field-types.js.map +0 -1
- package/lib/fields/index.d.ts.map +0 -1
- package/lib/fields/option-types.d.ts +0 -105
- package/lib/fields/option-types.d.ts.map +0 -1
- package/lib/fields/option-types.js +0 -18
- package/lib/fields/option-types.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/model.d.ts.map +0 -1
- package/lib/op.d.ts +0 -45
- package/lib/op.d.ts.map +0 -1
- package/lib/op.js +0 -225
- package/lib/op.js.map +0 -1
- package/lib/table.d.ts +0 -56
- package/lib/table.d.ts.map +0 -1
- package/lib/table.js +0 -456
- package/lib/table.js.map +0 -1
- package/lib/utils.d.ts +0 -26
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -438
- package/lib/utils.js.map +0 -1
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
import { Collection } from '@nocobase/database';
|
|
2
|
+
import Database from '../../database';
|
|
3
|
+
import { BelongsToManyRepository } from '../../relation-repository/belongs-to-many-repository';
|
|
4
|
+
import { mockDatabase } from '../index';
|
|
5
|
+
|
|
6
|
+
describe('belongs to many with target key', function () {
|
|
7
|
+
let db: Database;
|
|
8
|
+
let Tag: Collection;
|
|
9
|
+
let Post: Collection;
|
|
10
|
+
let Color: Collection;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
db = mockDatabase();
|
|
14
|
+
|
|
15
|
+
Post = db.collection({
|
|
16
|
+
name: 'posts',
|
|
17
|
+
filterTargetKey: 'title',
|
|
18
|
+
autoGenId: false,
|
|
19
|
+
fields: [
|
|
20
|
+
{ type: 'string', name: 'title', primaryKey: true },
|
|
21
|
+
{
|
|
22
|
+
type: 'belongsToMany',
|
|
23
|
+
name: 'tags',
|
|
24
|
+
sourceKey: 'title',
|
|
25
|
+
foreignKey: 'postTitle',
|
|
26
|
+
targetKey: 'name',
|
|
27
|
+
otherKey: 'tagName',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
Tag = db.collection({
|
|
33
|
+
name: 'tags',
|
|
34
|
+
filterTargetKey: 'name',
|
|
35
|
+
autoGenId: false,
|
|
36
|
+
fields: [
|
|
37
|
+
{ type: 'string', name: 'name', primaryKey: true },
|
|
38
|
+
{ type: 'string', name: 'status' },
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await db.sync({ force: true });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterEach(async () => {
|
|
46
|
+
await db.close();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('destroy by target key', async () => {
|
|
50
|
+
const t1 = await Tag.repository.create({
|
|
51
|
+
values: {
|
|
52
|
+
name: 't1',
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const t2 = await Tag.repository.create({
|
|
57
|
+
values: {
|
|
58
|
+
name: 't2',
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const p1 = await Post.repository.create({
|
|
63
|
+
values: { title: 'p1' },
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.get('title') as string);
|
|
67
|
+
|
|
68
|
+
await PostTagRepository.set([t1.get('name') as string, t2.get('name')]);
|
|
69
|
+
|
|
70
|
+
await PostTagRepository.destroy();
|
|
71
|
+
|
|
72
|
+
const [_, count] = await PostTagRepository.findAndCount();
|
|
73
|
+
expect(count).toEqual(0);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('destroy with target key and filter', async () => {
|
|
77
|
+
let t1 = await Tag.repository.create({
|
|
78
|
+
values: {
|
|
79
|
+
name: 't1',
|
|
80
|
+
status: 'published',
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const t2 = await Tag.repository.create({
|
|
85
|
+
values: {
|
|
86
|
+
name: 't2',
|
|
87
|
+
status: 'draft',
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const p1 = await Post.repository.create({
|
|
92
|
+
values: { title: 'p1' },
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.get('title') as string);
|
|
96
|
+
|
|
97
|
+
await PostTagRepository.set([t1.get('name') as string, t2.get('name') as string]);
|
|
98
|
+
|
|
99
|
+
let [_, count] = await PostTagRepository.findAndCount();
|
|
100
|
+
expect(count).toEqual(2);
|
|
101
|
+
|
|
102
|
+
await PostTagRepository.destroy({
|
|
103
|
+
filterByTk: t1.get('name') as string,
|
|
104
|
+
filter: {
|
|
105
|
+
status: 'draft',
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
[_, count] = await PostTagRepository.findAndCount();
|
|
110
|
+
expect(count).toEqual(2);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe('belongs to many', () => {
|
|
115
|
+
let db: Database;
|
|
116
|
+
let User;
|
|
117
|
+
let Post;
|
|
118
|
+
let Tag;
|
|
119
|
+
let PostTag;
|
|
120
|
+
let Color;
|
|
121
|
+
|
|
122
|
+
beforeEach(async () => {
|
|
123
|
+
db = mockDatabase();
|
|
124
|
+
PostTag = db.collection({
|
|
125
|
+
name: 'posts_tags',
|
|
126
|
+
fields: [{ type: 'string', name: 'tagged_at' }],
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
User = db.collection({
|
|
130
|
+
name: 'users',
|
|
131
|
+
fields: [
|
|
132
|
+
{
|
|
133
|
+
type: 'string',
|
|
134
|
+
name: 'name',
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
type: 'hasMany',
|
|
138
|
+
name: 'posts',
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
Post = db.collection({
|
|
144
|
+
name: 'posts',
|
|
145
|
+
fields: [
|
|
146
|
+
{ type: 'belongsToMany', name: 'tags', through: 'posts_tags' },
|
|
147
|
+
{ type: 'string', name: 'title' },
|
|
148
|
+
{
|
|
149
|
+
type: 'belongsTo',
|
|
150
|
+
name: 'user',
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
Tag = db.collection({
|
|
156
|
+
name: 'tags',
|
|
157
|
+
fields: [
|
|
158
|
+
{ type: 'belongsToMany', name: 'posts', through: 'posts_tags' },
|
|
159
|
+
{ type: 'string', name: 'name' },
|
|
160
|
+
{ type: 'string', name: 'status' },
|
|
161
|
+
{ type: 'hasMany', name: 'colors' },
|
|
162
|
+
],
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
Color = db.collection({
|
|
166
|
+
name: 'colors',
|
|
167
|
+
fields: [
|
|
168
|
+
{ type: 'string', name: 'name' },
|
|
169
|
+
{ type: 'belongsTo', name: 'tag' },
|
|
170
|
+
],
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
await db.sync({ force: true });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
afterEach(async () => {
|
|
177
|
+
await db.close();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('create with through values', async () => {
|
|
181
|
+
const p1 = await Post.repository.create({
|
|
182
|
+
values: {
|
|
183
|
+
title: 'p1',
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
188
|
+
|
|
189
|
+
await PostTagRepository.create({
|
|
190
|
+
values: {
|
|
191
|
+
name: 't1',
|
|
192
|
+
posts_tags: {
|
|
193
|
+
tagged_at: '123',
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const t1 = await PostTagRepository.findOne();
|
|
199
|
+
expect(t1.posts_tags.tagged_at).toEqual('123');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('create', async () => {
|
|
203
|
+
const p1 = await Post.repository.create({
|
|
204
|
+
values: {
|
|
205
|
+
title: 'p1',
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
210
|
+
|
|
211
|
+
const t1 = await PostTagRepository.create({
|
|
212
|
+
values: {
|
|
213
|
+
name: 't1',
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
expect(t1).toBeDefined();
|
|
218
|
+
|
|
219
|
+
const t2 = await Tag.repository.create({
|
|
220
|
+
values: {
|
|
221
|
+
name: 't2',
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
await PostTagRepository.add(t2.id);
|
|
226
|
+
|
|
227
|
+
const findResult = await PostTagRepository.find();
|
|
228
|
+
expect(findResult.length).toEqual(2);
|
|
229
|
+
|
|
230
|
+
const findFilterResult = await PostTagRepository.find({
|
|
231
|
+
filter: { name: 't2' },
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
expect(findFilterResult.length).toEqual(1);
|
|
235
|
+
expect(findFilterResult[0].name).toEqual('t2');
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test('find and count', async () => {
|
|
239
|
+
const p1 = await Post.repository.create({
|
|
240
|
+
values: {
|
|
241
|
+
title: 'p1',
|
|
242
|
+
tags: [{ name: 't1' }, { name: 't2' }],
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
247
|
+
let [findResult, count] = await PostTagRepository.findAndCount({
|
|
248
|
+
fields: ['id'],
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
expect(count).toEqual(2);
|
|
252
|
+
|
|
253
|
+
[findResult, count] = await PostTagRepository.findAndCount({
|
|
254
|
+
filter: {
|
|
255
|
+
name: 't1',
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
expect(count).toEqual(1);
|
|
260
|
+
expect(findResult[0].name).toEqual('t1');
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test('find one', async () => {
|
|
264
|
+
const p1 = await Post.repository.create({
|
|
265
|
+
values: { title: 'p1', tags: [{ name: 't1' }, { name: 't2' }] },
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
269
|
+
let t1 = await PostTagRepository.findOne({
|
|
270
|
+
filter: {
|
|
271
|
+
name: 't1',
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
expect(t1.name).toEqual('t1');
|
|
276
|
+
|
|
277
|
+
t1 = await PostTagRepository.findOne({
|
|
278
|
+
filter: {
|
|
279
|
+
name: 'tabcaa',
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
expect(t1).toBeNull();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('find with sort & appends', async () => {
|
|
286
|
+
const p1 = await Post.repository.create({
|
|
287
|
+
values: {
|
|
288
|
+
title: 'p1',
|
|
289
|
+
tags: [
|
|
290
|
+
{
|
|
291
|
+
name: 't1',
|
|
292
|
+
colors: [
|
|
293
|
+
{
|
|
294
|
+
name: 'red',
|
|
295
|
+
},
|
|
296
|
+
],
|
|
297
|
+
},
|
|
298
|
+
{ name: 't2', colors: [{ name: 'green' }] },
|
|
299
|
+
],
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
const PostTagsRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
304
|
+
const tags = await PostTagsRepository.find({
|
|
305
|
+
appends: ['colors'],
|
|
306
|
+
sort: ['name'],
|
|
307
|
+
limit: 20,
|
|
308
|
+
offset: 0,
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
console.log(tags);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('update raw attribute', async () => {
|
|
315
|
+
const otherTag = await Tag.repository.create({
|
|
316
|
+
values: { name: 'other_tag' },
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
const p1 = await Post.repository.create({
|
|
320
|
+
values: {
|
|
321
|
+
title: 'p1',
|
|
322
|
+
tags: [{ name: 't1' }, { name: 't2' }],
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
327
|
+
|
|
328
|
+
// rename t1 to t3
|
|
329
|
+
await PostTagRepository.update({
|
|
330
|
+
filter: {
|
|
331
|
+
name: 't1',
|
|
332
|
+
},
|
|
333
|
+
values: {
|
|
334
|
+
name: 't3',
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
const t1 = await PostTagRepository.findOne({
|
|
339
|
+
filter: {
|
|
340
|
+
name: 't1',
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
expect(t1).toBeNull();
|
|
345
|
+
|
|
346
|
+
const t3 = await PostTagRepository.findOne({
|
|
347
|
+
filter: {
|
|
348
|
+
name: 't3',
|
|
349
|
+
},
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
expect(t3.name).toEqual('t3');
|
|
353
|
+
|
|
354
|
+
await PostTagRepository.update({
|
|
355
|
+
values: {
|
|
356
|
+
name: 'updated',
|
|
357
|
+
},
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
await otherTag.reload();
|
|
361
|
+
expect(otherTag.name).toEqual('other_tag');
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test('update through table attribute', async () => {
|
|
365
|
+
const p1 = await Post.repository.create({
|
|
366
|
+
values: {
|
|
367
|
+
title: 'p1',
|
|
368
|
+
tags: [
|
|
369
|
+
{
|
|
370
|
+
name: 't1',
|
|
371
|
+
posts_tags: {
|
|
372
|
+
tagged_at: '123',
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
{ name: 't2' },
|
|
376
|
+
],
|
|
377
|
+
},
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
381
|
+
|
|
382
|
+
let t1 = await PostTagRepository.findOne({
|
|
383
|
+
filter: {
|
|
384
|
+
name: 't1',
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
expect(t1.posts_tags.tagged_at).toEqual('123');
|
|
389
|
+
|
|
390
|
+
const p2 = await Post.repository.create({
|
|
391
|
+
values: {
|
|
392
|
+
title: 'p2',
|
|
393
|
+
tags: [t1.id],
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const Post2TagRepository = new BelongsToManyRepository(Post, 'tags', p2.id);
|
|
398
|
+
const p2Tag = await Post2TagRepository.findOne();
|
|
399
|
+
expect(p2Tag.posts_tags.tagged_at).toBeNull();
|
|
400
|
+
|
|
401
|
+
// 设置p1与t1关联的tagged_at
|
|
402
|
+
await PostTagRepository.update({
|
|
403
|
+
filter: {
|
|
404
|
+
name: 't1',
|
|
405
|
+
},
|
|
406
|
+
values: {
|
|
407
|
+
posts_tags: {
|
|
408
|
+
tagged_at: '456',
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
await t1.reload();
|
|
414
|
+
|
|
415
|
+
expect(t1.posts_tags.tagged_at).toEqual('456');
|
|
416
|
+
|
|
417
|
+
await p2Tag.reload();
|
|
418
|
+
// p2-tag1 still not change
|
|
419
|
+
expect(p2Tag.posts_tags.tagged_at).toBeNull();
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
test('update association values', async () => {
|
|
423
|
+
const u1 = await User.repository.create({
|
|
424
|
+
values: {
|
|
425
|
+
name: 'u1',
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
const p1 = await Post.repository.create({
|
|
430
|
+
values: {
|
|
431
|
+
title: 'p1',
|
|
432
|
+
tags: [{ name: 't1' }, { name: 't2' }],
|
|
433
|
+
user: u1.id,
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
const tag = await Tag.repository.findOne();
|
|
438
|
+
const tagPostsRepository = new BelongsToManyRepository(Tag, 'posts', tag.id);
|
|
439
|
+
|
|
440
|
+
await tagPostsRepository.update({
|
|
441
|
+
values: {
|
|
442
|
+
user: {
|
|
443
|
+
id: u1.get('id'),
|
|
444
|
+
name: 'u0',
|
|
445
|
+
},
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
await u1.reload();
|
|
450
|
+
expect(u1.get('name')).toEqual('u1');
|
|
451
|
+
|
|
452
|
+
await tagPostsRepository.update({
|
|
453
|
+
values: {
|
|
454
|
+
user: {
|
|
455
|
+
id: u1.get('id'),
|
|
456
|
+
name: 'u0',
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
updateAssociationValues: ['user'],
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
await u1.reload();
|
|
463
|
+
expect(u1.get('name')).toEqual('u0');
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
test('add', async () => {
|
|
467
|
+
let t1 = await Tag.repository.create({
|
|
468
|
+
values: { name: 't1' },
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
const p1 = await Post.repository.create({
|
|
472
|
+
values: { title: 'p1' },
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
476
|
+
await PostTagRepository.add([[t1.id, { tagged_at: '123' }]]);
|
|
477
|
+
|
|
478
|
+
let p1Tag = await PostTagRepository.findOne();
|
|
479
|
+
expect(p1Tag.posts_tags.tagged_at).toEqual('123');
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
test('set', async () => {
|
|
483
|
+
let t1 = await Tag.repository.create({
|
|
484
|
+
values: { name: 't1' },
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
const t2 = await Tag.repository.create({
|
|
488
|
+
values: { name: 't2' },
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
const p1 = await Post.repository.create({
|
|
492
|
+
values: { title: 'p1' },
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
496
|
+
|
|
497
|
+
await PostTagRepository.set([t1.id]);
|
|
498
|
+
|
|
499
|
+
let p1Tags = await PostTagRepository.find();
|
|
500
|
+
expect(p1Tags.length).toEqual(1);
|
|
501
|
+
|
|
502
|
+
await PostTagRepository.set([[t1.id, { tagged_at: '999' }]]);
|
|
503
|
+
|
|
504
|
+
t1 = await PostTagRepository.findOne({
|
|
505
|
+
filter: {
|
|
506
|
+
name: 't1',
|
|
507
|
+
},
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
expect(t1.posts_tags.tagged_at).toEqual('999');
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
test('find by pk', async () => {
|
|
514
|
+
let t1 = await Tag.repository.create({
|
|
515
|
+
values: {
|
|
516
|
+
name: 't1',
|
|
517
|
+
},
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const t2 = await Tag.repository.create({
|
|
521
|
+
values: {
|
|
522
|
+
name: 't2',
|
|
523
|
+
},
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
const p1 = await Post.repository.create({
|
|
527
|
+
values: { title: 'p1' },
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
531
|
+
|
|
532
|
+
await PostTagRepository.set([t1.id, t2.id]);
|
|
533
|
+
|
|
534
|
+
const findByPkResult = await PostTagRepository.findOne({
|
|
535
|
+
filterByTk: t2.id,
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
expect(findByPkResult.name).toEqual('t2');
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
test('toggle', async () => {
|
|
542
|
+
let t1 = await Tag.repository.create({
|
|
543
|
+
values: { name: 't1' },
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
const p1 = await Post.repository.create({
|
|
547
|
+
values: { title: 'p1' },
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
551
|
+
|
|
552
|
+
await PostTagRepository.toggle(t1.id);
|
|
553
|
+
expect(await PostTagRepository.findOne()).not.toBeNull();
|
|
554
|
+
|
|
555
|
+
await PostTagRepository.toggle(t1.id);
|
|
556
|
+
expect(await PostTagRepository.findOne()).toBeNull();
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
test('remove', async () => {
|
|
560
|
+
let t1 = await Tag.repository.create({
|
|
561
|
+
values: { name: 't1' },
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
const p1 = await Post.repository.create({
|
|
565
|
+
values: { title: 'p1' },
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
569
|
+
|
|
570
|
+
await PostTagRepository.add(t1.id);
|
|
571
|
+
expect(await PostTagRepository.findOne()).not.toBeNull();
|
|
572
|
+
|
|
573
|
+
await PostTagRepository.remove(t1.id);
|
|
574
|
+
expect(await PostTagRepository.findOne()).toBeNull();
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
test('destroy all', async () => {
|
|
578
|
+
let t1 = await Tag.repository.create({
|
|
579
|
+
values: {
|
|
580
|
+
name: 't1',
|
|
581
|
+
},
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
const t2 = await Tag.repository.create({
|
|
585
|
+
values: {
|
|
586
|
+
name: 't2',
|
|
587
|
+
},
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
const p1 = await Post.repository.create({
|
|
591
|
+
values: { title: 'p1' },
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
595
|
+
|
|
596
|
+
await PostTagRepository.set([t1.id, t2.id]);
|
|
597
|
+
|
|
598
|
+
await PostTagRepository.destroy();
|
|
599
|
+
|
|
600
|
+
const [_, count] = await PostTagRepository.findAndCount();
|
|
601
|
+
expect(count).toEqual(0);
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
test('destroy by id and filter', async () => {
|
|
605
|
+
let t1 = await Tag.repository.create({
|
|
606
|
+
values: {
|
|
607
|
+
name: 't1',
|
|
608
|
+
status: 'published',
|
|
609
|
+
},
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
const t2 = await Tag.repository.create({
|
|
613
|
+
values: {
|
|
614
|
+
name: 't2',
|
|
615
|
+
status: 'draft',
|
|
616
|
+
},
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
const p1 = await Post.repository.create({
|
|
620
|
+
values: { title: 'p1' },
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
624
|
+
|
|
625
|
+
await PostTagRepository.set([t1.id, t2.id]);
|
|
626
|
+
|
|
627
|
+
let [_, count] = await PostTagRepository.findAndCount();
|
|
628
|
+
expect(count).toEqual(2);
|
|
629
|
+
|
|
630
|
+
await PostTagRepository.destroy({
|
|
631
|
+
filterByTk: t1.get('id') as number,
|
|
632
|
+
filter: {
|
|
633
|
+
status: 'draft',
|
|
634
|
+
},
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
[_, count] = await PostTagRepository.findAndCount();
|
|
638
|
+
expect(count).toEqual(2);
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
test('destroy with id', async () => {
|
|
642
|
+
let t1 = await Tag.repository.create({
|
|
643
|
+
values: {
|
|
644
|
+
name: 't1',
|
|
645
|
+
},
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
const t2 = await Tag.repository.create({
|
|
649
|
+
values: {
|
|
650
|
+
name: 't2',
|
|
651
|
+
},
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
const p1 = await Post.repository.create({
|
|
655
|
+
values: { title: 'p1' },
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
659
|
+
|
|
660
|
+
await PostTagRepository.set([t1.id, t2.id]);
|
|
661
|
+
|
|
662
|
+
expect(await PostTagRepository.count()).toEqual(2);
|
|
663
|
+
|
|
664
|
+
await PostTagRepository.destroy(t2.id);
|
|
665
|
+
|
|
666
|
+
expect(await PostTagRepository.count()).toEqual(1);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
test('transaction', async () => {
|
|
670
|
+
let t1 = await Tag.repository.create({
|
|
671
|
+
values: {
|
|
672
|
+
name: 't1',
|
|
673
|
+
},
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
const t2 = await Tag.repository.create({
|
|
677
|
+
values: {
|
|
678
|
+
name: 't2',
|
|
679
|
+
},
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
const p1 = await Post.repository.create({
|
|
683
|
+
values: { title: 'p1' },
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
const transaction = await Tag.model.sequelize.transaction();
|
|
687
|
+
|
|
688
|
+
const PostTagRepository = new BelongsToManyRepository(Post, 'tags', p1.id);
|
|
689
|
+
|
|
690
|
+
await PostTagRepository.set({
|
|
691
|
+
tk: [t1.id, t2.id],
|
|
692
|
+
transaction,
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
await transaction.commit();
|
|
696
|
+
});
|
|
697
|
+
});
|