Skip to content

Commit

Permalink
fix(db-sqlite): working point field CRUD and default value (#9989)
Browse files Browse the repository at this point in the history
Previously, the point field with SQLite was incorrectly built to the
schema and not parsed to the result.
Now, it works with SQLite, just doesn't support queries (`near` etc.).
Fixes
#9987 (comment)
  • Loading branch information
r1tsuu authored Dec 16, 2024
1 parent 2ec4d0c commit 00909ec
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/db-sqlite/src/schema/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ export const traverseFields = ({
}

case 'point': {
targetTable[fieldName] = withDefault(text(columnName, { mode: 'json' }), field)
break
}

Expand Down
9 changes: 9 additions & 0 deletions packages/db-sqlite/src/schema/withDefault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export const withDefault = (
return column
}

if (field.type === 'point' && Array.isArray(field.defaultValue)) {
return column.default(
JSON.stringify({
type: 'Point',
coordinates: [field.defaultValue[0], field.defaultValue[1]],
}),
)
}

if (typeof field.defaultValue === 'string' && field.defaultValue.includes("'")) {
const escapedString = field.defaultValue.replaceAll("'", "''")
return column.default(escapedString)
Expand Down
8 changes: 8 additions & 0 deletions packages/drizzle/src/transform/read/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,14 @@ export const traverseFields = <T extends Record<string, unknown>>({
break
}

case 'point': {
if (typeof fieldData === 'string') {
val = JSON.parse(fieldData)
}

break
}

case 'relationship':
case 'upload': {
if (
Expand Down
17 changes: 13 additions & 4 deletions test/database/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,17 @@ describe('database', () => {
expect(helloDocs).toHaveLength(5)
expect(worldDocs).toHaveLength(5)
})

it('should CRUD point field', async () => {
const result = await payload.create({
collection: 'default-values',
data: {
point: [5, 10],
},
})

expect(result.point).toEqual([5, 10])
})
})

describe('defaultValue', () => {
Expand All @@ -773,12 +784,10 @@ describe('database', () => {
expect(result.array[0].defaultValue).toStrictEqual('default value from database')
expect(result.group.defaultValue).toStrictEqual('default value from database')
expect(result.select).toStrictEqual('default')
// eslint-disable-next-line jest/no-conditional-in-test
if (payload.db.name !== 'sqlite') {
expect(result.point).toStrictEqual({ coordinates: [10, 20], type: 'Point' })
}
expect(result.point).toStrictEqual({ coordinates: [10, 20], type: 'Point' })
})
})

describe('drizzle: schema hooks', () => {
it('should add tables with hooks', async () => {
// eslint-disable-next-line jest/no-conditional-in-test
Expand Down

0 comments on commit 00909ec

Please sign in to comment.