Files
payload/demo/collections/Geolocation.ts
Jarrod Flesch 8591d97fa5 Adjusts Point field docs and test variables (#382)
* fix: corrects the label order and removes confusion for the values saved on a Point field type

* fix: adjusts code to mirror the doc change, replaces [x, y] with [lng, lat] and add type names to Geolocation

* chore: add nested point field test cases

* fix: index true creates a 2dsphere spatial index on point fields

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
2022-01-05 17:46:01 -05:00

91 lines
2.5 KiB
TypeScript

/* eslint-disable no-param-reassign */
import { CollectionConfig } from '../../src/collections/config/types';
const validateFieldTransformAction = (hook: string, value) => {
if (value !== undefined && value !== null && !Array.isArray(value)) {
console.error(hook, value);
throw new Error('Field transformAction should convert value to array [x, y] and not { coordinates: [x, y] }');
}
return value;
};
const Geolocation: CollectionConfig = {
slug: 'geolocation',
labels: {
singular: 'Geolocation',
plural: 'Geolocations',
},
access: {
read: () => true,
},
hooks: {
beforeRead: [
(operation) => operation.doc,
],
beforeChange: [
(operation) => {
operation.data.beforeChange = !operation.data.location?.coordinates;
return operation.data;
},
],
afterRead: [
(operation) => {
const { doc } = operation;
doc.afterReadHook = !doc.location?.coordinates;
return doc;
},
],
afterChange: [
(operation) => {
const { doc } = operation;
doc.afterChangeHook = !doc.location?.coordinates;
return doc;
},
],
afterDelete: [
(operation) => {
const { doc } = operation;
operation.doc.afterDeleteHook = !doc.location?.coordinates;
return doc;
},
],
},
fields: [
{
name: 'location',
type: 'point',
label: 'Location',
hooks: {
beforeValidate: [({ value }) => validateFieldTransformAction('beforeValidate', value)],
beforeChange: [({ value }) => validateFieldTransformAction('beforeChange', value)],
afterChange: [({ value }) => validateFieldTransformAction('afterChange', value)],
afterRead: [({ value }) => validateFieldTransformAction('afterRead', value)],
},
},
{
name: 'localizedPoint',
type: 'point',
label: 'Localized Point',
localized: true,
hooks: {
beforeValidate: [({ value }) => validateFieldTransformAction('beforeValidate', value)],
beforeChange: [({ value }) => validateFieldTransformAction('beforeChange', value)],
afterChange: [({ value }) => validateFieldTransformAction('afterChange', value)],
afterRead: [({ value }) => validateFieldTransformAction('afterRead', value)],
},
},
{
type: 'group',
name: 'group',
fields: [
{
name: 'point',
type: 'point',
},
],
},
],
};
export default Geolocation;