Skip to content

Commit

Permalink
fix(client): date parsing (#4984)
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jan 13, 2021
1 parent 4d3b606 commit abdad8a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/packages/client/src/__tests__/dateWhere.test.ts
Expand Up @@ -48,7 +48,8 @@ describe('date where filter', () => {
data: {
status: 'published',
publishedAt: new Date('2020-11-13T10:36:43.261Z'),
updatedAt: new Date('2020-11-13T10:36:43.261Z'),
// tests RFC 3339
updatedAt: '2021-01-13T12:40:47+01:00',
},
}
const document = makeDocument({
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('date where filter', () => {
data: {
status: "published"
publishedAt: "2020-11-13T10:36:43.261Z"
updatedAt: "2020-11-13T10:36:43.261Z"
updatedAt: "2021-01-13T12:40:47+01:00"
}
) {
count
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('date where filter', () => {
data: {
status: "published"
publishedAt: "2020-11-13T10:36:43.261Z"
updatedAt: "2020-11-13T10:36:43.261Z"
updatedAt: "2021-01-13T12:40:47+01:00"
}
) {
count
Expand Down
@@ -0,0 +1 @@
!dev.db
Binary file not shown.
@@ -0,0 +1,31 @@
datasource db {
provider = "sqlite"
url = "file:dev.db"
default = true
}

generator client {
provider = "prisma-client-js"
previewFeatures = ["groupBy"]
}

// / User model comment
model User {
id String @id @default(uuid())
email String @unique
age Int
// / name comment
name String?
posts Post[]
}

model Post {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean
title String
content String?
authorId String?
author User? @relation(fields: [authorId], references: [id])
}
@@ -0,0 +1,25 @@
import { getTestClient } from '../../../../utils/getTestClient'

let prisma

beforeAll(async () => {
const PrismaClient = await getTestClient()
prisma = new PrismaClient()
})

afterAll(() => {
prisma.$disconnect()
})

test('findMany filter by rfc3339 date string', async () => {
console.log('going for it')
const user = await prisma.post.findMany({
where: {
createdAt: {
gt: '2019-01-13T12:40:47+01:00',
},
},
})

expect(user).toMatchInlineSnapshot(`Array []`)
})
17 changes: 7 additions & 10 deletions src/packages/client/src/runtime/utils/common.ts
Expand Up @@ -123,6 +123,11 @@ export function wrapWithList(str: string, isList: boolean) {
return str
}

// from https://github.com/excitement-engineer/graphql-iso-date/blob/master/src/utils/validator.js#L121
const RFC_3339_REGEX = /^(\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60))(\.\d{1,})?(([Z])|([+|-]([01][0-9]|2[0-3]):[0-5][0-9]))$/

const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i

export function getGraphQLType(
value: any,
potentialType?: string | DMMF.SchemaEnum | DMMF.InputType,
Expand Down Expand Up @@ -172,11 +177,7 @@ export function getGraphQLType(
return 'DateTime'
}
if (jsType === 'string') {
if (
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
value,
)
) {
if (UUID_REGEX.test(value)) {
return 'UUID'
}
const date = new Date(value)
Expand All @@ -191,11 +192,7 @@ export function getGraphQLType(
if (date.toString() === 'Invalid Date') {
return 'String'
}
if (
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/.test(
value,
)
) {
if (RFC_3339_REGEX.test(value)) {
return 'DateTime'
}
}
Expand Down

0 comments on commit abdad8a

Please sign in to comment.