@nocobase/database 0.5.0-alpha.38 → 0.7.0-alpha.2
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,376 @@
|
|
|
1
|
+
import { Collection } from '../collection';
|
|
2
|
+
import { mockDatabase } from './index';
|
|
3
|
+
import { UpdateGuard } from '../update-guard';
|
|
4
|
+
import lodash from 'lodash';
|
|
5
|
+
import { Database } from '../database';
|
|
6
|
+
|
|
7
|
+
describe('update-guard', () => {
|
|
8
|
+
let db: Database;
|
|
9
|
+
let User: Collection;
|
|
10
|
+
let Post: Collection;
|
|
11
|
+
let Comment: Collection;
|
|
12
|
+
|
|
13
|
+
beforeEach(async () => {
|
|
14
|
+
db = mockDatabase();
|
|
15
|
+
|
|
16
|
+
User = db.collection({
|
|
17
|
+
name: 'users',
|
|
18
|
+
fields: [
|
|
19
|
+
{ type: 'string', name: 'name' },
|
|
20
|
+
{ type: 'integer', name: 'age' },
|
|
21
|
+
{ type: 'hasMany', name: 'posts' },
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
Post = db.collection({
|
|
26
|
+
name: 'posts',
|
|
27
|
+
fields: [
|
|
28
|
+
{ type: 'string', name: 'title' },
|
|
29
|
+
{ type: 'string', name: 'content' },
|
|
30
|
+
{
|
|
31
|
+
type: 'belongsTo',
|
|
32
|
+
name: 'user',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
type: 'hasMany',
|
|
36
|
+
name: 'comments',
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
Comment = db.collection({
|
|
42
|
+
name: 'comments',
|
|
43
|
+
fields: [
|
|
44
|
+
{ type: 'string', name: 'content' },
|
|
45
|
+
{ type: 'string', name: 'comment_as' },
|
|
46
|
+
{ type: 'belongsTo', name: 'post' },
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
await db.sync({
|
|
51
|
+
force: true,
|
|
52
|
+
alter: { drop: false },
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const repository = User.repository;
|
|
56
|
+
|
|
57
|
+
await repository.createMany({
|
|
58
|
+
records: [
|
|
59
|
+
{
|
|
60
|
+
name: 'u1',
|
|
61
|
+
age: 10,
|
|
62
|
+
posts: [{ title: 'u1t1', comments: [{ content: 'u1t1c1' }] }],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: 'u2',
|
|
66
|
+
age: 20,
|
|
67
|
+
posts: [{ title: 'u2t1', comments: [{ content: 'u2t1c1' }] }],
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'u3',
|
|
71
|
+
age: 30,
|
|
72
|
+
posts: [{ title: 'u3t1', comments: [{ content: 'u3t1c1' }] }],
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
afterEach(async () => {
|
|
79
|
+
await db.close();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('white list', () => {
|
|
83
|
+
const values = {
|
|
84
|
+
name: '123',
|
|
85
|
+
age: 30,
|
|
86
|
+
};
|
|
87
|
+
const guard = new UpdateGuard();
|
|
88
|
+
guard.setModel(User.model);
|
|
89
|
+
guard.setWhiteList(['name']);
|
|
90
|
+
|
|
91
|
+
expect(guard.sanitize(values)).toEqual({
|
|
92
|
+
name: '123',
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('black list', () => {
|
|
97
|
+
const values = {
|
|
98
|
+
name: '123',
|
|
99
|
+
age: 30,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const guard = new UpdateGuard();
|
|
103
|
+
guard.setModel(User.model);
|
|
104
|
+
guard.setBlackList(['name']);
|
|
105
|
+
|
|
106
|
+
expect(guard.sanitize(values)).toEqual({
|
|
107
|
+
age: 30,
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('association black list', () => {
|
|
112
|
+
const values = {
|
|
113
|
+
name: 'username123',
|
|
114
|
+
age: 30,
|
|
115
|
+
posts: [
|
|
116
|
+
{
|
|
117
|
+
title: 'post-title123',
|
|
118
|
+
content: '345',
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const guard = new UpdateGuard();
|
|
124
|
+
guard.setModel(User.model);
|
|
125
|
+
guard.setBlackList(['name', 'posts']);
|
|
126
|
+
|
|
127
|
+
expect(guard.sanitize(values)).toEqual({
|
|
128
|
+
age: 30,
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('association fields black list', () => {
|
|
133
|
+
const values = {
|
|
134
|
+
name: 'username123',
|
|
135
|
+
age: 30,
|
|
136
|
+
posts: [
|
|
137
|
+
{
|
|
138
|
+
title: 'post-title123',
|
|
139
|
+
content: '345',
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const guard = new UpdateGuard();
|
|
145
|
+
guard.setModel(User.model);
|
|
146
|
+
guard.setBlackList(['name', 'posts.content']);
|
|
147
|
+
|
|
148
|
+
expect(guard.sanitize(values)).toEqual({
|
|
149
|
+
age: 30,
|
|
150
|
+
posts: values.posts.map((p) => {
|
|
151
|
+
return {
|
|
152
|
+
title: p.title,
|
|
153
|
+
};
|
|
154
|
+
}),
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('association subfield white list', () => {
|
|
159
|
+
const values = {
|
|
160
|
+
name: 'username123',
|
|
161
|
+
age: 30,
|
|
162
|
+
posts: [
|
|
163
|
+
{
|
|
164
|
+
title: 'post-title123',
|
|
165
|
+
content: '345',
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const guard = new UpdateGuard();
|
|
171
|
+
guard.setModel(User.model);
|
|
172
|
+
guard.setWhiteList(['name', 'posts.title']);
|
|
173
|
+
|
|
174
|
+
expect(guard.sanitize(values)).toEqual({
|
|
175
|
+
name: values.name,
|
|
176
|
+
posts: values.posts.map((post) => {
|
|
177
|
+
return {
|
|
178
|
+
title: post.title,
|
|
179
|
+
};
|
|
180
|
+
}),
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('association nested fields white list', () => {
|
|
185
|
+
const values = {
|
|
186
|
+
name: 'username123',
|
|
187
|
+
age: 30,
|
|
188
|
+
posts: [
|
|
189
|
+
{
|
|
190
|
+
title: 'post-title123',
|
|
191
|
+
content: '345',
|
|
192
|
+
comments: [1, 2, 3],
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const guard = new UpdateGuard();
|
|
198
|
+
guard.setModel(User.model);
|
|
199
|
+
guard.setWhiteList(['name', 'posts.comments']);
|
|
200
|
+
|
|
201
|
+
expect(guard.sanitize(values)).toEqual({
|
|
202
|
+
name: values.name,
|
|
203
|
+
posts: values.posts.map((post) => {
|
|
204
|
+
return {
|
|
205
|
+
comments: post.comments,
|
|
206
|
+
};
|
|
207
|
+
}),
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
test('association white list', () => {
|
|
212
|
+
const values = {
|
|
213
|
+
name: '123',
|
|
214
|
+
age: 30,
|
|
215
|
+
posts: [
|
|
216
|
+
{
|
|
217
|
+
title: '123',
|
|
218
|
+
content: '345',
|
|
219
|
+
},
|
|
220
|
+
],
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const guard = new UpdateGuard();
|
|
224
|
+
guard.setModel(User.model);
|
|
225
|
+
guard.setWhiteList(['posts']);
|
|
226
|
+
|
|
227
|
+
expect(guard.sanitize(values)).toEqual({
|
|
228
|
+
posts: values.posts,
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test('associationKeysToBeUpdate', () => {
|
|
233
|
+
const values = {
|
|
234
|
+
name: '123',
|
|
235
|
+
age: 30,
|
|
236
|
+
posts: [
|
|
237
|
+
{
|
|
238
|
+
title: '123',
|
|
239
|
+
content: '345',
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
id: 1,
|
|
243
|
+
title: '456',
|
|
244
|
+
content: '789',
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const guard = new UpdateGuard();
|
|
250
|
+
guard.setModel(User.model);
|
|
251
|
+
|
|
252
|
+
expect(guard.sanitize(values)).toEqual({
|
|
253
|
+
name: '123',
|
|
254
|
+
age: 30,
|
|
255
|
+
posts: [
|
|
256
|
+
{
|
|
257
|
+
title: '123',
|
|
258
|
+
content: '345',
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
id: 1,
|
|
262
|
+
},
|
|
263
|
+
],
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
guard.setAssociationKeysToBeUpdate(['posts']);
|
|
267
|
+
expect(guard.sanitize(values)).toEqual(values);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test('associationKeysToBeUpdate nested association', () => {
|
|
271
|
+
const values = {
|
|
272
|
+
name: '123',
|
|
273
|
+
age: 30,
|
|
274
|
+
posts: [
|
|
275
|
+
{
|
|
276
|
+
title: '123',
|
|
277
|
+
content: '345',
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
id: 1,
|
|
281
|
+
title: '456',
|
|
282
|
+
content: '789',
|
|
283
|
+
comments: [
|
|
284
|
+
{
|
|
285
|
+
id: 1,
|
|
286
|
+
content: '123',
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
},
|
|
290
|
+
],
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const guard = new UpdateGuard();
|
|
294
|
+
guard.setModel(User.model);
|
|
295
|
+
|
|
296
|
+
expect(guard.sanitize(lodash.clone(values))).toEqual({
|
|
297
|
+
name: '123',
|
|
298
|
+
age: 30,
|
|
299
|
+
posts: [
|
|
300
|
+
{
|
|
301
|
+
title: '123',
|
|
302
|
+
content: '345',
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
id: 1,
|
|
306
|
+
comments: [
|
|
307
|
+
{
|
|
308
|
+
id: 1,
|
|
309
|
+
},
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
],
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
guard.setAssociationKeysToBeUpdate(['posts']);
|
|
316
|
+
|
|
317
|
+
expect(guard.sanitize(lodash.clone(values))).toEqual({
|
|
318
|
+
name: '123',
|
|
319
|
+
age: 30,
|
|
320
|
+
posts: [
|
|
321
|
+
{
|
|
322
|
+
title: '123',
|
|
323
|
+
content: '345',
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
id: 1,
|
|
327
|
+
title: '456',
|
|
328
|
+
content: '789',
|
|
329
|
+
comments: [
|
|
330
|
+
{
|
|
331
|
+
id: 1,
|
|
332
|
+
},
|
|
333
|
+
],
|
|
334
|
+
},
|
|
335
|
+
],
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
describe('One2One Association', () => {
|
|
341
|
+
test('associationKeysToBeUpdate hasOne & BelongsTo', () => {
|
|
342
|
+
const db = mockDatabase();
|
|
343
|
+
const Post = db.collection({
|
|
344
|
+
name: 'posts',
|
|
345
|
+
fields: [{ type: 'belongsTo', name: 'user', targetKey: 'uid' }],
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
const User = db.collection({
|
|
349
|
+
name: 'users',
|
|
350
|
+
fields: [{ type: 'string', name: 'uid', unique: true }],
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const values = {
|
|
354
|
+
title: '123',
|
|
355
|
+
content: '456',
|
|
356
|
+
user: {
|
|
357
|
+
uid: 1,
|
|
358
|
+
name: '123',
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const guard = new UpdateGuard();
|
|
363
|
+
guard.setModel(Post.model);
|
|
364
|
+
|
|
365
|
+
expect(guard.sanitize(values)).toEqual({
|
|
366
|
+
title: '123',
|
|
367
|
+
content: '456',
|
|
368
|
+
user: {
|
|
369
|
+
uid: 1,
|
|
370
|
+
},
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
guard.setAssociationKeysToBeUpdate(['user']);
|
|
374
|
+
expect(guard.sanitize(values)).toEqual(values);
|
|
375
|
+
});
|
|
376
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import lodash from 'lodash';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export type ImportFileExtension = 'js' | 'ts' | 'json';
|
|
6
|
+
|
|
7
|
+
async function requireModule(module: any) {
|
|
8
|
+
if (typeof module === 'string') {
|
|
9
|
+
module = require(module);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof module !== 'object') {
|
|
13
|
+
return module;
|
|
14
|
+
}
|
|
15
|
+
return module.__esModule ? module.default : module;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class ImporterReader {
|
|
19
|
+
directory: string;
|
|
20
|
+
extensions: Set<string>;
|
|
21
|
+
|
|
22
|
+
constructor(directory: string, extensions?: ImportFileExtension[]) {
|
|
23
|
+
this.directory = directory;
|
|
24
|
+
|
|
25
|
+
if (!extensions) {
|
|
26
|
+
extensions = ['js', 'ts', 'json'];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.extensions = new Set(extensions);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async read() {
|
|
33
|
+
const modules = (
|
|
34
|
+
await fs.promises.readdir(this.directory, {
|
|
35
|
+
encoding: 'utf-8',
|
|
36
|
+
})
|
|
37
|
+
)
|
|
38
|
+
.filter((fileName) => {
|
|
39
|
+
if (fileName.endsWith('.d.ts')) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const ext = path.parse(fileName).ext.replace('.', '');
|
|
43
|
+
return this.extensions.has(ext);
|
|
44
|
+
})
|
|
45
|
+
.map(async (fileName) => await requireModule(path.join(this.directory, fileName)));
|
|
46
|
+
|
|
47
|
+
return (await Promise.all(modules)).filter((module) => lodash.isPlainObject(module));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import merge from 'deepmerge';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
3
|
+
import { default as lodash, default as _ } from 'lodash';
|
|
4
|
+
import { col, ModelCtor, ModelOptions, SyncOptions } from 'sequelize';
|
|
5
|
+
import { Database } from './database';
|
|
6
|
+
import { Field, FieldOptions } from './fields';
|
|
7
|
+
import { Model } from './model';
|
|
8
|
+
import { Repository } from './repository';
|
|
9
|
+
|
|
10
|
+
export type RepositoryType = typeof Repository;
|
|
11
|
+
|
|
12
|
+
export type CollectionSortable = string | boolean | { name?: string; scopeKey?: string };
|
|
13
|
+
|
|
14
|
+
export interface CollectionOptions extends Omit<ModelOptions, 'name' | 'hooks'> {
|
|
15
|
+
name: string;
|
|
16
|
+
tableName?: string;
|
|
17
|
+
filterTargetKey?: string;
|
|
18
|
+
fields?: FieldOptions[];
|
|
19
|
+
model?: string | ModelCtor<Model>;
|
|
20
|
+
repository?: string | RepositoryType;
|
|
21
|
+
sortable?: CollectionSortable;
|
|
22
|
+
/**
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
autoGenId?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* @default 'options'
|
|
28
|
+
*/
|
|
29
|
+
magicAttribute?: string;
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CollectionContext {
|
|
34
|
+
database: Database;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class Collection<
|
|
38
|
+
TModelAttributes extends {} = any,
|
|
39
|
+
TCreationAttributes extends {} = TModelAttributes,
|
|
40
|
+
> extends EventEmitter {
|
|
41
|
+
options: CollectionOptions;
|
|
42
|
+
context: CollectionContext;
|
|
43
|
+
isThrough?: boolean;
|
|
44
|
+
fields: Map<string, any> = new Map<string, any>();
|
|
45
|
+
model: ModelCtor<Model>;
|
|
46
|
+
repository: Repository<TModelAttributes, TCreationAttributes>;
|
|
47
|
+
|
|
48
|
+
get filterTargetKey() {
|
|
49
|
+
return lodash.get(this.options, 'filterTargetKey', this.model.primaryKeyAttribute);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get name() {
|
|
53
|
+
return this.options.name;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
constructor(options: CollectionOptions, context?: CollectionContext) {
|
|
57
|
+
super();
|
|
58
|
+
this.context = context;
|
|
59
|
+
this.options = options;
|
|
60
|
+
this.bindFieldEventListener();
|
|
61
|
+
this.modelInit();
|
|
62
|
+
this.setFields(options.fields);
|
|
63
|
+
this.setRepository(options.repository);
|
|
64
|
+
this.setSortable(options.sortable);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private sequelizeModelOptions() {
|
|
68
|
+
const { name, tableName } = this.options;
|
|
69
|
+
return {
|
|
70
|
+
..._.omit(this.options, ['name', 'fields', 'model', 'targetKey']),
|
|
71
|
+
modelName: name,
|
|
72
|
+
sequelize: this.context.database.sequelize,
|
|
73
|
+
tableName: tableName || name,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* TODO
|
|
79
|
+
*/
|
|
80
|
+
modelInit() {
|
|
81
|
+
if (this.model) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const { name, model, autoGenId = true } = this.options;
|
|
85
|
+
let M: ModelCtor<Model> = Model;
|
|
86
|
+
if (this.context.database.sequelize.isDefined(name)) {
|
|
87
|
+
const m = this.context.database.sequelize.model(name);
|
|
88
|
+
if ((m as any).isThrough) {
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
this.model = m;
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
this.model.database = this.context.database;
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
this.model.collection = this;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (typeof model === 'string') {
|
|
99
|
+
M = this.context.database.models.get(model) || Model;
|
|
100
|
+
} else if (model) {
|
|
101
|
+
M = model;
|
|
102
|
+
}
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
this.model = class extends M {};
|
|
105
|
+
this.model.init(null, this.sequelizeModelOptions());
|
|
106
|
+
|
|
107
|
+
if (!autoGenId) {
|
|
108
|
+
this.model.removeAttribute('id');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
this.model.database = this.context.database;
|
|
113
|
+
// @ts-ignore
|
|
114
|
+
this.model.collection = this;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
setRepository(repository?: RepositoryType | string) {
|
|
118
|
+
let repo = Repository;
|
|
119
|
+
if (typeof repository === 'string') {
|
|
120
|
+
repo = this.context.database.repositories.get(repository) || Repository;
|
|
121
|
+
}
|
|
122
|
+
this.repository = new repo(this);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private bindFieldEventListener() {
|
|
126
|
+
this.on('field.afterAdd', (field: Field) => {
|
|
127
|
+
field.bind();
|
|
128
|
+
});
|
|
129
|
+
this.on('field.afterRemove', (field) => field.unbind());
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
forEachField(callback: (field: Field) => void) {
|
|
133
|
+
return [...this.fields.values()].forEach(callback);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
findField(callback: (field: Field) => boolean) {
|
|
137
|
+
return [...this.fields.values()].find(callback);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
hasField(name: string) {
|
|
141
|
+
return this.fields.has(name);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
getField<F extends Field>(name: string): F {
|
|
145
|
+
return this.fields.get(name);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
addField(name: string, options: FieldOptions): Field {
|
|
149
|
+
return this.setField(name, options);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
setField(name: string, options: FieldOptions): Field {
|
|
153
|
+
const { database } = this.context;
|
|
154
|
+
|
|
155
|
+
const field = database.buildField(
|
|
156
|
+
{ name, ...options },
|
|
157
|
+
{
|
|
158
|
+
...this.context,
|
|
159
|
+
collection: this,
|
|
160
|
+
},
|
|
161
|
+
);
|
|
162
|
+
this.removeField(name);
|
|
163
|
+
this.fields.set(name, field);
|
|
164
|
+
this.emit('field.afterAdd', field);
|
|
165
|
+
return field;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
setFields(fields: FieldOptions[], resetFields = true) {
|
|
169
|
+
if (!Array.isArray(fields)) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (resetFields) {
|
|
174
|
+
this.resetFields();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const { name, ...options } of fields) {
|
|
178
|
+
this.addField(name, options);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
resetFields() {
|
|
183
|
+
const fieldNames = this.fields.keys();
|
|
184
|
+
for (const fieldName of fieldNames) {
|
|
185
|
+
this.removeField(fieldName);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
removeField(name) {
|
|
190
|
+
if (!this.fields.has(name)) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const field = this.fields.get(name);
|
|
194
|
+
const bool = this.fields.delete(name);
|
|
195
|
+
if (bool) {
|
|
196
|
+
this.emit('field.afterRemove', field);
|
|
197
|
+
}
|
|
198
|
+
return bool;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* TODO
|
|
203
|
+
*/
|
|
204
|
+
updateOptions(options: CollectionOptions, mergeOptions?: any) {
|
|
205
|
+
let newOptions = lodash.cloneDeep(options);
|
|
206
|
+
newOptions = merge(this.options, newOptions, mergeOptions);
|
|
207
|
+
|
|
208
|
+
this.context.database.emit('beforeUpdateCollection', this, newOptions);
|
|
209
|
+
|
|
210
|
+
this.setFields(options.fields, false);
|
|
211
|
+
this.setRepository(options.repository);
|
|
212
|
+
|
|
213
|
+
this.context.database.emit('afterUpdateCollection', this);
|
|
214
|
+
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
setSortable(sortable) {
|
|
219
|
+
if (!sortable) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (sortable === true) {
|
|
223
|
+
this.setField('sort', {
|
|
224
|
+
type: 'sort',
|
|
225
|
+
hidden: true,
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
if (typeof sortable === 'string') {
|
|
229
|
+
this.setField(sortable, {
|
|
230
|
+
type: 'sort',
|
|
231
|
+
hidden: true,
|
|
232
|
+
});
|
|
233
|
+
} else if (typeof sortable === 'object') {
|
|
234
|
+
const { name, ...opts } = sortable;
|
|
235
|
+
this.setField(name || 'sort', { type: 'sort', hidden: true, ...opts });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* TODO
|
|
241
|
+
*
|
|
242
|
+
* @param name
|
|
243
|
+
* @param options
|
|
244
|
+
*/
|
|
245
|
+
updateField(name: string, options: FieldOptions) {
|
|
246
|
+
if (!this.hasField(name)) {
|
|
247
|
+
throw new Error(`field ${name} not exists`);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (options.name && options.name !== name) {
|
|
251
|
+
this.removeField(name);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
this.setField(options.name || name, options);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async sync(syncOptions?: SyncOptions) {
|
|
258
|
+
const modelNames = [this.model.name];
|
|
259
|
+
|
|
260
|
+
const associations = this.model.associations;
|
|
261
|
+
|
|
262
|
+
for (const associationKey in associations) {
|
|
263
|
+
const association = associations[associationKey];
|
|
264
|
+
modelNames.push(association.target.name);
|
|
265
|
+
if ((<any>association).through) {
|
|
266
|
+
modelNames.push((<any>association).through.model.name);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const models: ModelCtor<Model>[] = [];
|
|
271
|
+
// @ts-ignore
|
|
272
|
+
this.context.database.sequelize.modelManager.forEachModel((model) => {
|
|
273
|
+
if (modelNames.includes(model.name)) {
|
|
274
|
+
models.push(model);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
for (const model of models) {
|
|
279
|
+
await model.sync(syncOptions);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|