Skip to content

Commit

Permalink
Correctly inline field values (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
leo authored Feb 6, 2025
1 parent d25004e commit 0806bef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/utils/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ export const prepareStatementValue = (
// which is desired in cases where there is no risk of SQL injection and where the
// values must be plainly visible for manual human inspection.
if (!statementParams) {
if (typeof value === 'string') return `'${value}'`;

const valueString =
typeof value === 'object'
? `json('${JSON.stringify(value, replaceJSON)}')`
: `'${value!.toString()}'`;
: value!.toString();

return valueString;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ test('create new model', () => {
{
params: [],
statement:
'CREATE TRIGGER "trigger_slug" AFTER INSERT ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES (\'2000\'); END',
'CREATE TRIGGER "trigger_slug" AFTER INSERT ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES (2000); END',
},
{
statement:
Expand Down Expand Up @@ -1316,7 +1316,7 @@ test('create new trigger for creating records', () => {

expect(transaction.statements).toEqual([
{
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES ('2000'); END`,
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES (2000); END`,
params: [],
},
{
Expand Down Expand Up @@ -1375,7 +1375,7 @@ test('create new trigger for creating records with targeted fields', () => {

expect(transaction.statements).toEqual([
{
statement: `CREATE TRIGGER "trigger_slug" AFTER UPDATE OF ("email") ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES ('2000'); END`,
statement: `CREATE TRIGGER "trigger_slug" AFTER UPDATE OF ("email") ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES (2000); END`,
params: [],
},
{
Expand Down Expand Up @@ -1441,7 +1441,7 @@ test('create new trigger for creating records with multiple effects', () => {

expect(transaction.statements).toEqual([
{
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES ('2000'); INSERT INTO "candidates" ("year") VALUES ('2020'); END`,
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "accounts" BEGIN INSERT INTO "signups" ("year") VALUES (2000); INSERT INTO "candidates" ("year") VALUES (2020); END`,
params: [],
},
{
Expand Down Expand Up @@ -1508,7 +1508,7 @@ test('create new per-record trigger for creating records', () => {

expect(transaction.statements).toEqual([
{
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "teams" FOR EACH ROW BEGIN INSERT INTO "members" ("account", "role", "pending") VALUES (NEW."createdBy", 'owner', 'false'); END`,
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "teams" FOR EACH ROW BEGIN INSERT INTO "members" ("account", "role", "pending") VALUES (NEW."createdBy", 'owner', false); END`,
params: [],
},
{
Expand Down Expand Up @@ -1644,7 +1644,7 @@ test('create new per-record trigger with filters for creating records', () => {

expect(transaction.statements).toEqual([
{
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "teams" FOR EACH ROW WHEN (NEW."handle" LIKE '%_hidden') BEGIN INSERT INTO "members" ("account", "role", "pending") VALUES (NEW."createdBy", 'owner', 'false'); END`,
statement: `CREATE TRIGGER "trigger_slug" AFTER INSERT ON "teams" FOR EACH ROW WHEN (NEW."handle" LIKE '%_hidden') BEGIN INSERT INTO "members" ("account", "role", "pending") VALUES (NEW."createdBy", 'owner', false); END`,
params: [],
},
{
Expand Down
46 changes: 46 additions & 0 deletions tests/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,52 @@ test('inline statement parameters containing serialized expression', async () =>
});
});

test('inline statement parameters containing boolean', async () => {
const queries: Array<Query> = [
{
get: {
member: {
with: {
pending: false,
},
},
},
},
];

const models: Array<Model> = [
{
slug: 'member',
fields: [
{
slug: 'pending',
type: 'boolean',
},
],
},
];

const transaction = new Transaction(queries, {
models,
inlineParams: true,
});

expect(transaction.statements).toEqual([
{
statement: `SELECT "id", "ronin.locked", "ronin.createdAt", "ronin.createdBy", "ronin.updatedAt", "ronin.updatedBy", "pending" FROM "members" WHERE "pending" = false LIMIT 1`,
params: [],
returning: true,
},
]);

const rawResults = await queryEphemeralDatabase(models, transaction.statements);
const result = transaction.formatResults(rawResults)[0] as SingleRecordResult;

expect(result.record).toMatchObject({
pending: false,
});
});

// Ensure that default fields are not repeated if they are already present.
test('provide models containing default fields', async () => {
const queries: Array<Query> = [
Expand Down

0 comments on commit 0806bef

Please sign in to comment.