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

event.params in log query event is not always a valid JSON #12441

Open
unlight opened this issue Mar 20, 2022 · 3 comments
Open

event.params in log query event is not always a valid JSON #12441

unlight opened this issue Mar 20, 2022 · 3 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: logging

Comments

@unlight
Copy link

unlight commented Mar 20, 2022

Bug description

event.params in log query event is not always a valid JSON

How to reproduce

  1. git clone https://github.com/prisma/prisma-examples.git
  2. cd prisma-examples\typescript\script
  3. npm install
  4. Change script.ts
const prisma = new PrismaClient({
  log: [
    {
      emit: 'event',
      level: 'query',
    },
  ],
})

prisma.$on('query', (event) => {
  console.log(event)
})

await prisma.post.create({
  data: {
    title: `"title"`,
  },
})
  1. Run npx ts-node ./script.ts
  2. Check string in params
{
  timestamp: 2022-03-20T13:45:23.604Z,
  query: 'INSERT INTO `main`.`Post` (`title`, `published`) VALUES (?,?) RETURNING id',
  params: '[""title"",false]', // <--- invalid json
}

Invalid escaping happens with:

  1. String with double quotes
  2. Dates

Expected behavior

event.params should be valid JSON

Prisma information

Valid JSON and therefore param substitution will be helpful in custom loggers for prisma

unlight/prisma-query-log#4

Environment & setup

node v16.13.2

Prisma Version

prisma                  : 3.11.0
@prisma/client          : 3.11.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : b371888aaf8f51357c7457d836b86d12da91658b
Studio                  : 0.458.0
@unlight unlight added the kind/bug A reported bug. label Mar 20, 2022
@unlight unlight changed the title event.arams in log query event is not always a valid JSON event.params in log query event is not always a valid JSON Mar 20, 2022
@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. topic: logging labels Mar 21, 2022
@terrierscript
Copy link

terrierscript commented Aug 30, 2022

Workaround: I can parse invalid params as CSV ( this sample use papaparse )

import * as Papa from "papaparse"

const parseParam = (param: string) => {
  const removeBlacket = param
    .replace(/^\[/, "")
    .replace(/\]$/, "")
  const parsed = Papa.parse(removeBlacket, {
    dynamicTyping: true
  })
  return parsed.data[0]
}

@revero-doug
Copy link

you'll want to pass this option to Papa.parse as well: transform: a => a == "null" ? null : a or else you'll get instances of null as strings

@revero-doug
Copy link

update: in the absence of escaping, csv parsing does not generally work, either. whether papaparse or any other known workaround works for you will be highly dependent on the contents of your params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: logging
Projects
None yet
Development

No branches or pull requests

5 participants