Skip to content

Commit

Permalink
fix: encode JSON fields on load (#256)
Browse files Browse the repository at this point in the history
JSON fields have to be encoded when saving on PostgreSQL.
We already encode them when set manually, but don't when we get those
values from database on load.
  • Loading branch information
Sekhmet authored Sep 26, 2023
1 parent 1dfc562 commit 6eaba3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ export const codegen = (
contents += ` const model = new ${modelName}(id);\n`;
contents += ` model.setExists();\n\n`;
contents += ` for (const key in entity) {\n`;
contents += ` model.set(key, entity[key]);\n`;
contents += ` const value = entity[key] !== null && typeof entity[key] === 'object'\n`;
contents += ` ? JSON.stringify(entity[key])\n`;
contents += ` : entity[key];\n`;
contents += ` model.set(key, value);\n`;
contents += ` }\n\n`;
contents += ` return model;\n`;
contents += ` }\n\n`;
Expand Down
20 changes: 16 additions & 4 deletions test/unit/__snapshots__/codegen.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export class Space extends Model {
model.setExists();
for (const key in entity) {
model.set(key, entity[key]);
const value = entity[key] !== null && typeof entity[key] === 'object'
? JSON.stringify(entity[key])
: entity[key];
model.set(key, value);
}
return model;
Expand Down Expand Up @@ -130,7 +133,10 @@ export class Proposal extends Model {
model.setExists();
for (const key in entity) {
model.set(key, entity[key]);
const value = entity[key] !== null && typeof entity[key] === 'object'
? JSON.stringify(entity[key])
: entity[key];
model.set(key, value);
}
return model;
Expand Down Expand Up @@ -223,7 +229,10 @@ export class Space extends Model {
model.setExists();
for (const key in entity) {
model.set(key, entity[key]);
const value = entity[key] !== null && typeof entity[key] === 'object'
? JSON.stringify(entity[key])
: entity[key];
model.set(key, value);
}
return model;
Expand Down Expand Up @@ -325,7 +334,10 @@ export class Proposal extends Model {
model.setExists();
for (const key in entity) {
model.set(key, entity[key]);
const value = entity[key] !== null && typeof entity[key] === 'object'
? JSON.stringify(entity[key])
: entity[key];
model.set(key, value);
}
return model;
Expand Down

0 comments on commit 6eaba3a

Please sign in to comment.