Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
"test": "rimraf src/uploads && jest --detectOpenHandles --forceExit"
},
"dependencies": {
"@payloadcms/db-mongodb": "^1.4.3",
"@payloadcms/plugin-nested-docs": "^1.0.12",
"@payloadcms/richtext-lexical": "^0.7.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"payload": "^1.9.5",
"payload": "^2.11.2",
"rimraf": "^5.0.1"
},
"devDependencies": {
Expand All @@ -30,4 +33,4 @@
"ts-node": "^10.9.1",
"typescript": "^4.1.3"
}
}
}
23 changes: 20 additions & 3 deletions dev/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Plugin tests', () => {

beforeAll(async () => {
server = await start({ local: true })
})
}, 20000)

afterAll(async () => {
await mongoose.connection.dropDatabase()
Expand Down Expand Up @@ -39,7 +39,13 @@ describe('Plugin tests', () => {
})

it('stores relations as object ids', async () => {
const docs = await payload.collections.relations.Model.find()
const relationsQuery = await payload.find({
collection: 'relations',
sort: 'createdAt',
})

const docs = relationsQuery.docs

expect(typeof docs[0].hasOne).toBe('object')
expect(typeof docs[0].hasOnePoly.value).toBe('object')
expect(typeof docs[0].hasMany[0]).toBe('object')
Expand Down Expand Up @@ -93,7 +99,18 @@ describe('Plugin tests', () => {
},
},
})
})

expect(totalDocs).toStrictEqual(1)
it('populates parent relations', async () => {
const r1 = await payload.find({
collection: 'relations',
})

const r2 = await payload.create({
collection: 'relations', // required
data: {
parent: r1.docs[0],
},
})
})
})
130 changes: 96 additions & 34 deletions dev/src/payload-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* tslint:disable */
/* eslint-disable */
/**
* This file was automatically generated by Payload.
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
Expand All @@ -12,80 +13,141 @@ export interface Config {
posts: Post;
relations: Relation;
users: User;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
globals: {};
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "uploads".
*/
export interface Upload {
id: string;
updatedAt: string;
createdAt: string;
url?: string;
filename?: string;
mimeType?: string;
filesize?: number;
width?: number;
height?: number;
url?: string | null;
filename?: string | null;
mimeType?: string | null;
filesize?: number | null;
width?: number | null;
height?: number | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "pages".
*/
export interface Page {
id: string;
title: string;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "posts".
*/
export interface Post {
id: string;
title: string;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "relations".
*/
export interface Relation {
id: string;
hasOne?: string | Post;
hasOne?: (string | null) | Post;
hasOnePoly?:
| {
value: string | Page;
| ({
relationTo: 'pages';
}
| {
value: string | Post;
value: string | Page;
} | null)
| ({
relationTo: 'posts';
};
hasMany?: string[] | Post[];
value: string | Post;
} | null);
hasMany?: (string | Post)[] | null;
hasManyPoly?:
| (
| {
value: string;
relationTo: 'pages';
value: string | Page;
}
| {
value: string;
relationTo: 'posts';
value: string | Post;
}
)[]
| (
| {
value: Page;
relationTo: 'pages';
}
| {
value: Post;
relationTo: 'posts';
}
)[];
upload?: string | Upload;
| null;
upload?: string | Upload | null;
parent?: (string | null) | Relation;
breadcrumbs?:
| {
doc?: (string | null) | Relation;
url?: string | null;
label?: string | null;
id?: string | null;
}[]
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users".
*/
export interface User {
id: string;
updatedAt: string;
createdAt: string;
email: string;
resetPasswordToken?: string;
resetPasswordExpiration?: string;
salt?: string;
hash?: string;
loginAttempts?: number;
lockUntil?: string;
password?: string;
resetPasswordToken?: string | null;
resetPasswordExpiration?: string | null;
salt?: string | null;
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
password: string | null;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-preferences".
*/
export interface PayloadPreference {
id: string;
user: {
relationTo: 'users';
value: string | User;
};
key?: string | null;
value?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-migrations".
*/
export interface PayloadMigration {
id: string;
name?: string | null;
batch?: number | null;
updatedAt: string;
createdAt: string;
}


declare module 'payload' {
export interface GeneratedTypes extends Config {}
}
20 changes: 18 additions & 2 deletions dev/src/payload.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { buildConfig } from 'payload/config'
import path from 'path'
import { relationshipsAsObjectID } from '../../src'
import { mongooseAdapter } from '@payloadcms/db-mongodb'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import nestedDocs from '@payloadcms/plugin-nested-docs'

export default buildConfig({
db: mongooseAdapter({
url: process.env.MONGODB_URI,
}), // or postgresAdapter({}),
editor: lexicalEditor({}), // or slateEditor({})
serverURL: 'http://localhost:3000',
collections: [
{
Expand Down Expand Up @@ -66,8 +73,17 @@ export default buildConfig({
typescript: {
outputFile: path.resolve(__dirname, 'payload-types.ts'),
},
plugins: [relationshipsAsObjectID()],
onInit: async payload => {
plugins: [
relationshipsAsObjectID(),
/*
nestedDocs({
collections: ['relations'],
generateLabel: (_, doc: any) => doc.title,
generateURL: (docs) => docs.reduce((url, doc) => `${url}/${doc.slug}`, ''),
}),
*/
],
onInit: async (payload) => {
await payload.create({
collection: 'users',
data: {
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "cd dev && yarn test"
},
"peerDependencies": {
"payload": "^1.9.5"
"payload": "^2.11.2"
},
"files": [
"dist",
Expand All @@ -36,10 +36,14 @@
"eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "^4.0.0",
"nodemon": "^2.0.6",
"payload": "^1.9.5",
"payload": "^2.11.2",
"prettier": "^2.7.1",
"rimraf": "^4.1.2",
"ts-node": "^10.9.1",
"typescript": "^4.1.3"
},
"dependencies": {
"mongoose": "^8.2.3",
"webpack": "^5.91.0"
}
}
Loading