@bhofstaetter/payloadcms-repository 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +98 -0
- package/dist/collection/CollectionQuery.d.ts +7 -0
- package/dist/collection/CollectionQuery.js +7 -0
- package/dist/collection/CollectionRepository.d.ts +64 -0
- package/dist/collection/CollectionRepository.js +128 -0
- package/dist/global/GlobalQuery.d.ts +7 -0
- package/dist/global/GlobalQuery.js +7 -0
- package/dist/global/GlobalRepository.d.ts +27 -0
- package/dist/global/GlobalRepository.js +40 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +4 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.js +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benedikt Hofstätter
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# payload-repository
|
|
2
|
+
|
|
3
|
+
Opinionated repository and query object wrapper around Payload's Local API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @bhofstaetter/payloadcms-repository
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Pass your generated Payload `Config` type as the first type argument to get full type safety on fields, select, and return types.
|
|
14
|
+
|
|
15
|
+
### CollectionQuery
|
|
16
|
+
|
|
17
|
+
Extend `CollectionQuery` to encapsulate domain-specific query logic for a collection:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import type {BasePayload} from 'payload';
|
|
21
|
+
import type {Config} from '@/payload-types';
|
|
22
|
+
import {CollectionQuery} from '@bhofstaetter/payloadcms-repository';
|
|
23
|
+
|
|
24
|
+
class PostsQuery extends CollectionQuery<Config, 'posts'> {
|
|
25
|
+
constructor(payload: BasePayload) {
|
|
26
|
+
super(payload, 'posts');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
create(title: string) {
|
|
30
|
+
return this.repository.create({title});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
findAll() {
|
|
34
|
+
return this.repository.find();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
findById(id: number) {
|
|
38
|
+
return this.repository.findById(id);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
findPublished() {
|
|
42
|
+
return this.repository.find({where: {status: {equals: 'published'}}});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
publish(id: number) {
|
|
46
|
+
return this.repository.updateById(id, {status: 'published'});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
deleteById(id: number) {
|
|
50
|
+
return this.repository.deleteById(id);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Usage
|
|
55
|
+
const postsQuery = new PostsQuery(payload);
|
|
56
|
+
|
|
57
|
+
const post = await postsQuery.create('Hello World');
|
|
58
|
+
const published = await postsQuery.findPublished();
|
|
59
|
+
await postsQuery.publish(post.id);
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### GlobalQuery
|
|
63
|
+
|
|
64
|
+
Extend `GlobalQuery` to encapsulate domain-specific query logic for a global:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import type {BasePayload} from 'payload';
|
|
68
|
+
import type {Config} from '@/payload-types';
|
|
69
|
+
import {GlobalQuery} from '@bhofstaetter/payloadcms-repository';
|
|
70
|
+
|
|
71
|
+
class SettingsQuery extends GlobalQuery<Config, 'settings'> {
|
|
72
|
+
constructor(payload: BasePayload) {
|
|
73
|
+
super(payload, 'settings');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get() {
|
|
77
|
+
return this.repository.find();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setSiteTitle(title: string) {
|
|
81
|
+
return this.repository.update({siteTitle: title});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Usage
|
|
86
|
+
const settingsQuery = new SettingsQuery(payload);
|
|
87
|
+
|
|
88
|
+
const settings = await settingsQuery.get();
|
|
89
|
+
await settingsQuery.setSiteTitle('My Site');
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
MIT
|
|
95
|
+
|
|
96
|
+
## Todo
|
|
97
|
+
|
|
98
|
+
- [ ] Github Actions
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BasePayload, CollectionSlug } from 'payload';
|
|
2
|
+
import type { AnyCollectionConfig } from '@/types';
|
|
3
|
+
import { CollectionRepository } from './CollectionRepository';
|
|
4
|
+
export declare abstract class CollectionQuery<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug> {
|
|
5
|
+
protected readonly repository: CollectionRepository<TConfig, TSlug>;
|
|
6
|
+
protected constructor(payload: BasePayload, collectionSlug: TSlug);
|
|
7
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { BasePayload, BulkOperationResult, CollectionSlug, DataFromCollectionSlug, PaginatedDistinctDocs, PaginatedDocs, RequiredDataFromCollectionSlug, SelectType, TransformCollectionWithSelect, TypeWithVersion, Where } from 'payload';
|
|
2
|
+
import type { DeepPartial } from 'ts-essentials';
|
|
3
|
+
import type { AnyCollectionConfig, DbId } from '@/types';
|
|
4
|
+
export declare class CollectionRepository<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug> {
|
|
5
|
+
protected readonly payload: BasePayload;
|
|
6
|
+
protected readonly collectionSlug: TSlug;
|
|
7
|
+
constructor(payload: BasePayload, collectionSlug: TSlug);
|
|
8
|
+
create<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(data: RequiredDataFromCollectionSlug<TSlug>, options?: CreateOptions<TConfig, TSlug, TSelect> & {
|
|
9
|
+
draft?: false;
|
|
10
|
+
}): Promise<SelectResult<TSlug, TSelect>>;
|
|
11
|
+
create<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(data: DraftDataFromCollectionSlug<TSlug>, options: CreateOptions<TConfig, TSlug, TSelect> & {
|
|
12
|
+
draft: true;
|
|
13
|
+
}): Promise<SelectResult<TSlug, TSelect>>;
|
|
14
|
+
duplicate<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(id: DbId, options?: DuplicateOptions<TConfig, TSlug, TSelect>): Promise<SelectResult<TSlug, TSelect>>;
|
|
15
|
+
find<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(options?: FindOptions<TConfig, TSlug, TSelect>): Promise<PaginatedSelectResult<TSlug, TSelect>>;
|
|
16
|
+
findById<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(id: DbId, options?: FindByIdOptions<TConfig, TSlug, TSelect>): Promise<SelectResult<TSlug, TSelect>>;
|
|
17
|
+
findByIds<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(ids: DbId[], options?: FindOptions<TConfig, TSlug, TSelect>): Promise<PaginatedSelectResult<TSlug, TSelect>>;
|
|
18
|
+
findDistinct<TField extends keyof DataFromCollectionSlug<TSlug> & string>(field: TField, options?: FindDistinctOptions): Promise<PaginatedDistinctDocs<Record<TField, DataFromCollectionSlug<TSlug>[TField]>>>;
|
|
19
|
+
findVersionById(id: string, options?: FindVersionByIdOptions): Promise<TypeWithVersion<DataFromCollectionSlug<TSlug>>>;
|
|
20
|
+
findVersions(options?: FindVersionsOptions): Promise<PaginatedDocs<TypeWithVersion<DataFromCollectionSlug<TSlug>>>>;
|
|
21
|
+
count(options?: CountOptions): CountResult;
|
|
22
|
+
countVersions(options?: CollectionCountVersionsOptions): CountVersionsResult;
|
|
23
|
+
update<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(where: Where, data: UpdateData<TSlug>, options?: UpdateOptions<TConfig, TSlug, TSelect>): Promise<BulkOperationResult<TSlug, TSelect>>;
|
|
24
|
+
updateById<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(id: DbId, data: UpdateData<TSlug>, options?: UpdateByIdOptions<TConfig, TSlug, TSelect>): Promise<SelectResult<TSlug, TSelect>>;
|
|
25
|
+
updateByIds<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(ids: DbId[], data: UpdateData<TSlug>, options?: UpdateOptions<TConfig, TSlug, TSelect>): Promise<BulkOperationResult<TSlug, TSelect>>;
|
|
26
|
+
delete<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(where: Where, options?: DeleteOptions<TConfig, TSlug, TSelect>): Promise<BulkOperationResult<TSlug, TSelect>>;
|
|
27
|
+
deleteById<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(id: DbId, options?: DeleteOptions<TConfig, TSlug, TSelect>): Promise<SelectResult<TSlug, TSelect>>;
|
|
28
|
+
deleteByIds<TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>>(ids: DbId[], options?: DeleteOptions<TConfig, TSlug, TSelect>): Promise<BulkOperationResult<TSlug, TSelect>>;
|
|
29
|
+
}
|
|
30
|
+
type TypedSelect<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug> = TConfig['collectionsSelect'][TSlug & string] & SelectType;
|
|
31
|
+
type SelectResult<TSlug extends CollectionSlug, TSelect extends SelectType = SelectType> = TransformCollectionWithSelect<TSlug, TSelect>;
|
|
32
|
+
type PaginatedSelectResult<TSlug extends CollectionSlug, TSelect extends SelectType = SelectType> = PaginatedDocs<SelectResult<TSlug, TSelect>>;
|
|
33
|
+
type DraftDataFromCollectionSlug<TSlug extends CollectionSlug> = Partial<DataFromCollectionSlug<TSlug>>;
|
|
34
|
+
type CreateOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['create']>[0], 'collection' | 'select' | 'data' | 'draft'> & {
|
|
35
|
+
select?: TSelect;
|
|
36
|
+
};
|
|
37
|
+
type DuplicateOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['duplicate']>[0], 'collection' | 'id' | 'select' | 'data'> & {
|
|
38
|
+
select?: TSelect;
|
|
39
|
+
data?: DeepPartial<RequiredDataFromCollectionSlug<TSlug>>;
|
|
40
|
+
};
|
|
41
|
+
type FindByIdOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['findByID']>[0], 'collection' | 'select' | 'id'> & {
|
|
42
|
+
select?: TSelect;
|
|
43
|
+
};
|
|
44
|
+
type FindOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['find']>[0], 'collection' | 'select' | 'where'> & {
|
|
45
|
+
select?: TSelect;
|
|
46
|
+
};
|
|
47
|
+
type FindDistinctOptions = Omit<Parameters<BasePayload['findDistinct']>[0], 'collection' | 'field'>;
|
|
48
|
+
type FindVersionByIdOptions = Omit<Parameters<BasePayload['findVersionByID']>[0], 'collection' | 'id'>;
|
|
49
|
+
type FindVersionsOptions = Omit<Parameters<BasePayload['findVersions']>[0], 'collection'>;
|
|
50
|
+
type CountOptions = Omit<Parameters<BasePayload['count']>[0], 'collection'>;
|
|
51
|
+
type CollectionCountVersionsOptions = Omit<Parameters<BasePayload['countVersions']>[0], 'collection'>;
|
|
52
|
+
type CountResult = ReturnType<BasePayload['count']>;
|
|
53
|
+
type CountVersionsResult = ReturnType<BasePayload['countVersions']>;
|
|
54
|
+
type UpdateData<TSlug extends CollectionSlug> = DeepPartial<RequiredDataFromCollectionSlug<TSlug>>;
|
|
55
|
+
type UpdateByIdOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['update']>[0], 'collection' | 'select' | 'id' | 'data'> & {
|
|
56
|
+
select?: TSelect;
|
|
57
|
+
};
|
|
58
|
+
type UpdateOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['update']>[0], 'collection' | 'select' | 'data' | 'where' | 'id'> & {
|
|
59
|
+
select?: TSelect;
|
|
60
|
+
};
|
|
61
|
+
type DeleteOptions<TConfig extends AnyCollectionConfig, TSlug extends CollectionSlug, TSelect extends TypedSelect<TConfig, TSlug> = TypedSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['delete']>[0], 'collection' | 'select' | 'where' | 'id'> & {
|
|
62
|
+
select?: TSelect;
|
|
63
|
+
};
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
export class CollectionRepository {
|
|
2
|
+
payload;
|
|
3
|
+
collectionSlug;
|
|
4
|
+
constructor(payload, collectionSlug) {
|
|
5
|
+
this.payload = payload;
|
|
6
|
+
this.collectionSlug = collectionSlug;
|
|
7
|
+
}
|
|
8
|
+
async create(data, options) {
|
|
9
|
+
const { draft, ...createOptions } = options ?? {};
|
|
10
|
+
if (draft) {
|
|
11
|
+
return this.payload.create({
|
|
12
|
+
collection: this.collectionSlug,
|
|
13
|
+
data: data,
|
|
14
|
+
draft: true,
|
|
15
|
+
...createOptions,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return this.payload.create({
|
|
19
|
+
collection: this.collectionSlug,
|
|
20
|
+
data: { ...data, _status: 'published' },
|
|
21
|
+
...createOptions,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async duplicate(id, options) {
|
|
25
|
+
return this.payload.duplicate({
|
|
26
|
+
collection: this.collectionSlug,
|
|
27
|
+
id,
|
|
28
|
+
...options,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async find(options) {
|
|
32
|
+
return this.payload.find({
|
|
33
|
+
collection: this.collectionSlug,
|
|
34
|
+
...options,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
async findById(id, options) {
|
|
38
|
+
return this.payload.findByID({
|
|
39
|
+
collection: this.collectionSlug,
|
|
40
|
+
id,
|
|
41
|
+
...options,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async findByIds(ids, options) {
|
|
45
|
+
return this.payload.find({
|
|
46
|
+
collection: this.collectionSlug,
|
|
47
|
+
where: { id: { in: ids } },
|
|
48
|
+
...options,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async findDistinct(field, options) {
|
|
52
|
+
return this.payload.findDistinct({
|
|
53
|
+
collection: this.collectionSlug,
|
|
54
|
+
field,
|
|
55
|
+
...options,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async findVersionById(id, options) {
|
|
59
|
+
return this.payload.findVersionByID({
|
|
60
|
+
collection: this.collectionSlug,
|
|
61
|
+
id,
|
|
62
|
+
...options,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async findVersions(options) {
|
|
66
|
+
return this.payload.findVersions({
|
|
67
|
+
collection: this.collectionSlug,
|
|
68
|
+
...options,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
async count(options) {
|
|
72
|
+
return this.payload.count({
|
|
73
|
+
collection: this.collectionSlug,
|
|
74
|
+
...options,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async countVersions(options) {
|
|
78
|
+
return this.payload.countVersions({
|
|
79
|
+
collection: this.collectionSlug,
|
|
80
|
+
...options,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async update(where, data, options) {
|
|
84
|
+
return this.payload.update({
|
|
85
|
+
collection: this.collectionSlug,
|
|
86
|
+
where,
|
|
87
|
+
data,
|
|
88
|
+
...options,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async updateById(id, data, options) {
|
|
92
|
+
return this.payload.update({
|
|
93
|
+
collection: this.collectionSlug,
|
|
94
|
+
id,
|
|
95
|
+
data,
|
|
96
|
+
...options,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async updateByIds(ids, data, options) {
|
|
100
|
+
return this.payload.update({
|
|
101
|
+
collection: this.collectionSlug,
|
|
102
|
+
where: { id: { in: ids } },
|
|
103
|
+
data,
|
|
104
|
+
...options,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
async delete(where, options) {
|
|
108
|
+
return this.payload.delete({
|
|
109
|
+
collection: this.collectionSlug,
|
|
110
|
+
where,
|
|
111
|
+
...options,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async deleteById(id, options) {
|
|
115
|
+
return this.payload.delete({
|
|
116
|
+
collection: this.collectionSlug,
|
|
117
|
+
id,
|
|
118
|
+
...options,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
async deleteByIds(ids, options) {
|
|
122
|
+
return this.payload.delete({
|
|
123
|
+
collection: this.collectionSlug,
|
|
124
|
+
where: { id: { in: ids } },
|
|
125
|
+
...options,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BasePayload, GlobalSlug } from 'payload';
|
|
2
|
+
import type { AnyGlobalConfig } from '@/types';
|
|
3
|
+
import { GlobalRepository } from './GlobalRepository';
|
|
4
|
+
export declare abstract class GlobalQuery<TConfig extends AnyGlobalConfig, TSlug extends GlobalSlug> {
|
|
5
|
+
protected readonly repository: GlobalRepository<TConfig, TSlug>;
|
|
6
|
+
protected constructor(payload: BasePayload, globalSlug: TSlug);
|
|
7
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BasePayload, DataFromGlobalSlug, GlobalSlug, PaginatedDocs, SelectType, TransformGlobalWithSelect, TypeWithVersion } from 'payload';
|
|
2
|
+
import type { DeepPartial } from 'ts-essentials';
|
|
3
|
+
import type { AnyGlobalConfig, DbId } from '@/types';
|
|
4
|
+
export declare class GlobalRepository<TConfig extends AnyGlobalConfig, TSlug extends GlobalSlug> {
|
|
5
|
+
protected readonly payload: BasePayload;
|
|
6
|
+
protected readonly globalSlug: TSlug;
|
|
7
|
+
constructor(payload: BasePayload, globalSlug: TSlug);
|
|
8
|
+
find<TSelect extends TypedGlobalSelect<TConfig, TSlug> = TypedGlobalSelect<TConfig, TSlug>>(options?: FindGlobalOptions<TConfig, TSlug, TSelect>): Promise<GlobalSelectResult<TSlug, TSelect>>;
|
|
9
|
+
findVersionById(id: DbId, options?: FindGlobalVersionByIdOptions): Promise<TypeWithVersion<DataFromGlobalSlug<TSlug>>>;
|
|
10
|
+
findVersions(options?: FindGlobalVersionsOptions): Promise<PaginatedDocs<TypeWithVersion<DataFromGlobalSlug<TSlug>>>>;
|
|
11
|
+
countVersions(options?: GlobalCountVersionsOptions): CountVersionsResult;
|
|
12
|
+
update<TSelect extends TypedGlobalSelect<TConfig, TSlug> = TypedGlobalSelect<TConfig, TSlug>>(data: UpdateGlobalData<TSlug>, options?: UpdateGlobalOptions<TConfig, TSlug, TSelect>): Promise<GlobalSelectResult<TSlug, TSelect>>;
|
|
13
|
+
}
|
|
14
|
+
type TypedGlobalSelect<TConfig extends AnyGlobalConfig, TSlug extends GlobalSlug> = TConfig['globalsSelect'][TSlug & string] & SelectType;
|
|
15
|
+
type GlobalSelectResult<TSlug extends GlobalSlug, TSelect extends SelectType = SelectType> = TransformGlobalWithSelect<TSlug, TSelect>;
|
|
16
|
+
type FindGlobalOptions<TConfig extends AnyGlobalConfig, TSlug extends GlobalSlug, TSelect extends TypedGlobalSelect<TConfig, TSlug> = TypedGlobalSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['findGlobal']>[0], 'slug' | 'select'> & {
|
|
17
|
+
select?: TSelect;
|
|
18
|
+
};
|
|
19
|
+
type FindGlobalVersionByIdOptions = Omit<Parameters<BasePayload['findGlobalVersionByID']>[0], 'slug' | 'id'>;
|
|
20
|
+
type FindGlobalVersionsOptions = Omit<Parameters<BasePayload['findGlobalVersions']>[0], 'slug'>;
|
|
21
|
+
type GlobalCountVersionsOptions = Omit<Parameters<BasePayload['countGlobalVersions']>[0], 'global'>;
|
|
22
|
+
type CountVersionsResult = ReturnType<BasePayload['countGlobalVersions']>;
|
|
23
|
+
type UpdateGlobalData<TSlug extends GlobalSlug> = DeepPartial<Omit<DataFromGlobalSlug<TSlug>, 'id'>>;
|
|
24
|
+
type UpdateGlobalOptions<TConfig extends AnyGlobalConfig, TSlug extends GlobalSlug, TSelect extends TypedGlobalSelect<TConfig, TSlug> = TypedGlobalSelect<TConfig, TSlug>> = Omit<Parameters<BasePayload['updateGlobal']>[0], 'slug' | 'select' | 'data'> & {
|
|
25
|
+
select?: TSelect;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class GlobalRepository {
|
|
2
|
+
payload;
|
|
3
|
+
globalSlug;
|
|
4
|
+
constructor(payload, globalSlug) {
|
|
5
|
+
this.payload = payload;
|
|
6
|
+
this.globalSlug = globalSlug;
|
|
7
|
+
}
|
|
8
|
+
async find(options) {
|
|
9
|
+
return this.payload.findGlobal({
|
|
10
|
+
slug: this.globalSlug,
|
|
11
|
+
...options,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
async findVersionById(id, options) {
|
|
15
|
+
return this.payload.findGlobalVersionByID({
|
|
16
|
+
slug: this.globalSlug,
|
|
17
|
+
id,
|
|
18
|
+
...options,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async findVersions(options) {
|
|
22
|
+
return this.payload.findGlobalVersions({
|
|
23
|
+
slug: this.globalSlug,
|
|
24
|
+
...options,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async countVersions(options) {
|
|
28
|
+
return this.payload.countGlobalVersions({
|
|
29
|
+
global: this.globalSlug,
|
|
30
|
+
...options,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async update(data, options) {
|
|
34
|
+
return this.payload.updateGlobal({
|
|
35
|
+
slug: this.globalSlug,
|
|
36
|
+
data,
|
|
37
|
+
...options,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CollectionQuery } from './collection/CollectionQuery';
|
|
2
|
+
export { CollectionRepository } from './collection/CollectionRepository';
|
|
3
|
+
export { GlobalQuery } from './global/GlobalQuery';
|
|
4
|
+
export { GlobalRepository } from './global/GlobalRepository';
|
|
5
|
+
export type { AnyCollectionConfig, AnyGlobalConfig, DbId } from './types.js';
|
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bhofstaetter/payloadcms-repository",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Opinionated repository and query object wrapper around Payload's local API.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Benedikt Hofstätter",
|
|
7
|
+
"homepage": "https://github.com/bhofstaetter/payloadcms-repository",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/bhofstaetter/payloadcms-repository.git"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"payload",
|
|
14
|
+
"payloadcms",
|
|
15
|
+
"repository",
|
|
16
|
+
"repository-pattern",
|
|
17
|
+
"query-object",
|
|
18
|
+
"local api",
|
|
19
|
+
"data fetching"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.build.json",
|
|
33
|
+
"typecheck": "tsc",
|
|
34
|
+
"lint": "biome check",
|
|
35
|
+
"lint:fix": "biome check --fix",
|
|
36
|
+
"prepublishOnly": "npm run lint && npm run typecheck && npm run build"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"payload": ">=3",
|
|
40
|
+
"ts-essentials": ">=10"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"payload": "^3.78.0",
|
|
44
|
+
"@payloadcms/db-postgres": "^3.78.0",
|
|
45
|
+
"ts-essentials": "^10.0.0",
|
|
46
|
+
"typescript": "^5",
|
|
47
|
+
"@biomejs/biome": "2.4.4",
|
|
48
|
+
"@testcontainers/postgresql": "^11.11.0",
|
|
49
|
+
"vite-tsconfig-paths": "^6.1.1",
|
|
50
|
+
"vitest": "^4.0.18"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=22.0.0"
|
|
54
|
+
}
|
|
55
|
+
}
|