This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcel Kloubert
committed
Feb 21, 2024
1 parent
a0e3f7d
commit e0b1bd7
Showing
6 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// This file is part of the @egomobile/orm-pg distribution. | ||
// Copyright (c) Next.e.GO Mobile SE, Aachen, Germany (https://e-go-mobile.com/) | ||
// | ||
// @egomobile/orm-pg is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as | ||
// published by the Free Software Foundation, version 3. | ||
// | ||
// @egomobile/orm-pg is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import type { EntityConfigurations, IDataContext } from "@egomobile/orm"; | ||
import { escapeStringForQuery } from "./internal"; | ||
|
||
/** | ||
* Options for `updateComments()` function. | ||
*/ | ||
export interface IUpdateCommentsOptions { | ||
/** | ||
* The configurations from where to get the documentation/comments from. | ||
*/ | ||
configurations: EntityConfigurations; | ||
/** | ||
* The underlying database connection / context. | ||
*/ | ||
context: IDataContext; | ||
} | ||
|
||
/** | ||
* Updates the COMMENT attributes of documented tables and columns. | ||
* | ||
* @param {IUpdateCommentsOptions} options The options. | ||
*/ | ||
export async function updateComments(options: IUpdateCommentsOptions) { | ||
const { | ||
configurations, | ||
context | ||
} = options; | ||
|
||
const entityConfigEntries = Object.entries(configurations); | ||
for (const [tableName, tableConfig] of entityConfigEntries) { | ||
const tableComment = tableConfig?.comment?.trim(); | ||
if (typeof tableComment === "string") { | ||
let updateTableCommentQuery: string; | ||
if (tableComment !== "") { | ||
const escapedTableComment = escapeStringForQuery(tableComment); | ||
|
||
updateTableCommentQuery = `COMMENT ON TABLE ${tableName} IS '${escapedTableComment}';`; | ||
} | ||
else { | ||
updateTableCommentQuery = `COMMENT ON TABLE ${tableName} IS NULL;`; | ||
} | ||
|
||
await context.query(updateTableCommentQuery); | ||
} | ||
|
||
const tableColumnConfigEntries = Object.entries(tableConfig.fields ?? {}); | ||
for (const [columnName, columnConfig] of tableColumnConfigEntries) { | ||
const fullColumnName = `${tableName}.${columnName}`; | ||
|
||
const tableColumnComment = columnConfig?.comment?.trim(); | ||
if (typeof tableColumnComment === "string") { | ||
let updateTableColumnCommentQuery: string; | ||
if (tableColumnComment !== "") { | ||
const escapedTableColumnComment = escapeStringForQuery(tableColumnComment); | ||
|
||
updateTableColumnCommentQuery = `COMMENT ON TABLE ${fullColumnName} IS '${escapedTableColumnComment}';`; | ||
} | ||
else { | ||
updateTableColumnCommentQuery = `COMMENT ON TABLE ${fullColumnName} IS NULL;`; | ||
} | ||
|
||
await context.query(updateTableColumnCommentQuery); | ||
} | ||
} | ||
} | ||
} |