@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
package/examples/index.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import Database from '../src';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import dbDriven from './plugins/db-driven';
|
|
4
|
-
|
|
5
|
-
const sync = {
|
|
6
|
-
force: true,
|
|
7
|
-
alter: {
|
|
8
|
-
drop: true,
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const db = new Database({
|
|
13
|
-
username: 'test',
|
|
14
|
-
password: 'test',
|
|
15
|
-
database: 'test',
|
|
16
|
-
host: '127.0.0.1',
|
|
17
|
-
port: 45432,
|
|
18
|
-
dialect: 'postgres',
|
|
19
|
-
logging: false,
|
|
20
|
-
define: {
|
|
21
|
-
},
|
|
22
|
-
sync,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
(async () => {
|
|
26
|
-
|
|
27
|
-
const tables = db.import({
|
|
28
|
-
directory: path.resolve(__dirname, 'tables'),
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
await db.sync({ tables });
|
|
32
|
-
|
|
33
|
-
await db.plugin(dbDriven());
|
|
34
|
-
|
|
35
|
-
if (!sync.force) {
|
|
36
|
-
await db.sequelize.drop();
|
|
37
|
-
await db.sync();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const [Table, Field] = db.getModels(['tables', 'fields']);
|
|
41
|
-
|
|
42
|
-
const [table] = await Table.findOrCreate({
|
|
43
|
-
where: {
|
|
44
|
-
name: 'demos',
|
|
45
|
-
},
|
|
46
|
-
defaults: {
|
|
47
|
-
options: {
|
|
48
|
-
name: 'demos',
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
await db.getModel('demos').create({});
|
|
54
|
-
|
|
55
|
-
await Field.bulkCreate([
|
|
56
|
-
{
|
|
57
|
-
name: 'col1',
|
|
58
|
-
table_name: 'demos',
|
|
59
|
-
options: {
|
|
60
|
-
type: 'string',
|
|
61
|
-
name: 'col1',
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
name: 'col2',
|
|
66
|
-
table_name: 'demos',
|
|
67
|
-
options: {
|
|
68
|
-
type: 'string',
|
|
69
|
-
name: 'col2',
|
|
70
|
-
},
|
|
71
|
-
},
|
|
72
|
-
]);
|
|
73
|
-
|
|
74
|
-
await db.getModel('demos').create({
|
|
75
|
-
col1: 'col1',
|
|
76
|
-
col2: 'col2',
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
await table.createField({
|
|
80
|
-
name: 'col3',
|
|
81
|
-
options: {
|
|
82
|
-
type: 'string',
|
|
83
|
-
name: 'col3',
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
await db.getModel('demos').create({
|
|
88
|
-
col1: 'col1',
|
|
89
|
-
col2: 'col2',
|
|
90
|
-
col3: 'col3',
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
await table.createField({
|
|
94
|
-
name: 'col4',
|
|
95
|
-
options: {
|
|
96
|
-
type: 'string',
|
|
97
|
-
name: 'col4',
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
await db.getModel('demos').create({
|
|
102
|
-
col1: 'col1',
|
|
103
|
-
col2: 'col2',
|
|
104
|
-
col3: 'col3',
|
|
105
|
-
col4: 'col4',
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
await table.createField({
|
|
109
|
-
name: 'col5',
|
|
110
|
-
options: {
|
|
111
|
-
type: 'string',
|
|
112
|
-
name: 'col5',
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
await db.getModel('demos').create({
|
|
117
|
-
col1: 'col1',
|
|
118
|
-
col2: 'col2',
|
|
119
|
-
col3: 'col3',
|
|
120
|
-
col4: 'col4',
|
|
121
|
-
col5: 'col5',
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
await db.close();
|
|
125
|
-
})();
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import Database, { Model } from '../../../src';
|
|
3
|
-
|
|
4
|
-
export default (options?: any) => {
|
|
5
|
-
return async (db: Database) => {
|
|
6
|
-
const tables = db.import({
|
|
7
|
-
directory: path.resolve(__dirname, 'tables'),
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
await db.sync({ tables });
|
|
11
|
-
|
|
12
|
-
const Table = db.getModel('tables');
|
|
13
|
-
const items = await Table.findAll();
|
|
14
|
-
|
|
15
|
-
await Promise.all(items.map(async item => {
|
|
16
|
-
const fields: Model[] = await item.getFields();
|
|
17
|
-
const table = db.table({
|
|
18
|
-
...item.options,
|
|
19
|
-
fields: fields.map(field => field.options),
|
|
20
|
-
});
|
|
21
|
-
}));
|
|
22
|
-
|
|
23
|
-
await db.sync({ tables: items.map(item => item.name) });
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import Database, { TableOptions, Table } from '../../../../src';
|
|
2
|
-
|
|
3
|
-
export default (db: Database) => ({
|
|
4
|
-
name: 'fields',
|
|
5
|
-
tableName: 'nocobase_fields',
|
|
6
|
-
fields: [
|
|
7
|
-
{
|
|
8
|
-
type: 'string',
|
|
9
|
-
name: 'table_name',
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
type: 'string',
|
|
13
|
-
name: 'name',
|
|
14
|
-
index: {
|
|
15
|
-
fields: ['table_name', 'name'],
|
|
16
|
-
unique: true,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
type: 'json',
|
|
21
|
-
name: 'options',
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
type: 'belongsTo',
|
|
25
|
-
name: 'table',
|
|
26
|
-
foreignKey: 'table_name',
|
|
27
|
-
targetKey: 'name',
|
|
28
|
-
},
|
|
29
|
-
],
|
|
30
|
-
hooks: {
|
|
31
|
-
async afterCreate(model: any) {
|
|
32
|
-
if (!model.table_name) {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
const table = db.getTable(model.table_name);
|
|
36
|
-
table.addField(model.options);
|
|
37
|
-
// console.log(table);
|
|
38
|
-
await table.sync({
|
|
39
|
-
force: false,
|
|
40
|
-
alter: {
|
|
41
|
-
drop: false,
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
async afterUpdate(model: any) {
|
|
46
|
-
if (!model.table_name) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const table = db.getTable(model.table_name);
|
|
50
|
-
table.addField(model.options);
|
|
51
|
-
await table.sync({
|
|
52
|
-
force: false,
|
|
53
|
-
alter: {
|
|
54
|
-
drop: false,
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
},
|
|
58
|
-
async afterBulkCreate(models) {
|
|
59
|
-
const tables = new Map<string, Table>();
|
|
60
|
-
for (const model of new Map<string, any>(Object.entries(models)).values()) {
|
|
61
|
-
if (!model.table_name) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const table = db.getTable(model.table_name);
|
|
65
|
-
table.addField(model.options);
|
|
66
|
-
tables.set(table.getName(), table);
|
|
67
|
-
}
|
|
68
|
-
for (const table of tables.values()) {
|
|
69
|
-
await table.sync({
|
|
70
|
-
force: false,
|
|
71
|
-
alter: {
|
|
72
|
-
drop: false,
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
}
|
|
78
|
-
} as TableOptions);
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { FindOrCreateOptions } from 'sequelize';
|
|
2
|
-
import Database, { TableOptions, Model } from '../../../../src';
|
|
3
|
-
|
|
4
|
-
export class Table extends Model {
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default (db: Database) => ({
|
|
9
|
-
name: 'tables',
|
|
10
|
-
tableName: 'nocobase_tables',
|
|
11
|
-
model: Table,
|
|
12
|
-
fields: [
|
|
13
|
-
{
|
|
14
|
-
type: 'string',
|
|
15
|
-
name: 'name',
|
|
16
|
-
unique: true,
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
type: 'json',
|
|
20
|
-
name: 'options',
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
type: 'hasMany',
|
|
24
|
-
name: 'fields',
|
|
25
|
-
sourceKey: 'name',
|
|
26
|
-
foreignKey: 'table_name',
|
|
27
|
-
},
|
|
28
|
-
],
|
|
29
|
-
hooks: {
|
|
30
|
-
async afterCreate(model: Table) {
|
|
31
|
-
const fields: Model[] = await model.getFields();
|
|
32
|
-
const table = db.table({...model.options, fields: fields.map(field => field.options)});
|
|
33
|
-
await table.sync();
|
|
34
|
-
},
|
|
35
|
-
async afterUpdate(model: Table) {
|
|
36
|
-
const fields: Model[] = await model.getFields();
|
|
37
|
-
const table = db.table({...model.options, fields: fields.map(field => field.options)});
|
|
38
|
-
await table.sync({
|
|
39
|
-
force: false,
|
|
40
|
-
alter: {
|
|
41
|
-
drop: false,
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
async afterBulkCreate(models: Table[]) {
|
|
46
|
-
await Promise.all(models.map(async (model: any) => {
|
|
47
|
-
const fields: Model[] = await model.getFields();
|
|
48
|
-
const table = db.table({...model.options, fields: fields.map(field => field.options)});
|
|
49
|
-
await table.sync();
|
|
50
|
-
}));
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
} as TableOptions);
|
package/examples/tables/bar.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
name: 'bar',
|
|
3
|
-
fields: [
|
|
4
|
-
{
|
|
5
|
-
type: 'string',
|
|
6
|
-
name: 'title',
|
|
7
|
-
},
|
|
8
|
-
{
|
|
9
|
-
type: 'text',
|
|
10
|
-
length: 'long',
|
|
11
|
-
name: 'content',
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
type: 'belongsTo',
|
|
15
|
-
name: 'user',
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
type: 'belongsToMany',
|
|
19
|
-
name: 'tags',
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
type: 'hasMany',
|
|
23
|
-
name: 'comments'
|
|
24
|
-
},
|
|
25
|
-
],
|
|
26
|
-
};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { TableOptions } from '../../src';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'comments',
|
|
5
|
-
fields: [
|
|
6
|
-
{
|
|
7
|
-
type: 'text',
|
|
8
|
-
name: 'content',
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
type: 'belongsTo',
|
|
12
|
-
name: 'user',
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
type: 'belongsTo',
|
|
16
|
-
name: 'post',
|
|
17
|
-
},
|
|
18
|
-
],
|
|
19
|
-
} as TableOptions;
|
package/examples/tables/foo.json
DELETED
package/examples/tables/posts.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { TableOptions } from '../../src';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'posts',
|
|
5
|
-
fields: [
|
|
6
|
-
{
|
|
7
|
-
type: 'string',
|
|
8
|
-
name: 'title',
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
type: 'text',
|
|
12
|
-
length: 'long',
|
|
13
|
-
name: 'content',
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
type: 'belongsTo',
|
|
17
|
-
name: 'user',
|
|
18
|
-
},
|
|
19
|
-
{
|
|
20
|
-
type: 'belongsToMany',
|
|
21
|
-
name: 'tags',
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
type: 'hasMany',
|
|
25
|
-
name: 'comments'
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
|
-
} as TableOptions;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { TableOptions } from '../../src';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'profiles',
|
|
5
|
-
fields: [
|
|
6
|
-
{
|
|
7
|
-
type: 'string',
|
|
8
|
-
name: 'realname',
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
type: 'string',
|
|
12
|
-
name: 'email',
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
type: 'string',
|
|
16
|
-
name: 'gender',
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
type: 'date',
|
|
20
|
-
name: 'birthday',
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
} as TableOptions;
|
package/examples/tables/tags.ts
DELETED
package/examples/tables/users.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { TableOptions } from '../../src';
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'users',
|
|
5
|
-
fields: [
|
|
6
|
-
{
|
|
7
|
-
type: 'string',
|
|
8
|
-
name: 'username',
|
|
9
|
-
unique: true,
|
|
10
|
-
},
|
|
11
|
-
{
|
|
12
|
-
type: 'string',
|
|
13
|
-
name: 'password',
|
|
14
|
-
// index: true,
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
type: 'string',
|
|
18
|
-
name: 'openid',
|
|
19
|
-
index: true,
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
type: 'hasOne',
|
|
23
|
-
name: 'profile',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
type: 'hasMany',
|
|
27
|
-
name: 'posts',
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
type: 'hasMany',
|
|
31
|
-
name: 'comments',
|
|
32
|
-
}
|
|
33
|
-
],
|
|
34
|
-
} as TableOptions;
|
package/lib/database.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["database.ts"],"names":[],"mappings":"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","file":"database.d.ts","sourcesContent":["import {\n Options,\n Sequelize,\n SyncOptions as SequelizeSyncOptions,\n} from 'sequelize';\nimport glob from 'glob';\nimport Table, { MergeOptions, TableOptions } from './table';\nimport { Model, ModelCtor } from './model';\nimport { requireModule } from './utils';\nimport _ from 'lodash';\nimport { EventEmitter } from 'events';\n\nexport interface SyncOptions extends SequelizeSyncOptions {\n\n /**\n * 指定需要更新字段的 tables\n */\n tables?: string[] | Table[] | Map<string, Table>;\n}\n\nexport interface ImportOptions {\n\n /**\n * 指定配置所在路径\n */\n directory: string;\n\n /**\n * 文件后缀,默认值 ['js', 'ts', 'json']\n */\n extensions?: string[];\n}\n\nexport interface DatabaseOptions extends Options {\n}\n\n// export type HookType = 'beforeTableInit' | 'afterTableInit' | 'beforeAddField' | 'afterAddField';\n\nexport class Extend {\n tableOptions: TableOptions;\n mergeOptions: MergeOptions\n constructor(tableOptions: TableOptions, mergeOptions?: MergeOptions) {\n this.tableOptions = tableOptions;\n this.mergeOptions = mergeOptions;\n }\n}\n\nexport function extend(tableOptions: TableOptions, mergeOptions: MergeOptions = {}) {\n return new Extend(tableOptions, mergeOptions);\n}\n\ntype HookType =\n 'beforeValidate' |\n 'afterValidate' |\n 'beforeCreate' |\n 'afterCreate' |\n 'beforeDestroy' |\n 'afterDestroy' |\n 'beforeRestore' |\n 'afterRestore' |\n 'beforeUpdate' |\n 'afterUpdate' |\n 'beforeSave' |\n 'afterSave' |\n 'beforeBulkCreate' |\n 'afterBulkCreate' |\n 'beforeBulkDestroy' |\n 'afterBulkDestroy' |\n 'beforeBulkRestore' |\n 'afterBulkRestore' |\n 'beforeBulkUpdate' |\n 'afterBulkUpdate' |\n 'beforeSync' |\n 'afterSync' |\n 'beforeBulkSync' |\n 'afterBulkSync' |\n 'beforeDefine' |\n 'afterDefine' |\n 'beforeInit' |\n 'afterInit' |\n 'beforeConnect' |\n 'afterConnect' |\n 'beforeDisconnect' |\n 'afterDisconnect';\n\n export default class Database extends EventEmitter {\n\n public readonly sequelize: Sequelize;\n\n /**\n * 哪些 Model 需要建立表关系\n */\n public readonly associating = new Set<string>();\n\n /**\n * 中间表\n */\n public readonly throughTables = new Map<string, Array<string>>();\n\n protected tables = new Map<string, Table>();\n\n protected options: DatabaseOptions;\n\n protected hooks = {};\n\n protected extTableOptions = new Map<string, any>();\n\n protected hookTypes = new Map(Object.entries({\n beforeValidate: 1,\n afterValidate: 1,\n beforeCreate: 1,\n afterCreate: 1,\n beforeDestroy: 1,\n afterDestroy: 1,\n beforeRestore: 1,\n afterRestore: 1,\n beforeUpdate: 1,\n afterUpdate: 1,\n beforeSave: 1,\n afterSave: 1,\n\n beforeBulkCreate: 2,\n afterBulkCreate: 2,\n\n beforeBulkDestroy: 3,\n afterBulkDestroy: 3,\n beforeBulkRestore: 3,\n afterBulkRestore: 3,\n beforeBulkUpdate: 3,\n afterBulkUpdate: 3,\n\n beforeSync: 4,\n afterSync: 4,\n beforeBulkSync: 4,\n afterBulkSync: 4,\n\n beforeDefine: 0,\n afterDefine: 0,\n beforeInit: 0,\n afterInit: 0,\n beforeConnect: 0,\n afterConnect: 0,\n beforeDisconnect: 0,\n afterDisconnect: 0,\n }));\n\n constructor(options?: DatabaseOptions) {\n super();\n this.options = options;\n this.sequelize = new Sequelize(options);\n }\n\n private _getHookType(event: any) {\n if (typeof event === 'string') {\n event = event.split('.');\n }\n if (!Array.isArray(event)) {\n return;\n }\n const hookType = [...event].pop();\n if (!this.hookTypes.has(hookType)) {\n return;\n }\n return hookType;\n }\n\n on(event: HookType | Omit<string, HookType> | symbol, listener: (...args: any[]) => void) {\n const hookType = this._getHookType(event);\n if (hookType) {\n const state = this.hookTypes.get(hookType);\n\n this.sequelize.addHook(hookType, async (...args: any[]) => {\n let modelName: string;\n switch (state) {\n case 1:\n modelName = args?.[0]?.constructor?.name;\n break;\n case 2:\n modelName = args?.[1]?.model?.name;\n break;\n case 3:\n modelName = args?.[0]?.model?.name;\n break;\n }\n // console.log({ modelName, args });\n if (modelName) {\n await this.emitAsync(`${modelName}.${hookType}`, ...args);\n }\n await this.emitAsync(hookType, ...args);\n });\n this.hookTypes.delete(hookType);\n }\n return super.on(event as any, listener);\n }\n\n async emitAsync(event: string | symbol, ...args: any[]): Promise<boolean> {\n // @ts-ignore\n const events = this._events;\n let callbacks = events?.[event];\n if (!callbacks) {\n return false;\n }\n // helper function to reuse as much code as possible\n const run = (cb) => {\n switch (args.length) {\n // fast cases\n case 0:\n cb = cb.call(this);\n break;\n case 1:\n cb = cb.call(this, args[0]);\n break;\n case 2:\n cb = cb.call(this, args[0], args[1]);\n break;\n case 3:\n cb = cb.call(this, args[0], args[1], args[2]);\n break;\n // slower\n default:\n cb = cb.apply(this, args);\n }\n\n if (\n cb && (\n cb instanceof Promise ||\n typeof cb.then === 'function'\n )\n ) {\n return cb;\n }\n\n return Promise.resolve(true);\n };\n\n if (typeof callbacks === 'function') {\n await run(callbacks);\n } else if (typeof callbacks === 'object') {\n callbacks = callbacks.slice().filter(Boolean);\n await callbacks.reduce((prev, next) => {\n return prev.then((res) => {\n return run(next).then((result) => Promise.resolve(res.concat(result)));\n });\n }, Promise.resolve([]));\n }\n\n return true;\n }\n\n /**\n * 载入指定目录下 tables 配置(配置的文件驱动)\n * \n * TODO: 配置的文件驱动现在会全部初始化,大数据时可能存在性能瓶颈,后续可以加入动态加载\n * \n * @param {object} [options]\n * @param {string} [options.directory] 指定配置所在路径\n * @param {array} [options.extensions = ['js', 'ts', 'json']] 文件后缀\n */\n public import(options: ImportOptions): Map<string, Table> {\n const { extensions = ['js', 'ts', 'json'], directory } = options;\n const patten = `${directory}/*.{${extensions.join(',')}}`;\n const files = glob.sync(patten, {\n ignore: [\n '**/*.d.ts'\n ]\n });\n const tables = new Map<string, Table>();\n files.forEach((file: string) => {\n const result = requireModule(file);\n if (result instanceof Extend) {\n // 如果还没初始化,extend 的先暂存起来,后续处理\n if (!this.tables.has(result.tableOptions.name)) {\n this.extTableOptions.set(result.tableOptions.name, result);\n } else {\n const table = this.extend(result.tableOptions, result.mergeOptions);\n tables.set(table.getName(), table);\n }\n } else {\n let table = this.extend(typeof result === 'function' ? result(this) : result);\n // 如果有未处理的 extend 取回来合并\n if (this.extTableOptions.has(table.getName())) {\n const result = this.extTableOptions.get(table.getName());\n table = this.extend(result.tableOptions, result.mergeOptions);\n this.extTableOptions.delete(table.getName());\n }\n tables.set(table.getName(), table);\n }\n });\n return tables;\n }\n\n /**\n * 配置表\n *\n * @param options \n */\n public table(options: TableOptions): Table {\n const { name } = options;\n const table = new Table(options, { database: this });\n this.tables.set(name, table);\n // 在 source 或 target 之后定义 through,需要更新 source 和 target 的 model\n if (this.throughTables.has(name)) {\n const [sourceTable, targetTable] = this.getTables(this.throughTables.get(name));\n sourceTable && sourceTable.modelInit(true);\n targetTable && targetTable.modelInit(true);\n // this.throughTables.delete(name);\n }\n return table;\n }\n\n /**\n * 扩展配置(实验性 API)\n * \n * @param options \n */\n public extend(options: TableOptions, mergeOptions?: MergeOptions): Table {\n const { name } = options;\n let table: Table;\n if (this.tables.has(name)) {\n table = this.tables.get(name);\n table.extend(options, mergeOptions);\n } else {\n table = this.table(options);\n this.tables.set(name, table);\n }\n return table;\n }\n\n /**\n * 是否已配置\n * \n * @param name \n */\n public isDefined(name: string): boolean {\n return this.sequelize.isDefined(name);\n }\n\n /**\n * 获取 Model\n * \n * TODO: 动态初始化并加载配置(懒汉式)\n * 动态初始化需要支持文件驱动和数据库驱动\n *\n * @param name \n */\n public getModel(name: string): ModelCtor<Model> {\n return this.isDefined(name) ? this.sequelize.model(name) as any : undefined;\n }\n\n /**\n * 获取指定 names 的 Models\n *\n * @param names \n */\n public getModels(names: string[] = []): Array<ModelCtor<Model>> {\n if (names.length === 0) {\n return this.sequelize.models as any;\n }\n return names.map(name => this.getModel(name));\n }\n\n /**\n * 获取 table 配置\n * \n * TODO:\n * 未单独配置多对多中间表时,取不到中间表的 table,但是可以取到 Model\n * 动态初始化并加载配置(懒汉式),动态初始化需要支持文件驱动和数据库驱动\n * \n * @param name \n */\n public getTable(name: string): Table {\n return this.tables.has(name) ? this.tables.get(name) : undefined;\n }\n\n /**\n * 获取指定 names 的 table 配置\n *\n * @param names \n */\n public getTables(names: string[] = []): Array<Table> {\n if (names.length === 0) {\n return [...this.tables.values()];\n }\n return names.map(name => this.getTable(name));\n }\n\n /**\n * 建立表关系\n * \n * 表关系相关字段是在 Model.init 之后进行的\n */\n public associate() {\n for (const name of this.associating) {\n const Model: any = this.getModel(name);\n Model.associate && Model.associate(this.sequelize.models);\n }\n }\n\n /**\n * 插件扩展\n * \n * TODO: 细节待定\n *\n * @param plugin \n * @param options \n */\n public async plugin(plugin: any, options = {}) {\n await plugin(this, options);\n }\n\n /**\n * 表字段更新\n * \n * @param options \n */\n public async sync(options: SyncOptions = {}) {\n const { tables = [], ...restOptions } = options;\n let items: Array<any>;\n\n if (tables instanceof Map) {\n items = Array.from(tables.values());\n } else {\n items = tables;\n }\n\n /**\n * sequelize.sync 只能处理全部 model 的字段更新\n * Model.sync 只能处理当前 Model 的字段更新,不处理关系表\n * database.sync 可以指定 tables 进行字段更新,也可以自动处理关系表的字段更新\n */\n if (items.length > 0) {\n // 指定 tables 时,新建 sequelize 实例来单独处理这些 tables 相关 models 的 sync\n const sequelize = new Sequelize(this.options);\n const names = new Set<string>();\n for (const key in items) {\n let table = items[key];\n if (typeof table === 'string') {\n table = this.getTable(table);\n }\n if (table instanceof Table) {\n for (const name of table.getRelatedTableNames()) {\n names.add(name);\n }\n }\n }\n for (const name of names) {\n // @ts-ignore\n const model = this.getModel(name);\n if (model) {\n sequelize.modelManager.addModel(model);\n }\n }\n await sequelize.sync(restOptions);\n await sequelize.close();\n } else {\n await this.sequelize.sync(restOptions);\n }\n }\n\n /**\n * 关闭数据库连接\n */\n public async close() {\n this.removeAllListeners();\n return this.sequelize.close();\n }\n\n /**\n * 添加 hook\n * \n * @param hookType \n * @param fn \n */\n public addHook(hookType: HookType | string, fn: Function) {\n const hooks = this.hooks[hookType] || [];\n hooks.push(fn);\n this.hooks[hookType] = hooks;\n }\n\n /**\n * 运行 hook\n *\n * @param hookType \n * @param args \n */\n public async runHooks(hookType: HookType | string, ...args) {\n const hooks = this.hooks[hookType] || [];\n for (const hook of hooks) {\n if (typeof hook === 'function') {\n await hook(...args);\n }\n }\n }\n\n public getFieldByPath(fieldPath: string) {\n const [tableName, fieldName] = fieldPath.split('.');\n return this.getTable(tableName).getField(fieldName);\n }\n}\n"]}
|