@matvdt/js-toolkit 0.0.1-canary.18 → 0.0.1-canary.19

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.
@@ -0,0 +1,6 @@
1
+ import type { CollectionSlug, GlobalSlug } from 'payload';
2
+ export declare function cacheKey(collection: CollectionSlug, method: string, args: unknown[]): string[];
3
+ export declare function withCache<T>(collection: CollectionSlug, method: string, args: unknown[], tags: string[], fn: () => Promise<T>): Promise<T>;
4
+ export declare function revalidateCollection(collection: CollectionSlug): void;
5
+ export declare function revalidateDoc(collection: CollectionSlug, id: string | number): void;
6
+ export declare function revalidateGlobal(slug: GlobalSlug): void;
@@ -20,14 +20,37 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/payload-cms/server/index.ts
21
21
  var server_exports = {};
22
22
  __export(server_exports, {
23
+ cacheKey: () => cacheKey,
23
24
  configurePayload: () => configurePayload,
24
25
  createCollection: () => createCollection,
25
26
  createGlobal: () => createGlobal,
26
27
  getPayload: () => getPayload,
27
- payload: () => payload
28
+ payload: () => payload,
29
+ revalidateCollection: () => revalidateCollection,
30
+ revalidateDoc: () => revalidateDoc,
31
+ revalidateGlobal: () => revalidateGlobal,
32
+ withCache: () => withCache
28
33
  });
29
34
  module.exports = __toCommonJS(server_exports);
30
35
 
36
+ // src/payload-cms/server/cache.ts
37
+ var import_cache = require("next/cache");
38
+ function cacheKey(collection, method, args) {
39
+ return [`payload:${collection}:${method}`, JSON.stringify(args)];
40
+ }
41
+ function withCache(collection, method, args, tags, fn) {
42
+ return (0, import_cache.unstable_cache)(fn, cacheKey(collection, method, args), { tags })();
43
+ }
44
+ function revalidateCollection(collection) {
45
+ (0, import_cache.revalidateTag)(`collection:${collection}`, "max");
46
+ }
47
+ function revalidateDoc(collection, id) {
48
+ (0, import_cache.revalidateTag)(`doc:${collection}:${id}`, "max");
49
+ }
50
+ function revalidateGlobal(slug) {
51
+ (0, import_cache.revalidateTag)(`global:${slug}`, "max");
52
+ }
53
+
31
54
  // src/payload-cms/server/payload.ts
32
55
  var import_react = require("react");
33
56
 
@@ -54,13 +77,15 @@ var getClient = (0, import_react.cache)(async () => {
54
77
  });
55
78
  var payload = {
56
79
  findOne: async (collection, options) => {
57
- const payload2 = await getClient();
58
- const res = await payload2.find({
59
- ...options,
60
- collection,
61
- limit: 1
80
+ return withCache(collection, "findOne", [options], [`collection:${collection}`], async () => {
81
+ const client = await getClient();
82
+ const res = await client.find({
83
+ ...options,
84
+ collection,
85
+ limit: 1
86
+ });
87
+ return res.docs[0] ?? null;
62
88
  });
63
- return res.docs[0] ?? null;
64
89
  },
65
90
  findBySlug: async (collection, slug, options) => {
66
91
  return payload.findOne(
@@ -75,81 +100,103 @@ var payload = {
75
100
  );
76
101
  },
77
102
  find: async (collection, options) => {
78
- const payload2 = await getClient();
79
- return payload2.find({
80
- ...options,
81
- collection
103
+ return withCache(collection, "find", [options], [`collection:${collection}`], async () => {
104
+ const client = await getClient();
105
+ return client.find({
106
+ ...options,
107
+ collection
108
+ });
82
109
  });
83
110
  },
84
111
  findAll: async (collection, options) => {
85
- const payload2 = await getClient();
86
- const res = await payload2.find({
87
- ...options,
88
- collection,
89
- limit: options?.limit ?? 100
112
+ return withCache(collection, "findAll", [options], [`collection:${collection}`], async () => {
113
+ const client = await getClient();
114
+ const res = await client.find({
115
+ ...options,
116
+ collection,
117
+ limit: options?.limit ?? 100
118
+ });
119
+ return res.docs;
90
120
  });
91
- return res.docs;
92
121
  },
93
122
  findByID: async (collection, id, options) => {
94
- const payload2 = await getClient();
95
- return payload2.findByID({
96
- ...options,
97
- collection,
98
- id
123
+ return withCache(collection, "findByID", [id, options], [`collection:${collection}`, `doc:${collection}:${id}`], async () => {
124
+ const client = await getClient();
125
+ return client.findByID({
126
+ ...options,
127
+ collection,
128
+ id
129
+ });
99
130
  });
100
131
  },
101
132
  create: async (collection, data, options) => {
102
- const payload2 = await getClient();
103
- return payload2.create({
133
+ const client = await getClient();
134
+ const result = await client.create({
104
135
  ...options,
105
136
  collection,
106
137
  data
107
138
  });
139
+ revalidateCollection(collection);
140
+ return result;
108
141
  },
109
142
  update: async (collection, id, data, options) => {
110
- const payload2 = await getClient();
111
- return payload2.update({
143
+ const client = await getClient();
144
+ const result = await client.update({
112
145
  ...options,
113
146
  collection,
114
147
  id,
115
148
  data
116
149
  });
150
+ revalidateCollection(collection);
151
+ revalidateDoc(collection, id);
152
+ return result;
117
153
  },
118
154
  updateMany: async (collection, where, data, options) => {
119
- const payload2 = await getClient();
120
- return payload2.update({
155
+ const client = await getClient();
156
+ const result = await client.update({
121
157
  ...options,
122
158
  collection,
123
159
  where,
124
160
  data,
125
161
  id: void 0
126
162
  });
163
+ revalidateCollection(collection);
164
+ return result;
127
165
  },
128
166
  delete: async (collection, id, options) => {
129
- const payload2 = await getClient();
130
- return payload2.delete({
167
+ const client = await getClient();
168
+ const result = await client.delete({
131
169
  ...options,
132
170
  collection,
133
171
  id,
134
172
  where: void 0
135
173
  });
174
+ revalidateCollection(collection);
175
+ revalidateDoc(collection, id);
176
+ return result;
136
177
  },
137
178
  deleteMany: async (collection, where, options) => {
138
- const payload2 = await getClient();
139
- return payload2.delete({
179
+ const client = await getClient();
180
+ const result = await client.delete({
140
181
  ...options,
141
182
  collection,
142
183
  where,
143
184
  id: void 0
144
185
  });
186
+ revalidateCollection(collection);
187
+ return result;
145
188
  },
146
189
  global: async (slug) => {
147
- const payload2 = await getClient();
148
- return payload2.findGlobal({ slug });
190
+ return withCache("global", slug, [], [`global:${slug}`], async () => {
191
+ const client = await getClient();
192
+ return client.findGlobal({ slug });
193
+ });
149
194
  },
150
195
  aggregate: async (collection, pipeline, options) => {
151
- const payload2 = await getClient();
152
- return payload2.db.collections[collection].aggregate(pipeline, options);
196
+ return withCache(collection, "aggregate", [pipeline, options], [`collection:${collection}`], async () => {
197
+ const client = await getClient();
198
+ return client.db.collections[collection].aggregate(pipeline, options);
199
+ });
153
200
  },
154
201
  raw: getClient
155
202
  };
@@ -187,9 +234,14 @@ function createGlobal(slug, extend) {
187
234
  }
188
235
  // Annotate the CommonJS export names for ESM import in node:
189
236
  0 && (module.exports = {
237
+ cacheKey,
190
238
  configurePayload,
191
239
  createCollection,
192
240
  createGlobal,
193
241
  getPayload,
194
- payload
242
+ payload,
243
+ revalidateCollection,
244
+ revalidateDoc,
245
+ revalidateGlobal,
246
+ withCache
195
247
  });
@@ -1,3 +1,4 @@
1
+ export * from './cache';
1
2
  export * from './create-collection';
2
3
  export * from './create-global';
3
4
  export * from './get-payload';
@@ -1,3 +1,21 @@
1
+ // src/payload-cms/server/cache.ts
2
+ import { unstable_cache, revalidateTag } from "next/cache";
3
+ function cacheKey(collection, method, args) {
4
+ return [`payload:${collection}:${method}`, JSON.stringify(args)];
5
+ }
6
+ function withCache(collection, method, args, tags, fn) {
7
+ return unstable_cache(fn, cacheKey(collection, method, args), { tags })();
8
+ }
9
+ function revalidateCollection(collection) {
10
+ revalidateTag(`collection:${collection}`, "max");
11
+ }
12
+ function revalidateDoc(collection, id) {
13
+ revalidateTag(`doc:${collection}:${id}`, "max");
14
+ }
15
+ function revalidateGlobal(slug) {
16
+ revalidateTag(`global:${slug}`, "max");
17
+ }
18
+
1
19
  // src/payload-cms/server/payload.ts
2
20
  import { cache } from "react";
3
21
 
@@ -24,13 +42,15 @@ var getClient = cache(async () => {
24
42
  });
25
43
  var payload = {
26
44
  findOne: async (collection, options) => {
27
- const payload2 = await getClient();
28
- const res = await payload2.find({
29
- ...options,
30
- collection,
31
- limit: 1
45
+ return withCache(collection, "findOne", [options], [`collection:${collection}`], async () => {
46
+ const client = await getClient();
47
+ const res = await client.find({
48
+ ...options,
49
+ collection,
50
+ limit: 1
51
+ });
52
+ return res.docs[0] ?? null;
32
53
  });
33
- return res.docs[0] ?? null;
34
54
  },
35
55
  findBySlug: async (collection, slug, options) => {
36
56
  return payload.findOne(
@@ -45,81 +65,103 @@ var payload = {
45
65
  );
46
66
  },
47
67
  find: async (collection, options) => {
48
- const payload2 = await getClient();
49
- return payload2.find({
50
- ...options,
51
- collection
68
+ return withCache(collection, "find", [options], [`collection:${collection}`], async () => {
69
+ const client = await getClient();
70
+ return client.find({
71
+ ...options,
72
+ collection
73
+ });
52
74
  });
53
75
  },
54
76
  findAll: async (collection, options) => {
55
- const payload2 = await getClient();
56
- const res = await payload2.find({
57
- ...options,
58
- collection,
59
- limit: options?.limit ?? 100
77
+ return withCache(collection, "findAll", [options], [`collection:${collection}`], async () => {
78
+ const client = await getClient();
79
+ const res = await client.find({
80
+ ...options,
81
+ collection,
82
+ limit: options?.limit ?? 100
83
+ });
84
+ return res.docs;
60
85
  });
61
- return res.docs;
62
86
  },
63
87
  findByID: async (collection, id, options) => {
64
- const payload2 = await getClient();
65
- return payload2.findByID({
66
- ...options,
67
- collection,
68
- id
88
+ return withCache(collection, "findByID", [id, options], [`collection:${collection}`, `doc:${collection}:${id}`], async () => {
89
+ const client = await getClient();
90
+ return client.findByID({
91
+ ...options,
92
+ collection,
93
+ id
94
+ });
69
95
  });
70
96
  },
71
97
  create: async (collection, data, options) => {
72
- const payload2 = await getClient();
73
- return payload2.create({
98
+ const client = await getClient();
99
+ const result = await client.create({
74
100
  ...options,
75
101
  collection,
76
102
  data
77
103
  });
104
+ revalidateCollection(collection);
105
+ return result;
78
106
  },
79
107
  update: async (collection, id, data, options) => {
80
- const payload2 = await getClient();
81
- return payload2.update({
108
+ const client = await getClient();
109
+ const result = await client.update({
82
110
  ...options,
83
111
  collection,
84
112
  id,
85
113
  data
86
114
  });
115
+ revalidateCollection(collection);
116
+ revalidateDoc(collection, id);
117
+ return result;
87
118
  },
88
119
  updateMany: async (collection, where, data, options) => {
89
- const payload2 = await getClient();
90
- return payload2.update({
120
+ const client = await getClient();
121
+ const result = await client.update({
91
122
  ...options,
92
123
  collection,
93
124
  where,
94
125
  data,
95
126
  id: void 0
96
127
  });
128
+ revalidateCollection(collection);
129
+ return result;
97
130
  },
98
131
  delete: async (collection, id, options) => {
99
- const payload2 = await getClient();
100
- return payload2.delete({
132
+ const client = await getClient();
133
+ const result = await client.delete({
101
134
  ...options,
102
135
  collection,
103
136
  id,
104
137
  where: void 0
105
138
  });
139
+ revalidateCollection(collection);
140
+ revalidateDoc(collection, id);
141
+ return result;
106
142
  },
107
143
  deleteMany: async (collection, where, options) => {
108
- const payload2 = await getClient();
109
- return payload2.delete({
144
+ const client = await getClient();
145
+ const result = await client.delete({
110
146
  ...options,
111
147
  collection,
112
148
  where,
113
149
  id: void 0
114
150
  });
151
+ revalidateCollection(collection);
152
+ return result;
115
153
  },
116
154
  global: async (slug) => {
117
- const payload2 = await getClient();
118
- return payload2.findGlobal({ slug });
155
+ return withCache("global", slug, [], [`global:${slug}`], async () => {
156
+ const client = await getClient();
157
+ return client.findGlobal({ slug });
158
+ });
119
159
  },
120
160
  aggregate: async (collection, pipeline, options) => {
121
- const payload2 = await getClient();
122
- return payload2.db.collections[collection].aggregate(pipeline, options);
161
+ return withCache(collection, "aggregate", [pipeline, options], [`collection:${collection}`], async () => {
162
+ const client = await getClient();
163
+ return client.db.collections[collection].aggregate(pipeline, options);
164
+ });
123
165
  },
124
166
  raw: getClient
125
167
  };
@@ -156,9 +198,14 @@ function createGlobal(slug, extend) {
156
198
  };
157
199
  }
158
200
  export {
201
+ cacheKey,
159
202
  configurePayload,
160
203
  createCollection,
161
204
  createGlobal,
162
205
  getPayload,
163
- payload
206
+ payload,
207
+ revalidateCollection,
208
+ revalidateDoc,
209
+ revalidateGlobal,
210
+ withCache
164
211
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matvdt/js-toolkit",
3
- "version": "0.0.1-canary.18",
3
+ "version": "0.0.1-canary.19",
4
4
  "description": "JS/TS toolkit",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -32,7 +32,9 @@
32
32
  "version:beta": "npm version prerelease --preid=beta",
33
33
  "version:rc": "npm version prerelease --preid=rc",
34
34
  "version:latest": "npm version patch",
35
+
35
36
  "publish:canary": "npm publish --access public --tag canary",
37
+
36
38
  "publish:alpha": "npm publish --access public --tag alpha",
37
39
  "publish:beta": "npm publish --access public --tag beta",
38
40
  "publish:rc": "npm publish --access public --tag rc",
@@ -60,12 +62,14 @@
60
62
  "payload": "^3.88.0",
61
63
  "react": "^19.2.8",
62
64
  "tsup": "^8.5.1",
63
- "typescript": "^7.0.2"
65
+ "typescript": "^7.0.2",
66
+ "next": "^16.0.0"
64
67
  },
65
68
  "peerDependencies": {
66
69
  "@payloadcms/db-mongodb": ">=3.0.0",
67
70
  "payload": ">=3.0.0",
68
- "react": ">=18.0.0"
71
+ "react": ">=18.0.0",
72
+ "next": ">=15.0.0"
69
73
  },
70
74
  "peerDependenciesMeta": {
71
75
  "payload": {