joist-codegen 2.3.0-next.9 → 2.3.0-next.90
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/build/EntityDbMetadata.cjs +642 -0
- package/build/EntityDbMetadata.cjs.map +1 -0
- package/build/EntityDbMetadata.d.cts +299 -0
- package/build/EntityDbMetadata.d.cts.map +1 -0
- package/build/EntityDbMetadata.d.mts +299 -0
- package/build/EntityDbMetadata.d.mts.map +1 -0
- package/build/EntityDbMetadata.js +519 -661
- package/build/EntityDbMetadata.js.map +1 -1
- package/build/_virtual/_rolldown/runtime.cjs +23 -0
- package/build/_virtual/_rolldown/runtime.js +5 -0
- package/build/assignTags.cjs +45 -0
- package/build/assignTags.cjs.map +1 -0
- package/build/{assignTags.d.ts → assignTags.d.cts} +8 -4
- package/build/assignTags.d.cts.map +1 -0
- package/build/assignTags.d.mts +16 -0
- package/build/assignTags.d.mts.map +1 -0
- package/build/assignTags.js +37 -59
- package/build/assignTags.js.map +1 -1
- package/build/codemods/Codemod.cjs +2 -0
- package/build/codemods/Codemod.d.cts +17 -0
- package/build/codemods/Codemod.d.cts.map +1 -0
- package/build/codemods/Codemod.d.mts +17 -0
- package/build/codemods/Codemod.d.mts.map +1 -0
- package/build/codemods/Codemod.js +2 -3
- package/build/codemods/JscodeshiftMod.cjs +42 -0
- package/build/codemods/JscodeshiftMod.cjs.map +1 -0
- package/build/codemods/JscodeshiftMod.d.cts +14 -0
- package/build/codemods/JscodeshiftMod.d.cts.map +1 -0
- package/build/codemods/JscodeshiftMod.d.mts +14 -0
- package/build/codemods/JscodeshiftMod.d.mts.map +1 -0
- package/build/codemods/JscodeshiftMod.js +33 -33
- package/build/codemods/JscodeshiftMod.js.map +1 -1
- package/build/codemods/codemod_0001-rename_has_async_property.cjs +30 -0
- package/build/codemods/codemod_0001-rename_has_async_property.cjs.map +1 -0
- package/build/codemods/codemod_0001-rename_has_async_property.d.cts +9 -0
- package/build/codemods/codemod_0001-rename_has_async_property.d.cts.map +1 -0
- package/build/codemods/codemod_0001-rename_has_async_property.d.mts +9 -0
- package/build/codemods/codemod_0001-rename_has_async_property.d.mts.map +1 -0
- package/build/codemods/codemod_0001-rename_has_async_property.js +21 -25
- package/build/codemods/codemod_0001-rename_has_async_property.js.map +1 -1
- package/build/codemods/codemod_0002-rename_async_query_fields.cjs +50 -0
- package/build/codemods/codemod_0002-rename_async_query_fields.cjs.map +1 -0
- package/build/codemods/codemod_0002-rename_async_query_fields.d.cts +9 -0
- package/build/codemods/codemod_0002-rename_async_query_fields.d.cts.map +1 -0
- package/build/codemods/codemod_0002-rename_async_query_fields.d.mts +9 -0
- package/build/codemods/codemod_0002-rename_async_query_fields.d.mts.map +1 -0
- package/build/codemods/codemod_0002-rename_async_query_fields.js +38 -52
- package/build/codemods/codemod_0002-rename_async_query_fields.js.map +1 -1
- package/build/codemods/codemod_0003-rename_find_paginated.cjs +25 -0
- package/build/codemods/codemod_0003-rename_find_paginated.cjs.map +1 -0
- package/build/codemods/codemod_0003-rename_find_paginated.d.cts +9 -0
- package/build/codemods/codemod_0003-rename_find_paginated.d.cts.map +1 -0
- package/build/codemods/codemod_0003-rename_find_paginated.d.mts +9 -0
- package/build/codemods/codemod_0003-rename_find_paginated.d.mts.map +1 -0
- package/build/codemods/codemod_0003-rename_find_paginated.js +17 -26
- package/build/codemods/codemod_0003-rename_find_paginated.js.map +1 -1
- package/build/codemods/index.cjs +49 -0
- package/build/codemods/index.cjs.map +1 -0
- package/build/codemods/index.d.cts +12 -0
- package/build/codemods/index.d.cts.map +1 -0
- package/build/codemods/index.d.mts +12 -0
- package/build/codemods/index.d.mts.map +1 -0
- package/build/codemods/index.js +34 -42
- package/build/codemods/index.js.map +1 -1
- package/build/config.cjs +377 -0
- package/build/config.cjs.map +1 -0
- package/build/config.d.cts +181 -0
- package/build/config.d.cts.map +1 -0
- package/build/config.d.mts +181 -0
- package/build/config.d.mts.map +1 -0
- package/build/config.js +309 -347
- package/build/config.js.map +1 -1
- package/build/docs/cache.cjs +62 -0
- package/build/docs/cache.cjs.map +1 -0
- package/build/docs/cache.d.cts +17 -0
- package/build/docs/cache.d.cts.map +1 -0
- package/build/docs/cache.d.mts +17 -0
- package/build/docs/cache.d.mts.map +1 -0
- package/build/docs/cache.js +52 -53
- package/build/docs/cache.js.map +1 -1
- package/build/docs/generate-metadata-docs.cjs +32 -0
- package/build/docs/generate-metadata-docs.cjs.map +1 -0
- package/build/docs/generate-metadata-docs.d.cts +9 -0
- package/build/docs/generate-metadata-docs.d.cts.map +1 -0
- package/build/docs/generate-metadata-docs.d.mts +9 -0
- package/build/docs/generate-metadata-docs.d.mts.map +1 -0
- package/build/docs/generate-metadata-docs.js +19 -24
- package/build/docs/generate-metadata-docs.js.map +1 -1
- package/build/docs/index.cjs +90 -0
- package/build/docs/index.cjs.map +1 -0
- package/build/docs/index.d.cts +16 -0
- package/build/docs/index.d.cts.map +1 -0
- package/build/docs/index.d.mts +16 -0
- package/build/docs/index.d.mts.map +1 -0
- package/build/docs/index.js +75 -80
- package/build/docs/index.js.map +1 -1
- package/build/docs/markdown.cjs +181 -0
- package/build/docs/markdown.cjs.map +1 -0
- package/build/docs/{markdown.d.ts → markdown.d.cts} +14 -12
- package/build/docs/markdown.d.cts.map +1 -0
- package/build/docs/markdown.d.mts +32 -0
- package/build/docs/markdown.d.mts.map +1 -0
- package/build/docs/markdown.js +152 -220
- package/build/docs/markdown.js.map +1 -1
- package/build/docs/parsing.cjs +117 -0
- package/build/docs/parsing.cjs.map +1 -0
- package/build/docs/parsing.d.cts +48 -0
- package/build/docs/parsing.d.cts.map +1 -0
- package/build/docs/parsing.d.mts +48 -0
- package/build/docs/parsing.d.mts.map +1 -0
- package/build/docs/parsing.js +89 -88
- package/build/docs/parsing.js.map +1 -1
- package/build/findEntityScopes.cjs +119 -0
- package/build/findEntityScopes.cjs.map +1 -0
- package/build/findEntityScopes.d.cts +14 -0
- package/build/findEntityScopes.d.cts.map +1 -0
- package/build/findEntityScopes.d.mts +14 -0
- package/build/findEntityScopes.d.mts.map +1 -0
- package/build/findEntityScopes.js +83 -111
- package/build/findEntityScopes.js.map +1 -1
- package/build/foreignKeyOrdering.cjs +52 -0
- package/build/foreignKeyOrdering.cjs.map +1 -0
- package/build/foreignKeyOrdering.d.cts +8 -0
- package/build/foreignKeyOrdering.d.cts.map +1 -0
- package/build/foreignKeyOrdering.d.mts +8 -0
- package/build/foreignKeyOrdering.d.mts.map +1 -0
- package/build/foreignKeyOrdering.js +46 -55
- package/build/foreignKeyOrdering.js.map +1 -1
- package/build/generate.cjs +128 -0
- package/build/generate.cjs.map +1 -0
- package/build/generate.d.cts +17 -0
- package/build/generate.d.cts.map +1 -0
- package/build/generate.d.mts +17 -0
- package/build/generate.d.mts.map +1 -0
- package/build/generate.js +111 -124
- package/build/generate.js.map +1 -1
- package/build/generateEntitiesFile.cjs +46 -0
- package/build/generateEntitiesFile.cjs.map +1 -0
- package/build/generateEntitiesFile.d.cts +11 -0
- package/build/generateEntitiesFile.d.cts.map +1 -0
- package/build/generateEntitiesFile.d.mts +11 -0
- package/build/generateEntitiesFile.d.mts.map +1 -0
- package/build/generateEntitiesFile.js +28 -25
- package/build/generateEntitiesFile.js.map +1 -1
- package/build/generateEntityCodegenFile.cjs +1010 -0
- package/build/generateEntityCodegenFile.cjs.map +1 -0
- package/build/generateEntityCodegenFile.d.cts +16 -0
- package/build/generateEntityCodegenFile.d.cts.map +1 -0
- package/build/generateEntityCodegenFile.d.mts +16 -0
- package/build/generateEntityCodegenFile.d.mts.map +1 -0
- package/build/generateEntityCodegenFile.js +793 -827
- package/build/generateEntityCodegenFile.js.map +1 -1
- package/build/generateEntityFile.cjs +23 -0
- package/build/generateEntityFile.cjs.map +1 -0
- package/build/generateEntityFile.d.cts +9 -0
- package/build/generateEntityFile.d.cts.map +1 -0
- package/build/generateEntityFile.d.mts +9 -0
- package/build/generateEntityFile.d.mts.map +1 -0
- package/build/generateEntityFile.js +11 -8
- package/build/generateEntityFile.js.map +1 -1
- package/build/generateEntityTestFile.cjs +25 -0
- package/build/generateEntityTestFile.cjs.map +1 -0
- package/build/generateEntityTestFile.d.cts +9 -0
- package/build/generateEntityTestFile.d.cts.map +1 -0
- package/build/generateEntityTestFile.d.mts +9 -0
- package/build/generateEntityTestFile.d.mts.map +1 -0
- package/build/generateEntityTestFile.js +10 -7
- package/build/generateEntityTestFile.js.map +1 -1
- package/build/generateEnumFile.cjs +73 -0
- package/build/generateEnumFile.cjs.map +1 -0
- package/build/generateEnumFile.d.cts +9 -0
- package/build/generateEnumFile.d.cts.map +1 -0
- package/build/generateEnumFile.d.mts +9 -0
- package/build/generateEnumFile.d.mts.map +1 -0
- package/build/generateEnumFile.js +33 -43
- package/build/generateEnumFile.js.map +1 -1
- package/build/generateFactoriesFiles.cjs +28 -0
- package/build/generateFactoriesFiles.cjs.map +1 -0
- package/build/generateFactoriesFiles.d.cts +7 -0
- package/build/generateFactoriesFiles.d.cts.map +1 -0
- package/build/generateFactoriesFiles.d.mts +7 -0
- package/build/generateFactoriesFiles.d.mts.map +1 -0
- package/build/generateFactoriesFiles.js +21 -16
- package/build/generateFactoriesFiles.js.map +1 -1
- package/build/generateFlushFunction.cjs +61 -0
- package/build/generateFlushFunction.cjs.map +1 -0
- package/build/{generateFlushFunction.d.ts → generateFlushFunction.d.cts} +8 -5
- package/build/generateFlushFunction.d.cts.map +1 -0
- package/build/generateFlushFunction.d.mts +23 -0
- package/build/generateFlushFunction.d.mts.map +1 -0
- package/build/generateFlushFunction.js +32 -78
- package/build/generateFlushFunction.js.map +1 -1
- package/build/generateMetadataFile.cjs +307 -0
- package/build/generateMetadataFile.cjs.map +1 -0
- package/build/generateMetadataFile.d.cts +10 -0
- package/build/generateMetadataFile.d.cts.map +1 -0
- package/build/generateMetadataFile.d.mts +10 -0
- package/build/generateMetadataFile.d.mts.map +1 -0
- package/build/generateMetadataFile.js +148 -156
- package/build/generateMetadataFile.js.map +1 -1
- package/build/generatePgEnumFile.cjs +21 -0
- package/build/generatePgEnumFile.cjs.map +1 -0
- package/build/generatePgEnumFile.d.cts +9 -0
- package/build/generatePgEnumFile.d.cts.map +1 -0
- package/build/generatePgEnumFile.d.mts +9 -0
- package/build/generatePgEnumFile.d.mts.map +1 -0
- package/build/generatePgEnumFile.js +12 -9
- package/build/generatePgEnumFile.js.map +1 -1
- package/build/index.cjs +135 -0
- package/build/index.cjs.map +1 -0
- package/build/index.d.cts +12 -0
- package/build/index.d.cts.map +1 -0
- package/build/index.d.mts +12 -0
- package/build/index.d.mts.map +1 -0
- package/build/index.js +108 -127
- package/build/index.js.map +1 -1
- package/build/inheritance.cjs +257 -0
- package/build/inheritance.cjs.map +1 -0
- package/build/inheritance.d.cts +13 -0
- package/build/inheritance.d.cts.map +1 -0
- package/build/inheritance.d.mts +13 -0
- package/build/inheritance.d.mts.map +1 -0
- package/build/inheritance.js +237 -247
- package/build/inheritance.js.map +1 -1
- package/build/installSkills.cjs +68 -0
- package/build/installSkills.cjs.map +1 -0
- package/build/installSkills.d.cts +16 -0
- package/build/installSkills.d.cts.map +1 -0
- package/build/installSkills.d.mts +16 -0
- package/build/installSkills.d.mts.map +1 -0
- package/build/installSkills.js +67 -0
- package/build/installSkills.js.map +1 -0
- package/build/keywords.cjs +7 -0
- package/build/keywords.cjs.map +1 -0
- package/build/keywords.d.cts +5 -0
- package/build/keywords.d.cts.map +1 -0
- package/build/keywords.d.mts +5 -0
- package/build/keywords.d.mts.map +1 -0
- package/build/keywords.js +5 -4
- package/build/keywords.js.map +1 -1
- package/build/loadMetadata.cjs +39 -0
- package/build/loadMetadata.cjs.map +1 -0
- package/build/loadMetadata.d.cts +31 -0
- package/build/loadMetadata.d.cts.map +1 -0
- package/build/loadMetadata.d.mts +31 -0
- package/build/loadMetadata.d.mts.map +1 -0
- package/build/loadMetadata.js +32 -39
- package/build/loadMetadata.js.map +1 -1
- package/build/logger.cjs +28 -0
- package/build/logger.cjs.map +1 -0
- package/build/logger.d.cts +13 -0
- package/build/logger.d.cts.map +1 -0
- package/build/logger.d.mts +13 -0
- package/build/logger.d.mts.map +1 -0
- package/build/logger.js +17 -16
- package/build/logger.js.map +1 -1
- package/build/scanEntityFiles.cjs +46 -0
- package/build/scanEntityFiles.cjs.map +1 -0
- package/build/scanEntityFiles.d.cts +8 -0
- package/build/scanEntityFiles.d.cts.map +1 -0
- package/build/scanEntityFiles.d.mts +8 -0
- package/build/scanEntityFiles.d.mts.map +1 -0
- package/build/scanEntityFiles.js +37 -42
- package/build/scanEntityFiles.js.map +1 -1
- package/build/sortForeignKeys.cjs +99 -0
- package/build/sortForeignKeys.cjs.map +1 -0
- package/build/{sortForeignKeys.d.ts → sortForeignKeys.d.cts} +8 -5
- package/build/sortForeignKeys.d.cts.map +1 -0
- package/build/sortForeignKeys.d.mts +35 -0
- package/build/sortForeignKeys.d.mts.map +1 -0
- package/build/sortForeignKeys.js +88 -117
- package/build/sortForeignKeys.js.map +1 -1
- package/build/symbols.cjs +238 -0
- package/build/symbols.cjs.map +1 -0
- package/build/symbols.d.cts +120 -0
- package/build/symbols.d.cts.map +1 -0
- package/build/symbols.d.mts +120 -0
- package/build/symbols.d.mts.map +1 -0
- package/build/symbols.js +121 -113
- package/build/symbols.js.map +1 -1
- package/build/tsdoc.cjs +22 -0
- package/build/tsdoc.cjs.map +1 -0
- package/build/tsdoc.d.cts +15 -0
- package/build/tsdoc.d.cts.map +1 -0
- package/build/tsdoc.d.mts +15 -0
- package/build/tsdoc.d.mts.map +1 -0
- package/build/tsdoc.js +18 -15
- package/build/tsdoc.js.map +1 -1
- package/build/utils.cjs +173 -0
- package/build/utils.cjs.map +1 -0
- package/build/utils.d.cts +34 -0
- package/build/utils.d.cts.map +1 -0
- package/build/utils.d.mts +34 -0
- package/build/utils.d.mts.map +1 -0
- package/build/utils.js +112 -166
- package/build/utils.js.map +1 -1
- package/build/write-json-schema.cjs +23 -0
- package/build/write-json-schema.cjs.map +1 -0
- package/build/write-json-schema.d.cts +1 -0
- package/build/write-json-schema.d.mts +1 -0
- package/build/write-json-schema.js +19 -13
- package/build/write-json-schema.js.map +1 -1
- package/package.json +37 -17
- package/skills/joist-docs/SKILL.md +142 -0
- package/skills/joist-em-basics/SKILL.md +114 -0
- package/skills/joist-partial-updates/SKILL.md +352 -0
- package/skills/joist-reactive-hints/SKILL.md +150 -0
- package/skills/joist-test-factories/SKILL.md +397 -0
- package/skills/joist-upsert/SKILL.md +95 -0
- package/build/EntityDbMetadata.d.ts +0 -269
- package/build/EntityDbMetadata.d.ts.map +0 -1
- package/build/EntityDbMetadata.test.d.ts +0 -2
- package/build/EntityDbMetadata.test.d.ts.map +0 -1
- package/build/EntityDbMetadata.test.js +0 -211
- package/build/EntityDbMetadata.test.js.map +0 -1
- package/build/assignTags.d.ts.map +0 -1
- package/build/codemods/Codemod.d.ts +0 -14
- package/build/codemods/Codemod.d.ts.map +0 -1
- package/build/codemods/Codemod.js.map +0 -1
- package/build/codemods/JscodeshiftMod.d.ts +0 -11
- package/build/codemods/JscodeshiftMod.d.ts.map +0 -1
- package/build/codemods/__tests__/codemod_0001-rename_has_async_property.test.d.ts +0 -2
- package/build/codemods/__tests__/codemod_0001-rename_has_async_property.test.d.ts.map +0 -1
- package/build/codemods/__tests__/codemod_0001-rename_has_async_property.test.js +0 -9
- package/build/codemods/__tests__/codemod_0001-rename_has_async_property.test.js.map +0 -1
- package/build/codemods/__tests__/codemod_0002-rename_async_query_fields.test.d.ts +0 -2
- package/build/codemods/__tests__/codemod_0002-rename_async_query_fields.test.d.ts.map +0 -1
- package/build/codemods/__tests__/codemod_0002-rename_async_query_fields.test.js +0 -9
- package/build/codemods/__tests__/codemod_0002-rename_async_query_fields.test.js.map +0 -1
- package/build/codemods/__tests__/codemod_0003-rename_find_paginated.test.d.ts +0 -2
- package/build/codemods/__tests__/codemod_0003-rename_find_paginated.test.d.ts.map +0 -1
- package/build/codemods/__tests__/codemod_0003-rename_find_paginated.test.js +0 -9
- package/build/codemods/__tests__/codemod_0003-rename_find_paginated.test.js.map +0 -1
- package/build/codemods/codemod_0001-rename_has_async_property.d.ts +0 -7
- package/build/codemods/codemod_0001-rename_has_async_property.d.ts.map +0 -1
- package/build/codemods/codemod_0002-rename_async_query_fields.d.ts +0 -7
- package/build/codemods/codemod_0002-rename_async_query_fields.d.ts.map +0 -1
- package/build/codemods/codemod_0003-rename_find_paginated.d.ts +0 -7
- package/build/codemods/codemod_0003-rename_find_paginated.d.ts.map +0 -1
- package/build/codemods/index.d.ts +0 -9
- package/build/codemods/index.d.ts.map +0 -1
- package/build/codemods/index.test.d.ts +0 -2
- package/build/codemods/index.test.d.ts.map +0 -1
- package/build/codemods/index.test.js +0 -24
- package/build/codemods/index.test.js.map +0 -1
- package/build/config.d.ts +0 -167
- package/build/config.d.ts.map +0 -1
- package/build/config.test.d.ts +0 -2
- package/build/config.test.d.ts.map +0 -1
- package/build/config.test.js +0 -78
- package/build/config.test.js.map +0 -1
- package/build/docs/cache.d.ts +0 -14
- package/build/docs/cache.d.ts.map +0 -1
- package/build/docs/cache.test.d.ts +0 -2
- package/build/docs/cache.test.d.ts.map +0 -1
- package/build/docs/cache.test.js +0 -47
- package/build/docs/cache.test.js.map +0 -1
- package/build/docs/generate-metadata-docs.d.ts +0 -6
- package/build/docs/generate-metadata-docs.d.ts.map +0 -1
- package/build/docs/index.d.ts +0 -9
- package/build/docs/index.d.ts.map +0 -1
- package/build/docs/index.test.d.ts +0 -2
- package/build/docs/index.test.d.ts.map +0 -1
- package/build/docs/index.test.js +0 -100
- package/build/docs/index.test.js.map +0 -1
- package/build/docs/markdown.d.ts.map +0 -1
- package/build/docs/markdown.test.d.ts +0 -2
- package/build/docs/markdown.test.d.ts.map +0 -1
- package/build/docs/markdown.test.js +0 -280
- package/build/docs/markdown.test.js.map +0 -1
- package/build/docs/parsing.d.ts +0 -42
- package/build/docs/parsing.d.ts.map +0 -1
- package/build/docs/parsing.test.d.ts +0 -2
- package/build/docs/parsing.test.d.ts.map +0 -1
- package/build/docs/parsing.test.js +0 -190
- package/build/docs/parsing.test.js.map +0 -1
- package/build/findEntityScopes.d.ts +0 -11
- package/build/findEntityScopes.d.ts.map +0 -1
- package/build/foreignKeyOrdering.d.ts +0 -3
- package/build/foreignKeyOrdering.d.ts.map +0 -1
- package/build/generate.d.ts +0 -12
- package/build/generate.d.ts.map +0 -1
- package/build/generateEntitiesFile.d.ts +0 -7
- package/build/generateEntitiesFile.d.ts.map +0 -1
- package/build/generateEntityCodegenFile.d.ts +0 -11
- package/build/generateEntityCodegenFile.d.ts.map +0 -1
- package/build/generateEntityFile.d.ts +0 -6
- package/build/generateEntityFile.d.ts.map +0 -1
- package/build/generateEntityTestFile.d.ts +0 -6
- package/build/generateEntityTestFile.d.ts.map +0 -1
- package/build/generateEnumFile.d.ts +0 -5
- package/build/generateEnumFile.d.ts.map +0 -1
- package/build/generateFactoriesFiles.d.ts +0 -4
- package/build/generateFactoriesFiles.d.ts.map +0 -1
- package/build/generateFlushFunction.d.ts.map +0 -1
- package/build/generateMetadataFile.d.ts +0 -5
- package/build/generateMetadataFile.d.ts.map +0 -1
- package/build/generatePgEnumFile.d.ts +0 -5
- package/build/generatePgEnumFile.d.ts.map +0 -1
- package/build/index.d.ts +0 -11
- package/build/index.d.ts.map +0 -1
- package/build/inheritance.d.ts +0 -10
- package/build/inheritance.d.ts.map +0 -1
- package/build/keywords.d.ts +0 -2
- package/build/keywords.d.ts.map +0 -1
- package/build/loadMetadata.d.ts +0 -26
- package/build/loadMetadata.d.ts.map +0 -1
- package/build/logger.d.ts +0 -10
- package/build/logger.d.ts.map +0 -1
- package/build/scanEntityFiles.d.ts +0 -5
- package/build/scanEntityFiles.d.ts.map +0 -1
- package/build/sortForeignKeys.d.ts.map +0 -1
- package/build/symbols.d.ts +0 -108
- package/build/symbols.d.ts.map +0 -1
- package/build/tsdoc.d.ts +0 -12
- package/build/tsdoc.d.ts.map +0 -1
- package/build/utils.d.ts +0 -31
- package/build/utils.d.ts.map +0 -1
- package/build/write-json-schema.d.ts +0 -2
- package/build/write-json-schema.d.ts.map +0 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("./config.cjs");
|
|
3
|
+
require("./EntityDbMetadata.cjs");
|
|
4
|
+
let path = require("path");
|
|
5
|
+
let fs_promises = require("fs/promises");
|
|
6
|
+
//#region src/scanEntityFiles.ts
|
|
7
|
+
const regex = /config\.setDefault\([\s\n]*["'](\w+)["']/g;
|
|
8
|
+
/** Scans the entity files themselves for usage hints (like `setDefault` calls) to drive our codegen output. */
|
|
9
|
+
async function scanEntityFiles(config, dbMeta) {
|
|
10
|
+
await Promise.all(dbMeta.entities.map(async (entity) => {
|
|
11
|
+
try {
|
|
12
|
+
const tsCode = (await (0, fs_promises.readFile)((0, path.join)(config.entitiesDirectory, `${entity.name}.ts`))).toString();
|
|
13
|
+
const defaultableFields = recursiveDefaultableFields(dbMeta, entity);
|
|
14
|
+
for (const match of tsCode.matchAll(regex)) {
|
|
15
|
+
const field = defaultableFields.find((f) => f.fieldName === match[1]);
|
|
16
|
+
if (field) field.hasConfigDefault = true;
|
|
17
|
+
}
|
|
18
|
+
} catch (e) {}
|
|
19
|
+
}));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Traverse the entity inheritance tree to find all fields that can have defaults set on them.
|
|
23
|
+
*
|
|
24
|
+
* I.e. a column defined in a base CTI table like `publishers.foo_bar` having a `config.setDefault` in
|
|
25
|
+
* `SmallPublisher` and/or `LargePublisher`, or the same scenario for an STI table like `tasks.foo_bar`
|
|
26
|
+
* with `TaskNew` and/or `TaskOld`.
|
|
27
|
+
*
|
|
28
|
+
* NOTE: I suspect here could be some edge cases here that will need to be ironed out, but should work
|
|
29
|
+
* for simple inheritance structures. In particular, there may be unexpected side effects if a base
|
|
30
|
+
* class field has a default set in one subtype, but not another.
|
|
31
|
+
*/
|
|
32
|
+
function recursiveDefaultableFields(dbMeta, entity) {
|
|
33
|
+
if (!entity) return [];
|
|
34
|
+
return [
|
|
35
|
+
...entity.primitives,
|
|
36
|
+
...entity.enums,
|
|
37
|
+
...entity.pgEnums,
|
|
38
|
+
...entity.manyToOnes,
|
|
39
|
+
...entity.polymorphics,
|
|
40
|
+
...entity.baseClassName ? recursiveDefaultableFields(dbMeta, dbMeta.entitiesByName[entity.baseClassName]) : []
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
exports.scanEntityFiles = scanEntityFiles;
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=scanEntityFiles.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanEntityFiles.cjs","names":["readFile","join"],"sources":["../src/scanEntityFiles.ts"],"sourcesContent":["import { readFile } from \"fs/promises\";\nimport { join } from \"path\";\n\nimport { type Config } from \"./config.ts\";\nimport {\n type DbMetadata,\n type EntityDbMetadata,\n type EnumField,\n type ManyToOneField,\n type PgEnumField,\n type PolymorphicField,\n type PrimitiveField,\n} from \"./EntityDbMetadata.ts\";\n\n// Erg, we need a regex in case the fieldName arg is wrapped onto a new line... :-/\nconst regex = /config\\.setDefault\\([\\s\\n]*[\"'](\\w+)[\"']/g;\n\n/** Scans the entity files themselves for usage hints (like `setDefault` calls) to drive our codegen output. */\nexport async function scanEntityFiles(config: Config, dbMeta: DbMetadata): Promise<void> {\n await Promise.all(\n dbMeta.entities.map(async (entity) => {\n try {\n // load the entity.ts file (as a promise with fs/promises)\n const tsCode = (await readFile(join(config.entitiesDirectory, `${entity.name}.ts`))).toString();\n const defaultableFields = recursiveDefaultableFields(dbMeta, entity);\n for (const match of tsCode.matchAll(regex)) {\n const field = defaultableFields.find((f) => f.fieldName === match[1]);\n if (field) {\n field.hasConfigDefault = true;\n }\n }\n } catch (e) {\n // Ignore file not existing yet\n }\n }),\n );\n}\n\n/**\n * Traverse the entity inheritance tree to find all fields that can have defaults set on them.\n *\n * I.e. a column defined in a base CTI table like `publishers.foo_bar` having a `config.setDefault` in\n * `SmallPublisher` and/or `LargePublisher`, or the same scenario for an STI table like `tasks.foo_bar`\n * with `TaskNew` and/or `TaskOld`.\n *\n * NOTE: I suspect here could be some edge cases here that will need to be ironed out, but should work\n * for simple inheritance structures. In particular, there may be unexpected side effects if a base\n * class field has a default set in one subtype, but not another.\n */\nfunction recursiveDefaultableFields(\n dbMeta: DbMetadata,\n entity: EntityDbMetadata | undefined,\n): Array<PrimitiveField | EnumField | PgEnumField | ManyToOneField | PolymorphicField> {\n if (!entity) return [];\n return [\n ...entity.primitives,\n ...entity.enums,\n ...entity.pgEnums,\n ...entity.manyToOnes,\n ...entity.polymorphics,\n ...(entity.baseClassName ? recursiveDefaultableFields(dbMeta, dbMeta.entitiesByName[entity.baseClassName]) : []),\n ];\n}\n"],"mappings":";;;;;;AAeA,MAAM,QAAQ;;AAGd,eAAsB,gBAAgB,QAAgB,QAAmC;CACvF,MAAM,QAAQ,IACZ,OAAO,SAAS,IAAI,OAAO,WAAW;EACpC,IAAI;GAEF,MAAM,UAAU,OAAA,GAAMA,YAAAA,SAAAA,EAAAA,GAASC,KAAAA,KAAAA,CAAK,OAAO,mBAAmB,GAAG,OAAO,KAAK,IAAI,CAAC,EAAA,CAAG,SAAS;GAC9F,MAAM,oBAAoB,2BAA2B,QAAQ,MAAM;GACnE,KAAK,MAAM,SAAS,OAAO,SAAS,KAAK,GAAG;IAC1C,MAAM,QAAQ,kBAAkB,MAAM,MAAM,EAAE,cAAc,MAAM,EAAE;IACpE,IAAI,OACF,MAAM,mBAAmB;GAE7B;EACF,SAAS,GAAG,CAEZ;CACF,CAAC,CACH;AACF;;;;;;;;;;;;AAaA,SAAS,2BACP,QACA,QACqF;CACrF,IAAI,CAAC,QAAQ,OAAO,CAAC;CACrB,OAAO;EACL,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAI,OAAO,gBAAgB,2BAA2B,QAAQ,OAAO,eAAe,OAAO,cAAc,IAAI,CAAC;CAChH;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Config } from "./config.cjs";
|
|
2
|
+
import { DbMetadata } from "./EntityDbMetadata.cjs";
|
|
3
|
+
//#region src/scanEntityFiles.d.ts
|
|
4
|
+
/** Scans the entity files themselves for usage hints (like `setDefault` calls) to drive our codegen output. */
|
|
5
|
+
declare function scanEntityFiles(config: Config, dbMeta: DbMetadata): Promise<void>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { scanEntityFiles };
|
|
8
|
+
//# sourceMappingURL=scanEntityFiles.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanEntityFiles.d.cts","names":[],"sources":["../src/scanEntityFiles.ts"],"mappings":";;;;iBAkBsB,gBAAgB,QAAQ,QAAQ,QAAQ,aAAa"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Config } from "./config.mjs";
|
|
2
|
+
import { DbMetadata } from "./EntityDbMetadata.mjs";
|
|
3
|
+
//#region src/scanEntityFiles.d.ts
|
|
4
|
+
/** Scans the entity files themselves for usage hints (like `setDefault` calls) to drive our codegen output. */
|
|
5
|
+
declare function scanEntityFiles(config: Config, dbMeta: DbMetadata): Promise<void>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { scanEntityFiles };
|
|
8
|
+
//# sourceMappingURL=scanEntityFiles.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scanEntityFiles.d.mts","names":[],"sources":["../src/scanEntityFiles.ts"],"mappings":";;;;iBAkBsB,gBAAgB,QAAQ,QAAQ,QAAQ,aAAa"}
|
package/build/scanEntityFiles.js
CHANGED
|
@@ -1,50 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
// Erg, we need a regex in case the fieldName arg is wrapped onto a new line... :-/
|
|
1
|
+
import "./config.js";
|
|
2
|
+
import "./EntityDbMetadata.js";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { readFile } from "fs/promises";
|
|
5
|
+
//#region src/scanEntityFiles.ts
|
|
7
6
|
const regex = /config\.setDefault\([\s\n]*["'](\w+)["']/g;
|
|
8
7
|
/** Scans the entity files themselves for usage hints (like `setDefault` calls) to drive our codegen output. */
|
|
9
8
|
async function scanEntityFiles(config, dbMeta) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
catch (e) {
|
|
23
|
-
// Ignore file not existing yet
|
|
24
|
-
}
|
|
25
|
-
}));
|
|
9
|
+
await Promise.all(dbMeta.entities.map(async (entity) => {
|
|
10
|
+
try {
|
|
11
|
+
const tsCode = (await readFile(join(config.entitiesDirectory, `${entity.name}.ts`))).toString();
|
|
12
|
+
const defaultableFields = recursiveDefaultableFields(dbMeta, entity);
|
|
13
|
+
for (const match of tsCode.matchAll(regex)) {
|
|
14
|
+
const field = defaultableFields.find((f) => f.fieldName === match[1]);
|
|
15
|
+
if (field) field.hasConfigDefault = true;
|
|
16
|
+
}
|
|
17
|
+
} catch (e) {}
|
|
18
|
+
}));
|
|
26
19
|
}
|
|
27
20
|
/**
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
21
|
+
* Traverse the entity inheritance tree to find all fields that can have defaults set on them.
|
|
22
|
+
*
|
|
23
|
+
* I.e. a column defined in a base CTI table like `publishers.foo_bar` having a `config.setDefault` in
|
|
24
|
+
* `SmallPublisher` and/or `LargePublisher`, or the same scenario for an STI table like `tasks.foo_bar`
|
|
25
|
+
* with `TaskNew` and/or `TaskOld`.
|
|
26
|
+
*
|
|
27
|
+
* NOTE: I suspect here could be some edge cases here that will need to be ironed out, but should work
|
|
28
|
+
* for simple inheritance structures. In particular, there may be unexpected side effects if a base
|
|
29
|
+
* class field has a default set in one subtype, but not another.
|
|
30
|
+
*/
|
|
38
31
|
function recursiveDefaultableFields(dbMeta, entity) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
];
|
|
32
|
+
if (!entity) return [];
|
|
33
|
+
return [
|
|
34
|
+
...entity.primitives,
|
|
35
|
+
...entity.enums,
|
|
36
|
+
...entity.pgEnums,
|
|
37
|
+
...entity.manyToOnes,
|
|
38
|
+
...entity.polymorphics,
|
|
39
|
+
...entity.baseClassName ? recursiveDefaultableFields(dbMeta, dbMeta.entitiesByName[entity.baseClassName]) : []
|
|
40
|
+
];
|
|
49
41
|
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { scanEntityFiles };
|
|
44
|
+
|
|
50
45
|
//# sourceMappingURL=scanEntityFiles.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scanEntityFiles.js","
|
|
1
|
+
{"version":3,"file":"scanEntityFiles.js","names":[],"sources":["../src/scanEntityFiles.ts"],"sourcesContent":["import { readFile } from \"fs/promises\";\nimport { join } from \"path\";\n\nimport { type Config } from \"./config.ts\";\nimport {\n type DbMetadata,\n type EntityDbMetadata,\n type EnumField,\n type ManyToOneField,\n type PgEnumField,\n type PolymorphicField,\n type PrimitiveField,\n} from \"./EntityDbMetadata.ts\";\n\n// Erg, we need a regex in case the fieldName arg is wrapped onto a new line... :-/\nconst regex = /config\\.setDefault\\([\\s\\n]*[\"'](\\w+)[\"']/g;\n\n/** Scans the entity files themselves for usage hints (like `setDefault` calls) to drive our codegen output. */\nexport async function scanEntityFiles(config: Config, dbMeta: DbMetadata): Promise<void> {\n await Promise.all(\n dbMeta.entities.map(async (entity) => {\n try {\n // load the entity.ts file (as a promise with fs/promises)\n const tsCode = (await readFile(join(config.entitiesDirectory, `${entity.name}.ts`))).toString();\n const defaultableFields = recursiveDefaultableFields(dbMeta, entity);\n for (const match of tsCode.matchAll(regex)) {\n const field = defaultableFields.find((f) => f.fieldName === match[1]);\n if (field) {\n field.hasConfigDefault = true;\n }\n }\n } catch (e) {\n // Ignore file not existing yet\n }\n }),\n );\n}\n\n/**\n * Traverse the entity inheritance tree to find all fields that can have defaults set on them.\n *\n * I.e. a column defined in a base CTI table like `publishers.foo_bar` having a `config.setDefault` in\n * `SmallPublisher` and/or `LargePublisher`, or the same scenario for an STI table like `tasks.foo_bar`\n * with `TaskNew` and/or `TaskOld`.\n *\n * NOTE: I suspect here could be some edge cases here that will need to be ironed out, but should work\n * for simple inheritance structures. In particular, there may be unexpected side effects if a base\n * class field has a default set in one subtype, but not another.\n */\nfunction recursiveDefaultableFields(\n dbMeta: DbMetadata,\n entity: EntityDbMetadata | undefined,\n): Array<PrimitiveField | EnumField | PgEnumField | ManyToOneField | PolymorphicField> {\n if (!entity) return [];\n return [\n ...entity.primitives,\n ...entity.enums,\n ...entity.pgEnums,\n ...entity.manyToOnes,\n ...entity.polymorphics,\n ...(entity.baseClassName ? recursiveDefaultableFields(dbMeta, dbMeta.entitiesByName[entity.baseClassName]) : []),\n ];\n}\n"],"mappings":";;;;;AAeA,MAAM,QAAQ;;AAGd,eAAsB,gBAAgB,QAAgB,QAAmC;CACvF,MAAM,QAAQ,IACZ,OAAO,SAAS,IAAI,OAAO,WAAW;EACpC,IAAI;GAEF,MAAM,UAAU,MAAM,SAAS,KAAK,OAAO,mBAAmB,GAAG,OAAO,KAAK,IAAI,CAAC,EAAA,CAAG,SAAS;GAC9F,MAAM,oBAAoB,2BAA2B,QAAQ,MAAM;GACnE,KAAK,MAAM,SAAS,OAAO,SAAS,KAAK,GAAG;IAC1C,MAAM,QAAQ,kBAAkB,MAAM,MAAM,EAAE,cAAc,MAAM,EAAE;IACpE,IAAI,OACF,MAAM,mBAAmB;GAE7B;EACF,SAAS,GAAG,CAEZ;CACF,CAAC,CACH;AACF;;;;;;;;;;;;AAaA,SAAS,2BACP,QACA,QACqF;CACrF,IAAI,CAAC,QAAQ,OAAO,CAAC;CACrB,OAAO;EACL,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAG,OAAO;EACV,GAAI,OAAO,gBAAgB,2BAA2B,QAAQ,OAAO,eAAe,OAAO,cAAc,IAAI,CAAC;CAChH;AACF"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
require("./EntityDbMetadata.cjs");
|
|
3
|
+
//#region src/sortForeignKeys.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sorts entities by non-deferred foreign key order.
|
|
6
|
+
*
|
|
7
|
+
* Ideally databases/schemas will support/enable deferred FKs, which let us insert/delete rows
|
|
8
|
+
* in any arbitrary order, and the FK checks will not be applied until txn commit.
|
|
9
|
+
*
|
|
10
|
+
* However, if not enabled/support, we have to worry about two things:
|
|
11
|
+
*
|
|
12
|
+
* 1. Inserting "parents" before their children
|
|
13
|
+
* - And if the parent has a circular reference to not-yet-inserted cycle
|
|
14
|
+
* 2. Deleting "children" before their parents
|
|
15
|
+
* - Again if the parent has a circular reference to the child
|
|
16
|
+
*
|
|
17
|
+
* For both of these, we can just look at the FK ordering to "put the parent first",
|
|
18
|
+
* but run into two problems:
|
|
19
|
+
*
|
|
20
|
+
* - If there is a cycle for not-null FKs, that breaks both INSERTs and DELETEs, so we
|
|
21
|
+
* must warn the user about these to fix.
|
|
22
|
+
* - If there is a cycle with nullable FKs, we can work around that by using NULL as a
|
|
23
|
+
* transient/dummy value, for the initial insert, do the 2nd insert, and then "fixup" the
|
|
24
|
+
* first insert with a UPDATE to the actual column value.
|
|
25
|
+
*
|
|
26
|
+
* Given ^, we set the `entity.nonDeferredFkOrder` of each entity, and then also return
|
|
27
|
+
* two arrays of all soft-cycle (nullable, fixup-able) and hard-cycle (not-nullable, blocking)
|
|
28
|
+
* keys.
|
|
29
|
+
*/
|
|
30
|
+
function sortByNonDeferredForeignKeys(entities) {
|
|
31
|
+
const graph = Object.fromEntries(entities.map((e) => [e.name, {
|
|
32
|
+
entity: e,
|
|
33
|
+
upstreamFks: /* @__PURE__ */ new Set(),
|
|
34
|
+
downstreamFks: /* @__PURE__ */ new Set()
|
|
35
|
+
}]));
|
|
36
|
+
const nullableCycles = [];
|
|
37
|
+
const notNullCycles = [];
|
|
38
|
+
entities.forEach((entity) => {
|
|
39
|
+
entity.nonDeferredFks.filter((m2o) => m2o.notNull).forEach((m2o) => {
|
|
40
|
+
const added = addDependency(graph, entity, m2o);
|
|
41
|
+
if (Array.isArray(added)) notNullCycles.push(added.join(" -> "));
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
entities.forEach((entity) => {
|
|
45
|
+
entity.nonDeferredFks.filter((m2o) => !m2o.notNull).forEach((m2o) => {
|
|
46
|
+
const added = addDependency(graph, entity, m2o);
|
|
47
|
+
if (Array.isArray(added)) nullableCycles.push(added.join(" -> "));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
let queue = Object.values(graph).filter((n) => n.upstreamFks.size === 0);
|
|
51
|
+
let level = 1;
|
|
52
|
+
while (queue.length > 0) {
|
|
53
|
+
const nextLevel = [];
|
|
54
|
+
for (const current of queue) {
|
|
55
|
+
current.entity.nonDeferredFkOrder = level;
|
|
56
|
+
current.downstreamFks.forEach((node) => {
|
|
57
|
+
if (node.upstreamFks.delete(current) && node.upstreamFks.size === 0) nextLevel.push(node);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
queue = nextLevel;
|
|
61
|
+
level++;
|
|
62
|
+
}
|
|
63
|
+
entities.filter((e) => e.nonDeferredFkOrder === -1).forEach((e) => e.nonDeferredFkOrder = 1e5);
|
|
64
|
+
return {
|
|
65
|
+
nullableCycles,
|
|
66
|
+
notNullCycles
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function addDependency(graph, entity, m2o) {
|
|
70
|
+
const from = graph[entity.name];
|
|
71
|
+
const to = graph[m2o.otherEntity.name];
|
|
72
|
+
const cycle = willCreateCycle(graph, from, to);
|
|
73
|
+
if (cycle) return cycle;
|
|
74
|
+
from.upstreamFks.add(to);
|
|
75
|
+
to.downstreamFks.add(from);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
function willCreateCycle(graph, from, to) {
|
|
79
|
+
return dfs(graph, /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set(), [], from, to);
|
|
80
|
+
}
|
|
81
|
+
function dfs(graph, visited, stack, path, current, target) {
|
|
82
|
+
visited.add(current);
|
|
83
|
+
stack.add(current);
|
|
84
|
+
path.push(current);
|
|
85
|
+
for (const neighbor of current.downstreamFks) {
|
|
86
|
+
if (neighbor === target) return [...path, neighbor].map((n) => n.entity.name);
|
|
87
|
+
if (!visited.has(neighbor)) {
|
|
88
|
+
const cycle = dfs(graph, visited, stack, path, neighbor, target);
|
|
89
|
+
if (cycle) return cycle;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
stack.delete(current);
|
|
93
|
+
path.pop();
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
exports.sortByNonDeferredForeignKeys = sortByNonDeferredForeignKeys;
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=sortForeignKeys.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortForeignKeys.cjs","names":[],"sources":["../src/sortForeignKeys.ts"],"sourcesContent":["import { type EntityDbMetadata, type ManyToOneField, type PolymorphicFieldComponent } from \"./EntityDbMetadata.ts\";\n\n/**\n * Sorts entities by non-deferred foreign key order.\n *\n * Ideally databases/schemas will support/enable deferred FKs, which let us insert/delete rows\n * in any arbitrary order, and the FK checks will not be applied until txn commit.\n *\n * However, if not enabled/support, we have to worry about two things:\n *\n * 1. Inserting \"parents\" before their children\n * - And if the parent has a circular reference to not-yet-inserted cycle\n * 2. Deleting \"children\" before their parents\n * - Again if the parent has a circular reference to the child\n *\n * For both of these, we can just look at the FK ordering to \"put the parent first\",\n * but run into two problems:\n *\n * - If there is a cycle for not-null FKs, that breaks both INSERTs and DELETEs, so we\n * must warn the user about these to fix.\n * - If there is a cycle with nullable FKs, we can work around that by using NULL as a\n * transient/dummy value, for the initial insert, do the 2nd insert, and then \"fixup\" the\n * first insert with a UPDATE to the actual column value.\n *\n * Given ^, we set the `entity.nonDeferredFkOrder` of each entity, and then also return\n * two arrays of all soft-cycle (nullable, fixup-able) and hard-cycle (not-nullable, blocking)\n * keys.\n */\nexport function sortByNonDeferredForeignKeys(entities: EntityDbMetadata[]): {\n nullableCycles: string[];\n notNullCycles: string[];\n} {\n // Kahn's algorithm, O(V + E)\n\n const graph: Record<string, Node> = Object.fromEntries(\n entities.map((e) => [\n e.name,\n {\n entity: e,\n upstreamFks: new Set(),\n downstreamFks: new Set(),\n },\n ]),\n );\n\n // Add all not-null deps first, as they are most important, cannot be fixed up.\n // - Failures here are fatal-ish\n // Then add nullable deps next, as they can be fixed-up\n\n const nullableCycles: string[] = [];\n const notNullCycles: string[] = [];\n\n // If we see `books.author_id`, mark `author.inDegree++`\n entities.forEach((entity) => {\n entity.nonDeferredFks\n .filter((m2o) => m2o.notNull)\n .forEach((m2o) => {\n const added = addDependency(graph, entity, m2o);\n if (Array.isArray(added)) notNullCycles.push(added.join(\" -> \"));\n });\n });\n\n // Do it again for nullable\n entities.forEach((entity) => {\n entity.nonDeferredFks\n .filter((m2o) => !m2o.notNull)\n .forEach((m2o) => {\n const added = addDependency(graph, entity, m2o);\n if (Array.isArray(added)) nullableCycles.push(added.join(\" -> \"));\n });\n });\n\n // Topological Sort\n // Enqueue entities with 0 out-degree (reverse Kahn), i.e. `authors`, they'll be at the top of the tree\n let queue = Object.values(graph).filter((n) => n.upstreamFks.size === 0);\n let level = 1;\n while (queue.length > 0) {\n // console.log(`Queue: ${queue.map((n) => n.entity.name)}`);\n const nextLevel: Node[] = [];\n for (const current of queue) {\n current.entity.nonDeferredFkOrder = level;\n current.downstreamFks.forEach((node) => {\n if (node.upstreamFks.delete(current) && node.upstreamFks.size === 0) {\n nextLevel.push(node);\n }\n });\n }\n queue = nextLevel;\n level++;\n }\n\n // Anything left with a -1 value means there is a cycle\n // Use a marker value for \"cyclic\"\n entities.filter((e) => e.nonDeferredFkOrder === -1).forEach((e) => (e.nonDeferredFkOrder = 100_000));\n\n return { nullableCycles, notNullCycles: notNullCycles };\n}\n\ntype Node = {\n entity: EntityDbMetadata;\n upstreamFks: Set<Node>;\n downstreamFks: Set<Node>;\n};\n\nfunction addDependency(\n graph: Record<string, Node>,\n entity: EntityDbMetadata,\n m2o: ManyToOneField | PolymorphicFieldComponent,\n): true | string[] {\n const from = graph[entity.name]; // i.e. books\n const to = graph[m2o.otherEntity.name]; // i.e. authors\n const cycle = willCreateCycle(graph, from, to);\n if (cycle) {\n // console.log(`Skipped ${entity.name}.${m2o.columnName} -> ${m2o.otherEntity.name}`);\n return cycle;\n }\n // console.log(`Added ${entity.name}.${m2o.columnName} -> ${m2o.otherEntity.name}`);\n from.upstreamFks.add(to);\n to.downstreamFks.add(from);\n return true;\n}\n\nfunction willCreateCycle(graph: Record<string, Node>, from: Node, to: Node): false | string[] {\n const visited: Set<Node> = new Set();\n const stack: Set<Node> = new Set();\n const path: Node[] = [];\n // If `from=books[.author_id]` pointing at `to=authors`, we want to do `to.downstreamFks.add(from)`,\n // but first make sure that none of from's dowstreams are to, because if so it would make a cycle.\n return dfs(graph, visited, stack, path, from, to);\n}\n\nfunction dfs(\n graph: Record<string, Node>,\n visited: Set<Node>,\n stack: Set<Node>,\n path: Node[],\n current: Node,\n target: Node,\n): false | string[] {\n visited.add(current);\n stack.add(current);\n path.push(current);\n for (const neighbor of current.downstreamFks) {\n if (neighbor === target) {\n return [...path, neighbor].map((n) => n.entity.name);\n }\n if (!visited.has(neighbor)) {\n const cycle = dfs(graph, visited, stack, path, neighbor, target);\n if (cycle) return cycle;\n }\n }\n stack.delete(current);\n path.pop();\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,6BAA6B,UAG3C;CAGA,MAAM,QAA8B,OAAO,YACzC,SAAS,KAAK,MAAM,CAClB,EAAE,MACF;EACE,QAAQ;EACR,6BAAa,IAAI,IAAI;EACrB,+BAAe,IAAI,IAAI;CACzB,CACF,CAAC,CACH;CAMA,MAAM,iBAA2B,CAAC;CAClC,MAAM,gBAA0B,CAAC;CAGjC,SAAS,SAAS,WAAW;EAC3B,OAAO,eACJ,QAAQ,QAAQ,IAAI,OAAO,CAAC,CAC5B,SAAS,QAAQ;GAChB,MAAM,QAAQ,cAAc,OAAO,QAAQ,GAAG;GAC9C,IAAI,MAAM,QAAQ,KAAK,GAAG,cAAc,KAAK,MAAM,KAAK,MAAM,CAAC;EACjE,CAAC;CACL,CAAC;CAGD,SAAS,SAAS,WAAW;EAC3B,OAAO,eACJ,QAAQ,QAAQ,CAAC,IAAI,OAAO,CAAC,CAC7B,SAAS,QAAQ;GAChB,MAAM,QAAQ,cAAc,OAAO,QAAQ,GAAG;GAC9C,IAAI,MAAM,QAAQ,KAAK,GAAG,eAAe,KAAK,MAAM,KAAK,MAAM,CAAC;EAClE,CAAC;CACL,CAAC;CAID,IAAI,QAAQ,OAAO,OAAO,KAAK,CAAC,CAAC,QAAQ,MAAM,EAAE,YAAY,SAAS,CAAC;CACvE,IAAI,QAAQ;CACZ,OAAO,MAAM,SAAS,GAAG;EAEvB,MAAM,YAAoB,CAAC;EAC3B,KAAK,MAAM,WAAW,OAAO;GAC3B,QAAQ,OAAO,qBAAqB;GACpC,QAAQ,cAAc,SAAS,SAAS;IACtC,IAAI,KAAK,YAAY,OAAO,OAAO,KAAK,KAAK,YAAY,SAAS,GAChE,UAAU,KAAK,IAAI;GAEvB,CAAC;EACH;EACA,QAAQ;EACR;CACF;CAIA,SAAS,QAAQ,MAAM,EAAE,uBAAuB,EAAE,CAAC,CAAC,SAAS,MAAO,EAAE,qBAAqB,GAAQ;CAEnG,OAAO;EAAE;EAA+B;CAAc;AACxD;AAQA,SAAS,cACP,OACA,QACA,KACiB;CACjB,MAAM,OAAO,MAAM,OAAO;CAC1B,MAAM,KAAK,MAAM,IAAI,YAAY;CACjC,MAAM,QAAQ,gBAAgB,OAAO,MAAM,EAAE;CAC7C,IAAI,OAEF,OAAO;CAGT,KAAK,YAAY,IAAI,EAAE;CACvB,GAAG,cAAc,IAAI,IAAI;CACzB,OAAO;AACT;AAEA,SAAS,gBAAgB,OAA6B,MAAY,IAA4B;CAM5F,OAAO,IAAI,uBAAO,IALa,IAKP,mBAAG,IAJE,IAIE,GAAG,CAAG,GAAG,MAAM,EAAE;AAClD;AAEA,SAAS,IACP,OACA,SACA,OACA,MACA,SACA,QACkB;CAClB,QAAQ,IAAI,OAAO;CACnB,MAAM,IAAI,OAAO;CACjB,KAAK,KAAK,OAAO;CACjB,KAAK,MAAM,YAAY,QAAQ,eAAe;EAC5C,IAAI,aAAa,QACf,OAAO,CAAC,GAAG,MAAM,QAAQ,CAAC,CAAC,KAAK,MAAM,EAAE,OAAO,IAAI;EAErD,IAAI,CAAC,QAAQ,IAAI,QAAQ,GAAG;GAC1B,MAAM,QAAQ,IAAI,OAAO,SAAS,OAAO,MAAM,UAAU,MAAM;GAC/D,IAAI,OAAO,OAAO;EACpB;CACF;CACA,MAAM,OAAO,OAAO;CACpB,KAAK,IAAI;CACT,OAAO;AACT"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { EntityDbMetadata } from "./EntityDbMetadata";
|
|
1
|
+
import { EntityDbMetadata } from "./EntityDbMetadata.cjs";
|
|
2
|
+
//#region src/sortForeignKeys.d.ts
|
|
2
3
|
/**
|
|
3
4
|
* Sorts entities by non-deferred foreign key order.
|
|
4
5
|
*
|
|
@@ -25,8 +26,10 @@ import { EntityDbMetadata } from "./EntityDbMetadata";
|
|
|
25
26
|
* two arrays of all soft-cycle (nullable, fixup-able) and hard-cycle (not-nullable, blocking)
|
|
26
27
|
* keys.
|
|
27
28
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
declare function sortByNonDeferredForeignKeys(entities: EntityDbMetadata[]): {
|
|
30
|
+
nullableCycles: string[];
|
|
31
|
+
notNullCycles: string[];
|
|
31
32
|
};
|
|
32
|
-
//#
|
|
33
|
+
//#endregion
|
|
34
|
+
export { sortByNonDeferredForeignKeys };
|
|
35
|
+
//# sourceMappingURL=sortForeignKeys.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortForeignKeys.d.cts","names":[],"sources":["../src/sortForeignKeys.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4BgB,6BAA6B,UAAU;EACrD;EACA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EntityDbMetadata } from "./EntityDbMetadata.mjs";
|
|
2
|
+
//#region src/sortForeignKeys.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Sorts entities by non-deferred foreign key order.
|
|
5
|
+
*
|
|
6
|
+
* Ideally databases/schemas will support/enable deferred FKs, which let us insert/delete rows
|
|
7
|
+
* in any arbitrary order, and the FK checks will not be applied until txn commit.
|
|
8
|
+
*
|
|
9
|
+
* However, if not enabled/support, we have to worry about two things:
|
|
10
|
+
*
|
|
11
|
+
* 1. Inserting "parents" before their children
|
|
12
|
+
* - And if the parent has a circular reference to not-yet-inserted cycle
|
|
13
|
+
* 2. Deleting "children" before their parents
|
|
14
|
+
* - Again if the parent has a circular reference to the child
|
|
15
|
+
*
|
|
16
|
+
* For both of these, we can just look at the FK ordering to "put the parent first",
|
|
17
|
+
* but run into two problems:
|
|
18
|
+
*
|
|
19
|
+
* - If there is a cycle for not-null FKs, that breaks both INSERTs and DELETEs, so we
|
|
20
|
+
* must warn the user about these to fix.
|
|
21
|
+
* - If there is a cycle with nullable FKs, we can work around that by using NULL as a
|
|
22
|
+
* transient/dummy value, for the initial insert, do the 2nd insert, and then "fixup" the
|
|
23
|
+
* first insert with a UPDATE to the actual column value.
|
|
24
|
+
*
|
|
25
|
+
* Given ^, we set the `entity.nonDeferredFkOrder` of each entity, and then also return
|
|
26
|
+
* two arrays of all soft-cycle (nullable, fixup-able) and hard-cycle (not-nullable, blocking)
|
|
27
|
+
* keys.
|
|
28
|
+
*/
|
|
29
|
+
declare function sortByNonDeferredForeignKeys(entities: EntityDbMetadata[]): {
|
|
30
|
+
nullableCycles: string[];
|
|
31
|
+
notNullCycles: string[];
|
|
32
|
+
};
|
|
33
|
+
//#endregion
|
|
34
|
+
export { sortByNonDeferredForeignKeys };
|
|
35
|
+
//# sourceMappingURL=sortForeignKeys.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortForeignKeys.d.mts","names":[],"sources":["../src/sortForeignKeys.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4BgB,6BAA6B,UAAU;EACrD;EACA"}
|
package/build/sortForeignKeys.js
CHANGED
|
@@ -1,127 +1,98 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.sortByNonDeferredForeignKeys = sortByNonDeferredForeignKeys;
|
|
1
|
+
import "./EntityDbMetadata.js";
|
|
2
|
+
//#region src/sortForeignKeys.ts
|
|
4
3
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
4
|
+
* Sorts entities by non-deferred foreign key order.
|
|
5
|
+
*
|
|
6
|
+
* Ideally databases/schemas will support/enable deferred FKs, which let us insert/delete rows
|
|
7
|
+
* in any arbitrary order, and the FK checks will not be applied until txn commit.
|
|
8
|
+
*
|
|
9
|
+
* However, if not enabled/support, we have to worry about two things:
|
|
10
|
+
*
|
|
11
|
+
* 1. Inserting "parents" before their children
|
|
12
|
+
* - And if the parent has a circular reference to not-yet-inserted cycle
|
|
13
|
+
* 2. Deleting "children" before their parents
|
|
14
|
+
* - Again if the parent has a circular reference to the child
|
|
15
|
+
*
|
|
16
|
+
* For both of these, we can just look at the FK ordering to "put the parent first",
|
|
17
|
+
* but run into two problems:
|
|
18
|
+
*
|
|
19
|
+
* - If there is a cycle for not-null FKs, that breaks both INSERTs and DELETEs, so we
|
|
20
|
+
* must warn the user about these to fix.
|
|
21
|
+
* - If there is a cycle with nullable FKs, we can work around that by using NULL as a
|
|
22
|
+
* transient/dummy value, for the initial insert, do the 2nd insert, and then "fixup" the
|
|
23
|
+
* first insert with a UPDATE to the actual column value.
|
|
24
|
+
*
|
|
25
|
+
* Given ^, we set the `entity.nonDeferredFkOrder` of each entity, and then also return
|
|
26
|
+
* two arrays of all soft-cycle (nullable, fixup-able) and hard-cycle (not-nullable, blocking)
|
|
27
|
+
* keys.
|
|
28
|
+
*/
|
|
30
29
|
function sortByNonDeferredForeignKeys(entities) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let level = 1;
|
|
69
|
-
while (queue.length > 0) {
|
|
70
|
-
// console.log(`Queue: ${queue.map((n) => n.entity.name)}`);
|
|
71
|
-
const nextLevel = [];
|
|
72
|
-
for (const current of queue) {
|
|
73
|
-
current.entity.nonDeferredFkOrder = level;
|
|
74
|
-
current.downstreamFks.forEach((node) => {
|
|
75
|
-
if (node.upstreamFks.delete(current) && node.upstreamFks.size === 0) {
|
|
76
|
-
nextLevel.push(node);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
queue = nextLevel;
|
|
81
|
-
level++;
|
|
82
|
-
}
|
|
83
|
-
// Anything left with a -1 value means there is a cycle
|
|
84
|
-
// Use a marker value for "cyclic"
|
|
85
|
-
entities.filter((e) => e.nonDeferredFkOrder === -1).forEach((e) => (e.nonDeferredFkOrder = 100_000));
|
|
86
|
-
return { nullableCycles, notNullCycles: notNullCycles };
|
|
30
|
+
const graph = Object.fromEntries(entities.map((e) => [e.name, {
|
|
31
|
+
entity: e,
|
|
32
|
+
upstreamFks: /* @__PURE__ */ new Set(),
|
|
33
|
+
downstreamFks: /* @__PURE__ */ new Set()
|
|
34
|
+
}]));
|
|
35
|
+
const nullableCycles = [];
|
|
36
|
+
const notNullCycles = [];
|
|
37
|
+
entities.forEach((entity) => {
|
|
38
|
+
entity.nonDeferredFks.filter((m2o) => m2o.notNull).forEach((m2o) => {
|
|
39
|
+
const added = addDependency(graph, entity, m2o);
|
|
40
|
+
if (Array.isArray(added)) notNullCycles.push(added.join(" -> "));
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
entities.forEach((entity) => {
|
|
44
|
+
entity.nonDeferredFks.filter((m2o) => !m2o.notNull).forEach((m2o) => {
|
|
45
|
+
const added = addDependency(graph, entity, m2o);
|
|
46
|
+
if (Array.isArray(added)) nullableCycles.push(added.join(" -> "));
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
let queue = Object.values(graph).filter((n) => n.upstreamFks.size === 0);
|
|
50
|
+
let level = 1;
|
|
51
|
+
while (queue.length > 0) {
|
|
52
|
+
const nextLevel = [];
|
|
53
|
+
for (const current of queue) {
|
|
54
|
+
current.entity.nonDeferredFkOrder = level;
|
|
55
|
+
current.downstreamFks.forEach((node) => {
|
|
56
|
+
if (node.upstreamFks.delete(current) && node.upstreamFks.size === 0) nextLevel.push(node);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
queue = nextLevel;
|
|
60
|
+
level++;
|
|
61
|
+
}
|
|
62
|
+
entities.filter((e) => e.nonDeferredFkOrder === -1).forEach((e) => e.nonDeferredFkOrder = 1e5);
|
|
63
|
+
return {
|
|
64
|
+
nullableCycles,
|
|
65
|
+
notNullCycles
|
|
66
|
+
};
|
|
87
67
|
}
|
|
88
68
|
function addDependency(graph, entity, m2o) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
// console.log(`Added ${entity.name}.${m2o.columnName} -> ${m2o.otherEntity.name}`);
|
|
97
|
-
from.upstreamFks.add(to);
|
|
98
|
-
to.downstreamFks.add(from);
|
|
99
|
-
return true;
|
|
69
|
+
const from = graph[entity.name];
|
|
70
|
+
const to = graph[m2o.otherEntity.name];
|
|
71
|
+
const cycle = willCreateCycle(graph, from, to);
|
|
72
|
+
if (cycle) return cycle;
|
|
73
|
+
from.upstreamFks.add(to);
|
|
74
|
+
to.downstreamFks.add(from);
|
|
75
|
+
return true;
|
|
100
76
|
}
|
|
101
77
|
function willCreateCycle(graph, from, to) {
|
|
102
|
-
|
|
103
|
-
const stack = new Set();
|
|
104
|
-
const path = [];
|
|
105
|
-
// If `from=books[.author_id]` pointing at `to=authors`, we want to do `to.downstreamFks.add(from)`,
|
|
106
|
-
// but first make sure that none of from's dowstreams are to, because if so it would make a cycle.
|
|
107
|
-
return dfs(graph, visited, stack, path, from, to);
|
|
78
|
+
return dfs(graph, /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set(), [], from, to);
|
|
108
79
|
}
|
|
109
80
|
function dfs(graph, visited, stack, path, current, target) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
stack.delete(current);
|
|
124
|
-
path.pop();
|
|
125
|
-
return false;
|
|
81
|
+
visited.add(current);
|
|
82
|
+
stack.add(current);
|
|
83
|
+
path.push(current);
|
|
84
|
+
for (const neighbor of current.downstreamFks) {
|
|
85
|
+
if (neighbor === target) return [...path, neighbor].map((n) => n.entity.name);
|
|
86
|
+
if (!visited.has(neighbor)) {
|
|
87
|
+
const cycle = dfs(graph, visited, stack, path, neighbor, target);
|
|
88
|
+
if (cycle) return cycle;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
stack.delete(current);
|
|
92
|
+
path.pop();
|
|
93
|
+
return false;
|
|
126
94
|
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { sortByNonDeferredForeignKeys };
|
|
97
|
+
|
|
127
98
|
//# sourceMappingURL=sortForeignKeys.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sortForeignKeys.js","
|
|
1
|
+
{"version":3,"file":"sortForeignKeys.js","names":[],"sources":["../src/sortForeignKeys.ts"],"sourcesContent":["import { type EntityDbMetadata, type ManyToOneField, type PolymorphicFieldComponent } from \"./EntityDbMetadata.ts\";\n\n/**\n * Sorts entities by non-deferred foreign key order.\n *\n * Ideally databases/schemas will support/enable deferred FKs, which let us insert/delete rows\n * in any arbitrary order, and the FK checks will not be applied until txn commit.\n *\n * However, if not enabled/support, we have to worry about two things:\n *\n * 1. Inserting \"parents\" before their children\n * - And if the parent has a circular reference to not-yet-inserted cycle\n * 2. Deleting \"children\" before their parents\n * - Again if the parent has a circular reference to the child\n *\n * For both of these, we can just look at the FK ordering to \"put the parent first\",\n * but run into two problems:\n *\n * - If there is a cycle for not-null FKs, that breaks both INSERTs and DELETEs, so we\n * must warn the user about these to fix.\n * - If there is a cycle with nullable FKs, we can work around that by using NULL as a\n * transient/dummy value, for the initial insert, do the 2nd insert, and then \"fixup\" the\n * first insert with a UPDATE to the actual column value.\n *\n * Given ^, we set the `entity.nonDeferredFkOrder` of each entity, and then also return\n * two arrays of all soft-cycle (nullable, fixup-able) and hard-cycle (not-nullable, blocking)\n * keys.\n */\nexport function sortByNonDeferredForeignKeys(entities: EntityDbMetadata[]): {\n nullableCycles: string[];\n notNullCycles: string[];\n} {\n // Kahn's algorithm, O(V + E)\n\n const graph: Record<string, Node> = Object.fromEntries(\n entities.map((e) => [\n e.name,\n {\n entity: e,\n upstreamFks: new Set(),\n downstreamFks: new Set(),\n },\n ]),\n );\n\n // Add all not-null deps first, as they are most important, cannot be fixed up.\n // - Failures here are fatal-ish\n // Then add nullable deps next, as they can be fixed-up\n\n const nullableCycles: string[] = [];\n const notNullCycles: string[] = [];\n\n // If we see `books.author_id`, mark `author.inDegree++`\n entities.forEach((entity) => {\n entity.nonDeferredFks\n .filter((m2o) => m2o.notNull)\n .forEach((m2o) => {\n const added = addDependency(graph, entity, m2o);\n if (Array.isArray(added)) notNullCycles.push(added.join(\" -> \"));\n });\n });\n\n // Do it again for nullable\n entities.forEach((entity) => {\n entity.nonDeferredFks\n .filter((m2o) => !m2o.notNull)\n .forEach((m2o) => {\n const added = addDependency(graph, entity, m2o);\n if (Array.isArray(added)) nullableCycles.push(added.join(\" -> \"));\n });\n });\n\n // Topological Sort\n // Enqueue entities with 0 out-degree (reverse Kahn), i.e. `authors`, they'll be at the top of the tree\n let queue = Object.values(graph).filter((n) => n.upstreamFks.size === 0);\n let level = 1;\n while (queue.length > 0) {\n // console.log(`Queue: ${queue.map((n) => n.entity.name)}`);\n const nextLevel: Node[] = [];\n for (const current of queue) {\n current.entity.nonDeferredFkOrder = level;\n current.downstreamFks.forEach((node) => {\n if (node.upstreamFks.delete(current) && node.upstreamFks.size === 0) {\n nextLevel.push(node);\n }\n });\n }\n queue = nextLevel;\n level++;\n }\n\n // Anything left with a -1 value means there is a cycle\n // Use a marker value for \"cyclic\"\n entities.filter((e) => e.nonDeferredFkOrder === -1).forEach((e) => (e.nonDeferredFkOrder = 100_000));\n\n return { nullableCycles, notNullCycles: notNullCycles };\n}\n\ntype Node = {\n entity: EntityDbMetadata;\n upstreamFks: Set<Node>;\n downstreamFks: Set<Node>;\n};\n\nfunction addDependency(\n graph: Record<string, Node>,\n entity: EntityDbMetadata,\n m2o: ManyToOneField | PolymorphicFieldComponent,\n): true | string[] {\n const from = graph[entity.name]; // i.e. books\n const to = graph[m2o.otherEntity.name]; // i.e. authors\n const cycle = willCreateCycle(graph, from, to);\n if (cycle) {\n // console.log(`Skipped ${entity.name}.${m2o.columnName} -> ${m2o.otherEntity.name}`);\n return cycle;\n }\n // console.log(`Added ${entity.name}.${m2o.columnName} -> ${m2o.otherEntity.name}`);\n from.upstreamFks.add(to);\n to.downstreamFks.add(from);\n return true;\n}\n\nfunction willCreateCycle(graph: Record<string, Node>, from: Node, to: Node): false | string[] {\n const visited: Set<Node> = new Set();\n const stack: Set<Node> = new Set();\n const path: Node[] = [];\n // If `from=books[.author_id]` pointing at `to=authors`, we want to do `to.downstreamFks.add(from)`,\n // but first make sure that none of from's dowstreams are to, because if so it would make a cycle.\n return dfs(graph, visited, stack, path, from, to);\n}\n\nfunction dfs(\n graph: Record<string, Node>,\n visited: Set<Node>,\n stack: Set<Node>,\n path: Node[],\n current: Node,\n target: Node,\n): false | string[] {\n visited.add(current);\n stack.add(current);\n path.push(current);\n for (const neighbor of current.downstreamFks) {\n if (neighbor === target) {\n return [...path, neighbor].map((n) => n.entity.name);\n }\n if (!visited.has(neighbor)) {\n const cycle = dfs(graph, visited, stack, path, neighbor, target);\n if (cycle) return cycle;\n }\n }\n stack.delete(current);\n path.pop();\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,6BAA6B,UAG3C;CAGA,MAAM,QAA8B,OAAO,YACzC,SAAS,KAAK,MAAM,CAClB,EAAE,MACF;EACE,QAAQ;EACR,6BAAa,IAAI,IAAI;EACrB,+BAAe,IAAI,IAAI;CACzB,CACF,CAAC,CACH;CAMA,MAAM,iBAA2B,CAAC;CAClC,MAAM,gBAA0B,CAAC;CAGjC,SAAS,SAAS,WAAW;EAC3B,OAAO,eACJ,QAAQ,QAAQ,IAAI,OAAO,CAAC,CAC5B,SAAS,QAAQ;GAChB,MAAM,QAAQ,cAAc,OAAO,QAAQ,GAAG;GAC9C,IAAI,MAAM,QAAQ,KAAK,GAAG,cAAc,KAAK,MAAM,KAAK,MAAM,CAAC;EACjE,CAAC;CACL,CAAC;CAGD,SAAS,SAAS,WAAW;EAC3B,OAAO,eACJ,QAAQ,QAAQ,CAAC,IAAI,OAAO,CAAC,CAC7B,SAAS,QAAQ;GAChB,MAAM,QAAQ,cAAc,OAAO,QAAQ,GAAG;GAC9C,IAAI,MAAM,QAAQ,KAAK,GAAG,eAAe,KAAK,MAAM,KAAK,MAAM,CAAC;EAClE,CAAC;CACL,CAAC;CAID,IAAI,QAAQ,OAAO,OAAO,KAAK,CAAC,CAAC,QAAQ,MAAM,EAAE,YAAY,SAAS,CAAC;CACvE,IAAI,QAAQ;CACZ,OAAO,MAAM,SAAS,GAAG;EAEvB,MAAM,YAAoB,CAAC;EAC3B,KAAK,MAAM,WAAW,OAAO;GAC3B,QAAQ,OAAO,qBAAqB;GACpC,QAAQ,cAAc,SAAS,SAAS;IACtC,IAAI,KAAK,YAAY,OAAO,OAAO,KAAK,KAAK,YAAY,SAAS,GAChE,UAAU,KAAK,IAAI;GAEvB,CAAC;EACH;EACA,QAAQ;EACR;CACF;CAIA,SAAS,QAAQ,MAAM,EAAE,uBAAuB,EAAE,CAAC,CAAC,SAAS,MAAO,EAAE,qBAAqB,GAAQ;CAEnG,OAAO;EAAE;EAA+B;CAAc;AACxD;AAQA,SAAS,cACP,OACA,QACA,KACiB;CACjB,MAAM,OAAO,MAAM,OAAO;CAC1B,MAAM,KAAK,MAAM,IAAI,YAAY;CACjC,MAAM,QAAQ,gBAAgB,OAAO,MAAM,EAAE;CAC7C,IAAI,OAEF,OAAO;CAGT,KAAK,YAAY,IAAI,EAAE;CACvB,GAAG,cAAc,IAAI,IAAI;CACzB,OAAO;AACT;AAEA,SAAS,gBAAgB,OAA6B,MAAY,IAA4B;CAM5F,OAAO,IAAI,uBAAO,IALa,IAKP,mBAAG,IAJE,IAIE,GAAG,CAAG,GAAG,MAAM,EAAE;AAClD;AAEA,SAAS,IACP,OACA,SACA,OACA,MACA,SACA,QACkB;CAClB,QAAQ,IAAI,OAAO;CACnB,MAAM,IAAI,OAAO;CACjB,KAAK,KAAK,OAAO;CACjB,KAAK,MAAM,YAAY,QAAQ,eAAe;EAC5C,IAAI,aAAa,QACf,OAAO,CAAC,GAAG,MAAM,QAAQ,CAAC,CAAC,KAAK,MAAM,EAAE,OAAO,IAAI;EAErD,IAAI,CAAC,QAAQ,IAAI,QAAQ,GAAG;GAC1B,MAAM,QAAQ,IAAI,OAAO,SAAS,OAAO,MAAM,UAAU,MAAM;GAC/D,IAAI,OAAO,OAAO;EACpB;CACF;CACA,MAAM,OAAO,OAAO;CACpB,KAAK,IAAI;CACT,OAAO;AACT"}
|