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

RDS: insert and update support #23

Merged
merged 2 commits into from
Feb 9, 2024
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
54 changes: 54 additions & 0 deletions __tests__/__snapshots__/resolvers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ exports[`dynamodb resolvers something 2`] = `
]
`;

exports[`rds resolvers mysql insert 1`] = `
{
"statements": [
"INSERT INTO \`persons\` (\`name\`) VALUES (:P0)",
],
"variableMap": {
":P0": "test",
},
"variableTypeHintMap": {},
}
`;

exports[`rds resolvers mysql remove 1`] = `
{
"statements": [
Expand Down Expand Up @@ -81,6 +93,33 @@ exports[`rds resolvers mysql type hints 1`] = `
}
`;

exports[`rds resolvers mysql update 1`] = `
{
"statements": [
"UPDATE \`persons\` SET \`name\` = :P0, \`birthday\` = :P1, \`country\` = :P2 WHERE \`id\` = :P3",
],
"variableMap": {
":P0": "name",
":P1": "today",
":P2": "home",
":P3": "abc123",
},
"variableTypeHintMap": {},
}
`;

exports[`rds resolvers postgresql insert 1`] = `
{
"statements": [
"INSERT INTO "persons" ("name") VALUES (:P0)",
],
"variableMap": {
":P0": "test",
},
"variableTypeHintMap": {},
}
`;

exports[`rds resolvers postgresql remove 1`] = `
{
"statements": [
Expand Down Expand Up @@ -124,6 +163,21 @@ exports[`rds resolvers postgresql type hints 1`] = `
}
`;

exports[`rds resolvers postgresql update 1`] = `
{
"statements": [
"UPDATE "persons" SET "name" = :P0, "birthday" = :P1, "country" = :P2 WHERE "id" = :P3",
],
"variableMap": {
":P0": "name",
":P1": "today",
":P2": "home",
":P3": "abc123",
},
"variableTypeHintMap": {},
}
`;

exports[`rds resolvers toJsonObject 1`] = `
[
[
Expand Down
111 changes: 111 additions & 0 deletions __tests__/resolvers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,61 @@ describe("rds resolvers", () => {

});

test("update", async () => {
const code = `
export function request(ctx) {
const { input: { id, ...values }, condition } = ctx.args;
const where = {
...condition,
id: { eq: id },
};
const updateStatement = rds.update({
table: 'persons',
values,
where,
});

return rds.createMySQLStatement(updateStatement)
}
export function response(ctx) {}
`;
const requestContext = {
arguments: {
input: {
id: "abc123",
name: "name",
birthday: "today",
country: "home",
},
},
};

await checkResolverValid(code, requestContext, "request");
});

test("insert", async () => {
const code = `
export function request(ctx) {
const { input: values } = ctx.args;
const insertStatement = rds.insert({ table: 'persons', values });

return rds.createMySQLStatement(insertStatement)
}

export function response(ctx) {}
`;

const requestContext = {
arguments: {
input: {
name: "test",
},
}
};

await checkResolverValid(code, requestContext, "request");
});

test("remove", async () => {
const code = `
export function request(ctx) {
Expand Down Expand Up @@ -335,6 +390,62 @@ describe("rds resolvers", () => {

});

test("update", async () => {
const code = `
export function request(ctx) {
const { input: { id, ...values }, condition } = ctx.args;
const where = {
...condition,
id: { eq: id },
};
const updateStatement = rds.update({
table: 'persons',
values,
where,
});

return rds.createPgStatement(updateStatement)
}
export function response(ctx) {}
`;
const requestContext = {
arguments: {
input: {
id: "abc123",
name: "name",
birthday: "today",
country: "home",
},
},
};

await checkResolverValid(code, requestContext, "request");
});


test("insert", async () => {
const code = `
export function request(ctx) {
const { input: values } = ctx.args;
const insertStatement = rds.insert({ table: 'persons', values });

return rds.createPgStatement(insertStatement)
}

export function response(ctx) {}
`;

const requestContext = {
arguments: {
input: {
name: "test",
},
}
};

await checkResolverValid(code, requestContext, "request");
});

test("remove", async () => {
const code = `
export function request(ctx) {
Expand Down
57 changes: 55 additions & 2 deletions rds/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function select(s) {
return { type: "SELECT", properties: s };
}

export function insert(s) {
return { type: "INSERT", properties: s };
}

export function update(s) {
return { type: "UPDATE", properties: s };
}

export function remove(s) {
return { type: "REMOVE", properties: s };
}
Expand All @@ -61,7 +69,7 @@ class StatementBuilder {
switch (type) {
case "SELECT": {
const { table, columns, where, orderBy, limit, offset } = properties;
const tableName = `${this.quoteChar}${table}${this.quoteChar}`;
const tableName = this.getTableName(table);
let query;

if (columns) {
Expand Down Expand Up @@ -103,7 +111,7 @@ class StatementBuilder {
}
case "REMOVE": {
const { table, where, returning, } = properties;
const tableName = `${this.quoteChar}${table}${this.quoteChar}`;
const tableName = this.getTableName(table);

let query = `DELETE FROM ${tableName}`;

Expand All @@ -120,6 +128,47 @@ class StatementBuilder {
this.result.statements.push(query);
break;
}
case "INSERT": {
const { table, values } = properties;
const tableName = this.getTableName(table);

let query = `INSERT INTO ${tableName}`;

let columnTextItems = [];
let valuesTextItems = [];
for (const [columnName, value] of Object.entries(values)) {
columnTextItems.push(`${this.quoteChar}${columnName}${this.quoteChar}`);
const placeholder = this.newVariable(value);
valuesTextItems.push(placeholder);
}
query = `${query} (${columnTextItems.join(', ')}) VALUES (${valuesTextItems.join(', ')})`;

this.result.statements.push(query);
break;
}
case "UPDATE": {
const { table, values, where } = properties;
const tableName = this.getTableName(table);

let query = `UPDATE ${tableName} SET`;

let columnDefinitionItems = [];
for (const [columnName, value] of Object.entries(values)) {
const placeholder = this.newVariable(value);
columnDefinitionItems.push(`${this.quoteChar}${columnName}${this.quoteChar} = ${placeholder}`);

}
query = `${query} ${columnDefinitionItems.join(', ')}`;

if (where) {
const parts = this.buildWhereClause(where);
query = `${query} WHERE ${parts}`;
}

this.result.statements.push(query);

break;
}
default:
throw new Error(`TODO: "${type}" query unsupported`);
}
Expand Down Expand Up @@ -189,6 +238,10 @@ class StatementBuilder {
throw new Error(`Unhandled condition type ${conditionType}`);
}
}

getTableName(rawName) {
return `${this.quoteChar}${rawName}${this.quoteChar}`;
}
}

export function createPgStatement(...statements) {
Expand Down
Loading