-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
Provide an option to disable JSON parsing #1072
Comments
curious what's your reason to have it? If it for perf reasons, or you want to use custom json deserialisation or something else? |
I’m dumping the rows to a JSON file (for seeding the DB later) and this transformation causes JSON columns to be returned as objects rather than a string of JSON. |
gotcha. Right now you can do this via I'm probably OK with adding |
Yeah, here is what I settled on to get unblocked (in case anyone else finds it helpful): async function performWithTypeCastsDisabled(ctx: AppContext, fn: () => Promise<any>) {
const oldTypeCast = ctx.db.client.connectionSettings.typeCast;
const oldDateStrings = ctx.db.client.connectionSettings.dateStrings;
// Provide a custom typeCast functionm that disables JSON parsing
// See https://github.com/sidorares/node-mysql2/issues/1072
ctx.db.client.connectionSettings.typeCast = function (field: any, next: any, packet: any) {
if (field.type === "JSON") {
return field.string();
} else {
return next();
}
}
// Disable parsing to date object because this causes the format
// to change and it isn't directly re-importable after.
ctx.db.client.connectionSettings.dateStrings = true;
await ctx.db.destroy();
await ctx.db.initialize();
await fn();
ctx.db.client.connectionSettings.typeCast = oldTypeCast;
ctx.db.client.connectionSettings.dateStrings = oldDateStrings;
await ctx.db.destroy();
return ctx.db.initialize();
} |
typeorm mysql driver will try to parse string data from json type. but pure string is json too. if stored string data in json column will crash in typeorm typeorm/typeorm#7445 (comment) |
I need to disable this as well. It's for perf reasons. Also |
For those finding this issue - you should probably use |
For the reasons listed at #1287 I think adding jsonStrings would be super userful :) |
Agree, unfortunately would have to be true by default. In the hindsight I should have probably introduced this feature toggled off by default but too late to change now |
Any updates on it? |
We have encountered the same issue while upgrading to MySQL 8 and using @Table('user')
class UserRow {
@Column({ primary: true })
id!: string;
@Column()
name!: string;
@Column()
metadata!: string; // this is a JSON column type
} The private toEntity(row: UserRow): User {
return {
id: row.id,
name: row.name,
metadata: JSON.parse(metadata) as UserMetadata
}
} This will cause a I was able to overcome this challenge by adding a connection: {
user: config.user,
password: config.password,
host: config.host,
port: config.port,
database: config.database,
timezone: '+00:00',
dateStrings: true,
charset: 'utf8mb4',
typeCast: (field: TypeCastField, next: TypeCastNext) => {
if (field.type === 'JSON') {
return field.string('utf8');
}
return next();
},
} |
Thanks @wlarch ! These approach worked for me also! Any plans to add an setting to remove the auto json parse ?
|
Hi guys, |
In both
binary_parser.js
andtext_parser.js
we're parsing the JSON string to a native object:Similarly to the configuration option
dateStrings
, it would be nice to provide something likejsonStrings
which would avoid this and just return the utf8 string.The text was updated successfully, but these errors were encountered: