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: add field checks to partials when on staging #477

Merged
merged 1 commit into from
Nov 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/code-gen/src/generator/sql/partial-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export function getInsertPartial(context, type) {

for (let i = 0; i < insert.length; ++i) {
const it = insert[i];
checkFieldsInSet("${type.name}", "insert", ${type.name}FieldSet, it);

q.append(query\`(
$\{options?.includePrimaryKey ? query\`$\{it.${primaryKey}}, \` : undefined}
$\{${type.partial.fields
Expand Down Expand Up @@ -182,6 +184,8 @@ export function getUpdatePartial(context, type) {
const strings = [];
const values = [];

checkFieldsInSet("${type.name}", "update", ${type.name}FieldSet, update);

${partials}
// Remove the comma suffix
strings[0] = strings[0].substring(2);
Expand Down
55 changes: 54 additions & 1 deletion packages/code-gen/src/generator/sql/query-partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getQueryEnabledObjects,
getSortedKeysForType,
} from "./utils.js";
import { getWherePartial } from "./where-type.js";
import { getWhereFieldSet, getWherePartial } from "./where-type.js";

/**
* Generate all usefull query partials
Expand All @@ -15,6 +15,16 @@ import { getWherePartial } from "./where-type.js";
export function generateQueryPartials(context) {
const partials = [];

// Generate field sets and the check function
partials.push(knownFieldsCheckFunction());
for (const type of getQueryEnabledObjects(context)) {
partials.push(getWhereFieldSet(context, type));
if (!type.queryOptions.isView) {
partials.push(getFieldSet(context, type));
}
}

// Generate the query partials
for (const type of getQueryEnabledObjects(context)) {
partials.push(getFieldsPartial(context, type));
partials.push(getWherePartial(context, type));
Expand All @@ -26,6 +36,7 @@ export function generateQueryPartials(context) {
}

const file = js`
import { AppError, isStaging } from "@lbu/stdlib";
import { query, isQueryObject } from "@lbu/store";

${partials}
Expand All @@ -40,6 +51,48 @@ export function generateQueryPartials(context) {
);
}

/**
* Static field in set check function
*
* @returns {string}
*/
export function knownFieldsCheckFunction() {
// We create a copy of the Set & convert to array before throwing, in case someone
// tries to mutate it. We can also safely skip 'undefined' values, since they will
// never be used in queries.
return js`
/**
*
* @param {string} entity
* @param {string} subType
* @param {Set} set
* @param {*} value
*/
function checkFieldsInSet(entity, subType, set, value) {
if (isStaging()) {
for (const key of Object.keys(value)) {
if (!set.has(key) && value[key] !== undefined) {
throw new AppError(\`query.$\{entity}.$\{subType}Fields\`, 500, {
unknownKey: key, knownKeys: [ ...set ],
});
}
}
}
}
`;
}

/**
*
* @param {CodeGenContext} context
* @param {CodeGenObjectType} type
*/
export function getFieldSet(context, type) {
return `const ${type.name}FieldSet = new Set(["${Object.keys(type.keys).join(
`", "`,
)}"]);`;
}

/**
* A list of fields for the provided type, with dynamic tableName
* @property {CodeGenContext} context
Expand Down
13 changes: 13 additions & 0 deletions packages/code-gen/src/generator/sql/where-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ export function createWhereTypes(context) {
}
}

/**
*
* @param {CodeGenContext} context
* @param {CodeGenObjectType} type
*/
export function getWhereFieldSet(context, type) {
return `const ${type.name}WhereFieldSet = new Set(["${type.where.fields
.map((it) => it.name)
.join(`", "`)}"]);`;
}

/**
*
* @param {CodeGenContext} context
Expand Down Expand Up @@ -229,6 +240,8 @@ export function getWherePartial(context, type) {
tableName = \`$\{tableName}.\`;
}

checkFieldsInSet("${type.name}", "where", ${type.name}WhereFieldSet, where);

const strings = [ "1 = 1" ];
const values = [ undefined ];

Expand Down
35 changes: 34 additions & 1 deletion packages/code-gen/test/sql.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mainTestFn, test } from "@lbu/cli";
import { isNil, uuid } from "@lbu/stdlib";
import { AppError, isNil, uuid } from "@lbu/stdlib";
import {
cleanupTestPostgresDatabase,
createTestPostgresDatabase,
Expand Down Expand Up @@ -154,6 +154,39 @@ test("code-gen/e2e/sql", async (t) => {
t.equal(postCount, 0, "soft cascading deletes");
});

t.test("unknown key 'where'", async (t) => {
try {
await client.queries.userSelect(sql, { foo: "bar" });
t.fail("Should throw with AppError, based on checkFields function.");
} catch (e) {
t.ok(AppError.instanceOf(e));
t.equal(e.key, `query.user.whereFields`);
t.equal(e.info.unknownKey, "foo");
}
});

t.test("unknown key 'update'", async (t) => {
try {
await client.queries.postUpdate(sql, { baz: true }, { foo: "bar" });
t.fail("Should throw with AppError, based on checkFields function.");
} catch (e) {
t.ok(AppError.instanceOf(e));
t.equal(e.key, `query.post.updateFields`);
t.equal(e.info.unknownKey, "baz");
}
});

t.test("unknown key 'insert'", async (t) => {
try {
await client.queries.categoryInsert(sql, { quix: 6 });
t.fail("Should throw with AppError, based on checkFields function.");
} catch (e) {
t.ok(AppError.instanceOf(e));
t.equal(e.key, `query.category.insertFields`);
t.equal(e.info.unknownKey, "quix");
}
});

t.test("destroy test db", async (t) => {
await cleanupTestPostgresDatabase(sql);
t.ok(true);
Expand Down
8 changes: 8 additions & 0 deletions packages/stdlib/src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ export class AppError extends Error {
[inspect.custom]() {
return AppError.format(this);
}

/**
* Use AppError#format when AppError is passed to JSON.stringify().
* This is used in the lbu insight logger in production mode.
*/
toJSON() {
return AppError.format(this);
}
}
Loading