@matvdt/js-toolkit 0.0.1-canary.2

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # js-toolkit
@@ -0,0 +1,13 @@
1
+ // src/payload-cms/client/get-payload.ts
2
+ import { getPayload as getPayloadSDK } from "payload";
3
+ var getPayload = async (config) => {
4
+ if (config) {
5
+ return getPayloadSDK({ config });
6
+ }
7
+ const defaultConfig = await import("@payload-config");
8
+ return getPayloadSDK({ config: defaultConfig });
9
+ };
10
+
11
+ export {
12
+ getPayload
13
+ };
File without changes
@@ -0,0 +1,146 @@
1
+ import {
2
+ getPayload
3
+ } from "./chunk-CWTTD7TW.js";
4
+
5
+ // src/payload-cms/utils/is-populated.ts
6
+ function isPopulated(relation) {
7
+ return typeof relation !== "string";
8
+ }
9
+
10
+ // src/payload-cms/utils/as-populated.ts
11
+ function asPopulated(relation) {
12
+ return isPopulated(relation) ? relation : void 0;
13
+ }
14
+
15
+ // src/payload-cms/utils/is-object-id.ts
16
+ var MONGO_ID_REGEX = /^[0-9a-fA-F]{24}$/;
17
+ function isObjectId(value) {
18
+ return typeof value === "string" && MONGO_ID_REGEX.test(value);
19
+ }
20
+
21
+ // src/payload-cms/utils/assert-populated.ts
22
+ function assertPopulated(field, fieldName = "Field") {
23
+ if (field === null || field === void 0) {
24
+ throw new Error(`${fieldName} is undefined or null`);
25
+ }
26
+ if (isObjectId(field)) {
27
+ throw new Error(`${fieldName} is not populated (received ID: ${String(field)})`);
28
+ }
29
+ }
30
+
31
+ // src/payload-cms/utils/db.ts
32
+ import { cache } from "react";
33
+ var getClient = cache(async () => {
34
+ return await getPayload();
35
+ });
36
+ var db = {
37
+ findOne: async (collection, where) => {
38
+ const payload = await getClient();
39
+ const res = await payload.find({ collection, where, limit: 1 });
40
+ return res.docs[0] ?? null;
41
+ },
42
+ findBySlug: async (collection, slug) => {
43
+ return db.findOne(collection, { slug: { equals: slug } });
44
+ },
45
+ findAll: async (collection, options) => {
46
+ const payload = await getClient();
47
+ const res = await payload.find({
48
+ collection,
49
+ limit: options?.limit ?? 100,
50
+ ...options
51
+ });
52
+ return res.docs;
53
+ },
54
+ findByID: async (collection, id) => {
55
+ const payload = await getClient();
56
+ return payload.findByID({ collection, id });
57
+ },
58
+ create: async (collection, data, options) => {
59
+ const payload = await getClient();
60
+ return payload.create({ collection, data, ...options });
61
+ },
62
+ update: async (collection, id, data, options) => {
63
+ const payload = await getClient();
64
+ return payload.update({
65
+ collection,
66
+ id,
67
+ data,
68
+ ...options
69
+ });
70
+ },
71
+ updateMany: async (collection, where, data, options) => {
72
+ const payload = await getClient();
73
+ return payload.update({
74
+ collection,
75
+ where,
76
+ data,
77
+ ...options,
78
+ id: void 0
79
+ });
80
+ },
81
+ delete: async (collection, id, options) => {
82
+ const payload = await getClient();
83
+ return payload.delete({
84
+ collection,
85
+ id,
86
+ where: void 0,
87
+ ...options
88
+ });
89
+ },
90
+ deleteMany: async (collection, where, options) => {
91
+ const payload = await getClient();
92
+ return payload.delete({
93
+ collection,
94
+ where,
95
+ id: void 0,
96
+ ...options
97
+ });
98
+ },
99
+ global: async (slug) => {
100
+ const payload = await getClient();
101
+ return payload.findGlobal({ slug });
102
+ },
103
+ raw: getClient
104
+ };
105
+
106
+ // src/payload-cms/utils/create-collection.ts
107
+ function createCollection(collection, extend) {
108
+ const base = {
109
+ findOne: (where) => db.findOne(collection, where),
110
+ findByID: (id) => db.findByID(collection, id),
111
+ findBySlug: (slug) => db.findBySlug(collection, slug),
112
+ findAll: (options) => db.findAll(collection, options),
113
+ update: (id, data, options) => db.update(collection, id, data, options),
114
+ updateMany: (where, data, options) => db.updateMany(collection, where, data, options),
115
+ delete: (id, options) => db.delete(collection, id, options),
116
+ deleteMany: (where, options) => db.deleteMany(collection, where, options)
117
+ };
118
+ const extended = extend ? extend(base) : void 0;
119
+ return {
120
+ ...base,
121
+ ...extended
122
+ };
123
+ }
124
+
125
+ // src/payload-cms/utils/create-global.ts
126
+ function createGlobal(slug, extend) {
127
+ const base = {
128
+ get: () => db.global(slug)
129
+ };
130
+ const extended = extend ? extend(base) : void 0;
131
+ return {
132
+ ...base,
133
+ ...extended
134
+ };
135
+ }
136
+
137
+ export {
138
+ isPopulated,
139
+ asPopulated,
140
+ MONGO_ID_REGEX,
141
+ isObjectId,
142
+ assertPopulated,
143
+ db,
144
+ createCollection,
145
+ createGlobal
146
+ };
File without changes
package/dist/index.cjs ADDED
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ MONGO_ID_REGEX: () => MONGO_ID_REGEX,
34
+ asPopulated: () => asPopulated,
35
+ assertPopulated: () => assertPopulated,
36
+ createCollection: () => createCollection,
37
+ createGlobal: () => createGlobal,
38
+ db: () => db,
39
+ getPayload: () => getPayload,
40
+ isObjectId: () => isObjectId,
41
+ isPopulated: () => isPopulated
42
+ });
43
+ module.exports = __toCommonJS(index_exports);
44
+
45
+ // src/payload-cms/client/get-payload.ts
46
+ var import_payload = require("payload");
47
+ var getPayload = async (config) => {
48
+ if (config) {
49
+ return (0, import_payload.getPayload)({ config });
50
+ }
51
+ const defaultConfig = await import("@payload-config");
52
+ return (0, import_payload.getPayload)({ config: defaultConfig });
53
+ };
54
+
55
+ // src/payload-cms/utils/is-populated.ts
56
+ function isPopulated(relation) {
57
+ return typeof relation !== "string";
58
+ }
59
+
60
+ // src/payload-cms/utils/as-populated.ts
61
+ function asPopulated(relation) {
62
+ return isPopulated(relation) ? relation : void 0;
63
+ }
64
+
65
+ // src/payload-cms/utils/is-object-id.ts
66
+ var MONGO_ID_REGEX = /^[0-9a-fA-F]{24}$/;
67
+ function isObjectId(value) {
68
+ return typeof value === "string" && MONGO_ID_REGEX.test(value);
69
+ }
70
+
71
+ // src/payload-cms/utils/assert-populated.ts
72
+ function assertPopulated(field, fieldName = "Field") {
73
+ if (field === null || field === void 0) {
74
+ throw new Error(`${fieldName} is undefined or null`);
75
+ }
76
+ if (isObjectId(field)) {
77
+ throw new Error(`${fieldName} is not populated (received ID: ${String(field)})`);
78
+ }
79
+ }
80
+
81
+ // src/payload-cms/utils/db.ts
82
+ var import_react = require("react");
83
+ var getClient = (0, import_react.cache)(async () => {
84
+ return await getPayload();
85
+ });
86
+ var db = {
87
+ findOne: async (collection, where) => {
88
+ const payload = await getClient();
89
+ const res = await payload.find({ collection, where, limit: 1 });
90
+ return res.docs[0] ?? null;
91
+ },
92
+ findBySlug: async (collection, slug) => {
93
+ return db.findOne(collection, { slug: { equals: slug } });
94
+ },
95
+ findAll: async (collection, options) => {
96
+ const payload = await getClient();
97
+ const res = await payload.find({
98
+ collection,
99
+ limit: options?.limit ?? 100,
100
+ ...options
101
+ });
102
+ return res.docs;
103
+ },
104
+ findByID: async (collection, id) => {
105
+ const payload = await getClient();
106
+ return payload.findByID({ collection, id });
107
+ },
108
+ create: async (collection, data, options) => {
109
+ const payload = await getClient();
110
+ return payload.create({ collection, data, ...options });
111
+ },
112
+ update: async (collection, id, data, options) => {
113
+ const payload = await getClient();
114
+ return payload.update({
115
+ collection,
116
+ id,
117
+ data,
118
+ ...options
119
+ });
120
+ },
121
+ updateMany: async (collection, where, data, options) => {
122
+ const payload = await getClient();
123
+ return payload.update({
124
+ collection,
125
+ where,
126
+ data,
127
+ ...options,
128
+ id: void 0
129
+ });
130
+ },
131
+ delete: async (collection, id, options) => {
132
+ const payload = await getClient();
133
+ return payload.delete({
134
+ collection,
135
+ id,
136
+ where: void 0,
137
+ ...options
138
+ });
139
+ },
140
+ deleteMany: async (collection, where, options) => {
141
+ const payload = await getClient();
142
+ return payload.delete({
143
+ collection,
144
+ where,
145
+ id: void 0,
146
+ ...options
147
+ });
148
+ },
149
+ global: async (slug) => {
150
+ const payload = await getClient();
151
+ return payload.findGlobal({ slug });
152
+ },
153
+ raw: getClient
154
+ };
155
+
156
+ // src/payload-cms/utils/create-collection.ts
157
+ function createCollection(collection, extend) {
158
+ const base = {
159
+ findOne: (where) => db.findOne(collection, where),
160
+ findByID: (id) => db.findByID(collection, id),
161
+ findBySlug: (slug) => db.findBySlug(collection, slug),
162
+ findAll: (options) => db.findAll(collection, options),
163
+ update: (id, data, options) => db.update(collection, id, data, options),
164
+ updateMany: (where, data, options) => db.updateMany(collection, where, data, options),
165
+ delete: (id, options) => db.delete(collection, id, options),
166
+ deleteMany: (where, options) => db.deleteMany(collection, where, options)
167
+ };
168
+ const extended = extend ? extend(base) : void 0;
169
+ return {
170
+ ...base,
171
+ ...extended
172
+ };
173
+ }
174
+
175
+ // src/payload-cms/utils/create-global.ts
176
+ function createGlobal(slug, extend) {
177
+ const base = {
178
+ get: () => db.global(slug)
179
+ };
180
+ const extended = extend ? extend(base) : void 0;
181
+ return {
182
+ ...base,
183
+ ...extended
184
+ };
185
+ }
186
+ // Annotate the CommonJS export names for ESM import in node:
187
+ 0 && (module.exports = {
188
+ MONGO_ID_REGEX,
189
+ asPopulated,
190
+ assertPopulated,
191
+ createCollection,
192
+ createGlobal,
193
+ db,
194
+ getPayload,
195
+ isObjectId,
196
+ isPopulated
197
+ });
@@ -0,0 +1 @@
1
+ export * from './payload-cms';
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import "./chunk-EMH4G65N.js";
2
+ import "./chunk-NSY6BIWE.js";
3
+ import {
4
+ MONGO_ID_REGEX,
5
+ asPopulated,
6
+ assertPopulated,
7
+ createCollection,
8
+ createGlobal,
9
+ db,
10
+ isObjectId,
11
+ isPopulated
12
+ } from "./chunk-EZIPIKRW.js";
13
+ import {
14
+ getPayload
15
+ } from "./chunk-CWTTD7TW.js";
16
+ export {
17
+ MONGO_ID_REGEX,
18
+ asPopulated,
19
+ assertPopulated,
20
+ createCollection,
21
+ createGlobal,
22
+ db,
23
+ getPayload,
24
+ isObjectId,
25
+ isPopulated
26
+ };
@@ -0,0 +1,2 @@
1
+ import { SanitizedConfig, type Payload } from 'payload';
2
+ export declare const getPayload: (config?: Promise<SanitizedConfig> | SanitizedConfig) => Promise<Payload>;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/payload-cms/client/index.ts
31
+ var client_exports = {};
32
+ __export(client_exports, {
33
+ getPayload: () => getPayload
34
+ });
35
+ module.exports = __toCommonJS(client_exports);
36
+
37
+ // src/payload-cms/client/get-payload.ts
38
+ var import_payload = require("payload");
39
+ var getPayload = async (config) => {
40
+ if (config) {
41
+ return (0, import_payload.getPayload)({ config });
42
+ }
43
+ const defaultConfig = await import("@payload-config");
44
+ return (0, import_payload.getPayload)({ config: defaultConfig });
45
+ };
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ getPayload
49
+ });
@@ -0,0 +1 @@
1
+ export * from './get-payload';
@@ -0,0 +1,7 @@
1
+ import "../../chunk-NSY6BIWE.js";
2
+ import {
3
+ getPayload
4
+ } from "../../chunk-CWTTD7TW.js";
5
+ export {
6
+ getPayload
7
+ };
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/payload-cms/index.ts
31
+ var payload_cms_exports = {};
32
+ __export(payload_cms_exports, {
33
+ MONGO_ID_REGEX: () => MONGO_ID_REGEX,
34
+ asPopulated: () => asPopulated,
35
+ assertPopulated: () => assertPopulated,
36
+ createCollection: () => createCollection,
37
+ createGlobal: () => createGlobal,
38
+ db: () => db,
39
+ getPayload: () => getPayload,
40
+ isObjectId: () => isObjectId,
41
+ isPopulated: () => isPopulated
42
+ });
43
+ module.exports = __toCommonJS(payload_cms_exports);
44
+
45
+ // src/payload-cms/client/get-payload.ts
46
+ var import_payload = require("payload");
47
+ var getPayload = async (config) => {
48
+ if (config) {
49
+ return (0, import_payload.getPayload)({ config });
50
+ }
51
+ const defaultConfig = await import("@payload-config");
52
+ return (0, import_payload.getPayload)({ config: defaultConfig });
53
+ };
54
+
55
+ // src/payload-cms/utils/is-populated.ts
56
+ function isPopulated(relation) {
57
+ return typeof relation !== "string";
58
+ }
59
+
60
+ // src/payload-cms/utils/as-populated.ts
61
+ function asPopulated(relation) {
62
+ return isPopulated(relation) ? relation : void 0;
63
+ }
64
+
65
+ // src/payload-cms/utils/is-object-id.ts
66
+ var MONGO_ID_REGEX = /^[0-9a-fA-F]{24}$/;
67
+ function isObjectId(value) {
68
+ return typeof value === "string" && MONGO_ID_REGEX.test(value);
69
+ }
70
+
71
+ // src/payload-cms/utils/assert-populated.ts
72
+ function assertPopulated(field, fieldName = "Field") {
73
+ if (field === null || field === void 0) {
74
+ throw new Error(`${fieldName} is undefined or null`);
75
+ }
76
+ if (isObjectId(field)) {
77
+ throw new Error(`${fieldName} is not populated (received ID: ${String(field)})`);
78
+ }
79
+ }
80
+
81
+ // src/payload-cms/utils/db.ts
82
+ var import_react = require("react");
83
+ var getClient = (0, import_react.cache)(async () => {
84
+ return await getPayload();
85
+ });
86
+ var db = {
87
+ findOne: async (collection, where) => {
88
+ const payload = await getClient();
89
+ const res = await payload.find({ collection, where, limit: 1 });
90
+ return res.docs[0] ?? null;
91
+ },
92
+ findBySlug: async (collection, slug) => {
93
+ return db.findOne(collection, { slug: { equals: slug } });
94
+ },
95
+ findAll: async (collection, options) => {
96
+ const payload = await getClient();
97
+ const res = await payload.find({
98
+ collection,
99
+ limit: options?.limit ?? 100,
100
+ ...options
101
+ });
102
+ return res.docs;
103
+ },
104
+ findByID: async (collection, id) => {
105
+ const payload = await getClient();
106
+ return payload.findByID({ collection, id });
107
+ },
108
+ create: async (collection, data, options) => {
109
+ const payload = await getClient();
110
+ return payload.create({ collection, data, ...options });
111
+ },
112
+ update: async (collection, id, data, options) => {
113
+ const payload = await getClient();
114
+ return payload.update({
115
+ collection,
116
+ id,
117
+ data,
118
+ ...options
119
+ });
120
+ },
121
+ updateMany: async (collection, where, data, options) => {
122
+ const payload = await getClient();
123
+ return payload.update({
124
+ collection,
125
+ where,
126
+ data,
127
+ ...options,
128
+ id: void 0
129
+ });
130
+ },
131
+ delete: async (collection, id, options) => {
132
+ const payload = await getClient();
133
+ return payload.delete({
134
+ collection,
135
+ id,
136
+ where: void 0,
137
+ ...options
138
+ });
139
+ },
140
+ deleteMany: async (collection, where, options) => {
141
+ const payload = await getClient();
142
+ return payload.delete({
143
+ collection,
144
+ where,
145
+ id: void 0,
146
+ ...options
147
+ });
148
+ },
149
+ global: async (slug) => {
150
+ const payload = await getClient();
151
+ return payload.findGlobal({ slug });
152
+ },
153
+ raw: getClient
154
+ };
155
+
156
+ // src/payload-cms/utils/create-collection.ts
157
+ function createCollection(collection, extend) {
158
+ const base = {
159
+ findOne: (where) => db.findOne(collection, where),
160
+ findByID: (id) => db.findByID(collection, id),
161
+ findBySlug: (slug) => db.findBySlug(collection, slug),
162
+ findAll: (options) => db.findAll(collection, options),
163
+ update: (id, data, options) => db.update(collection, id, data, options),
164
+ updateMany: (where, data, options) => db.updateMany(collection, where, data, options),
165
+ delete: (id, options) => db.delete(collection, id, options),
166
+ deleteMany: (where, options) => db.deleteMany(collection, where, options)
167
+ };
168
+ const extended = extend ? extend(base) : void 0;
169
+ return {
170
+ ...base,
171
+ ...extended
172
+ };
173
+ }
174
+
175
+ // src/payload-cms/utils/create-global.ts
176
+ function createGlobal(slug, extend) {
177
+ const base = {
178
+ get: () => db.global(slug)
179
+ };
180
+ const extended = extend ? extend(base) : void 0;
181
+ return {
182
+ ...base,
183
+ ...extended
184
+ };
185
+ }
186
+ // Annotate the CommonJS export names for ESM import in node:
187
+ 0 && (module.exports = {
188
+ MONGO_ID_REGEX,
189
+ asPopulated,
190
+ assertPopulated,
191
+ createCollection,
192
+ createGlobal,
193
+ db,
194
+ getPayload,
195
+ isObjectId,
196
+ isPopulated
197
+ });
@@ -0,0 +1,2 @@
1
+ export * from './client';
2
+ export * from './utils';
@@ -0,0 +1,26 @@
1
+ import "../chunk-EMH4G65N.js";
2
+ import "../chunk-NSY6BIWE.js";
3
+ import {
4
+ MONGO_ID_REGEX,
5
+ asPopulated,
6
+ assertPopulated,
7
+ createCollection,
8
+ createGlobal,
9
+ db,
10
+ isObjectId,
11
+ isPopulated
12
+ } from "../chunk-EZIPIKRW.js";
13
+ import {
14
+ getPayload
15
+ } from "../chunk-CWTTD7TW.js";
16
+ export {
17
+ MONGO_ID_REGEX,
18
+ asPopulated,
19
+ assertPopulated,
20
+ createCollection,
21
+ createGlobal,
22
+ db,
23
+ getPayload,
24
+ isObjectId,
25
+ isPopulated
26
+ };
@@ -0,0 +1 @@
1
+ export declare function asPopulated<T>(relation: string | T): T | undefined;
@@ -0,0 +1 @@
1
+ export declare function assertPopulated<T extends object>(field: string | T | null | undefined, fieldName?: string): asserts field is NonNullable<T>;
@@ -0,0 +1,13 @@
1
+ import type { CollectionSlug, Where } from 'payload';
2
+ import { db, DeleteOptions, FindAllOptions, UpdateData, UpdateOptions } from './db';
3
+ export type BaseCollectionMethods<TSlug extends CollectionSlug> = {
4
+ findOne: (where?: Where) => ReturnType<typeof db.findOne<TSlug>>;
5
+ findBySlug: (slug: string) => ReturnType<typeof db.findBySlug<TSlug>>;
6
+ findByID: (id: string | number) => ReturnType<typeof db.findByID<TSlug>>;
7
+ findAll: (options?: FindAllOptions) => ReturnType<typeof db.findAll<TSlug>>;
8
+ update: (id: string | number, data: UpdateData, options?: UpdateOptions) => ReturnType<typeof db.update<TSlug>>;
9
+ updateMany: (where: Where, data: UpdateData, options?: UpdateOptions) => ReturnType<typeof db.updateMany<TSlug>>;
10
+ delete: (id: string | number, options?: DeleteOptions) => ReturnType<typeof db.delete<TSlug>>;
11
+ deleteMany: (where: Where, options?: DeleteOptions) => ReturnType<typeof db.deleteMany<TSlug>>;
12
+ };
13
+ export declare function createCollection<TSlug extends CollectionSlug, TExtend extends Record<string, (...args: any[]) => any> = {}>(collection: TSlug, extend?: (base: BaseCollectionMethods<TSlug>) => TExtend): BaseCollectionMethods<TSlug> & TExtend;
@@ -0,0 +1,7 @@
1
+ import type { GlobalSlug } from 'payload';
2
+ import { db } from './db';
3
+ type BaseGlobalMethods<TSlug extends GlobalSlug> = {
4
+ get: () => ReturnType<typeof db.global<TSlug>>;
5
+ };
6
+ export declare function createGlobal<TSlug extends GlobalSlug, TExtend extends Record<string, (...args: any[]) => any> = {}>(slug: TSlug, extend?: (base: BaseGlobalMethods<TSlug>) => TExtend): BaseGlobalMethods<TSlug> & TExtend;
7
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { CollectionSlug, GlobalSlug, Where } from 'payload';
2
+ import { getPayload } from '../client/get-payload';
3
+ export type FindAllOptions = Omit<Parameters<Awaited<ReturnType<typeof getPayload>>['find']>[0], 'collection'>;
4
+ export type UpdateOptions = Omit<Parameters<Awaited<ReturnType<typeof getPayload>>['update']>[0], 'collection' | 'id' | 'data' | 'where'>;
5
+ export type DeleteOptions = Omit<Parameters<Awaited<ReturnType<typeof getPayload>>['delete']>[0], 'collection' | 'id' | 'where'>;
6
+ export type UpdateData = Parameters<Awaited<ReturnType<typeof getPayload>>['update']>[0]['data'];
7
+ export declare const db: {
8
+ findOne: <TSlug extends CollectionSlug>(collection: TSlug, where?: Where) => Promise<import("payload").JsonObject & import("payload").TypeWithID>;
9
+ findBySlug: <TSlug extends CollectionSlug>(collection: TSlug, slug: string) => Promise<import("payload").JsonObject & import("payload").TypeWithID>;
10
+ findAll: <TSlug extends CollectionSlug>(collection: TSlug, options?: FindAllOptions) => Promise<(import("payload").JsonObject & import("payload").TypeWithID)[]>;
11
+ findByID: <TSlug extends CollectionSlug>(collection: TSlug, id: string | number) => Promise<import("payload").JsonObject & import("payload").TypeWithID>;
12
+ create: <TSlug extends CollectionSlug>(collection: TSlug, data: Parameters<Awaited<ReturnType<typeof getPayload>>['create']>[0]['data'], options?: Omit<Parameters<Awaited<ReturnType<typeof getPayload>>['create']>[0], 'collection' | 'data'>) => Promise<import("payload").JsonObject & import("payload").TypeWithID>;
13
+ update: <TSlug extends CollectionSlug>(collection: TSlug, id: string | number, data: UpdateData, options?: UpdateOptions) => Promise<import("payload").JsonObject & import("payload").TypeWithID>;
14
+ updateMany: <TSlug extends CollectionSlug>(collection: TSlug, where: Where, data: UpdateData, options?: UpdateOptions) => Promise<import("payload").BulkOperationResult<TSlug, import("payload").SelectExcludeType | import("payload").SelectIncludeType>>;
15
+ delete: <TSlug extends CollectionSlug>(collection: TSlug, id: string | number, options?: DeleteOptions) => Promise<import("payload").JsonObject & import("payload").TypeWithID>;
16
+ deleteMany: <TSlug extends CollectionSlug>(collection: TSlug, where: Where, options?: DeleteOptions) => Promise<import("payload").BulkOperationResult<TSlug, import("payload").SelectExcludeType | import("payload").SelectIncludeType>>;
17
+ global: <TSlug extends GlobalSlug>(slug: TSlug) => Promise<import("payload").JsonObject>;
18
+ raw: () => Promise<import("payload").BasePayload>;
19
+ };
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/payload-cms/utils/index.ts
31
+ var utils_exports = {};
32
+ __export(utils_exports, {
33
+ MONGO_ID_REGEX: () => MONGO_ID_REGEX,
34
+ asPopulated: () => asPopulated,
35
+ assertPopulated: () => assertPopulated,
36
+ createCollection: () => createCollection,
37
+ createGlobal: () => createGlobal,
38
+ db: () => db,
39
+ isObjectId: () => isObjectId,
40
+ isPopulated: () => isPopulated
41
+ });
42
+ module.exports = __toCommonJS(utils_exports);
43
+
44
+ // src/payload-cms/utils/is-populated.ts
45
+ function isPopulated(relation) {
46
+ return typeof relation !== "string";
47
+ }
48
+
49
+ // src/payload-cms/utils/as-populated.ts
50
+ function asPopulated(relation) {
51
+ return isPopulated(relation) ? relation : void 0;
52
+ }
53
+
54
+ // src/payload-cms/utils/is-object-id.ts
55
+ var MONGO_ID_REGEX = /^[0-9a-fA-F]{24}$/;
56
+ function isObjectId(value) {
57
+ return typeof value === "string" && MONGO_ID_REGEX.test(value);
58
+ }
59
+
60
+ // src/payload-cms/utils/assert-populated.ts
61
+ function assertPopulated(field, fieldName = "Field") {
62
+ if (field === null || field === void 0) {
63
+ throw new Error(`${fieldName} is undefined or null`);
64
+ }
65
+ if (isObjectId(field)) {
66
+ throw new Error(`${fieldName} is not populated (received ID: ${String(field)})`);
67
+ }
68
+ }
69
+
70
+ // src/payload-cms/utils/db.ts
71
+ var import_react = require("react");
72
+
73
+ // src/payload-cms/client/get-payload.ts
74
+ var import_payload = require("payload");
75
+ var getPayload = async (config) => {
76
+ if (config) {
77
+ return (0, import_payload.getPayload)({ config });
78
+ }
79
+ const defaultConfig = await import("@payload-config");
80
+ return (0, import_payload.getPayload)({ config: defaultConfig });
81
+ };
82
+
83
+ // src/payload-cms/utils/db.ts
84
+ var getClient = (0, import_react.cache)(async () => {
85
+ return await getPayload();
86
+ });
87
+ var db = {
88
+ findOne: async (collection, where) => {
89
+ const payload = await getClient();
90
+ const res = await payload.find({ collection, where, limit: 1 });
91
+ return res.docs[0] ?? null;
92
+ },
93
+ findBySlug: async (collection, slug) => {
94
+ return db.findOne(collection, { slug: { equals: slug } });
95
+ },
96
+ findAll: async (collection, options) => {
97
+ const payload = await getClient();
98
+ const res = await payload.find({
99
+ collection,
100
+ limit: options?.limit ?? 100,
101
+ ...options
102
+ });
103
+ return res.docs;
104
+ },
105
+ findByID: async (collection, id) => {
106
+ const payload = await getClient();
107
+ return payload.findByID({ collection, id });
108
+ },
109
+ create: async (collection, data, options) => {
110
+ const payload = await getClient();
111
+ return payload.create({ collection, data, ...options });
112
+ },
113
+ update: async (collection, id, data, options) => {
114
+ const payload = await getClient();
115
+ return payload.update({
116
+ collection,
117
+ id,
118
+ data,
119
+ ...options
120
+ });
121
+ },
122
+ updateMany: async (collection, where, data, options) => {
123
+ const payload = await getClient();
124
+ return payload.update({
125
+ collection,
126
+ where,
127
+ data,
128
+ ...options,
129
+ id: void 0
130
+ });
131
+ },
132
+ delete: async (collection, id, options) => {
133
+ const payload = await getClient();
134
+ return payload.delete({
135
+ collection,
136
+ id,
137
+ where: void 0,
138
+ ...options
139
+ });
140
+ },
141
+ deleteMany: async (collection, where, options) => {
142
+ const payload = await getClient();
143
+ return payload.delete({
144
+ collection,
145
+ where,
146
+ id: void 0,
147
+ ...options
148
+ });
149
+ },
150
+ global: async (slug) => {
151
+ const payload = await getClient();
152
+ return payload.findGlobal({ slug });
153
+ },
154
+ raw: getClient
155
+ };
156
+
157
+ // src/payload-cms/utils/create-collection.ts
158
+ function createCollection(collection, extend) {
159
+ const base = {
160
+ findOne: (where) => db.findOne(collection, where),
161
+ findByID: (id) => db.findByID(collection, id),
162
+ findBySlug: (slug) => db.findBySlug(collection, slug),
163
+ findAll: (options) => db.findAll(collection, options),
164
+ update: (id, data, options) => db.update(collection, id, data, options),
165
+ updateMany: (where, data, options) => db.updateMany(collection, where, data, options),
166
+ delete: (id, options) => db.delete(collection, id, options),
167
+ deleteMany: (where, options) => db.deleteMany(collection, where, options)
168
+ };
169
+ const extended = extend ? extend(base) : void 0;
170
+ return {
171
+ ...base,
172
+ ...extended
173
+ };
174
+ }
175
+
176
+ // src/payload-cms/utils/create-global.ts
177
+ function createGlobal(slug, extend) {
178
+ const base = {
179
+ get: () => db.global(slug)
180
+ };
181
+ const extended = extend ? extend(base) : void 0;
182
+ return {
183
+ ...base,
184
+ ...extended
185
+ };
186
+ }
187
+ // Annotate the CommonJS export names for ESM import in node:
188
+ 0 && (module.exports = {
189
+ MONGO_ID_REGEX,
190
+ asPopulated,
191
+ assertPopulated,
192
+ createCollection,
193
+ createGlobal,
194
+ db,
195
+ isObjectId,
196
+ isPopulated
197
+ });
@@ -0,0 +1,7 @@
1
+ export * from './as-populated';
2
+ export * from './assert-populated';
3
+ export * from './create-collection';
4
+ export * from './create-global';
5
+ export * from './db';
6
+ export * from './is-object-id';
7
+ export * from './is-populated';
@@ -0,0 +1,21 @@
1
+ import {
2
+ MONGO_ID_REGEX,
3
+ asPopulated,
4
+ assertPopulated,
5
+ createCollection,
6
+ createGlobal,
7
+ db,
8
+ isObjectId,
9
+ isPopulated
10
+ } from "../../chunk-EZIPIKRW.js";
11
+ import "../../chunk-CWTTD7TW.js";
12
+ export {
13
+ MONGO_ID_REGEX,
14
+ asPopulated,
15
+ assertPopulated,
16
+ createCollection,
17
+ createGlobal,
18
+ db,
19
+ isObjectId,
20
+ isPopulated
21
+ };
@@ -0,0 +1,2 @@
1
+ export declare const MONGO_ID_REGEX: RegExp;
2
+ export declare function isObjectId(value: unknown): value is string;
@@ -0,0 +1 @@
1
+ export declare function isPopulated<T>(relation: string | T): relation is T;
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@matvdt/js-toolkit",
3
+ "version": "0.0.1-canary.2",
4
+ "description": "JS/TS toolkit",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs"
13
+ },
14
+ "./payload-cms": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsup && tsc",
25
+ "publish:canary": "npm publish --access public --tag canary",
26
+ "publish:alpha" : "npm publish --access public --tag alpha",
27
+ "publish:beta" : "npm publish --access public --tag beta",
28
+ "publish:rc" : "npm publish --access public --tag rc",
29
+ "publish:latest": "npm publish --access public",
30
+ "prepublishOnly": "npm run build"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "keywords": [
36
+ "toolkit",
37
+ "javascript"
38
+ ],
39
+ "author": "Matteo Vidotto",
40
+ "license": "MIT",
41
+ "devDependencies": {
42
+ "@types/node": "^26.1.2",
43
+ "@types/react": "^19.2.17",
44
+ "payload": "^3.86.0",
45
+ "react": "^19.2.8",
46
+ "tsup": "^8.5.1",
47
+ "typescript": "^7.0.2"
48
+ },
49
+ "peerDependencies": {
50
+ "payload": ">=3.0.0",
51
+ "react": ">=18.0.0"
52
+ },
53
+ "peerDependenciesMeta": {
54
+ "payload": {
55
+ "optional": true
56
+ },
57
+ "react": {
58
+ "optional": true
59
+ }
60
+ }
61
+ }