eveningkid / denodb

MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
https://eveningkid.com/denodb-docs
MIT License
1.93k stars 129 forks source link

Issue when trying to save json #155

Open futbotism opened 3 years ago

futbotism commented 3 years ago

When I try to save json in the database I seem to get the error below, I am using postgres

export class ReflectionEntity extends Model {
  static table = "reflections";

  static fields: Record<keyof Reflection, FieldType> = {
    id: {
      primaryKey: true,
      type: DataTypes.INTEGER,
      autoIncrement: true,
    },
    notes: {
      type: DataTypes.JSON,
    }
  };
}
api_1       | ReferenceError: Buffer is not defined
api_1       |     at escapeFn (https://raw.githubusercontent.com/denjucks/dex/master/lib/query/string.js:44:16)
api_1       |     at finalEscape (https://raw.githubusercontent.com/denjucks/dex/master/lib/query/string.js:18:12)
api_1       |     at arrayString (https://raw.githubusercontent.com/denjucks/dex/master/lib/dialects/postgres/index.js:315:64)
api_1       |     at escapeArray (https://raw.githubusercontent.com/denjucks/dex/master/lib/dialects/postgres/index.js:56:18)
api_1       |     at escapeFn (https://raw.githubusercontent.com/denjucks/dex/master/lib/query/string.js:43:18)
api_1       |     at Client_PG.finalEscape [as _escapeBinding] (https://raw.githubusercontent.com/denjucks/dex/master/lib/query/string.js:18:12)
api_1       |     at https://raw.githubusercontent.com/denjucks/dex/master/lib/client.js:151:19
api_1       |     at String.replace (<anonymous>)
api_1       |     at Client_PG._formatQuery (https://raw.githubusercontent.com/denjucks/dex/master/lib/client.js:143:16)
api_1       |     at https://raw.githubusercontent.com/denjucks/dex/master/lib/interface.js:13:28
jrdx0 commented 3 years ago

Hi! If you still get this error, you have to parse the JSON to a string with JSON.stringify function. Tell me if that works!

JSEverything commented 3 years ago

This is still an issue but for a different reason. It is impossible to update entries containing a JSON field because getting the entry parses the JSON, and if the JSON field isn't updated it will still be parsed JSON when doing entry.update(), throwing an error because it's supposed to be stringified before saving.