-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
afbb37f
commit fd989ce
Showing
3 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/packages/client/src/__tests__/serializeRawParameters.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { replaceDates } from '../runtime/utils/serializeRawParameters' | ||
|
||
test('serializeRawParemeters object', () => { | ||
const data = { | ||
date: new Date('2020-06-22T17:07:16.348Z'), | ||
} | ||
|
||
expect(replaceDates(data)).toMatchInlineSnapshot(` | ||
Object { | ||
"date": Object { | ||
"prisma__type": "date", | ||
"prisma__value": "2020-06-22T17:07:16.348Z", | ||
}, | ||
} | ||
`) | ||
}) | ||
|
||
test('serializeRawParemeters array', () => { | ||
const data = { | ||
date: [new Date('2020-06-22T17:07:16.348Z')], | ||
} | ||
|
||
expect(replaceDates(data)).toMatchInlineSnapshot(` | ||
Object { | ||
"date": Array [ | ||
Object { | ||
"prisma__type": "date", | ||
"prisma__value": "2020-06-22T17:07:16.348Z", | ||
}, | ||
], | ||
} | ||
`) | ||
}) | ||
|
||
test('serializeRawParemeters scalar', () => { | ||
const data = new Date('2020-06-22T17:07:16.348Z') | ||
|
||
expect(replaceDates(data)).toMatchInlineSnapshot(` | ||
Object { | ||
"prisma__type": "date", | ||
"prisma__value": "2020-06-22T17:07:16.348Z", | ||
} | ||
`) | ||
}) | ||
|
||
test('serializeRawParemeters nested', () => { | ||
const data = { | ||
deep: { | ||
date: [ | ||
new Date('2020-06-22T17:07:16.348Z'), | ||
new Date('2020-06-22T17:07:16.348Z'), | ||
], | ||
}, | ||
} | ||
|
||
expect(replaceDates(data)).toMatchInlineSnapshot(` | ||
Object { | ||
"deep": Object { | ||
"date": Array [ | ||
Object { | ||
"prisma__type": "date", | ||
"prisma__value": "2020-06-22T17:07:16.348Z", | ||
}, | ||
Object { | ||
"prisma__type": "date", | ||
"prisma__value": "2020-06-22T17:07:16.348Z", | ||
}, | ||
], | ||
}, | ||
} | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/packages/client/src/runtime/utils/serializeRawParameters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export function serializeRawParameters(data: any): string { | ||
return JSON.stringify(replaceDates(data)) | ||
} | ||
|
||
/** | ||
* Replaces Date as needed in https://github.com/prisma/prisma-engines/pull/835 | ||
* @param data parameters | ||
*/ | ||
export function replaceDates(data: any): any { | ||
const type = Object.prototype.toString.call(data) | ||
|
||
if (type === '[object Date]') { | ||
return { | ||
prisma__type: 'date', | ||
prisma__value: data.toJSON(), | ||
} | ||
} | ||
|
||
if (type === '[object Object]') { | ||
const tmp = {} | ||
for (let key in data) { | ||
if (key !== '__proto__') { | ||
tmp[key] = replaceDates(data[key]) | ||
} | ||
} | ||
return tmp | ||
} | ||
|
||
if (type === '[object Array]') { | ||
let k = data.length | ||
let tmp | ||
for (tmp = new Array(k); k--; ) { | ||
tmp[k] = replaceDates(data[k]) | ||
} | ||
return tmp | ||
} | ||
|
||
return data | ||
} |