Skip to content
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

code-gen: sql follow references in DDL and where clause #315

Merged
merged 1 commit into from Sep 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/code-gen/src/generators/sql/builder.js
Expand Up @@ -261,7 +261,7 @@ function getWhereFields(item) {
};

for (const key of Object.keys(item.keys)) {
const it = item.keys[key];
const it = getItem(item.keys[key]);
// We don't support optional field searching, since it will break the way we do the
// query generation e.g. NULL IS NULL is always true and thus the search results are
// invalid. However if a default value is set, we expect that this will be honored
Expand All @@ -271,7 +271,7 @@ function getWhereFields(item) {
}

// Also supports referenced fields
const type = getItem(it)?.type;
const type = it.type;

if (type === "number" || type === "date") {
// Generate =, > and < queries
Expand Down
Expand Up @@ -10,7 +10,7 @@
{{ let count = Object.keys(item.keys || {}).length; }}
{{ for (const key of Object.keys(item.keys|| {})) { }}
{{ count--; }}
"{{= key }}" {{= sqlExec({ type: item.keys[key].type, item: { ...item.keys[key], isOptional: item.keys[key]?.relationInfo?.isOptional ?? item.keys[key].isOptional } }).trim() }}{{= item.keys[key].relationInfo ? ` REFERENCES "${getItem(item.keys[key].relationInfo.right).name}" ("${item.keys[key].relationInfo.rightKey}") ${item.keys[key].relationInfo.isOptional ? "ON DELETE SET NULL" : "ON DELETE CASCADE"}` : "" }}{{= count !== 0 ? "," : "" }}
"{{= key }}" {{= sqlExec({ type: getItem(item.keys[key]).type, item: { ...getItem(item.keys[key]), isOptional: getItem(item.keys[key])?.relationInfo?.isOptional ?? getItem(item.keys[key]).isOptional } }).trim() }}{{= getItem(item.keys[key]).relationInfo ? ` REFERENCES "${getItem(getItem(item.keys[key]).relationInfo.right).name}" ("${getItem(item.keys[key]).relationInfo.rightKey}") ${getItem(item.keys[key]).relationInfo.isOptional ? "ON DELETE SET NULL" : "ON DELETE CASCADE"}` : "" }}{{= count !== 0 ? "," : "" }}
{{ } }}
);

Expand All @@ -22,9 +22,9 @@
{{ count--; }}
{{ if (key === "updatedAt") { continue; } }}
{{ if (key === "id") { }}
"{{= item.name }}Id" {{= sqlExec({ type: item.keys[key].type, item: { ...item.keys[key], sql: {} }, }).trim() }} REFERENCES "{{= item.name }}" ("id") ON DELETE CASCADE{{= count !== 0 ? "," : "" }}
"{{= item.name }}Id" {{= sqlExec({ type: getItem(item.keys[key]).type, item: { ...getItem(item.keys[key]), sql: {} }, }).trim() }} REFERENCES "{{= item.name }}" ("id") ON DELETE CASCADE{{= count !== 0 ? "," : "" }}
{{ } else { }}
"{{= key }}" {{= sqlExec({ type: item.keys[key].type, item: item.keys[key] }).trim() }}{{= count !== 0 ? "," : "" }}
"{{= key }}" {{= sqlExec({ type: getItem(item.keys[key]).type, item: getItem(item.keys[key]) }).trim() }}{{= count !== 0 ? "," : "" }}
{{ } }}
{{ } }}
);
Expand All @@ -33,7 +33,7 @@

-- These are only for easy copy -pasting and should not be used as a guide!
{{ for (const key of Object.keys(item.keys || {})) { }}
{{ if (!item.keys[key]?.sql?.primary && item.keys[key]?.sql?.searchable) { }}
{{ if (!getItem(item.keys[key])?.sql?.primary && getItem(item.keys[key])?.sql?.searchable) { }}
CREATE INDEX {{= camelToSnakeCase(item.name) }}_{{= camelToSnakeCase(key) }}_idx ON "{{= item.name }}" ("{{= key }}");
{{ } }}
{{ } }}
Expand Down
7 changes: 7 additions & 0 deletions scripts/generate.js
Expand Up @@ -61,6 +61,13 @@ async function main() {
.keys("onlyProp"),

// SQL
T.object("settings")
.keys({
id: T.uuid().primary(),
name: T.string("settingKey").oneOf("foo", "bar").searchable(),
value: T.bool(),
})
.enableQueries(),
T.object("list")
.keys({
id: T.uuid().primary(),
Expand Down