Skip to content

Commit

Permalink
Merge branch 'main' into fix/where-builder-closing-on-remove
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed Feb 6, 2025
2 parents 65b07ab + 7a73265 commit d549016
Show file tree
Hide file tree
Showing 37 changed files with 456 additions and 274 deletions.
6 changes: 5 additions & 1 deletion packages/drizzle/src/createGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export async function createGlobal<T extends Record<string, unknown>>(

const tableName = this.tableNameMap.get(toSnakeCase(globalConfig.slug))

const result = await upsertRow<T>({
data.createdAt = new Date().toISOString()

const result = await upsertRow<{ globalType: string } & T>({
adapter: this,
data,
db,
Expand All @@ -26,5 +28,7 @@ export async function createGlobal<T extends Record<string, unknown>>(
tableName,
})

result.globalType = slug

return result
}
1 change: 1 addition & 0 deletions packages/drizzle/src/transform/write/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const transformArray = ({
data: arrayRow,
fieldPrefix: '',
fields: field.flattenedFields,
insideArrayOrBlock: true,
locales: newRow.locales,
numbers,
parentTableName: arrayTableName,
Expand Down
1 change: 1 addition & 0 deletions packages/drizzle/src/transform/write/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const transformBlocks = ({
data: blockRow,
fieldPrefix: '',
fields: matchedBlock.flattenedFields,
insideArrayOrBlock: true,
locales: newRow.locales,
numbers,
parentTableName: blockTableName,
Expand Down
24 changes: 18 additions & 6 deletions packages/drizzle/src/transform/write/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Args = {
fieldPrefix: string
fields: FlattenedField[]
forcedLocale?: string
/**
* Tracks whether the current traversion context is from array or block.
*/
insideArrayOrBlock?: boolean
locales: {
[locale: string]: Record<string, unknown>
}
Expand Down Expand Up @@ -77,6 +81,7 @@ export const traverseFields = ({
fieldPrefix,
fields,
forcedLocale,
insideArrayOrBlock = false,
locales,
numbers,
parentTableName,
Expand Down Expand Up @@ -230,6 +235,7 @@ export const traverseFields = ({
fieldPrefix: `${fieldName}_`,
fields: field.flattenedFields,
forcedLocale: localeKey,
insideArrayOrBlock,
locales,
numbers,
parentTableName,
Expand Down Expand Up @@ -258,6 +264,7 @@ export const traverseFields = ({
existingLocales,
fieldPrefix: `${fieldName}_`,
fields: field.flattenedFields,
insideArrayOrBlock,
locales,
numbers,
parentTableName,
Expand Down Expand Up @@ -420,7 +427,7 @@ export const traverseFields = ({
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
if (Array.isArray(localeData)) {
const newRows = transformSelects({
id: data._uuid || data.id,
id: insideArrayOrBlock ? data._uuid || data.id : undefined,
data: localeData,
locale: localeKey,
})
Expand All @@ -431,7 +438,7 @@ export const traverseFields = ({
}
} else if (Array.isArray(data[field.name])) {
const newRows = transformSelects({
id: data._uuid || data.id,
id: insideArrayOrBlock ? data._uuid || data.id : undefined,
data: data[field.name],
locale: withinArrayOrBlockLocale,
})
Expand Down Expand Up @@ -472,8 +479,9 @@ export const traverseFields = ({
}

valuesToTransform.forEach(({ localeKey, ref, value }) => {
let formattedValue = value

if (typeof value !== 'undefined') {
let formattedValue = value
if (value && field.type === 'point' && adapter.name !== 'sqlite') {
formattedValue = sql`ST_GeomFromGeoJSON(${JSON.stringify(value)})`
}
Expand All @@ -483,12 +491,16 @@ export const traverseFields = ({
formattedValue = new Date(value).toISOString()
} else if (value instanceof Date) {
formattedValue = value.toISOString()
} else if (fieldName === 'updatedAt') {
// let the db handle this
formattedValue = new Date().toISOString()
}
}
}

if (field.type === 'date' && fieldName === 'updatedAt') {
// let the db handle this
formattedValue = new Date().toISOString()
}

if (typeof formattedValue !== 'undefined') {
if (localeKey) {
ref[localeKey][fieldName] = formattedValue
} else {
Expand Down
4 changes: 3 additions & 1 deletion packages/drizzle/src/updateGlobal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function updateGlobal<T extends Record<string, unknown>>(

const existingGlobal = await db.query[tableName].findFirst({})

const result = await upsertRow<T>({
const result = await upsertRow<{ globalType: string } & T>({
...(existingGlobal ? { id: existingGlobal.id, operation: 'update' } : { operation: 'create' }),
adapter: this,
data,
Expand All @@ -28,5 +28,7 @@ export async function updateGlobal<T extends Record<string, unknown>>(
tableName,
})

result.globalType = slug

return result
}
14 changes: 14 additions & 0 deletions packages/next/src/layouts/Root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ export const RootLayout = async ({
importMap,
})

if (
clientConfig.localization &&
config.localization &&
typeof config.localization.filterAvailableLocales === 'function'
) {
clientConfig.localization.locales = (
await config.localization.filterAvailableLocales({
locales: config.localization.locales,
req,
})
).map(({ toString, ...rest }) => rest)
clientConfig.localization.localeCodes = config.localization.locales.map(({ code }) => code)
}

const locale = await getRequestLocale({
req,
})
Expand Down
8 changes: 8 additions & 0 deletions packages/payload/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ export type BaseLocalizationConfig = {
* @default true
*/
fallback?: boolean
/**
* Define a function to filter the locales made available in Payload admin UI
* based on user.
*/
filterAvailableLocales?: (args: {
locales: Locale[]
req: PayloadRequest
}) => Locale[] | Promise<Locale[]>
}

export type LocalizationConfigWithNoLabels = Prettify<
Expand Down
5 changes: 5 additions & 0 deletions packages/payload/src/globals/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ export const updateOperation = async <
// /////////////////////////////////////

if (!shouldSaveDraft) {
// Ensure global has createdAt
if (!result.createdAt) {
result.createdAt = new Date().toISOString()
}

if (globalExists) {
result = await payload.db.updateGlobal({
slug,
Expand Down
4 changes: 3 additions & 1 deletion packages/payload/src/utilities/configToJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ function entityOrFieldToJsDocs({
description = entity?.admin?.description?.[i18n.language]
}
} else if (typeof entity?.admin?.description === 'function' && i18n) {
description = entity?.admin?.description(i18n)
// do not evaluate description functions for generating JSDocs. The output of
// those can differ depending on where and when they are called, creating
// inconsistencies in the generated JSDocs.
}
}
return description
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-multi-tenant/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const defaults = {
tenantCollectionSlug: 'tenants',
tenantFieldName: 'tenant',
tenantsArrayFieldName: 'tenants',
tenantsArrayTenantFieldName: 'tenant',
}
5 changes: 3 additions & 2 deletions packages/plugin-multi-tenant/src/fields/tenantField/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type RelationshipField } from 'payload'
import { APIError } from 'payload'

import { defaults } from '../../defaults.js'
import { getCollectionIDType } from '../../utilities/getCollectionIDType.js'
import { getTenantFromCookie } from '../../utilities/getTenantFromCookie.js'

Expand All @@ -12,10 +13,10 @@ type Args = {
unique: boolean
}
export const tenantField = ({
name,
name = defaults.tenantFieldName,
access = undefined,
debug,
tenantsCollectionSlug,
tenantsCollectionSlug = defaults.tenantCollectionSlug,
unique,
}: Args): RelationshipField => ({
name,
Expand Down
26 changes: 18 additions & 8 deletions packages/plugin-multi-tenant/src/fields/tenantsArrayField/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import type { ArrayField, RelationshipField } from 'payload'

export const tenantsArrayField = (args: {
import { defaults } from '../../defaults.js'

type Args = {
arrayFieldAccess?: ArrayField['access']
rowFields?: ArrayField['fields']
tenantFieldAccess?: RelationshipField['access']
tenantsArrayFieldName: ArrayField['name']
tenantsArrayTenantFieldName: RelationshipField['name']
tenantsCollectionSlug: string
}): ArrayField => ({
name: args.tenantsArrayFieldName,
}
export const tenantsArrayField = ({
arrayFieldAccess,
rowFields,
tenantFieldAccess,
tenantsArrayFieldName = defaults.tenantsArrayFieldName,
tenantsArrayTenantFieldName = defaults.tenantsArrayFieldName,
tenantsCollectionSlug = defaults.tenantCollectionSlug,
}: Args): ArrayField => ({
name: tenantsArrayFieldName,
type: 'array',
access: args?.arrayFieldAccess,
access: arrayFieldAccess,
fields: [
{
name: args.tenantsArrayTenantFieldName,
name: tenantsArrayTenantFieldName,
type: 'relationship',
access: args.tenantFieldAccess,
access: tenantFieldAccess,
index: true,
relationTo: args.tenantsCollectionSlug,
relationTo: tenantsCollectionSlug,
required: true,
saveToJWT: true,
},
...(args?.rowFields || []),
...(rowFields || []),
],
saveToJWT: true,
})
8 changes: 1 addition & 7 deletions packages/plugin-multi-tenant/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ import type { CollectionConfig, Config } from 'payload'

import type { MultiTenantPluginConfig } from './types.js'

import { defaults } from './defaults.js'
import { tenantField } from './fields/tenantField/index.js'
import { tenantsArrayField } from './fields/tenantsArrayField/index.js'
import { addTenantCleanup } from './hooks/afterTenantDelete.js'
import { addCollectionAccess } from './utilities/addCollectionAccess.js'
import { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js'
import { withTenantListFilter } from './utilities/withTenantListFilter.js'

const defaults = {
tenantCollectionSlug: 'tenants',
tenantFieldName: 'tenant',
tenantsArrayFieldName: 'tenants',
tenantsArrayTenantFieldName: 'tenant',
}

export const multiTenantPlugin =
<ConfigType>(pluginConfig: MultiTenantPluginConfig<ConfigType>) =>
(incomingConfig: Config): Config => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ export const blockValidationHOC = (
schemaPath: '',
})

let errorPaths: string[] = []
const errorPathsSet = new Set<string>()
for (const fieldKey in result) {
if (result[fieldKey]?.errorPaths) {
errorPaths = errorPaths.concat(result[fieldKey].errorPaths)
if (result[fieldKey].errorPaths?.length) {
for (const errorPath of result[fieldKey].errorPaths) {
errorPathsSet.add(errorPath)
}
}
}
const errorPaths = Array.from(errorPathsSet)

if (errorPaths.length) {
return 'The following fields are invalid: ' + errorPaths.join(', ')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ export const linkValidation = (
schemaPath: '',
})

let errorPaths: string[] = []
const errorPathsSet = new Set<string>()
for (const fieldKey in result) {
if (result[fieldKey]?.errorPaths) {
errorPaths = errorPaths.concat(result[fieldKey].errorPaths)
if (result[fieldKey].errorPaths?.length) {
for (const errorPath of result[fieldKey].errorPaths) {
errorPathsSet.add(errorPath)
}
}
}
const errorPaths = Array.from(errorPathsSet)

if (errorPaths.length) {
return 'The following fields are invalid: ' + errorPaths.join(', ')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ export const uploadValidation = (
schemaPath: '',
})

let errorPaths: string[] = []
const errorPathsSet = new Set<string>()
for (const fieldKey in result) {
if (result[fieldKey].errorPaths) {
errorPaths = errorPaths.concat(result[fieldKey].errorPaths)
if (result[fieldKey].errorPaths?.length) {
for (const errorPath of result[fieldKey].errorPaths) {
errorPathsSet.add(errorPath)
}
}
}
const errorPaths = Array.from(errorPathsSet)

if (errorPaths.length) {
return 'The following fields are invalid: ' + errorPaths.join(', ')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DatePickerField } from '../../../DatePicker/index.js'

const baseClass = 'condition-value-date'

export const DateField: React.FC<Props> = ({ disabled, field: { admin }, onChange, value }) => {
export const DateFilter: React.FC<Props> = ({ disabled, field: { admin }, onChange, value }) => {
const { date } = admin || {}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import React from 'react'

import type { FieldCondition } from '../../types.js'

import { DateField } from '../Date/index.js'
import { NumberField } from '../Number/index.js'
import { RelationshipField } from '../Relationship/index.js'
import { DateFilter } from '../Date/index.js'
import { NumberFilter } from '../Number/index.js'
import { RelationshipFilter } from '../Relationship/index.js'
import { Select } from '../Select/index.js'
import { Text } from '../Text/index.js'

Expand Down Expand Up @@ -45,7 +45,7 @@ export const DefaultFilter: React.FC<Props> = ({
switch (internalField?.field?.type) {
case 'date': {
return (
<DateField
<DateFilter
disabled={disabled}
field={internalField.field}
onChange={onChange}
Expand All @@ -57,7 +57,7 @@ export const DefaultFilter: React.FC<Props> = ({

case 'number': {
return (
<NumberField
<NumberFilter
disabled={disabled}
field={internalField.field}
onChange={onChange}
Expand All @@ -69,7 +69,7 @@ export const DefaultFilter: React.FC<Props> = ({

case 'relationship': {
return (
<RelationshipField
<RelationshipFilter
disabled={disabled}
field={internalField.field}
onChange={onChange}
Expand Down
Loading

0 comments on commit d549016

Please sign in to comment.