Skip to content

feat: allow comments by removing them #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions examples/js/endpoints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

- path: /v1/collections/:collectionId/categories/count
get:
query: >-
query: |-
-- comment
SELECT COUNT(DISTINCT s.category_id) AS counter
-- comment2
FROM collections_series cs
-- comment3
JOIN series s
ON s.id = cs.series_id
WHERE cs.collection_id = :p.collectionId
Expand All @@ -30,7 +33,7 @@

- path: /v1/categories
get_list:
query: >-
query: |-
SELECT id
, name
, name_ru
Expand All @@ -42,7 +45,7 @@
id:
type: integer
post:
query: >-
query: |-
Copy link
Owner

@php-coder php-coder Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем везде менять понадобилось-то?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для наглядности, чтобы уменьшить шанс того, что кому-то придет в голову использовать >- с комментариями :)

INSERT
INTO categories
( name
Expand All @@ -67,7 +70,7 @@

- path: /v1/categories/:categoryId
get:
query: >-
query: |-
SELECT id
, name
, name_ru
Expand All @@ -80,7 +83,7 @@
id:
type: integer
put:
query: >-
query: |-
UPDATE categories
SET name = :b.name
, name_ru = :b.name_ru
Expand All @@ -89,7 +92,7 @@
, updated_by = :b.user_id
WHERE id = :p.categoryId
delete:
query: >-
query: |-
DELETE
FROM categories
WHERE id = :p.categoryId
6 changes: 4 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const createApp = async (destDir, lang) => {
fs.copyFileSync(`${__dirname}/templates/app.${ext}`, resultFile)
};

const removeComments = (query) => query.replace(/--.*\n/g, '');

// "SELECT *\n FROM foo" => "SELECT * FROM foo"
const flattenQuery = (query) => query.replace(/\n[ ]*/g, ' ');

Expand Down Expand Up @@ -142,7 +144,7 @@ const createEndpoints = async (destDir, lang, config) => {
queries = Object.values(method.aggregated_queries)
}
queries.forEach(query => {
const sql = removePlaceholders(flattenQuery(query));
const sql = removePlaceholders(flattenQuery(removeComments(query)));
console.log(`\t${sql}`);
})
});
Expand Down Expand Up @@ -196,7 +198,7 @@ const createEndpoints = async (destDir, lang, config) => {

// "SELECT *\n FROM foo WHERE id = :p.id" => "SELECT * FROM foo WHERE id = :id"
"formatQuery": (query) => {
return removePlaceholders(flattenQuery(query));
return removePlaceholders(flattenQuery(removeComments(query)));
},

// (used only with Golang)
Expand Down