@nocobase/database 0.5.0-alpha.37 → 0.7.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/esm/collection-importer.d.ts +7 -0
- package/esm/collection-importer.js +49 -0
- package/esm/collection-importer.js.map +1 -0
- package/esm/collection.d.ts +73 -0
- package/esm/collection.js +224 -0
- package/esm/collection.js.map +1 -0
- package/esm/database.d.ts +101 -0
- package/esm/database.js +275 -0
- package/esm/database.js.map +1 -0
- package/esm/fields/array-field.d.ts +11 -0
- package/esm/fields/array-field.js +26 -0
- package/esm/fields/array-field.js.map +1 -0
- package/esm/fields/belongs-to-field.d.ts +12 -0
- package/esm/fields/belongs-to-field.js +57 -0
- package/esm/fields/belongs-to-field.js.map +1 -0
- package/esm/fields/belongs-to-many-field.d.ts +11 -0
- package/esm/fields/belongs-to-many-field.js +55 -0
- package/esm/fields/belongs-to-many-field.js.map +1 -0
- package/esm/fields/boolean-field.d.ts +8 -0
- package/esm/fields/boolean-field.js +8 -0
- package/esm/fields/boolean-field.js.map +1 -0
- package/esm/fields/context-field.d.ts +13 -0
- package/esm/fields/context-field.js +43 -0
- package/esm/fields/context-field.js.map +1 -0
- package/esm/fields/date-field.d.ts +8 -0
- package/esm/fields/date-field.js +8 -0
- package/esm/fields/date-field.js.map +1 -0
- package/esm/fields/field.d.ts +37 -0
- package/esm/fields/field.js +74 -0
- package/esm/fields/field.js.map +1 -0
- package/esm/fields/has-inverse-field.d.ts +4 -0
- package/esm/fields/has-inverse-field.js +2 -0
- package/esm/fields/has-inverse-field.js.map +1 -0
- package/esm/fields/has-many-field.d.ts +64 -0
- package/esm/fields/has-many-field.js +58 -0
- package/esm/fields/has-many-field.js.map +1 -0
- package/esm/fields/has-one-field.d.ts +64 -0
- package/esm/fields/has-one-field.js +57 -0
- package/esm/fields/has-one-field.js.map +1 -0
- package/esm/fields/index.d.ts +40 -0
- package/esm/fields/index.js +21 -0
- package/esm/fields/index.js.map +1 -0
- package/esm/fields/json-field.d.ts +14 -0
- package/esm/fields/json-field.js +17 -0
- package/esm/fields/json-field.js.map +1 -0
- package/esm/fields/number-field.d.ts +32 -0
- package/esm/fields/number-field.js +28 -0
- package/esm/fields/number-field.js.map +1 -0
- package/esm/fields/password-field.d.ts +21 -0
- package/esm/fields/password-field.js +71 -0
- package/esm/fields/password-field.js.map +1 -0
- package/esm/fields/radio-field.d.ts +14 -0
- package/esm/fields/radio-field.js +49 -0
- package/esm/fields/radio-field.js.map +1 -0
- package/esm/fields/relation-field.d.ts +20 -0
- package/esm/fields/relation-field.js +27 -0
- package/esm/fields/relation-field.js.map +1 -0
- package/esm/fields/sort-field.d.ts +16 -0
- package/esm/fields/sort-field.js +90 -0
- package/esm/fields/sort-field.js.map +1 -0
- package/esm/fields/string-field.d.ts +8 -0
- package/esm/fields/string-field.js +8 -0
- package/esm/fields/string-field.js.map +1 -0
- package/esm/fields/text-field.d.ts +8 -0
- package/esm/fields/text-field.js +8 -0
- package/esm/fields/text-field.js.map +1 -0
- package/esm/fields/time-field.d.ts +8 -0
- package/esm/fields/time-field.js +8 -0
- package/esm/fields/time-field.js.map +1 -0
- package/esm/fields/uid-field.d.ts +10 -0
- package/esm/fields/uid-field.js +27 -0
- package/esm/fields/uid-field.js.map +1 -0
- package/esm/fields/virtual-field.d.ts +8 -0
- package/esm/fields/virtual-field.js +8 -0
- package/esm/fields/virtual-field.js.map +1 -0
- package/esm/filter-parser.d.ts +27 -0
- package/esm/filter-parser.js +185 -0
- package/esm/filter-parser.js.map +1 -0
- package/esm/index.d.ts +15 -0
- package/esm/index.js +16 -0
- package/esm/index.js.map +1 -0
- package/esm/magic-attribute-model.d.ts +7 -0
- package/esm/magic-attribute-model.js +70 -0
- package/esm/magic-attribute-model.js.map +1 -0
- package/esm/mock-database.d.ts +22 -0
- package/esm/mock-database.js +34 -0
- package/esm/mock-database.js.map +1 -0
- package/esm/model-hook.d.ts +12 -0
- package/esm/model-hook.js +61 -0
- package/esm/model-hook.js.map +1 -0
- package/esm/model.d.ts +15 -0
- package/esm/model.js +80 -0
- package/esm/model.js.map +1 -0
- package/esm/operators/array.d.ts +26 -0
- package/esm/operators/array.js +105 -0
- package/esm/operators/array.js.map +1 -0
- package/esm/operators/association.d.ts +10 -0
- package/esm/operators/association.js +14 -0
- package/esm/operators/association.js.map +1 -0
- package/esm/operators/date.d.ts +34 -0
- package/esm/operators/date.js +35 -0
- package/esm/operators/date.js.map +1 -0
- package/esm/operators/empty.d.ts +28 -0
- package/esm/operators/empty.js +58 -0
- package/esm/operators/empty.js.map +1 -0
- package/esm/operators/index.d.ts +2 -0
- package/esm/operators/index.js +2 -0
- package/esm/operators/index.js.map +1 -0
- package/esm/operators/ne.d.ts +10 -0
- package/esm/operators/ne.js +12 -0
- package/esm/operators/ne.js.map +1 -0
- package/esm/operators/string.d.ts +21 -0
- package/esm/operators/string.js +35 -0
- package/esm/operators/string.js.map +1 -0
- package/esm/operators/utils.d.ts +4 -0
- package/esm/operators/utils.js +11 -0
- package/esm/operators/utils.js.map +1 -0
- package/esm/options-parser.d.ts +31 -0
- package/esm/options-parser.js +225 -0
- package/esm/options-parser.js.map +1 -0
- package/esm/playground.d.ts +1 -0
- package/esm/playground.js +53 -0
- package/esm/playground.js.map +1 -0
- package/esm/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/esm/relation-repository/belongs-to-many-repository.js +199 -0
- package/esm/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/esm/relation-repository/belongs-to-repository.d.ts +17 -0
- package/esm/relation-repository/belongs-to-repository.js +4 -0
- package/esm/relation-repository/belongs-to-repository.js.map +1 -0
- package/esm/relation-repository/hasmany-repository.d.ts +23 -0
- package/esm/relation-repository/hasmany-repository.js +125 -0
- package/esm/relation-repository/hasmany-repository.js.map +1 -0
- package/esm/relation-repository/hasone-repository.d.ts +17 -0
- package/esm/relation-repository/hasone-repository.js +4 -0
- package/esm/relation-repository/hasone-repository.js.map +1 -0
- package/esm/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/esm/relation-repository/multiple-relation-repository.js +149 -0
- package/esm/relation-repository/multiple-relation-repository.js.map +1 -0
- package/esm/relation-repository/relation-repository.d.ts +32 -0
- package/esm/relation-repository/relation-repository.js +93 -0
- package/esm/relation-repository/relation-repository.js.map +1 -0
- package/esm/relation-repository/single-relation-repository.d.ts +23 -0
- package/esm/relation-repository/single-relation-repository.js +96 -0
- package/esm/relation-repository/single-relation-repository.js.map +1 -0
- package/esm/relation-repository/types.d.ts +7 -0
- package/esm/relation-repository/types.js +2 -0
- package/esm/relation-repository/types.js.map +1 -0
- package/esm/repository.d.ts +165 -0
- package/esm/repository.js +276 -0
- package/esm/repository.js.map +1 -0
- package/esm/transaction-decorator.d.ts +1 -0
- package/esm/transaction-decorator.js +63 -0
- package/esm/transaction-decorator.js.map +1 -0
- package/esm/update-associations.d.ts +60 -0
- package/esm/update-associations.js +362 -0
- package/esm/update-associations.js.map +1 -0
- package/esm/update-guard.d.ts +26 -0
- package/esm/update-guard.js +122 -0
- package/esm/update-guard.js.map +1 -0
- package/lib/collection-importer.d.ts +7 -0
- package/lib/collection-importer.js +75 -0
- package/lib/collection-importer.js.map +1 -0
- package/lib/collection.d.ts +73 -0
- package/lib/collection.js +231 -0
- package/lib/collection.js.map +1 -0
- package/lib/database.d.ts +93 -43
- package/lib/database.js +277 -471
- package/lib/database.js.map +1 -1
- package/lib/fields/array-field.d.ts +11 -0
- package/lib/fields/array-field.js +30 -0
- package/lib/fields/array-field.js.map +1 -0
- package/lib/fields/belongs-to-field.d.ts +12 -0
- package/lib/fields/belongs-to-field.js +61 -0
- package/lib/fields/belongs-to-field.js.map +1 -0
- package/lib/fields/belongs-to-many-field.d.ts +11 -0
- package/lib/fields/belongs-to-many-field.js +59 -0
- package/lib/fields/belongs-to-many-field.js.map +1 -0
- package/lib/fields/boolean-field.d.ts +8 -0
- package/lib/fields/boolean-field.js +12 -0
- package/lib/fields/boolean-field.js.map +1 -0
- package/lib/fields/context-field.d.ts +13 -0
- package/lib/fields/context-field.js +47 -0
- package/lib/fields/context-field.js.map +1 -0
- package/lib/fields/date-field.d.ts +8 -0
- package/lib/fields/date-field.js +12 -0
- package/lib/fields/date-field.js.map +1 -0
- package/lib/fields/field.d.ts +37 -0
- package/lib/fields/field.js +81 -0
- package/lib/fields/field.js.map +1 -0
- package/lib/fields/has-inverse-field.d.ts +4 -0
- package/lib/fields/has-inverse-field.js +3 -0
- package/lib/fields/has-inverse-field.js.map +1 -0
- package/lib/fields/has-many-field.d.ts +64 -0
- package/lib/fields/has-many-field.js +62 -0
- package/lib/fields/has-many-field.js.map +1 -0
- package/lib/fields/has-one-field.d.ts +64 -0
- package/lib/fields/has-one-field.js +61 -0
- package/lib/fields/has-one-field.js.map +1 -0
- package/lib/fields/index.d.ts +40 -10
- package/lib/fields/index.js +31 -138
- package/lib/fields/index.js.map +1 -1
- package/lib/fields/json-field.d.ts +14 -0
- package/lib/fields/json-field.js +22 -0
- package/lib/fields/json-field.js.map +1 -0
- package/lib/fields/number-field.d.ts +32 -0
- package/lib/fields/number-field.js +36 -0
- package/lib/fields/number-field.js.map +1 -0
- package/lib/fields/password-field.d.ts +21 -0
- package/lib/fields/password-field.js +78 -0
- package/lib/fields/password-field.js.map +1 -0
- package/lib/fields/radio-field.d.ts +14 -0
- package/lib/fields/radio-field.js +53 -0
- package/lib/fields/radio-field.js.map +1 -0
- package/lib/fields/relation-field.d.ts +20 -0
- package/lib/fields/relation-field.js +31 -0
- package/lib/fields/relation-field.js.map +1 -0
- package/lib/fields/sort-field.d.ts +16 -0
- package/lib/fields/sort-field.js +94 -0
- package/lib/fields/sort-field.js.map +1 -0
- package/lib/fields/string-field.d.ts +8 -0
- package/lib/fields/string-field.js +12 -0
- package/lib/fields/string-field.js.map +1 -0
- package/lib/fields/text-field.d.ts +8 -0
- package/lib/fields/text-field.js +12 -0
- package/lib/fields/text-field.js.map +1 -0
- package/lib/fields/time-field.d.ts +8 -0
- package/lib/fields/time-field.js +12 -0
- package/lib/fields/time-field.js.map +1 -0
- package/lib/fields/uid-field.d.ts +10 -0
- package/lib/fields/uid-field.js +31 -0
- package/lib/fields/uid-field.js.map +1 -0
- package/lib/fields/virtual-field.d.ts +8 -0
- package/lib/fields/virtual-field.js +12 -0
- package/lib/fields/virtual-field.js.map +1 -0
- package/lib/filter-parser.d.ts +27 -0
- package/lib/filter-parser.js +191 -0
- package/lib/filter-parser.js.map +1 -0
- package/lib/index.d.ts +13 -6
- package/lib/index.js +27 -60
- package/lib/index.js.map +1 -1
- package/lib/magic-attribute-model.d.ts +7 -0
- package/lib/magic-attribute-model.js +77 -0
- package/lib/magic-attribute-model.js.map +1 -0
- package/lib/mock-database.d.ts +22 -0
- package/lib/mock-database.js +40 -0
- package/lib/mock-database.js.map +1 -0
- package/lib/model-hook.d.ts +12 -0
- package/lib/model-hook.js +68 -0
- package/lib/model-hook.js.map +1 -0
- package/lib/model.d.ts +11 -45
- package/lib/model.js +76 -452
- package/lib/model.js.map +1 -1
- package/lib/operators/array.d.ts +26 -0
- package/lib/operators/array.js +107 -0
- package/lib/operators/array.js.map +1 -0
- package/lib/operators/association.d.ts +10 -0
- package/lib/operators/association.js +16 -0
- package/lib/operators/association.js.map +1 -0
- package/lib/operators/date.d.ts +34 -0
- package/lib/operators/date.js +40 -0
- package/lib/operators/date.js.map +1 -0
- package/lib/operators/empty.d.ts +28 -0
- package/lib/operators/empty.js +82 -0
- package/lib/operators/empty.js.map +1 -0
- package/lib/operators/index.d.ts +2 -0
- package/lib/operators/index.js +4 -0
- package/lib/operators/index.js.map +1 -0
- package/lib/operators/ne.d.ts +10 -0
- package/lib/operators/ne.js +14 -0
- package/lib/operators/ne.js.map +1 -0
- package/lib/operators/string.d.ts +21 -0
- package/lib/operators/string.js +37 -0
- package/lib/operators/string.js.map +1 -0
- package/lib/operators/utils.d.ts +4 -0
- package/lib/operators/utils.js +16 -0
- package/lib/operators/utils.js.map +1 -0
- package/lib/options-parser.d.ts +31 -0
- package/lib/options-parser.js +232 -0
- package/lib/options-parser.js.map +1 -0
- package/lib/playground.d.ts +1 -0
- package/lib/playground.js +55 -0
- package/lib/playground.js.map +1 -0
- package/lib/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/lib/relation-repository/belongs-to-many-repository.js +206 -0
- package/lib/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/lib/relation-repository/belongs-to-repository.d.ts +17 -0
- package/lib/relation-repository/belongs-to-repository.js +8 -0
- package/lib/relation-repository/belongs-to-repository.js.map +1 -0
- package/lib/relation-repository/hasmany-repository.d.ts +23 -0
- package/lib/relation-repository/hasmany-repository.js +129 -0
- package/lib/relation-repository/hasmany-repository.js.map +1 -0
- package/lib/relation-repository/hasone-repository.d.ts +17 -0
- package/lib/relation-repository/hasone-repository.js +8 -0
- package/lib/relation-repository/hasone-repository.js.map +1 -0
- package/lib/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/lib/relation-repository/multiple-relation-repository.js +153 -0
- package/lib/relation-repository/multiple-relation-repository.js.map +1 -0
- package/lib/relation-repository/relation-repository.d.ts +32 -0
- package/lib/relation-repository/relation-repository.js +100 -0
- package/lib/relation-repository/relation-repository.js.map +1 -0
- package/lib/relation-repository/single-relation-repository.d.ts +23 -0
- package/lib/relation-repository/single-relation-repository.js +103 -0
- package/lib/relation-repository/single-relation-repository.js.map +1 -0
- package/lib/relation-repository/types.d.ts +7 -0
- package/lib/relation-repository/types.js +3 -0
- package/lib/relation-repository/types.js.map +1 -0
- package/lib/repository.d.ts +165 -0
- package/lib/repository.js +302 -0
- package/lib/repository.js.map +1 -0
- package/lib/transaction-decorator.d.ts +1 -0
- package/lib/transaction-decorator.js +70 -0
- package/lib/transaction-decorator.js.map +1 -0
- package/lib/update-associations.d.ts +60 -0
- package/lib/update-associations.js +374 -0
- package/lib/update-associations.js.map +1 -0
- package/lib/update-guard.d.ts +26 -0
- package/lib/update-guard.js +129 -0
- package/lib/update-guard.js.map +1 -0
- package/package.json +17 -5
- package/src/__tests__/collection-importer.test.ts +21 -0
- package/src/__tests__/collection.sortable.test.ts +65 -0
- package/src/__tests__/collection.test.ts +218 -0
- package/src/__tests__/database.import.test.ts +33 -0
- package/src/__tests__/database.test.ts +229 -0
- package/src/__tests__/field-options/hidden.test.ts +302 -0
- package/src/__tests__/field-options/sort-by.test.ts +220 -0
- package/src/__tests__/fields/belongs-to-field.test.ts +162 -0
- package/src/__tests__/fields/belongs-to-many-field.test.ts +61 -0
- package/src/__tests__/fields/context-field.test.ts +140 -0
- package/src/__tests__/fields/has-many-field.test.ts +152 -0
- package/src/__tests__/fields/has-one-field.test.ts +67 -0
- package/src/__tests__/fields/password-field.test.ts +30 -0
- package/src/__tests__/fields/sort-field.test.ts +133 -0
- package/src/__tests__/fields/string-field.test.ts +77 -0
- package/src/__tests__/filter-parser.test.ts +104 -0
- package/src/__tests__/fixtures/c0/a.ts +6 -0
- package/src/__tests__/fixtures/c1/b.ts +6 -0
- package/src/__tests__/fixtures/c2/a.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/extend.ts +6 -0
- package/src/__tests__/fixtures/collections/extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/posts.ts +4 -0
- package/src/__tests__/fixtures/collections/tags.js +4 -0
- package/src/__tests__/fixtures/collections/test.jpg +0 -0
- package/src/__tests__/fixtures/collections/user.json +9 -0
- package/src/__tests__/index.ts +1 -0
- package/src/__tests__/magic-attribute-model.test.ts +103 -0
- package/src/__tests__/model.test.ts +60 -0
- package/src/__tests__/operator/array-operator.test.ts +268 -0
- package/src/__tests__/operator/association-operator.test.ts +263 -0
- package/src/__tests__/operator/date-operator.test.ts +165 -0
- package/src/__tests__/operator/empty-operator.test.ts +77 -0
- package/src/__tests__/operator/ne.test.ts +33 -0
- package/src/__tests__/operator/string-operator.test.ts +62 -0
- package/src/__tests__/option-parser.test.ts +185 -0
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +697 -0
- package/src/__tests__/relation-repository/has-many-repository.test.ts +414 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +77 -0
- package/src/__tests__/repository/count.test.ts +180 -0
- package/src/__tests__/repository/create.test.ts +163 -0
- package/src/__tests__/repository/destroy.test.ts +196 -0
- package/src/__tests__/repository/find.test.ts +247 -0
- package/src/__tests__/repository/update.test.ts +60 -0
- package/src/__tests__/repository.test.ts +438 -0
- package/src/__tests__/update-associations.test.ts +412 -0
- package/src/__tests__/update-guard.test.ts +376 -0
- package/src/collection-importer.ts +49 -0
- package/src/collection.ts +282 -0
- package/src/database.ts +340 -0
- package/src/fields/array-field.ts +35 -0
- package/src/fields/belongs-to-field.ts +76 -0
- package/src/fields/belongs-to-many-field.ts +77 -0
- package/src/fields/boolean-field.ts +12 -0
- package/src/fields/context-field.ts +45 -0
- package/src/fields/date-field.ts +12 -0
- package/src/fields/field.ts +105 -0
- package/src/fields/has-inverse-field.ts +5 -0
- package/src/fields/has-many-field.ts +143 -0
- package/src/fields/has-one-field.ts +136 -0
- package/src/fields/index.ts +72 -0
- package/src/fields/json-field.ts +25 -0
- package/src/fields/number-field.ts +52 -0
- package/src/fields/password-field.ts +72 -0
- package/src/fields/radio-field.ts +50 -0
- package/src/fields/relation-field.ts +37 -0
- package/src/fields/sort-field.ts +96 -0
- package/src/fields/string-field.ts +12 -0
- package/src/fields/text-field.ts +12 -0
- package/src/fields/time-field.ts +12 -0
- package/src/fields/uid-field.ts +24 -0
- package/src/fields/virtual-field.ts +12 -0
- package/src/filter-parser.ts +243 -0
- package/src/index.ts +16 -0
- package/src/magic-attribute-model.ts +62 -0
- package/src/mock-database.ts +42 -0
- package/src/model-hook.ts +69 -0
- package/src/model.ts +114 -0
- package/src/operators/array.ts +145 -0
- package/src/operators/association.ts +14 -0
- package/src/operators/date.ts +41 -0
- package/src/operators/empty.ts +75 -0
- package/src/operators/index.ts +8 -0
- package/src/operators/ne.ts +12 -0
- package/src/operators/string.ts +40 -0
- package/src/operators/utils.ts +13 -0
- package/src/options-parser.ts +285 -0
- package/src/playground.ts +52 -0
- package/src/relation-repository/belongs-to-many-repository.ts +240 -0
- package/src/relation-repository/belongs-to-repository.ts +23 -0
- package/src/relation-repository/hasmany-repository.ts +145 -0
- package/src/relation-repository/hasone-repository.ts +23 -0
- package/src/relation-repository/multiple-relation-repository.ts +198 -0
- package/src/relation-repository/relation-repository.ts +114 -0
- package/src/relation-repository/single-relation-repository.ts +99 -0
- package/src/relation-repository/types.ts +15 -0
- package/src/repository.ts +478 -0
- package/src/transaction-decorator.ts +58 -0
- package/src/update-associations.ts +478 -0
- package/src/update-guard.ts +167 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +5 -0
- package/examples/index.ts +0 -125
- package/examples/plugins/db-driven/index.ts +0 -25
- package/examples/plugins/db-driven/tables/fields.ts +0 -78
- package/examples/plugins/db-driven/tables/tables.ts +0 -53
- package/examples/tables/bar.js +0 -26
- package/examples/tables/comments.ts +0 -19
- package/examples/tables/foo.json +0 -3
- package/examples/tables/posts.ts +0 -28
- package/examples/tables/profiles.ts +0 -23
- package/examples/tables/tags.ts +0 -15
- package/examples/tables/users.ts +0 -34
- package/lib/database.d.ts.map +0 -1
- package/lib/fields/field-types.d.ts +0 -419
- package/lib/fields/field-types.d.ts.map +0 -1
- package/lib/fields/field-types.js +0 -1222
- package/lib/fields/field-types.js.map +0 -1
- package/lib/fields/index.d.ts.map +0 -1
- package/lib/fields/option-types.d.ts +0 -105
- package/lib/fields/option-types.d.ts.map +0 -1
- package/lib/fields/option-types.js +0 -18
- package/lib/fields/option-types.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/model.d.ts.map +0 -1
- package/lib/op.d.ts +0 -45
- package/lib/op.d.ts.map +0 -1
- package/lib/op.js +0 -225
- package/lib/op.js.map +0 -1
- package/lib/table.d.ts +0 -56
- package/lib/table.d.ts.map +0 -1
- package/lib/table.js +0 -456
- package/lib/table.js.map +0 -1
- package/lib/utils.d.ts +0 -26
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -438
- package/lib/utils.js.map +0 -1
package/src/database.ts
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
|
2
|
+
import merge from 'deepmerge';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import lodash from 'lodash';
|
|
5
|
+
import { ModelCtor, Op, Options, QueryInterfaceDropAllTablesOptions, Sequelize, SyncOptions, Utils } from 'sequelize';
|
|
6
|
+
import { Collection, CollectionOptions, RepositoryType } from './collection';
|
|
7
|
+
import { ImporterReader, ImportFileExtension } from './collection-importer';
|
|
8
|
+
import * as FieldTypes from './fields';
|
|
9
|
+
import { Field, FieldContext, RelationField } from './fields';
|
|
10
|
+
import { Model } from './model';
|
|
11
|
+
import { ModelHook } from './model-hook';
|
|
12
|
+
import extendOperators from './operators';
|
|
13
|
+
import { RelationRepository } from './relation-repository/relation-repository';
|
|
14
|
+
import { Repository } from './repository';
|
|
15
|
+
|
|
16
|
+
export interface MergeOptions extends merge.Options {}
|
|
17
|
+
|
|
18
|
+
export interface PendingOptions {
|
|
19
|
+
field: RelationField;
|
|
20
|
+
model: ModelCtor<Model>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface MapOf<T> {
|
|
24
|
+
[key: string]: T;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IDatabaseOptions extends Options {
|
|
28
|
+
tablePrefix?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type DatabaseOptions = IDatabaseOptions | Sequelize;
|
|
32
|
+
|
|
33
|
+
interface RegisterOperatorsContext {
|
|
34
|
+
db?: Database;
|
|
35
|
+
path?: string;
|
|
36
|
+
field?: Field;
|
|
37
|
+
app?: any;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface CleanOptions extends QueryInterfaceDropAllTablesOptions {
|
|
41
|
+
drop?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type OperatorFunc = (value: any, ctx?: RegisterOperatorsContext) => any;
|
|
45
|
+
|
|
46
|
+
export class Database extends EventEmitter implements AsyncEmitter {
|
|
47
|
+
sequelize: Sequelize;
|
|
48
|
+
fieldTypes = new Map();
|
|
49
|
+
options: IDatabaseOptions;
|
|
50
|
+
models = new Map<string, ModelCtor<Model>>();
|
|
51
|
+
repositories = new Map<string, RepositoryType>();
|
|
52
|
+
operators = new Map();
|
|
53
|
+
collections = new Map<string, Collection>();
|
|
54
|
+
pendingFields = new Map<string, RelationField[]>();
|
|
55
|
+
modelCollection = new Map<ModelCtor<any>, Collection>();
|
|
56
|
+
|
|
57
|
+
modelHook: ModelHook;
|
|
58
|
+
|
|
59
|
+
delayCollectionExtend = new Map<string, { collectionOptions: CollectionOptions; mergeOptions?: any }[]>();
|
|
60
|
+
|
|
61
|
+
constructor(options: DatabaseOptions) {
|
|
62
|
+
super();
|
|
63
|
+
|
|
64
|
+
if (options instanceof Sequelize) {
|
|
65
|
+
this.sequelize = options;
|
|
66
|
+
} else {
|
|
67
|
+
this.sequelize = new Sequelize(options);
|
|
68
|
+
this.options = options;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.collections = new Map();
|
|
72
|
+
this.modelHook = new ModelHook(this);
|
|
73
|
+
|
|
74
|
+
this.on('afterDefineCollection', (collection: Collection) => {
|
|
75
|
+
// after collection defined, call bind method on pending fields
|
|
76
|
+
this.pendingFields.get(collection.name)?.forEach((field) => field.bind());
|
|
77
|
+
this.delayCollectionExtend.get(collection.name)?.forEach((collectionExtend) => {
|
|
78
|
+
collection.updateOptions(collectionExtend.collectionOptions, collectionExtend.mergeOptions);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// register database field types
|
|
83
|
+
for (const [name, field] of Object.entries(FieldTypes)) {
|
|
84
|
+
if (['Field', 'RelationField'].includes(name)) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
let key = name.replace(/Field$/g, '');
|
|
88
|
+
key = key.substring(0, 1).toLowerCase() + key.substring(1);
|
|
89
|
+
this.registerFieldTypes({
|
|
90
|
+
[key]: field,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
this.initOperators();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Add collection to database
|
|
99
|
+
* @param options
|
|
100
|
+
*/
|
|
101
|
+
collection<Attributes = any, CreateAttributes = Attributes>(
|
|
102
|
+
options: CollectionOptions,
|
|
103
|
+
): Collection<Attributes, CreateAttributes> {
|
|
104
|
+
this.emit('beforeDefineCollection', options);
|
|
105
|
+
|
|
106
|
+
const collection = new Collection(options, {
|
|
107
|
+
database: this,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
this.collections.set(collection.name, collection);
|
|
111
|
+
this.modelCollection.set(collection.model, collection);
|
|
112
|
+
|
|
113
|
+
this.emit('afterDefineCollection', collection);
|
|
114
|
+
|
|
115
|
+
return collection;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getTablePrefix() {
|
|
119
|
+
return this.options.tablePrefix || '';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* get exists collection by its name
|
|
124
|
+
* @param name
|
|
125
|
+
*/
|
|
126
|
+
getCollection(name: string): Collection {
|
|
127
|
+
return this.collections.get(name);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
hasCollection(name: string): boolean {
|
|
131
|
+
return this.collections.has(name);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
removeCollection(name: string) {
|
|
135
|
+
const collection = this.collections.get(name);
|
|
136
|
+
this.emit('beforeRemoveCollection', collection);
|
|
137
|
+
|
|
138
|
+
const result = this.collections.delete(name);
|
|
139
|
+
|
|
140
|
+
if (result) {
|
|
141
|
+
this.emit('afterRemoveCollection', collection);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getModel<M extends Model>(name: string) {
|
|
146
|
+
return this.getCollection(name).model as ModelCtor<M>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getRepository<R extends Repository>(name: string): R;
|
|
150
|
+
getRepository<R extends RelationRepository>(name: string, relationId: string | number): R;
|
|
151
|
+
|
|
152
|
+
getRepository<R extends RelationRepository>(name: string, relationId?: string | number): Repository | R {
|
|
153
|
+
if (relationId) {
|
|
154
|
+
const [collection, relation] = name.split('.');
|
|
155
|
+
return this.getRepository(collection)?.relation(relation)?.of(relationId) as R;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return this.getCollection(name)?.repository;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
addPendingField(field: RelationField) {
|
|
162
|
+
const associating = this.pendingFields;
|
|
163
|
+
const items = this.pendingFields.get(field.target) || [];
|
|
164
|
+
items.push(field);
|
|
165
|
+
associating.set(field.target, items);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
removePendingField(field: RelationField) {
|
|
169
|
+
const items = this.pendingFields.get(field.target) || [];
|
|
170
|
+
const index = items.indexOf(field);
|
|
171
|
+
if (index !== -1) {
|
|
172
|
+
delete items[index];
|
|
173
|
+
this.pendingFields.set(field.target, items);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
registerFieldTypes(fieldTypes: MapOf<typeof Field>) {
|
|
178
|
+
for (const [type, fieldType] of Object.entries(fieldTypes)) {
|
|
179
|
+
this.fieldTypes.set(type, fieldType);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
registerModels(models: MapOf<ModelCtor<any>>) {
|
|
184
|
+
for (const [type, schemaType] of Object.entries(models)) {
|
|
185
|
+
this.models.set(type, schemaType);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
registerRepositories(repositories: MapOf<RepositoryType>) {
|
|
190
|
+
for (const [type, schemaType] of Object.entries(repositories)) {
|
|
191
|
+
this.repositories.set(type, schemaType);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
initOperators() {
|
|
196
|
+
const operators = new Map();
|
|
197
|
+
|
|
198
|
+
// Sequelize 内置
|
|
199
|
+
for (const key in Op) {
|
|
200
|
+
operators.set('$' + key, Op[key]);
|
|
201
|
+
const val = Utils.underscoredIf(key, true);
|
|
202
|
+
operators.set('$' + val, Op[key]);
|
|
203
|
+
operators.set('$' + val.replace(/_/g, ''), Op[key]);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
this.operators = operators;
|
|
207
|
+
|
|
208
|
+
this.registerOperators({
|
|
209
|
+
...extendOperators,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
registerOperators(operators: MapOf<OperatorFunc>) {
|
|
214
|
+
for (const [key, operator] of Object.entries(operators)) {
|
|
215
|
+
this.operators.set(key, operator);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
buildField(options, context: FieldContext) {
|
|
220
|
+
const { type } = options;
|
|
221
|
+
const Field = this.fieldTypes.get(type);
|
|
222
|
+
if (!Field) {
|
|
223
|
+
throw Error(`unsupported field type ${type}`);
|
|
224
|
+
}
|
|
225
|
+
return new Field(options, context);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async sync(options?: SyncOptions) {
|
|
229
|
+
const isMySQL = this.sequelize.getDialect() === 'mysql';
|
|
230
|
+
if (isMySQL) {
|
|
231
|
+
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 0', null);
|
|
232
|
+
}
|
|
233
|
+
const result = await this.sequelize.sync(options);
|
|
234
|
+
if (isMySQL) {
|
|
235
|
+
await this.sequelize.query('SET FOREIGN_KEY_CHECKS = 1', null);
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async clean(options: CleanOptions) {
|
|
241
|
+
const { drop, ...others } = options;
|
|
242
|
+
if (drop) {
|
|
243
|
+
await this.sequelize.getQueryInterface().dropAllTables(others);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
public isSqliteMemory() {
|
|
248
|
+
return this.sequelize.getDialect() === 'sqlite' && lodash.get(this.options, 'storage') == ':memory:';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async reconnect() {
|
|
252
|
+
if (this.isSqliteMemory()) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
// @ts-ignore
|
|
256
|
+
const ConnectionManager = this.sequelize.dialect.connectionManager.constructor;
|
|
257
|
+
// @ts-ignore
|
|
258
|
+
const connectionManager = new ConnectionManager(this.sequelize.dialect, this.sequelize);
|
|
259
|
+
// @ts-ignore
|
|
260
|
+
this.sequelize.dialect.connectionManager = connectionManager;
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
this.sequelize.connectionManager = connectionManager;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
closed() {
|
|
266
|
+
// @ts-ignore
|
|
267
|
+
return this.sequelize.connectionManager.pool._draining;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async close() {
|
|
271
|
+
if (this.isSqliteMemory()) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return this.sequelize.close();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
on(event: string | symbol, listener: (...args: any[]) => void): this {
|
|
279
|
+
const modelEventName = this.modelHook.isModelHook(event);
|
|
280
|
+
|
|
281
|
+
if (modelEventName && !this.modelHook.hasBindEvent(modelEventName)) {
|
|
282
|
+
this.sequelize.addHook(modelEventName, this.modelHook.sequelizeHookBuilder(modelEventName));
|
|
283
|
+
|
|
284
|
+
this.modelHook.bindEvent(modelEventName);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return super.on(event, listener);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async import(options: { directory: string; extensions?: ImportFileExtension[] }): Promise<Map<string, Collection>> {
|
|
291
|
+
const reader = new ImporterReader(options.directory, options.extensions);
|
|
292
|
+
const modules = await reader.read();
|
|
293
|
+
const result = new Map<string, Collection>();
|
|
294
|
+
|
|
295
|
+
for (const module of modules) {
|
|
296
|
+
if (module.extend) {
|
|
297
|
+
const collectionName = module.collectionOptions.name;
|
|
298
|
+
const existCollection = this.getCollection(collectionName);
|
|
299
|
+
if (existCollection) {
|
|
300
|
+
existCollection.updateOptions(module.collectionOptions, module.mergeOptions);
|
|
301
|
+
} else {
|
|
302
|
+
const existDelayExtends = this.delayCollectionExtend.get(collectionName) || [];
|
|
303
|
+
|
|
304
|
+
this.delayCollectionExtend.set(collectionName, [...existDelayExtends, module]);
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
const collection = this.collection(module);
|
|
308
|
+
result.set(collection.name, collection);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export function extend(collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) {
|
|
319
|
+
return {
|
|
320
|
+
collectionOptions,
|
|
321
|
+
mergeOptions,
|
|
322
|
+
extend: true,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export const defineCollection = (collectionOptions: CollectionOptions) => {
|
|
327
|
+
return collectionOptions;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
export const extendCollection = (collectionOptions: CollectionOptions, mergeOptions?: MergeOptions) => {
|
|
331
|
+
return {
|
|
332
|
+
collectionOptions,
|
|
333
|
+
mergeOptions,
|
|
334
|
+
extend: true,
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
applyMixins(Database, [AsyncEmitter]);
|
|
339
|
+
|
|
340
|
+
export default Database;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
2
|
+
import { DataTypes } from 'sequelize';
|
|
3
|
+
|
|
4
|
+
export class ArrayField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
if (this.database.sequelize.getDialect() === 'postgres') {
|
|
7
|
+
return DataTypes.JSONB;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return DataTypes.JSON;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
sortValue(model) {
|
|
14
|
+
const oldValue = model.get(this.options.name);
|
|
15
|
+
|
|
16
|
+
if (oldValue) {
|
|
17
|
+
const newValue = oldValue.sort();
|
|
18
|
+
model.set(this.options.name, newValue);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
bind() {
|
|
23
|
+
super.bind();
|
|
24
|
+
this.on('beforeSave', this.sortValue.bind(this));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
unbind() {
|
|
28
|
+
super.unbind();
|
|
29
|
+
this.off('beforeSave', this.sortValue.bind(this));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ArrayFieldOptions extends BaseColumnFieldOptions {
|
|
34
|
+
type: 'array';
|
|
35
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { omit } from 'lodash';
|
|
2
|
+
import { BelongsToOptions as SequelizeBelongsToOptions, Utils } from 'sequelize';
|
|
3
|
+
import { BaseRelationFieldOptions, RelationField } from './relation-field';
|
|
4
|
+
|
|
5
|
+
export class BelongsToField extends RelationField {
|
|
6
|
+
static type = 'belongsTo';
|
|
7
|
+
|
|
8
|
+
get target() {
|
|
9
|
+
const { target, name } = this.options;
|
|
10
|
+
return target || Utils.pluralize(name);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
bind() {
|
|
14
|
+
const { database, collection } = this.context;
|
|
15
|
+
const Target = this.TargetModel;
|
|
16
|
+
|
|
17
|
+
// if target model not exists, add it to pending field,
|
|
18
|
+
// it will bind later
|
|
19
|
+
if (!Target) {
|
|
20
|
+
database.addPendingField(this);
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (collection.model.associations[this.name]) {
|
|
25
|
+
delete collection.model.associations[this.name];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// define relation on sequelize model
|
|
29
|
+
const association = collection.model.belongsTo(Target, {
|
|
30
|
+
as: this.name,
|
|
31
|
+
...omit(this.options, ['name', 'type', 'target']),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// inverse relation
|
|
35
|
+
// this.TargetModel.hasMany(collection.model);
|
|
36
|
+
|
|
37
|
+
// 建立关系之后从 pending 列表中删除
|
|
38
|
+
database.removePendingField(this);
|
|
39
|
+
|
|
40
|
+
if (!this.options.foreignKey) {
|
|
41
|
+
this.options.foreignKey = association.foreignKey;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!this.options.sourceKey) {
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
this.options.sourceKey = association.sourceKey;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
unbind() {
|
|
53
|
+
const { database, collection } = this.context;
|
|
54
|
+
// 如果关系字段还没建立就删除了,也同步删除待建立关联的关系字段
|
|
55
|
+
database.removePendingField(this);
|
|
56
|
+
// 如果外键没有显式的创建,关系表也无反向关联字段,删除关系时,外键也删除掉
|
|
57
|
+
const tcoll = database.collections.get(this.target);
|
|
58
|
+
const foreignKey = this.options.foreignKey;
|
|
59
|
+
const field1 = collection.getField(foreignKey);
|
|
60
|
+
const field2 = tcoll.findField((field) => {
|
|
61
|
+
return field.type === 'hasMany' && field.foreignKey === foreignKey;
|
|
62
|
+
});
|
|
63
|
+
if (!field1 && !field2) {
|
|
64
|
+
collection.model.removeAttribute(foreignKey);
|
|
65
|
+
}
|
|
66
|
+
// 删掉 model 的关联字段
|
|
67
|
+
delete collection.model.associations[this.name];
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
collection.model.refreshAttributes();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface BelongsToFieldOptions extends BaseRelationFieldOptions, SequelizeBelongsToOptions {
|
|
74
|
+
type: 'belongsTo';
|
|
75
|
+
target?: string;
|
|
76
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { omit } from 'lodash';
|
|
2
|
+
import { BelongsToManyOptions as SequelizeBelongsToManyOptions, Utils } from 'sequelize';
|
|
3
|
+
import { Collection } from '../collection';
|
|
4
|
+
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
|
|
5
|
+
|
|
6
|
+
export class BelongsToManyField extends RelationField {
|
|
7
|
+
get through() {
|
|
8
|
+
return (
|
|
9
|
+
this.options.through ||
|
|
10
|
+
Utils.camelize(
|
|
11
|
+
[this.context.collection.model.name, this.target]
|
|
12
|
+
.map((name) => name.toLowerCase())
|
|
13
|
+
.sort()
|
|
14
|
+
.join('_'),
|
|
15
|
+
)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
bind() {
|
|
20
|
+
const { database, collection } = this.context;
|
|
21
|
+
const Target = this.TargetModel;
|
|
22
|
+
if (!Target) {
|
|
23
|
+
database.addPendingField(this);
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const through = this.through;
|
|
27
|
+
|
|
28
|
+
let Through: Collection;
|
|
29
|
+
|
|
30
|
+
if (database.hasCollection(through)) {
|
|
31
|
+
Through = database.getCollection(through);
|
|
32
|
+
} else {
|
|
33
|
+
Through = database.collection({
|
|
34
|
+
name: through,
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(Through.model, 'isThrough', { value: true });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const association = collection.model.belongsToMany(Target, {
|
|
40
|
+
...omit(this.options, ['name', 'type', 'target']),
|
|
41
|
+
as: this.name,
|
|
42
|
+
through: Through.model,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// 建立关系之后从 pending 列表中删除
|
|
46
|
+
database.removePendingField(this);
|
|
47
|
+
|
|
48
|
+
if (!this.options.foreignKey) {
|
|
49
|
+
this.options.foreignKey = association.foreignKey;
|
|
50
|
+
}
|
|
51
|
+
if (!this.options.sourceKey) {
|
|
52
|
+
this.options.sourceKey = association.sourceKey;
|
|
53
|
+
}
|
|
54
|
+
if (!this.options.otherKey) {
|
|
55
|
+
this.options.otherKey = association.otherKey;
|
|
56
|
+
}
|
|
57
|
+
if (!this.options.through) {
|
|
58
|
+
this.options.through = this.through;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
unbind() {
|
|
64
|
+
const { database, collection } = this.context;
|
|
65
|
+
// 如果关系字段还没建立就删除了,也同步删除待建立关联的关系字段
|
|
66
|
+
database.removePendingField(this);
|
|
67
|
+
// 删掉 model 的关联字段
|
|
68
|
+
delete collection.model.associations[this.name];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface BelongsToManyFieldOptions
|
|
73
|
+
extends MultipleRelationFieldOptions,
|
|
74
|
+
Omit<SequelizeBelongsToManyOptions, 'through'> {
|
|
75
|
+
type: 'belongsToMany';
|
|
76
|
+
through?: string;
|
|
77
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize';
|
|
2
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
|
+
|
|
4
|
+
export class BooleanField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
return DataTypes.BOOLEAN;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface BooleanFieldOptions extends BaseColumnFieldOptions {
|
|
11
|
+
type: 'boolean';
|
|
12
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { lodash } from '@umijs/utils';
|
|
2
|
+
import { DataTypes } from 'sequelize';
|
|
3
|
+
import { Model } from '../model';
|
|
4
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
5
|
+
|
|
6
|
+
export class ContextField extends Field {
|
|
7
|
+
get dataType() {
|
|
8
|
+
const type: string = this.options.dataType || 'string';
|
|
9
|
+
return DataTypes[type.toUpperCase()] || DataTypes.STRING;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
init() {
|
|
13
|
+
const { name, dataIndex } = this.options;
|
|
14
|
+
this.listener = async (model: Model, options) => {
|
|
15
|
+
const { context } = options;
|
|
16
|
+
model.set(name, lodash.get(context, dataIndex));
|
|
17
|
+
model.changed(name, true);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
bind() {
|
|
22
|
+
super.bind();
|
|
23
|
+
const { createOnly } = this.options;
|
|
24
|
+
this.on('beforeCreate', this.listener);
|
|
25
|
+
if (!createOnly) {
|
|
26
|
+
this.on('beforeUpdate', this.listener);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
unbind() {
|
|
31
|
+
super.unbind();
|
|
32
|
+
const { createOnly } = this.options;
|
|
33
|
+
this.off('beforeCreate', this.listener);
|
|
34
|
+
if (!createOnly) {
|
|
35
|
+
this.off('beforeUpdate', this.listener);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface ContextFieldOptions extends BaseColumnFieldOptions {
|
|
41
|
+
type: 'context';
|
|
42
|
+
dataIndex: string;
|
|
43
|
+
dataType?: string;
|
|
44
|
+
createOnly?: boolean;
|
|
45
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize';
|
|
2
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
|
+
|
|
4
|
+
export class DateField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
return DataTypes.DATE;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface DateFieldOptions extends BaseColumnFieldOptions {
|
|
11
|
+
type: 'date';
|
|
12
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import _ from 'lodash';
|
|
2
|
+
import { DataType, ModelAttributeColumnOptions, ModelIndexesOptions, SyncOptions } from 'sequelize';
|
|
3
|
+
import { Collection } from '../collection';
|
|
4
|
+
import { Database } from '../database';
|
|
5
|
+
|
|
6
|
+
export interface FieldContext {
|
|
7
|
+
database: Database;
|
|
8
|
+
collection: Collection;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface BaseFieldOptions {
|
|
12
|
+
name?: string;
|
|
13
|
+
hidden?: boolean;
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface BaseColumnFieldOptions extends BaseFieldOptions, Omit<ModelAttributeColumnOptions, 'type'> {
|
|
18
|
+
dataType?: DataType;
|
|
19
|
+
index?: boolean | ModelIndexesOptions;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export abstract class Field {
|
|
23
|
+
options: any;
|
|
24
|
+
context: FieldContext;
|
|
25
|
+
database: Database;
|
|
26
|
+
collection: Collection;
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
|
|
29
|
+
get name() {
|
|
30
|
+
return this.options.name;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get type() {
|
|
34
|
+
return this.options.type;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get dataType() {
|
|
38
|
+
return this.options.dataType;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
constructor(options?: any, context?: FieldContext) {
|
|
42
|
+
this.context = context;
|
|
43
|
+
this.database = context.database;
|
|
44
|
+
this.collection = context.collection;
|
|
45
|
+
this.options = options || {};
|
|
46
|
+
this.init();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// TODO
|
|
50
|
+
async sync(syncOptions: SyncOptions) {
|
|
51
|
+
await this.collection.sync({
|
|
52
|
+
...syncOptions,
|
|
53
|
+
force: false,
|
|
54
|
+
alter: {
|
|
55
|
+
drop: false,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
init() {
|
|
61
|
+
// code
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
on(eventName: string, listener: (...args: any[]) => void) {
|
|
65
|
+
this.database.on(`${this.collection.name}.${eventName}`, listener);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
off(eventName: string, listener: (...args: any[]) => void) {
|
|
70
|
+
this.database.off(`${this.collection.name}.${eventName}`, listener);
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get(name: string) {
|
|
75
|
+
return this.options[name];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
merge(obj: any) {
|
|
79
|
+
Object.assign(this.options, obj);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
bind() {
|
|
83
|
+
const { model } = this.context.collection;
|
|
84
|
+
model.rawAttributes[this.name] = this.toSequelize();
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
model.refreshAttributes();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
unbind() {
|
|
90
|
+
const { model } = this.context.collection;
|
|
91
|
+
model.removeAttribute(this.name);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
toSequelize(): any {
|
|
95
|
+
const opts = _.omit(this.options, ['name']);
|
|
96
|
+
if (this.dataType) {
|
|
97
|
+
Object.assign(opts, { type: this.dataType });
|
|
98
|
+
}
|
|
99
|
+
return opts;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
isSqlite() {
|
|
103
|
+
return this.database.sequelize.getDialect() === 'sqlite';
|
|
104
|
+
}
|
|
105
|
+
}
|