169 lines
3.5 KiB
TypeScript
169 lines
3.5 KiB
TypeScript
import type { CollectionConfig } from '../../src/collections/config/types';
|
|
import { devUser } from '../credentials';
|
|
import { buildConfig } from '../buildConfig';
|
|
import type { Post } from './payload-types';
|
|
|
|
export interface Relation {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
const openAccess = {
|
|
create: () => true,
|
|
read: () => true,
|
|
update: () => true,
|
|
delete: () => true,
|
|
};
|
|
|
|
const collectionWithName = (collectionSlug: string): CollectionConfig => {
|
|
return {
|
|
slug: collectionSlug,
|
|
access: openAccess,
|
|
fields: [
|
|
{
|
|
name: 'name',
|
|
type: 'text',
|
|
},
|
|
],
|
|
};
|
|
};
|
|
|
|
export const slug = 'posts';
|
|
export const relationSlug = 'relation';
|
|
export const pointSlug = 'point';
|
|
export default buildConfig({
|
|
collections: [
|
|
{
|
|
slug,
|
|
access: openAccess,
|
|
fields: [
|
|
{
|
|
name: 'title',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'description',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'number',
|
|
type: 'number',
|
|
},
|
|
// Relationship
|
|
{
|
|
name: 'relationField',
|
|
type: 'relationship',
|
|
relationTo: relationSlug,
|
|
},
|
|
// Relation hasMany
|
|
{
|
|
name: 'relationHasManyField',
|
|
type: 'relationship',
|
|
relationTo: relationSlug,
|
|
hasMany: true,
|
|
},
|
|
// Relation multiple relationTo
|
|
{
|
|
name: 'relationMultiRelationTo',
|
|
type: 'relationship',
|
|
relationTo: [relationSlug, 'dummy'],
|
|
},
|
|
// Relation multiple relationTo hasMany
|
|
{
|
|
name: 'relationMultiRelationToHasMany',
|
|
type: 'relationship',
|
|
relationTo: [relationSlug, 'dummy'],
|
|
hasMany: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: pointSlug,
|
|
access: openAccess,
|
|
fields: [
|
|
{
|
|
type: 'point',
|
|
name: 'point',
|
|
},
|
|
],
|
|
},
|
|
collectionWithName(relationSlug),
|
|
collectionWithName('dummy'),
|
|
],
|
|
onInit: async (payload) => {
|
|
await payload.create({
|
|
collection: 'users',
|
|
data: {
|
|
email: devUser.email,
|
|
password: devUser.password,
|
|
},
|
|
});
|
|
|
|
const rel1 = await payload.create<Relation>({
|
|
collection: relationSlug,
|
|
data: {
|
|
name: 'name',
|
|
},
|
|
});
|
|
const rel2 = await payload.create<Relation>({
|
|
collection: relationSlug,
|
|
data: {
|
|
name: 'name2',
|
|
},
|
|
});
|
|
|
|
await payload.create({
|
|
collection: pointSlug,
|
|
data: {
|
|
point: [10, 20],
|
|
},
|
|
});
|
|
|
|
// Relation - hasMany
|
|
await payload.create<Post>({
|
|
collection: slug,
|
|
data: {
|
|
title: 'rel to hasMany',
|
|
relationHasManyField: rel1.id,
|
|
},
|
|
});
|
|
await payload.create<Post>({
|
|
collection: slug,
|
|
data: {
|
|
title: 'rel to hasMany 2',
|
|
relationHasManyField: rel2.id,
|
|
},
|
|
});
|
|
|
|
// Relation - relationTo multi
|
|
await payload.create<Post>({
|
|
collection: slug,
|
|
data: {
|
|
title: 'rel to multi',
|
|
relationMultiRelationTo: {
|
|
relationTo: relationSlug,
|
|
value: rel2.id,
|
|
},
|
|
},
|
|
});
|
|
|
|
// Relation - relationTo multi hasMany
|
|
await payload.create<Post>({
|
|
collection: slug,
|
|
data: {
|
|
title: 'rel to multi hasMany',
|
|
relationMultiRelationToHasMany: [
|
|
{
|
|
relationTo: relationSlug,
|
|
value: rel1.id,
|
|
},
|
|
{
|
|
relationTo: relationSlug,
|
|
value: rel2.id,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
},
|
|
});
|