Skip to content

Commit 00909ec

Browse files
authored
fix(db-sqlite): working point field CRUD and default value (#9989)
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)
1 parent 2ec4d0c commit 00909ec

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

packages/db-sqlite/src/schema/traverseFields.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ export const traverseFields = ({
572572
}
573573

574574
case 'point': {
575+
targetTable[fieldName] = withDefault(text(columnName, { mode: 'json' }), field)
575576
break
576577
}
577578

packages/db-sqlite/src/schema/withDefault.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ export const withDefault = (
99
return column
1010
}
1111

12+
if (field.type === 'point' && Array.isArray(field.defaultValue)) {
13+
return column.default(
14+
JSON.stringify({
15+
type: 'Point',
16+
coordinates: [field.defaultValue[0], field.defaultValue[1]],
17+
}),
18+
)
19+
}
20+
1221
if (typeof field.defaultValue === 'string' && field.defaultValue.includes("'")) {
1322
const escapedString = field.defaultValue.replaceAll("'", "''")
1423
return column.default(escapedString)

packages/drizzle/src/transform/read/traverseFields.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,14 @@ export const traverseFields = <T extends Record<string, unknown>>({
599599
break
600600
}
601601

602+
case 'point': {
603+
if (typeof fieldData === 'string') {
604+
val = JSON.parse(fieldData)
605+
}
606+
607+
break
608+
}
609+
602610
case 'relationship':
603611
case 'upload': {
604612
if (

test/database/int.spec.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,17 @@ describe('database', () => {
754754
expect(helloDocs).toHaveLength(5)
755755
expect(worldDocs).toHaveLength(5)
756756
})
757+
758+
it('should CRUD point field', async () => {
759+
const result = await payload.create({
760+
collection: 'default-values',
761+
data: {
762+
point: [5, 10],
763+
},
764+
})
765+
766+
expect(result.point).toEqual([5, 10])
767+
})
757768
})
758769

759770
describe('defaultValue', () => {
@@ -773,12 +784,10 @@ describe('database', () => {
773784
expect(result.array[0].defaultValue).toStrictEqual('default value from database')
774785
expect(result.group.defaultValue).toStrictEqual('default value from database')
775786
expect(result.select).toStrictEqual('default')
776-
// eslint-disable-next-line jest/no-conditional-in-test
777-
if (payload.db.name !== 'sqlite') {
778-
expect(result.point).toStrictEqual({ coordinates: [10, 20], type: 'Point' })
779-
}
787+
expect(result.point).toStrictEqual({ coordinates: [10, 20], type: 'Point' })
780788
})
781789
})
790+
782791
describe('drizzle: schema hooks', () => {
783792
it('should add tables with hooks', async () => {
784793
// eslint-disable-next-line jest/no-conditional-in-test

0 commit comments

Comments
 (0)