Skip to content

Commit ec593b4

Browse files
chore(plugin-multi-tenant): add better defaults for imported components (#11030)
Creates a default variables file to use in exported components. Extension of #10975.
1 parent a63a3d0 commit ec593b4

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const defaults = {
2+
tenantCollectionSlug: 'tenants',
3+
tenantFieldName: 'tenant',
4+
tenantsArrayFieldName: 'tenants',
5+
tenantsArrayTenantFieldName: 'tenant',
6+
}

packages/plugin-multi-tenant/src/fields/tenantField/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type RelationshipField } from 'payload'
22
import { APIError } from 'payload'
33

4+
import { defaults } from '../../defaults.js'
45
import { getCollectionIDType } from '../../utilities/getCollectionIDType.js'
56
import { getTenantFromCookie } from '../../utilities/getTenantFromCookie.js'
67

@@ -12,10 +13,10 @@ type Args = {
1213
unique: boolean
1314
}
1415
export const tenantField = ({
15-
name,
16+
name = defaults.tenantFieldName,
1617
access = undefined,
1718
debug,
18-
tenantsCollectionSlug,
19+
tenantsCollectionSlug = defaults.tenantCollectionSlug,
1920
unique,
2021
}: Args): RelationshipField => ({
2122
name,
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
import type { ArrayField, RelationshipField } from 'payload'
22

3-
export const tenantsArrayField = (args: {
3+
import { defaults } from '../../defaults.js'
4+
5+
type Args = {
46
arrayFieldAccess?: ArrayField['access']
57
rowFields?: ArrayField['fields']
68
tenantFieldAccess?: RelationshipField['access']
79
tenantsArrayFieldName: ArrayField['name']
810
tenantsArrayTenantFieldName: RelationshipField['name']
911
tenantsCollectionSlug: string
10-
}): ArrayField => ({
11-
name: args.tenantsArrayFieldName,
12+
}
13+
export const tenantsArrayField = ({
14+
arrayFieldAccess,
15+
rowFields,
16+
tenantFieldAccess,
17+
tenantsArrayFieldName = defaults.tenantsArrayFieldName,
18+
tenantsArrayTenantFieldName = defaults.tenantsArrayFieldName,
19+
tenantsCollectionSlug = defaults.tenantCollectionSlug,
20+
}: Args): ArrayField => ({
21+
name: tenantsArrayFieldName,
1222
type: 'array',
13-
access: args?.arrayFieldAccess,
23+
access: arrayFieldAccess,
1424
fields: [
1525
{
16-
name: args.tenantsArrayTenantFieldName,
26+
name: tenantsArrayTenantFieldName,
1727
type: 'relationship',
18-
access: args.tenantFieldAccess,
28+
access: tenantFieldAccess,
1929
index: true,
20-
relationTo: args.tenantsCollectionSlug,
30+
relationTo: tenantsCollectionSlug,
2131
required: true,
2232
saveToJWT: true,
2333
},
24-
...(args?.rowFields || []),
34+
...(rowFields || []),
2535
],
2636
saveToJWT: true,
2737
})

packages/plugin-multi-tenant/src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,14 @@ import type { CollectionConfig, Config } from 'payload'
22

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

5+
import { defaults } from './defaults.js'
56
import { tenantField } from './fields/tenantField/index.js'
67
import { tenantsArrayField } from './fields/tenantsArrayField/index.js'
78
import { addTenantCleanup } from './hooks/afterTenantDelete.js'
89
import { addCollectionAccess } from './utilities/addCollectionAccess.js'
910
import { addFilterOptionsToFields } from './utilities/addFilterOptionsToFields.js'
1011
import { withTenantListFilter } from './utilities/withTenantListFilter.js'
1112

12-
const defaults = {
13-
tenantCollectionSlug: 'tenants',
14-
tenantFieldName: 'tenant',
15-
tenantsArrayFieldName: 'tenants',
16-
tenantsArrayTenantFieldName: 'tenant',
17-
}
18-
1913
export const multiTenantPlugin =
2014
<ConfigType>(pluginConfig: MultiTenantPluginConfig<ConfigType>) =>
2115
(incomingConfig: Config): Config => {

0 commit comments

Comments
 (0)