@nocobase/database 0.5.0-alpha.38 → 0.7.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/esm/collection-importer.d.ts +7 -0
- package/esm/collection-importer.js +49 -0
- package/esm/collection-importer.js.map +1 -0
- package/esm/collection.d.ts +73 -0
- package/esm/collection.js +224 -0
- package/esm/collection.js.map +1 -0
- package/esm/database.d.ts +101 -0
- package/esm/database.js +275 -0
- package/esm/database.js.map +1 -0
- package/esm/fields/array-field.d.ts +11 -0
- package/esm/fields/array-field.js +26 -0
- package/esm/fields/array-field.js.map +1 -0
- package/esm/fields/belongs-to-field.d.ts +12 -0
- package/esm/fields/belongs-to-field.js +57 -0
- package/esm/fields/belongs-to-field.js.map +1 -0
- package/esm/fields/belongs-to-many-field.d.ts +11 -0
- package/esm/fields/belongs-to-many-field.js +55 -0
- package/esm/fields/belongs-to-many-field.js.map +1 -0
- package/esm/fields/boolean-field.d.ts +8 -0
- package/esm/fields/boolean-field.js +8 -0
- package/esm/fields/boolean-field.js.map +1 -0
- package/esm/fields/context-field.d.ts +13 -0
- package/esm/fields/context-field.js +43 -0
- package/esm/fields/context-field.js.map +1 -0
- package/esm/fields/date-field.d.ts +8 -0
- package/esm/fields/date-field.js +8 -0
- package/esm/fields/date-field.js.map +1 -0
- package/esm/fields/field.d.ts +37 -0
- package/esm/fields/field.js +74 -0
- package/esm/fields/field.js.map +1 -0
- package/esm/fields/has-inverse-field.d.ts +4 -0
- package/esm/fields/has-inverse-field.js +2 -0
- package/esm/fields/has-inverse-field.js.map +1 -0
- package/esm/fields/has-many-field.d.ts +64 -0
- package/esm/fields/has-many-field.js +58 -0
- package/esm/fields/has-many-field.js.map +1 -0
- package/esm/fields/has-one-field.d.ts +64 -0
- package/esm/fields/has-one-field.js +57 -0
- package/esm/fields/has-one-field.js.map +1 -0
- package/esm/fields/index.d.ts +40 -0
- package/esm/fields/index.js +21 -0
- package/esm/fields/index.js.map +1 -0
- package/esm/fields/json-field.d.ts +14 -0
- package/esm/fields/json-field.js +17 -0
- package/esm/fields/json-field.js.map +1 -0
- package/esm/fields/number-field.d.ts +32 -0
- package/esm/fields/number-field.js +28 -0
- package/esm/fields/number-field.js.map +1 -0
- package/esm/fields/password-field.d.ts +21 -0
- package/esm/fields/password-field.js +71 -0
- package/esm/fields/password-field.js.map +1 -0
- package/esm/fields/radio-field.d.ts +14 -0
- package/esm/fields/radio-field.js +49 -0
- package/esm/fields/radio-field.js.map +1 -0
- package/esm/fields/relation-field.d.ts +20 -0
- package/esm/fields/relation-field.js +27 -0
- package/esm/fields/relation-field.js.map +1 -0
- package/esm/fields/sort-field.d.ts +16 -0
- package/esm/fields/sort-field.js +90 -0
- package/esm/fields/sort-field.js.map +1 -0
- package/esm/fields/string-field.d.ts +8 -0
- package/esm/fields/string-field.js +8 -0
- package/esm/fields/string-field.js.map +1 -0
- package/esm/fields/text-field.d.ts +8 -0
- package/esm/fields/text-field.js +8 -0
- package/esm/fields/text-field.js.map +1 -0
- package/esm/fields/time-field.d.ts +8 -0
- package/esm/fields/time-field.js +8 -0
- package/esm/fields/time-field.js.map +1 -0
- package/esm/fields/uid-field.d.ts +10 -0
- package/esm/fields/uid-field.js +27 -0
- package/esm/fields/uid-field.js.map +1 -0
- package/esm/fields/virtual-field.d.ts +8 -0
- package/esm/fields/virtual-field.js +8 -0
- package/esm/fields/virtual-field.js.map +1 -0
- package/esm/filter-parser.d.ts +27 -0
- package/esm/filter-parser.js +185 -0
- package/esm/filter-parser.js.map +1 -0
- package/esm/index.d.ts +15 -0
- package/esm/index.js +16 -0
- package/esm/index.js.map +1 -0
- package/esm/magic-attribute-model.d.ts +7 -0
- package/esm/magic-attribute-model.js +70 -0
- package/esm/magic-attribute-model.js.map +1 -0
- package/esm/mock-database.d.ts +22 -0
- package/esm/mock-database.js +34 -0
- package/esm/mock-database.js.map +1 -0
- package/esm/model-hook.d.ts +12 -0
- package/esm/model-hook.js +61 -0
- package/esm/model-hook.js.map +1 -0
- package/esm/model.d.ts +15 -0
- package/esm/model.js +80 -0
- package/esm/model.js.map +1 -0
- package/esm/operators/array.d.ts +26 -0
- package/esm/operators/array.js +105 -0
- package/esm/operators/array.js.map +1 -0
- package/esm/operators/association.d.ts +10 -0
- package/esm/operators/association.js +14 -0
- package/esm/operators/association.js.map +1 -0
- package/esm/operators/date.d.ts +34 -0
- package/esm/operators/date.js +35 -0
- package/esm/operators/date.js.map +1 -0
- package/esm/operators/empty.d.ts +28 -0
- package/esm/operators/empty.js +58 -0
- package/esm/operators/empty.js.map +1 -0
- package/esm/operators/index.d.ts +2 -0
- package/esm/operators/index.js +2 -0
- package/esm/operators/index.js.map +1 -0
- package/esm/operators/ne.d.ts +10 -0
- package/esm/operators/ne.js +12 -0
- package/esm/operators/ne.js.map +1 -0
- package/esm/operators/string.d.ts +21 -0
- package/esm/operators/string.js +35 -0
- package/esm/operators/string.js.map +1 -0
- package/esm/operators/utils.d.ts +4 -0
- package/esm/operators/utils.js +11 -0
- package/esm/operators/utils.js.map +1 -0
- package/esm/options-parser.d.ts +31 -0
- package/esm/options-parser.js +225 -0
- package/esm/options-parser.js.map +1 -0
- package/esm/playground.d.ts +1 -0
- package/esm/playground.js +53 -0
- package/esm/playground.js.map +1 -0
- package/esm/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/esm/relation-repository/belongs-to-many-repository.js +199 -0
- package/esm/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/esm/relation-repository/belongs-to-repository.d.ts +17 -0
- package/esm/relation-repository/belongs-to-repository.js +4 -0
- package/esm/relation-repository/belongs-to-repository.js.map +1 -0
- package/esm/relation-repository/hasmany-repository.d.ts +23 -0
- package/esm/relation-repository/hasmany-repository.js +125 -0
- package/esm/relation-repository/hasmany-repository.js.map +1 -0
- package/esm/relation-repository/hasone-repository.d.ts +17 -0
- package/esm/relation-repository/hasone-repository.js +4 -0
- package/esm/relation-repository/hasone-repository.js.map +1 -0
- package/esm/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/esm/relation-repository/multiple-relation-repository.js +149 -0
- package/esm/relation-repository/multiple-relation-repository.js.map +1 -0
- package/esm/relation-repository/relation-repository.d.ts +32 -0
- package/esm/relation-repository/relation-repository.js +93 -0
- package/esm/relation-repository/relation-repository.js.map +1 -0
- package/esm/relation-repository/single-relation-repository.d.ts +23 -0
- package/esm/relation-repository/single-relation-repository.js +96 -0
- package/esm/relation-repository/single-relation-repository.js.map +1 -0
- package/esm/relation-repository/types.d.ts +7 -0
- package/esm/relation-repository/types.js +2 -0
- package/esm/relation-repository/types.js.map +1 -0
- package/esm/repository.d.ts +165 -0
- package/esm/repository.js +276 -0
- package/esm/repository.js.map +1 -0
- package/esm/transaction-decorator.d.ts +1 -0
- package/esm/transaction-decorator.js +63 -0
- package/esm/transaction-decorator.js.map +1 -0
- package/esm/update-associations.d.ts +60 -0
- package/esm/update-associations.js +362 -0
- package/esm/update-associations.js.map +1 -0
- package/esm/update-guard.d.ts +26 -0
- package/esm/update-guard.js +122 -0
- package/esm/update-guard.js.map +1 -0
- package/lib/collection-importer.d.ts +7 -0
- package/lib/collection-importer.js +75 -0
- package/lib/collection-importer.js.map +1 -0
- package/lib/collection.d.ts +73 -0
- package/lib/collection.js +231 -0
- package/lib/collection.js.map +1 -0
- package/lib/database.d.ts +93 -43
- package/lib/database.js +277 -471
- package/lib/database.js.map +1 -1
- package/lib/fields/array-field.d.ts +11 -0
- package/lib/fields/array-field.js +30 -0
- package/lib/fields/array-field.js.map +1 -0
- package/lib/fields/belongs-to-field.d.ts +12 -0
- package/lib/fields/belongs-to-field.js +61 -0
- package/lib/fields/belongs-to-field.js.map +1 -0
- package/lib/fields/belongs-to-many-field.d.ts +11 -0
- package/lib/fields/belongs-to-many-field.js +59 -0
- package/lib/fields/belongs-to-many-field.js.map +1 -0
- package/lib/fields/boolean-field.d.ts +8 -0
- package/lib/fields/boolean-field.js +12 -0
- package/lib/fields/boolean-field.js.map +1 -0
- package/lib/fields/context-field.d.ts +13 -0
- package/lib/fields/context-field.js +47 -0
- package/lib/fields/context-field.js.map +1 -0
- package/lib/fields/date-field.d.ts +8 -0
- package/lib/fields/date-field.js +12 -0
- package/lib/fields/date-field.js.map +1 -0
- package/lib/fields/field.d.ts +37 -0
- package/lib/fields/field.js +81 -0
- package/lib/fields/field.js.map +1 -0
- package/lib/fields/has-inverse-field.d.ts +4 -0
- package/lib/fields/has-inverse-field.js +3 -0
- package/lib/fields/has-inverse-field.js.map +1 -0
- package/lib/fields/has-many-field.d.ts +64 -0
- package/lib/fields/has-many-field.js +62 -0
- package/lib/fields/has-many-field.js.map +1 -0
- package/lib/fields/has-one-field.d.ts +64 -0
- package/lib/fields/has-one-field.js +61 -0
- package/lib/fields/has-one-field.js.map +1 -0
- package/lib/fields/index.d.ts +40 -10
- package/lib/fields/index.js +31 -138
- package/lib/fields/index.js.map +1 -1
- package/lib/fields/json-field.d.ts +14 -0
- package/lib/fields/json-field.js +22 -0
- package/lib/fields/json-field.js.map +1 -0
- package/lib/fields/number-field.d.ts +32 -0
- package/lib/fields/number-field.js +36 -0
- package/lib/fields/number-field.js.map +1 -0
- package/lib/fields/password-field.d.ts +21 -0
- package/lib/fields/password-field.js +78 -0
- package/lib/fields/password-field.js.map +1 -0
- package/lib/fields/radio-field.d.ts +14 -0
- package/lib/fields/radio-field.js +53 -0
- package/lib/fields/radio-field.js.map +1 -0
- package/lib/fields/relation-field.d.ts +20 -0
- package/lib/fields/relation-field.js +31 -0
- package/lib/fields/relation-field.js.map +1 -0
- package/lib/fields/sort-field.d.ts +16 -0
- package/lib/fields/sort-field.js +94 -0
- package/lib/fields/sort-field.js.map +1 -0
- package/lib/fields/string-field.d.ts +8 -0
- package/lib/fields/string-field.js +12 -0
- package/lib/fields/string-field.js.map +1 -0
- package/lib/fields/text-field.d.ts +8 -0
- package/lib/fields/text-field.js +12 -0
- package/lib/fields/text-field.js.map +1 -0
- package/lib/fields/time-field.d.ts +8 -0
- package/lib/fields/time-field.js +12 -0
- package/lib/fields/time-field.js.map +1 -0
- package/lib/fields/uid-field.d.ts +10 -0
- package/lib/fields/uid-field.js +31 -0
- package/lib/fields/uid-field.js.map +1 -0
- package/lib/fields/virtual-field.d.ts +8 -0
- package/lib/fields/virtual-field.js +12 -0
- package/lib/fields/virtual-field.js.map +1 -0
- package/lib/filter-parser.d.ts +27 -0
- package/lib/filter-parser.js +191 -0
- package/lib/filter-parser.js.map +1 -0
- package/lib/index.d.ts +13 -6
- package/lib/index.js +27 -60
- package/lib/index.js.map +1 -1
- package/lib/magic-attribute-model.d.ts +7 -0
- package/lib/magic-attribute-model.js +77 -0
- package/lib/magic-attribute-model.js.map +1 -0
- package/lib/mock-database.d.ts +22 -0
- package/lib/mock-database.js +40 -0
- package/lib/mock-database.js.map +1 -0
- package/lib/model-hook.d.ts +12 -0
- package/lib/model-hook.js +68 -0
- package/lib/model-hook.js.map +1 -0
- package/lib/model.d.ts +11 -45
- package/lib/model.js +76 -452
- package/lib/model.js.map +1 -1
- package/lib/operators/array.d.ts +26 -0
- package/lib/operators/array.js +107 -0
- package/lib/operators/array.js.map +1 -0
- package/lib/operators/association.d.ts +10 -0
- package/lib/operators/association.js +16 -0
- package/lib/operators/association.js.map +1 -0
- package/lib/operators/date.d.ts +34 -0
- package/lib/operators/date.js +40 -0
- package/lib/operators/date.js.map +1 -0
- package/lib/operators/empty.d.ts +28 -0
- package/lib/operators/empty.js +82 -0
- package/lib/operators/empty.js.map +1 -0
- package/lib/operators/index.d.ts +2 -0
- package/lib/operators/index.js +4 -0
- package/lib/operators/index.js.map +1 -0
- package/lib/operators/ne.d.ts +10 -0
- package/lib/operators/ne.js +14 -0
- package/lib/operators/ne.js.map +1 -0
- package/lib/operators/string.d.ts +21 -0
- package/lib/operators/string.js +37 -0
- package/lib/operators/string.js.map +1 -0
- package/lib/operators/utils.d.ts +4 -0
- package/lib/operators/utils.js +16 -0
- package/lib/operators/utils.js.map +1 -0
- package/lib/options-parser.d.ts +31 -0
- package/lib/options-parser.js +232 -0
- package/lib/options-parser.js.map +1 -0
- package/lib/playground.d.ts +1 -0
- package/lib/playground.js +55 -0
- package/lib/playground.js.map +1 -0
- package/lib/relation-repository/belongs-to-many-repository.d.ts +36 -0
- package/lib/relation-repository/belongs-to-many-repository.js +206 -0
- package/lib/relation-repository/belongs-to-many-repository.js.map +1 -0
- package/lib/relation-repository/belongs-to-repository.d.ts +17 -0
- package/lib/relation-repository/belongs-to-repository.js +8 -0
- package/lib/relation-repository/belongs-to-repository.js.map +1 -0
- package/lib/relation-repository/hasmany-repository.d.ts +23 -0
- package/lib/relation-repository/hasmany-repository.js +129 -0
- package/lib/relation-repository/hasmany-repository.js.map +1 -0
- package/lib/relation-repository/hasone-repository.d.ts +17 -0
- package/lib/relation-repository/hasone-repository.js +8 -0
- package/lib/relation-repository/hasone-repository.js.map +1 -0
- package/lib/relation-repository/multiple-relation-repository.d.ts +23 -0
- package/lib/relation-repository/multiple-relation-repository.js +153 -0
- package/lib/relation-repository/multiple-relation-repository.js.map +1 -0
- package/lib/relation-repository/relation-repository.d.ts +32 -0
- package/lib/relation-repository/relation-repository.js +100 -0
- package/lib/relation-repository/relation-repository.js.map +1 -0
- package/lib/relation-repository/single-relation-repository.d.ts +23 -0
- package/lib/relation-repository/single-relation-repository.js +103 -0
- package/lib/relation-repository/single-relation-repository.js.map +1 -0
- package/lib/relation-repository/types.d.ts +7 -0
- package/lib/relation-repository/types.js +3 -0
- package/lib/relation-repository/types.js.map +1 -0
- package/lib/repository.d.ts +165 -0
- package/lib/repository.js +302 -0
- package/lib/repository.js.map +1 -0
- package/lib/transaction-decorator.d.ts +1 -0
- package/lib/transaction-decorator.js +70 -0
- package/lib/transaction-decorator.js.map +1 -0
- package/lib/update-associations.d.ts +60 -0
- package/lib/update-associations.js +374 -0
- package/lib/update-associations.js.map +1 -0
- package/lib/update-guard.d.ts +26 -0
- package/lib/update-guard.js +129 -0
- package/lib/update-guard.js.map +1 -0
- package/package.json +17 -5
- package/src/__tests__/collection-importer.test.ts +21 -0
- package/src/__tests__/collection.sortable.test.ts +65 -0
- package/src/__tests__/collection.test.ts +218 -0
- package/src/__tests__/database.import.test.ts +33 -0
- package/src/__tests__/database.test.ts +229 -0
- package/src/__tests__/field-options/hidden.test.ts +302 -0
- package/src/__tests__/field-options/sort-by.test.ts +220 -0
- package/src/__tests__/fields/belongs-to-field.test.ts +162 -0
- package/src/__tests__/fields/belongs-to-many-field.test.ts +61 -0
- package/src/__tests__/fields/context-field.test.ts +140 -0
- package/src/__tests__/fields/has-many-field.test.ts +152 -0
- package/src/__tests__/fields/has-one-field.test.ts +67 -0
- package/src/__tests__/fields/password-field.test.ts +30 -0
- package/src/__tests__/fields/sort-field.test.ts +133 -0
- package/src/__tests__/fields/string-field.test.ts +77 -0
- package/src/__tests__/filter-parser.test.ts +104 -0
- package/src/__tests__/fixtures/c0/a.ts +6 -0
- package/src/__tests__/fixtures/c1/b.ts +6 -0
- package/src/__tests__/fixtures/c2/a.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend.ts +6 -0
- package/src/__tests__/fixtures/collections/delay-extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/extend.ts +6 -0
- package/src/__tests__/fixtures/collections/extend2.ts +6 -0
- package/src/__tests__/fixtures/collections/posts.ts +4 -0
- package/src/__tests__/fixtures/collections/tags.js +4 -0
- package/src/__tests__/fixtures/collections/test.jpg +0 -0
- package/src/__tests__/fixtures/collections/user.json +9 -0
- package/src/__tests__/index.ts +1 -0
- package/src/__tests__/magic-attribute-model.test.ts +103 -0
- package/src/__tests__/model.test.ts +60 -0
- package/src/__tests__/operator/array-operator.test.ts +268 -0
- package/src/__tests__/operator/association-operator.test.ts +263 -0
- package/src/__tests__/operator/date-operator.test.ts +165 -0
- package/src/__tests__/operator/empty-operator.test.ts +77 -0
- package/src/__tests__/operator/ne.test.ts +33 -0
- package/src/__tests__/operator/string-operator.test.ts +62 -0
- package/src/__tests__/option-parser.test.ts +185 -0
- package/src/__tests__/relation-repository/belongs-to-many-repository.test.ts +697 -0
- package/src/__tests__/relation-repository/has-many-repository.test.ts +414 -0
- package/src/__tests__/relation-repository/hasone-repository.test.ts +77 -0
- package/src/__tests__/repository/count.test.ts +180 -0
- package/src/__tests__/repository/create.test.ts +163 -0
- package/src/__tests__/repository/destroy.test.ts +196 -0
- package/src/__tests__/repository/find.test.ts +247 -0
- package/src/__tests__/repository/update.test.ts +60 -0
- package/src/__tests__/repository.test.ts +438 -0
- package/src/__tests__/update-associations.test.ts +412 -0
- package/src/__tests__/update-guard.test.ts +376 -0
- package/src/collection-importer.ts +49 -0
- package/src/collection.ts +282 -0
- package/src/database.ts +340 -0
- package/src/fields/array-field.ts +35 -0
- package/src/fields/belongs-to-field.ts +76 -0
- package/src/fields/belongs-to-many-field.ts +77 -0
- package/src/fields/boolean-field.ts +12 -0
- package/src/fields/context-field.ts +45 -0
- package/src/fields/date-field.ts +12 -0
- package/src/fields/field.ts +105 -0
- package/src/fields/has-inverse-field.ts +5 -0
- package/src/fields/has-many-field.ts +143 -0
- package/src/fields/has-one-field.ts +136 -0
- package/src/fields/index.ts +72 -0
- package/src/fields/json-field.ts +25 -0
- package/src/fields/number-field.ts +52 -0
- package/src/fields/password-field.ts +72 -0
- package/src/fields/radio-field.ts +50 -0
- package/src/fields/relation-field.ts +37 -0
- package/src/fields/sort-field.ts +96 -0
- package/src/fields/string-field.ts +12 -0
- package/src/fields/text-field.ts +12 -0
- package/src/fields/time-field.ts +12 -0
- package/src/fields/uid-field.ts +24 -0
- package/src/fields/virtual-field.ts +12 -0
- package/src/filter-parser.ts +243 -0
- package/src/index.ts +16 -0
- package/src/magic-attribute-model.ts +62 -0
- package/src/mock-database.ts +42 -0
- package/src/model-hook.ts +69 -0
- package/src/model.ts +114 -0
- package/src/operators/array.ts +145 -0
- package/src/operators/association.ts +14 -0
- package/src/operators/date.ts +41 -0
- package/src/operators/empty.ts +75 -0
- package/src/operators/index.ts +8 -0
- package/src/operators/ne.ts +12 -0
- package/src/operators/string.ts +40 -0
- package/src/operators/utils.ts +13 -0
- package/src/options-parser.ts +285 -0
- package/src/playground.ts +52 -0
- package/src/relation-repository/belongs-to-many-repository.ts +240 -0
- package/src/relation-repository/belongs-to-repository.ts +23 -0
- package/src/relation-repository/hasmany-repository.ts +145 -0
- package/src/relation-repository/hasone-repository.ts +23 -0
- package/src/relation-repository/multiple-relation-repository.ts +198 -0
- package/src/relation-repository/relation-repository.ts +114 -0
- package/src/relation-repository/single-relation-repository.ts +99 -0
- package/src/relation-repository/types.ts +15 -0
- package/src/repository.ts +478 -0
- package/src/transaction-decorator.ts +58 -0
- package/src/update-associations.ts +478 -0
- package/src/update-guard.ts +167 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +5 -0
- package/examples/index.ts +0 -125
- package/examples/plugins/db-driven/index.ts +0 -25
- package/examples/plugins/db-driven/tables/fields.ts +0 -78
- package/examples/plugins/db-driven/tables/tables.ts +0 -53
- package/examples/tables/bar.js +0 -26
- package/examples/tables/comments.ts +0 -19
- package/examples/tables/foo.json +0 -3
- package/examples/tables/posts.ts +0 -28
- package/examples/tables/profiles.ts +0 -23
- package/examples/tables/tags.ts +0 -15
- package/examples/tables/users.ts +0 -34
- package/lib/database.d.ts.map +0 -1
- package/lib/fields/field-types.d.ts +0 -419
- package/lib/fields/field-types.d.ts.map +0 -1
- package/lib/fields/field-types.js +0 -1222
- package/lib/fields/field-types.js.map +0 -1
- package/lib/fields/index.d.ts.map +0 -1
- package/lib/fields/option-types.d.ts +0 -105
- package/lib/fields/option-types.d.ts.map +0 -1
- package/lib/fields/option-types.js +0 -18
- package/lib/fields/option-types.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/model.d.ts.map +0 -1
- package/lib/op.d.ts +0 -45
- package/lib/op.d.ts.map +0 -1
- package/lib/op.js +0 -225
- package/lib/op.js.map +0 -1
- package/lib/table.d.ts +0 -56
- package/lib/table.d.ts.map +0 -1
- package/lib/table.js +0 -456
- package/lib/table.js.map +0 -1
- package/lib/utils.d.ts +0 -26
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -438
- package/lib/utils.js.map +0 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize';
|
|
2
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
|
+
|
|
4
|
+
export class StringField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
return DataTypes.STRING;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface StringFieldOptions extends BaseColumnFieldOptions {
|
|
11
|
+
type: 'string';
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize';
|
|
2
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
|
+
|
|
4
|
+
export class TextField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
return DataTypes.TEXT;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TextFieldOptions extends BaseColumnFieldOptions {
|
|
11
|
+
type: 'text';
|
|
12
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize';
|
|
2
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
|
+
|
|
4
|
+
export class TimeField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
return DataTypes.TIME;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TimeFieldOptions extends BaseColumnFieldOptions {
|
|
11
|
+
type: 'time';
|
|
12
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { uid } from '@nocobase/utils';
|
|
2
|
+
import { DataTypes } from 'sequelize';
|
|
3
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
4
|
+
|
|
5
|
+
export class UidField extends Field {
|
|
6
|
+
get dataType() {
|
|
7
|
+
return DataTypes.STRING;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
init() {
|
|
11
|
+
const { name, prefix = '' } = this.options;
|
|
12
|
+
const { model } = this.context.collection;
|
|
13
|
+
model.beforeCreate(async (instance) => {
|
|
14
|
+
if (!instance.get(name)) {
|
|
15
|
+
instance.set(name, `${prefix}${uid()}`);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface UidFieldOptions extends BaseColumnFieldOptions {
|
|
22
|
+
type: 'uid';
|
|
23
|
+
prefix?: string;
|
|
24
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize';
|
|
2
|
+
import { BaseColumnFieldOptions, Field } from './field';
|
|
3
|
+
|
|
4
|
+
export class VirtualField extends Field {
|
|
5
|
+
get dataType() {
|
|
6
|
+
return DataTypes.VIRTUAL;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface VirtualFieldOptions extends BaseColumnFieldOptions {
|
|
11
|
+
type: 'virtual';
|
|
12
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { flatten, unflatten } from 'flat';
|
|
2
|
+
import { default as lodash, default as _ } from 'lodash';
|
|
3
|
+
import { ModelCtor } from 'sequelize';
|
|
4
|
+
import { Collection } from './collection';
|
|
5
|
+
import { Database } from './database';
|
|
6
|
+
import { Model } from './model';
|
|
7
|
+
|
|
8
|
+
const debug = require('debug')('noco-database');
|
|
9
|
+
|
|
10
|
+
type FilterType = any;
|
|
11
|
+
|
|
12
|
+
interface FilterParserContext {
|
|
13
|
+
collection: Collection;
|
|
14
|
+
app?: any;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default class FilterParser {
|
|
18
|
+
collection: Collection;
|
|
19
|
+
database: Database;
|
|
20
|
+
model: ModelCtor<Model>;
|
|
21
|
+
filter: FilterType;
|
|
22
|
+
context: FilterParserContext;
|
|
23
|
+
|
|
24
|
+
constructor(filter: FilterType, context: FilterParserContext) {
|
|
25
|
+
const { collection } = context;
|
|
26
|
+
this.collection = collection;
|
|
27
|
+
this.context = context;
|
|
28
|
+
this.model = collection.model;
|
|
29
|
+
this.filter = this.prepareFilter(filter);
|
|
30
|
+
this.database = collection.context.database;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
prepareFilter(filter: FilterType) {
|
|
34
|
+
if (lodash.isPlainObject(filter)) {
|
|
35
|
+
const renamedKey = {};
|
|
36
|
+
|
|
37
|
+
for (const key of Object.keys(filter)) {
|
|
38
|
+
if (key.endsWith('.$exists') || key.endsWith('.$notExists')) {
|
|
39
|
+
const keyArr = key.split('.');
|
|
40
|
+
if (keyArr[keyArr.length - 2] == 'id') {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
keyArr.splice(keyArr.length - 1, 0, 'id');
|
|
45
|
+
renamedKey[key] = keyArr.join('.');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
for (const [oldKey, newKey] of Object.entries(renamedKey)) {
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
filter[newKey] = filter[oldKey];
|
|
52
|
+
delete filter[oldKey];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return filter;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
toSequelizeParams() {
|
|
60
|
+
debug('filter %o', this.filter);
|
|
61
|
+
|
|
62
|
+
if (!this.filter) {
|
|
63
|
+
return {};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const filter = this.filter;
|
|
67
|
+
|
|
68
|
+
const model = this.model;
|
|
69
|
+
|
|
70
|
+
// supported operators
|
|
71
|
+
const operators = this.database.operators;
|
|
72
|
+
|
|
73
|
+
const originalFiler = lodash.cloneDeep(filter || {});
|
|
74
|
+
|
|
75
|
+
const flattenedFilter = flatten(filter || {});
|
|
76
|
+
|
|
77
|
+
debug('flattened filter %o', flattenedFilter);
|
|
78
|
+
|
|
79
|
+
const include = {};
|
|
80
|
+
const where = {};
|
|
81
|
+
const filter2 = lodash.cloneDeep(flattenedFilter);
|
|
82
|
+
|
|
83
|
+
let skipPrefix = null;
|
|
84
|
+
const associations = model.associations;
|
|
85
|
+
|
|
86
|
+
debug('associations %O', associations);
|
|
87
|
+
|
|
88
|
+
for (let [key, value] of Object.entries(flattenedFilter)) {
|
|
89
|
+
// 处理 filter 条件
|
|
90
|
+
if (skipPrefix && key.startsWith(skipPrefix)) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
debug('handle filter key "%s: "%s"', key, value);
|
|
95
|
+
let keys = key.split('.');
|
|
96
|
+
|
|
97
|
+
// paths ?
|
|
98
|
+
const paths = [];
|
|
99
|
+
|
|
100
|
+
// origins ?
|
|
101
|
+
const origins = [];
|
|
102
|
+
|
|
103
|
+
while (keys.length) {
|
|
104
|
+
debug('keys: %o, paths: %o, origins: %o', keys, paths, origins);
|
|
105
|
+
|
|
106
|
+
// move key from keys to origins
|
|
107
|
+
const firstKey = keys.shift();
|
|
108
|
+
origins.push(firstKey);
|
|
109
|
+
|
|
110
|
+
debug('origins: %o', origins);
|
|
111
|
+
|
|
112
|
+
if (firstKey.startsWith('$')) {
|
|
113
|
+
if (operators.has(firstKey)) {
|
|
114
|
+
debug('%s is operator', firstKey);
|
|
115
|
+
// if firstKey is operator
|
|
116
|
+
const opKey = operators.get(firstKey);
|
|
117
|
+
debug('operator key %s, operator: %o', firstKey, opKey);
|
|
118
|
+
|
|
119
|
+
// 默认操作符
|
|
120
|
+
if (typeof opKey === 'symbol') {
|
|
121
|
+
paths.push(opKey);
|
|
122
|
+
continue;
|
|
123
|
+
} else if (typeof opKey === 'function') {
|
|
124
|
+
skipPrefix = origins.join('.');
|
|
125
|
+
|
|
126
|
+
const queryValue = lodash.get(unflatten(originalFiler), skipPrefix);
|
|
127
|
+
|
|
128
|
+
value = opKey(queryValue, {
|
|
129
|
+
app: this.context.app,
|
|
130
|
+
db: this.database,
|
|
131
|
+
path: skipPrefix,
|
|
132
|
+
fieldName: this.getFieldNameFromQueryPath(skipPrefix),
|
|
133
|
+
model: this.model,
|
|
134
|
+
});
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
paths.push(firstKey);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// firstKey is number
|
|
144
|
+
if (!lodash.isNaN(parseInt(firstKey))) {
|
|
145
|
+
paths.push(firstKey);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// firstKey is not association
|
|
150
|
+
if (!associations[firstKey]) {
|
|
151
|
+
paths.push(firstKey);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const associationKeys = [];
|
|
156
|
+
|
|
157
|
+
associationKeys.push(firstKey);
|
|
158
|
+
|
|
159
|
+
debug('associationKeys %o', associationKeys);
|
|
160
|
+
|
|
161
|
+
// set sequelize include option
|
|
162
|
+
_.set(include, firstKey, {
|
|
163
|
+
association: firstKey,
|
|
164
|
+
attributes: [], // out put empty fields by default
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// association target model
|
|
168
|
+
let target = associations[firstKey].target;
|
|
169
|
+
debug('association target %o', target);
|
|
170
|
+
|
|
171
|
+
while (target) {
|
|
172
|
+
const attr = keys.shift();
|
|
173
|
+
origins.push(attr);
|
|
174
|
+
// if it is target model attribute
|
|
175
|
+
if (target.rawAttributes[attr]) {
|
|
176
|
+
associationKeys.push(attr);
|
|
177
|
+
target = null;
|
|
178
|
+
} else if (target.associations[attr]) {
|
|
179
|
+
// if it is target model association (nested association filter)
|
|
180
|
+
associationKeys.push(attr);
|
|
181
|
+
const assoc = [];
|
|
182
|
+
associationKeys.forEach((associationKey, index) => {
|
|
183
|
+
if (index > 0) {
|
|
184
|
+
assoc.push('include');
|
|
185
|
+
}
|
|
186
|
+
assoc.push(associationKey);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
_.set(include, assoc, {
|
|
190
|
+
association: attr,
|
|
191
|
+
attributes: [],
|
|
192
|
+
});
|
|
193
|
+
target = target.associations[attr].target;
|
|
194
|
+
} else {
|
|
195
|
+
throw new Error(`${attr} neither ${firstKey}'s association nor ${firstKey}'s attribute`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
debug('associationKeys %o', associationKeys);
|
|
200
|
+
|
|
201
|
+
if (associationKeys.length > 1) {
|
|
202
|
+
paths.push(`$${associationKeys.join('.')}$`);
|
|
203
|
+
} else {
|
|
204
|
+
paths.push(firstKey);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
debug('where %o, paths %o, value, %o', where, paths, value);
|
|
209
|
+
|
|
210
|
+
const values = _.get(where, paths);
|
|
211
|
+
|
|
212
|
+
if (values && typeof values === 'object' && value && typeof value === 'object') {
|
|
213
|
+
value = { ...value, ...values };
|
|
214
|
+
}
|
|
215
|
+
_.set(where, paths, value);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const toInclude = (items) => {
|
|
219
|
+
return Object.values(items).map((item: any) => {
|
|
220
|
+
if (item.include) {
|
|
221
|
+
item.include = toInclude(item.include);
|
|
222
|
+
}
|
|
223
|
+
return item;
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
debug('where %o, include %o', where, include);
|
|
227
|
+
return { where, include: toInclude(include) };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private getFieldNameFromQueryPath(queryPath: string) {
|
|
231
|
+
const paths = queryPath.split('.');
|
|
232
|
+
let fieldName;
|
|
233
|
+
for (const path of paths) {
|
|
234
|
+
if (path.startsWith('$') || !lodash.isNaN(parseInt(path))) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
fieldName = path;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return fieldName;
|
|
242
|
+
}
|
|
243
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { ModelCtor, Op, SyncOptions } from 'sequelize';
|
|
2
|
+
export * from './collection';
|
|
3
|
+
export * from './database';
|
|
4
|
+
export { Database as default } from './database';
|
|
5
|
+
export * from './fields';
|
|
6
|
+
export * from './magic-attribute-model';
|
|
7
|
+
export * from './mock-database';
|
|
8
|
+
export * from './model';
|
|
9
|
+
export * from './relation-repository/belongs-to-many-repository';
|
|
10
|
+
export * from './relation-repository/belongs-to-repository';
|
|
11
|
+
export * from './relation-repository/hasmany-repository';
|
|
12
|
+
export * from './relation-repository/multiple-relation-repository';
|
|
13
|
+
export * from './relation-repository/single-relation-repository';
|
|
14
|
+
export * from './repository';
|
|
15
|
+
export * from './update-associations';
|
|
16
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { merge } from '@nocobase/utils';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import Database from './database';
|
|
4
|
+
import { Model } from './model';
|
|
5
|
+
|
|
6
|
+
export class MagicAttributeModel extends Model {
|
|
7
|
+
get magicAttribute() {
|
|
8
|
+
const db: Database = (<any>this.constructor).database;
|
|
9
|
+
const collection = db.getCollection(this.constructor.name);
|
|
10
|
+
return collection.options.magicAttribute || 'options';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
set(key: any, value?: any, options?: any) {
|
|
14
|
+
if (typeof key === 'string') {
|
|
15
|
+
const [column] = key.split('.');
|
|
16
|
+
if ((this.constructor as any).hasAlias(column)) {
|
|
17
|
+
return super.set(key, value, options);
|
|
18
|
+
}
|
|
19
|
+
if ((this.constructor as any).rawAttributes[column]) {
|
|
20
|
+
return super.set(key, value, options);
|
|
21
|
+
}
|
|
22
|
+
if (_.isPlainObject(value)) {
|
|
23
|
+
const opts = super.get(this.magicAttribute) || {};
|
|
24
|
+
return super.set(`${this.magicAttribute}.${key}`, merge(opts?.[key], value), options);
|
|
25
|
+
}
|
|
26
|
+
return super.set(`${this.magicAttribute}.${key}`, value, options);
|
|
27
|
+
} else {
|
|
28
|
+
if (!key) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
Object.keys(key).forEach((k) => {
|
|
32
|
+
this.set(k, key[k], options);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
return super.set(key, value, options);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get(key?: any, value?: any): any {
|
|
39
|
+
if (typeof key === 'string') {
|
|
40
|
+
const [column] = key.split('.');
|
|
41
|
+
if ((this.constructor as any).hasAlias(column)) {
|
|
42
|
+
return super.get(key, value);
|
|
43
|
+
}
|
|
44
|
+
if ((this.constructor as any).rawAttributes[column]) {
|
|
45
|
+
return super.get(key, value);
|
|
46
|
+
}
|
|
47
|
+
const options = super.get(this.magicAttribute, value);
|
|
48
|
+
return _.get(options, key);
|
|
49
|
+
}
|
|
50
|
+
const data = super.get(key, value);
|
|
51
|
+
return {
|
|
52
|
+
..._.omit(data, this.magicAttribute),
|
|
53
|
+
...data[this.magicAttribute],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async update(values?: any, options?: any) {
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
this._changed = new Set();
|
|
60
|
+
return super.update(values, options);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { merge, uid } from '@nocobase/utils';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { Database, IDatabaseOptions } from './database';
|
|
4
|
+
|
|
5
|
+
export class MockDatabase extends Database {
|
|
6
|
+
constructor(options: IDatabaseOptions) {
|
|
7
|
+
super({
|
|
8
|
+
storage: ':memory:',
|
|
9
|
+
tablePrefix: `mock_${uid(6)}_`,
|
|
10
|
+
dialect: 'sqlite',
|
|
11
|
+
...options,
|
|
12
|
+
});
|
|
13
|
+
this.sequelize.beforeDefine((model, opts) => {
|
|
14
|
+
opts.tableName = `${this.getTablePrefix()}${opts.tableName || opts.modelName || opts.name.plural}`;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getConfigByEnv() {
|
|
20
|
+
return {
|
|
21
|
+
username: process.env.DB_USER,
|
|
22
|
+
password: process.env.DB_PASSWORD,
|
|
23
|
+
database: process.env.DB_DATABASE,
|
|
24
|
+
host: process.env.DB_HOST,
|
|
25
|
+
port: process.env.DB_PORT,
|
|
26
|
+
dialect: process.env.DB_DIALECT,
|
|
27
|
+
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
|
|
28
|
+
storage:
|
|
29
|
+
process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:'
|
|
30
|
+
? resolve(process.cwd(), process.env.DB_STORAGE)
|
|
31
|
+
: ':memory:',
|
|
32
|
+
define: {
|
|
33
|
+
charset: 'utf8mb4',
|
|
34
|
+
collate: 'utf8mb4_unicode_ci',
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function mockDatabase(options: IDatabaseOptions = {}): MockDatabase {
|
|
40
|
+
const dbOptions = merge(getConfigByEnv(), options);
|
|
41
|
+
return new MockDatabase(dbOptions);
|
|
42
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import lodash from 'lodash';
|
|
2
|
+
import type { SequelizeHooks } from 'sequelize/types/lib/hooks';
|
|
3
|
+
import Database from './database';
|
|
4
|
+
import { Model } from './model';
|
|
5
|
+
|
|
6
|
+
const { hooks } = require('sequelize/lib/hooks');
|
|
7
|
+
|
|
8
|
+
export class ModelHook {
|
|
9
|
+
database: Database;
|
|
10
|
+
boundEvent = new Set<string>();
|
|
11
|
+
|
|
12
|
+
constructor(database: Database) {
|
|
13
|
+
this.database = database;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
isModelHook(eventName: string | symbol): keyof SequelizeHooks | false {
|
|
17
|
+
if (lodash.isString(eventName)) {
|
|
18
|
+
const hookType = eventName.split('.').pop();
|
|
19
|
+
|
|
20
|
+
if (hooks[hookType]) {
|
|
21
|
+
return <keyof SequelizeHooks>hookType;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
findModelName(hookArgs) {
|
|
29
|
+
for (const arg of hookArgs) {
|
|
30
|
+
if (arg instanceof Model) {
|
|
31
|
+
return (<Model>arg).constructor.name;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (lodash.isPlainObject(arg)) {
|
|
35
|
+
if (arg['model']) {
|
|
36
|
+
return arg['model'].name;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (lodash.get(arg, 'name.plural')) {
|
|
40
|
+
return lodash.get(arg, 'name.plural');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bindEvent(eventName) {
|
|
49
|
+
this.boundEvent.add(eventName);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
hasBindEvent(eventName) {
|
|
53
|
+
return this.boundEvent.has(eventName);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
sequelizeHookBuilder(eventName) {
|
|
57
|
+
return async (...args: any[]) => {
|
|
58
|
+
const modelName = this.findModelName(args);
|
|
59
|
+
|
|
60
|
+
if (modelName) {
|
|
61
|
+
// emit model event
|
|
62
|
+
await this.database.emitAsync(`${modelName}.${eventName}`, ...args);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// emit sequelize global event
|
|
66
|
+
await this.database.emitAsync(eventName, ...args);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { Model as SequelizeModel, ModelCtor } from 'sequelize';
|
|
2
|
+
import { Collection } from './collection';
|
|
3
|
+
import { Database } from './database';
|
|
4
|
+
import lodash from 'lodash';
|
|
5
|
+
import { Field } from './fields';
|
|
6
|
+
|
|
7
|
+
interface IModel {
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface JSONTransformerOptions {
|
|
12
|
+
model: ModelCtor<any>;
|
|
13
|
+
collection: Collection;
|
|
14
|
+
db: Database;
|
|
15
|
+
key?: string;
|
|
16
|
+
field?: Field;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class Model<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>
|
|
20
|
+
extends SequelizeModel<TModelAttributes, TCreationAttributes>
|
|
21
|
+
implements IModel
|
|
22
|
+
{
|
|
23
|
+
public static database: Database;
|
|
24
|
+
public static collection: Collection;
|
|
25
|
+
|
|
26
|
+
public toJSON<T extends TModelAttributes>(): T {
|
|
27
|
+
const handleObj = (obj, options: JSONTransformerOptions) => {
|
|
28
|
+
const handles = [
|
|
29
|
+
(data) => {
|
|
30
|
+
if (data instanceof Model) {
|
|
31
|
+
return data.toJSON();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return data;
|
|
35
|
+
},
|
|
36
|
+
this.hiddenObjKey,
|
|
37
|
+
];
|
|
38
|
+
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), obj);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const handleArray = (arrayOfObj, options: JSONTransformerOptions) => {
|
|
42
|
+
const handles = [this.sortAssociations];
|
|
43
|
+
return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const opts = {
|
|
47
|
+
model: this.constructor as ModelCtor<any>,
|
|
48
|
+
collection: (this.constructor as any).collection,
|
|
49
|
+
db: (this.constructor as any).database as Database,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const traverseJSON = (data: T, options: JSONTransformerOptions): T => {
|
|
53
|
+
const { model, db, collection } = options;
|
|
54
|
+
// handle Object
|
|
55
|
+
data = handleObj(data, options);
|
|
56
|
+
|
|
57
|
+
const result = {};
|
|
58
|
+
for (const key of Object.keys(data)) {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
if (model.hasAlias(key)) {
|
|
61
|
+
const association = model.associations[key];
|
|
62
|
+
const opts = {
|
|
63
|
+
model: association.target,
|
|
64
|
+
collection: db.getCollection(association.target.name),
|
|
65
|
+
db,
|
|
66
|
+
key,
|
|
67
|
+
field: collection.getField(key),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if (['HasMany', 'BelongsToMany'].includes(association.associationType)) {
|
|
71
|
+
result[key] = handleArray(data[key], opts).map((item) => traverseJSON(item, opts));
|
|
72
|
+
} else {
|
|
73
|
+
result[key] = data[key] ? traverseJSON(data[key], opts) : null;
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
result[key] = data[key];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return result as T;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return traverseJSON(super.toJSON(), opts);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private hiddenObjKey(obj, options: JSONTransformerOptions) {
|
|
87
|
+
const hiddenFields = Array.from(options.collection.fields.values())
|
|
88
|
+
.filter((field) => field.options.hidden)
|
|
89
|
+
.map((field) => field.options.name);
|
|
90
|
+
|
|
91
|
+
return lodash.omit(obj, hiddenFields);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private sortAssociations(data, { field }: JSONTransformerOptions): any {
|
|
95
|
+
const sortBy = field.options.sortBy;
|
|
96
|
+
return sortBy ? this.sortArray(data, sortBy) : data;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private sortArray(data, sortBy: string | string[]) {
|
|
100
|
+
if (!lodash.isArray(sortBy)) {
|
|
101
|
+
sortBy = [sortBy];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const orderItems = [];
|
|
105
|
+
const orderDirections = [];
|
|
106
|
+
|
|
107
|
+
sortBy.forEach((sortItem) => {
|
|
108
|
+
orderDirections.push(sortItem.startsWith('-') ? 'desc' : 'asc');
|
|
109
|
+
orderItems.push(sortItem.replace('-', ''));
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return lodash.orderBy(data, orderItems, orderDirections);
|
|
113
|
+
}
|
|
114
|
+
}
|