convex-verify 1.0.5 → 1.2.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 +271 -235
- package/dist/core/index.d.mts +14 -85
- package/dist/core/index.d.ts +14 -85
- package/dist/core/index.js +520 -83
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +516 -80
- package/dist/core/index.mjs.map +1 -1
- package/dist/index.d.mts +9 -6
- package/dist/index.d.ts +9 -6
- package/dist/index.js +386 -233
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +383 -226
- package/dist/index.mjs.map +1 -1
- package/dist/types-B8ZkLuJ2.d.mts +141 -0
- package/dist/types-B8ZkLuJ2.d.ts +141 -0
- package/dist/utils/index.d.mts +3 -2
- package/dist/utils/index.d.ts +3 -2
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/index.mjs.map +1 -1
- package/dist/verifyConfig-CTrtqMr_.d.ts +94 -0
- package/dist/verifyConfig-Kn3Ikj00.d.mts +94 -0
- package/package.json +5 -22
- package/dist/configs/index.d.mts +0 -51
- package/dist/configs/index.d.ts +0 -51
- package/dist/configs/index.js +0 -38
- package/dist/configs/index.js.map +0 -1
- package/dist/configs/index.mjs +0 -11
- package/dist/configs/index.mjs.map +0 -1
- package/dist/plugin-BjJ7yjrc.d.ts +0 -141
- package/dist/plugin-mHMV2-SG.d.mts +0 -141
- package/dist/plugins/index.d.mts +0 -85
- package/dist/plugins/index.d.ts +0 -85
- package/dist/plugins/index.js +0 -312
- package/dist/plugins/index.js.map +0 -1
- package/dist/plugins/index.mjs +0 -284
- package/dist/plugins/index.mjs.map +0 -1
- package/dist/transforms/index.d.mts +0 -38
- package/dist/transforms/index.d.ts +0 -38
- package/dist/transforms/index.js +0 -46
- package/dist/transforms/index.js.map +0 -1
- package/dist/transforms/index.mjs +0 -19
- package/dist/transforms/index.mjs.map +0 -1
- package/dist/types-_64SXyva.d.mts +0 -151
- package/dist/types-_64SXyva.d.ts +0 -151
package/dist/index.mjs
CHANGED
|
@@ -1,101 +1,5 @@
|
|
|
1
|
-
// src/core/
|
|
2
|
-
|
|
3
|
-
return typeof obj === "object" && obj !== null && "_type" in obj && typeof obj._type === "string" && "verify" in obj && typeof obj.verify === "object";
|
|
4
|
-
}
|
|
5
|
-
async function runValidatePlugins(plugins, context, data) {
|
|
6
|
-
let result = data;
|
|
7
|
-
for (const plugin of plugins) {
|
|
8
|
-
const verifyFn = context.operation === "insert" ? plugin.verify.insert : plugin.verify.patch;
|
|
9
|
-
if (verifyFn) {
|
|
10
|
-
result = await verifyFn(context, result);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
return result;
|
|
14
|
-
}
|
|
15
|
-
function createValidatePlugin(type, config, verify) {
|
|
16
|
-
return {
|
|
17
|
-
_type: type,
|
|
18
|
-
config,
|
|
19
|
-
verify
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// src/core/verifyConfig.ts
|
|
24
|
-
var verifyConfig = (_schema, configs) => {
|
|
25
|
-
const validatePlugins = [
|
|
26
|
-
// Built-in validation configs (if provided as named keys)
|
|
27
|
-
...configs.uniqueRow ? [configs.uniqueRow] : [],
|
|
28
|
-
...configs.uniqueColumn ? [configs.uniqueColumn] : [],
|
|
29
|
-
// Additional plugins from the plugins array
|
|
30
|
-
...configs.plugins ?? []
|
|
31
|
-
];
|
|
32
|
-
const insert = async (ctx, tableName, data, options) => {
|
|
33
|
-
let verifiedData = data;
|
|
34
|
-
if (configs.defaultValues) {
|
|
35
|
-
verifiedData = await configs.defaultValues.verify(
|
|
36
|
-
tableName,
|
|
37
|
-
verifiedData
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
if (validatePlugins.length > 0) {
|
|
41
|
-
verifiedData = await runValidatePlugins(
|
|
42
|
-
validatePlugins,
|
|
43
|
-
{
|
|
44
|
-
ctx,
|
|
45
|
-
tableName,
|
|
46
|
-
operation: "insert",
|
|
47
|
-
onFail: options?.onFail,
|
|
48
|
-
schema: _schema
|
|
49
|
-
},
|
|
50
|
-
verifiedData
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
return await ctx.db.insert(tableName, verifiedData);
|
|
54
|
-
};
|
|
55
|
-
const patch = async (ctx, tableName, id, data, options) => {
|
|
56
|
-
let verifiedData = data;
|
|
57
|
-
if (validatePlugins.length > 0) {
|
|
58
|
-
verifiedData = await runValidatePlugins(
|
|
59
|
-
validatePlugins,
|
|
60
|
-
{
|
|
61
|
-
ctx,
|
|
62
|
-
tableName,
|
|
63
|
-
operation: "patch",
|
|
64
|
-
patchId: id,
|
|
65
|
-
onFail: options?.onFail,
|
|
66
|
-
schema: _schema
|
|
67
|
-
},
|
|
68
|
-
verifiedData
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
await ctx.db.patch(id, verifiedData);
|
|
72
|
-
};
|
|
73
|
-
const dangerouslyPatch = async (ctx, tableName, id, data, options) => {
|
|
74
|
-
let verifiedData = data;
|
|
75
|
-
if (validatePlugins.length > 0) {
|
|
76
|
-
verifiedData = await runValidatePlugins(
|
|
77
|
-
validatePlugins,
|
|
78
|
-
{
|
|
79
|
-
ctx,
|
|
80
|
-
tableName,
|
|
81
|
-
operation: "patch",
|
|
82
|
-
patchId: id,
|
|
83
|
-
onFail: options?.onFail,
|
|
84
|
-
schema: _schema
|
|
85
|
-
},
|
|
86
|
-
verifiedData
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
await ctx.db.patch(id, verifiedData);
|
|
90
|
-
};
|
|
91
|
-
return {
|
|
92
|
-
insert,
|
|
93
|
-
patch,
|
|
94
|
-
dangerouslyPatch,
|
|
95
|
-
// Expose configs for debugging/advanced usage
|
|
96
|
-
configs
|
|
97
|
-
};
|
|
98
|
-
};
|
|
1
|
+
// src/core/builtins.ts
|
|
2
|
+
import { ConvexError } from "convex/values";
|
|
99
3
|
|
|
100
4
|
// src/core/types.ts
|
|
101
5
|
function normalizeIndexConfigEntry(entry, defaultIdentifiers = ["_id"]) {
|
|
@@ -113,33 +17,6 @@ function normalizeIndexConfigEntry(entry, defaultIdentifiers = ["_id"]) {
|
|
|
113
17
|
};
|
|
114
18
|
}
|
|
115
19
|
|
|
116
|
-
// src/transforms/defaultValuesConfig.ts
|
|
117
|
-
var defaultValuesConfig = (_schema, config) => {
|
|
118
|
-
const verify = async (tableName, data) => {
|
|
119
|
-
const resolvedConfig = typeof config === "function" ? await config() : config;
|
|
120
|
-
return {
|
|
121
|
-
...resolvedConfig[tableName],
|
|
122
|
-
...data
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
return {
|
|
126
|
-
_type: "defaultValues",
|
|
127
|
-
verify,
|
|
128
|
-
config
|
|
129
|
-
};
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
// src/configs/protectedColumnsConfig.ts
|
|
133
|
-
var protectedColumnsConfig = (_schema, config) => {
|
|
134
|
-
return {
|
|
135
|
-
_type: "protectedColumns",
|
|
136
|
-
config
|
|
137
|
-
};
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
// src/plugins/uniqueRowConfig.ts
|
|
141
|
-
import { ConvexError } from "convex/values";
|
|
142
|
-
|
|
143
20
|
// src/utils/helpers.ts
|
|
144
21
|
var getTableIndexes = (schema, tableName) => {
|
|
145
22
|
return schema.tables[tableName][" indexes"]();
|
|
@@ -152,7 +29,7 @@ var constructColumnData = (fields, data, {
|
|
|
152
29
|
const columnData = fields.map((_, index) => {
|
|
153
30
|
const column = fields?.[index];
|
|
154
31
|
const value = data?.[column];
|
|
155
|
-
if (!column || !allowNullishValue &&
|
|
32
|
+
if (!column || !allowNullishValue && (value === void 0 || value === null)) {
|
|
156
33
|
return;
|
|
157
34
|
}
|
|
158
35
|
return {
|
|
@@ -196,37 +73,108 @@ var constructIndexData = (schema, tableName, indexConfig) => {
|
|
|
196
73
|
});
|
|
197
74
|
};
|
|
198
75
|
|
|
199
|
-
// src/
|
|
200
|
-
var
|
|
76
|
+
// src/core/builtins.ts
|
|
77
|
+
var stripProtectedPatchColumns = (protectedColumns, tableName, data) => {
|
|
78
|
+
const protectedKeys = protectedColumns[tableName] ?? [];
|
|
79
|
+
if (protectedKeys.length === 0) {
|
|
80
|
+
return {
|
|
81
|
+
filteredData: data,
|
|
82
|
+
removedColumns: []
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const protectedKeyStrings = protectedKeys.map(String);
|
|
86
|
+
const protectedKeySet = new Set(protectedKeyStrings);
|
|
87
|
+
const removedColumns = protectedKeyStrings.filter((key) => key in data);
|
|
88
|
+
if (removedColumns.length === 0) {
|
|
89
|
+
return {
|
|
90
|
+
filteredData: data,
|
|
91
|
+
removedColumns
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const filteredData = Object.fromEntries(
|
|
95
|
+
Object.entries(data).filter(([key]) => !protectedKeySet.has(key))
|
|
96
|
+
);
|
|
97
|
+
return {
|
|
98
|
+
filteredData,
|
|
99
|
+
removedColumns
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
var buildDefaultValuesVerifier = (config) => {
|
|
103
|
+
const verify = (async (...args) => {
|
|
104
|
+
const input = args.length === 2 ? {
|
|
105
|
+
tableName: args[0],
|
|
106
|
+
operation: "insert",
|
|
107
|
+
data: args[1]
|
|
108
|
+
} : args[0];
|
|
109
|
+
if (input.operation === "patch") {
|
|
110
|
+
return input.data;
|
|
111
|
+
}
|
|
112
|
+
const resolvedConfig = typeof config === "function" ? await config() : config;
|
|
113
|
+
return {
|
|
114
|
+
...resolvedConfig[input.tableName] ?? {},
|
|
115
|
+
...input.data
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
_type: "defaultValues",
|
|
120
|
+
config,
|
|
121
|
+
verify
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
var buildProtectedColumnsVerifier = (config) => {
|
|
125
|
+
const verify = (async (...args) => {
|
|
126
|
+
const input = args.length === 2 ? {
|
|
127
|
+
tableName: args[0],
|
|
128
|
+
operation: "patch",
|
|
129
|
+
data: args[1]
|
|
130
|
+
} : args[0];
|
|
131
|
+
if (input.operation === "insert") {
|
|
132
|
+
return input.data;
|
|
133
|
+
}
|
|
134
|
+
return stripProtectedPatchColumns(config, input.tableName, input.data).filteredData;
|
|
135
|
+
});
|
|
136
|
+
return {
|
|
137
|
+
_type: "protectedColumns",
|
|
138
|
+
config,
|
|
139
|
+
verify
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
var buildUniqueRowVerifier = (schema, config) => {
|
|
201
143
|
const uniqueRowError = (message) => {
|
|
202
144
|
throw new ConvexError({
|
|
203
145
|
message,
|
|
204
146
|
code: "UNIQUE_ROW_VERIFICATION_ERROR"
|
|
205
147
|
});
|
|
206
148
|
};
|
|
207
|
-
const
|
|
208
|
-
const
|
|
149
|
+
const verify = (async (...args) => {
|
|
150
|
+
const input = args.length === 3 ? {
|
|
151
|
+
ctx: args[0],
|
|
152
|
+
tableName: args[1],
|
|
153
|
+
operation: "insert",
|
|
154
|
+
data: args[2]
|
|
155
|
+
} : args.length === 4 ? {
|
|
156
|
+
ctx: args[0],
|
|
157
|
+
tableName: args[1],
|
|
158
|
+
operation: "patch",
|
|
159
|
+
patchId: args[2],
|
|
160
|
+
data: args[3]
|
|
161
|
+
} : args[0];
|
|
162
|
+
const { ctx, tableName, operation, patchId, onFail, data } = input;
|
|
209
163
|
const indexesData = constructIndexData(schema, tableName, config);
|
|
210
|
-
if (!indexesData && !!config[tableName]) {
|
|
211
|
-
uniqueRowError(`Index data was not found where there should have been.`);
|
|
212
|
-
}
|
|
213
164
|
if (!indexesData) {
|
|
214
165
|
return data;
|
|
215
166
|
}
|
|
216
167
|
for (const indexInfo of indexesData) {
|
|
217
|
-
const { name, fields, identifiers
|
|
218
|
-
|
|
219
|
-
if (!fields[0] && !fields[1]) {
|
|
168
|
+
const { name, fields, identifiers } = indexInfo;
|
|
169
|
+
if (fields.length < 2) {
|
|
220
170
|
uniqueRowError(
|
|
221
|
-
`Error in 'verifyRowUniqueness()'. There must be two columns to test against. If you are attempting to enforce a unique column, use the '
|
|
171
|
+
`Error in 'verifyRowUniqueness()'. There must be two columns to test against. If you are attempting to enforce a unique column, use the 'uniqueColumn' config option.`
|
|
222
172
|
);
|
|
223
173
|
}
|
|
224
174
|
const columnData = constructColumnData(fields, data, {});
|
|
225
175
|
const getExisting = async (cd) => {
|
|
226
176
|
let existingByIndex = [];
|
|
227
|
-
if (
|
|
228
|
-
existingByIndex = [];
|
|
229
|
-
} else {
|
|
177
|
+
if (cd) {
|
|
230
178
|
existingByIndex = await ctx.db.query(tableName).withIndex(
|
|
231
179
|
name,
|
|
232
180
|
(q) => cd.reduce((query, { column, value }) => query.eq(column, value), q)
|
|
@@ -235,10 +183,10 @@ var uniqueRowConfig = (schema, config) => {
|
|
|
235
183
|
if (existingByIndex.length > 1) {
|
|
236
184
|
console.warn(
|
|
237
185
|
`There was more than one existing result found for index ${name}. Check the following IDs:`,
|
|
238
|
-
existingByIndex.map((
|
|
186
|
+
existingByIndex.map((row) => row._id)
|
|
239
187
|
);
|
|
240
188
|
console.warn(
|
|
241
|
-
|
|
189
|
+
"It is recommended that you triage the rows listed above since they have data that go against a rule of row uniqueness."
|
|
242
190
|
);
|
|
243
191
|
}
|
|
244
192
|
return existingByIndex.length > 0 ? existingByIndex[0] : null;
|
|
@@ -254,91 +202,89 @@ var uniqueRowConfig = (schema, config) => {
|
|
|
254
202
|
}
|
|
255
203
|
});
|
|
256
204
|
uniqueRowError(
|
|
257
|
-
`Unable to [${operation}] document. In table [${tableName}], there is an existing row that has the same data combination in the columns: [${fields.join(
|
|
205
|
+
`Unable to [${operation}] document. In table [${tableName}], there is an existing row that has the same data combination in the columns: [${fields.join(", ")}].`
|
|
258
206
|
);
|
|
259
207
|
}
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
break;
|
|
271
|
-
}
|
|
208
|
+
if (!patchId) {
|
|
209
|
+
uniqueRowError("Unable to patch document without an id.");
|
|
210
|
+
}
|
|
211
|
+
const matchedToExisting = (_existing, _data) => {
|
|
212
|
+
let idMatchedToExisting = null;
|
|
213
|
+
if (_existing) {
|
|
214
|
+
for (const identifier of identifiers) {
|
|
215
|
+
if (_existing[identifier] !== void 0 && _data[identifier] !== void 0 && _existing[identifier] === _data[identifier] || identifier === "_id" && _existing[identifier] === patchId) {
|
|
216
|
+
idMatchedToExisting = String(identifier);
|
|
217
|
+
break;
|
|
272
218
|
}
|
|
273
219
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
uniqueRow: {
|
|
286
|
-
existingData: _existing
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
uniqueRowError(
|
|
290
|
-
`In '${tableName}' table, there already exists a value match of the columns: [${fields.join(`,`)}].`
|
|
291
|
-
);
|
|
292
|
-
}
|
|
293
|
-
};
|
|
294
|
-
if (!existing && !columnData && patchId) {
|
|
295
|
-
const match = await ctx.db.get(patchId);
|
|
296
|
-
if (!match) {
|
|
297
|
-
uniqueRowError(`No document found for id ${patchId}`);
|
|
298
|
-
return data;
|
|
299
|
-
}
|
|
300
|
-
const extensiveColumnData = constructColumnData(
|
|
301
|
-
fields,
|
|
302
|
-
{
|
|
303
|
-
...match,
|
|
304
|
-
...data
|
|
305
|
-
},
|
|
306
|
-
{}
|
|
307
|
-
);
|
|
308
|
-
if (extensiveColumnData) {
|
|
309
|
-
const extensiveExisting = await getExisting(extensiveColumnData);
|
|
310
|
-
checkExisting(extensiveExisting, data);
|
|
311
|
-
} else {
|
|
312
|
-
uniqueRowError(`Incomplete data when there should have been enough.`);
|
|
220
|
+
}
|
|
221
|
+
return idMatchedToExisting;
|
|
222
|
+
};
|
|
223
|
+
const checkExisting = (_existing, _data) => {
|
|
224
|
+
const matchedId = matchedToExisting(_existing, _data);
|
|
225
|
+
if (!_existing || matchedId) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
onFail?.({
|
|
229
|
+
uniqueRow: {
|
|
230
|
+
existingData: _existing
|
|
313
231
|
}
|
|
314
|
-
}
|
|
315
|
-
|
|
232
|
+
});
|
|
233
|
+
uniqueRowError(
|
|
234
|
+
`In '${tableName}' table, there already exists a value match of the columns: [${fields.join(",")}].`
|
|
235
|
+
);
|
|
236
|
+
};
|
|
237
|
+
if (!existing && !columnData) {
|
|
238
|
+
const match = await ctx.db.get(patchId);
|
|
239
|
+
if (!match) {
|
|
240
|
+
uniqueRowError(`No document found for id ${patchId}`);
|
|
241
|
+
}
|
|
242
|
+
const extensiveColumnData = constructColumnData(
|
|
243
|
+
fields,
|
|
244
|
+
{
|
|
245
|
+
...match,
|
|
246
|
+
...data
|
|
247
|
+
},
|
|
248
|
+
{}
|
|
249
|
+
);
|
|
250
|
+
if (!extensiveColumnData) {
|
|
251
|
+
uniqueRowError("Incomplete data when there should have been enough.");
|
|
316
252
|
}
|
|
253
|
+
const extensiveExisting = await getExisting(extensiveColumnData);
|
|
254
|
+
checkExisting(extensiveExisting, data);
|
|
255
|
+
continue;
|
|
317
256
|
}
|
|
257
|
+
checkExisting(existing, data);
|
|
318
258
|
}
|
|
319
259
|
return data;
|
|
320
|
-
};
|
|
321
|
-
return createValidatePlugin("uniqueRow", config, {
|
|
322
|
-
insert: async (context, data) => {
|
|
323
|
-
return verifyUniqueness(context, data, context.tableName);
|
|
324
|
-
},
|
|
325
|
-
patch: async (context, data) => {
|
|
326
|
-
return verifyUniqueness(context, data, context.tableName);
|
|
327
|
-
}
|
|
328
260
|
});
|
|
261
|
+
return {
|
|
262
|
+
_type: "uniqueRow",
|
|
263
|
+
config,
|
|
264
|
+
verify
|
|
265
|
+
};
|
|
329
266
|
};
|
|
330
|
-
|
|
331
|
-
// src/plugins/uniqueColumnConfig.ts
|
|
332
|
-
import { ConvexError as ConvexError2 } from "convex/values";
|
|
333
|
-
var uniqueColumnConfig = (_schema, config) => {
|
|
267
|
+
var buildUniqueColumnVerifier = (config) => {
|
|
334
268
|
const uniqueColumnError = (message) => {
|
|
335
|
-
throw new
|
|
269
|
+
throw new ConvexError({
|
|
336
270
|
message,
|
|
337
271
|
code: "UNIQUE_COLUMN_VERIFICATION_ERROR"
|
|
338
272
|
});
|
|
339
273
|
};
|
|
340
|
-
const
|
|
341
|
-
const
|
|
274
|
+
const verify = (async (...args) => {
|
|
275
|
+
const input = args.length === 3 ? {
|
|
276
|
+
ctx: args[0],
|
|
277
|
+
tableName: args[1],
|
|
278
|
+
operation: "insert",
|
|
279
|
+
data: args[2]
|
|
280
|
+
} : args.length === 4 ? {
|
|
281
|
+
ctx: args[0],
|
|
282
|
+
tableName: args[1],
|
|
283
|
+
operation: "patch",
|
|
284
|
+
patchId: args[2],
|
|
285
|
+
data: args[3]
|
|
286
|
+
} : args[0];
|
|
287
|
+
const { ctx, tableName, patchId, onFail, data } = input;
|
|
342
288
|
const tableConfig = config[tableName];
|
|
343
289
|
if (!tableConfig) {
|
|
344
290
|
return data;
|
|
@@ -362,7 +308,7 @@ var uniqueColumnConfig = (_schema, config) => {
|
|
|
362
308
|
isOwnDocument = true;
|
|
363
309
|
break;
|
|
364
310
|
}
|
|
365
|
-
if (existing[identifier] && data[identifier] && existing[identifier] === data[identifier]) {
|
|
311
|
+
if (existing[identifier] !== void 0 && data[identifier] !== void 0 && existing[identifier] === data[identifier]) {
|
|
366
312
|
isOwnDocument = true;
|
|
367
313
|
break;
|
|
368
314
|
}
|
|
@@ -381,28 +327,239 @@ var uniqueColumnConfig = (_schema, config) => {
|
|
|
381
327
|
);
|
|
382
328
|
}
|
|
383
329
|
return data;
|
|
330
|
+
});
|
|
331
|
+
return {
|
|
332
|
+
_type: "uniqueColumn",
|
|
333
|
+
config,
|
|
334
|
+
verify
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
// src/core/plugin.ts
|
|
339
|
+
function createExtension(schemaOrVerify, verify) {
|
|
340
|
+
const extensionVerify = typeof verify === "function" ? verify : schemaOrVerify;
|
|
341
|
+
return {
|
|
342
|
+
_type: "extension",
|
|
343
|
+
verify(input) {
|
|
344
|
+
return extensionVerify(input);
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
function isExtension(value) {
|
|
349
|
+
return typeof value === "object" && value !== null && "verify" in value && typeof value.verify === "function";
|
|
350
|
+
}
|
|
351
|
+
async function runExtensions(extensions, input) {
|
|
352
|
+
let verifiedData = input.data;
|
|
353
|
+
for (const extension of extensions) {
|
|
354
|
+
verifiedData = await extension.verify({
|
|
355
|
+
...input,
|
|
356
|
+
data: verifiedData
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return verifiedData;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// src/core/verifyConfig.ts
|
|
363
|
+
var verifyConfig = (_schema, configs) => {
|
|
364
|
+
const builtins = {
|
|
365
|
+
...configs.defaultValues ? {
|
|
366
|
+
defaultValues: buildDefaultValuesVerifier(configs.defaultValues)
|
|
367
|
+
} : {},
|
|
368
|
+
...configs.protectedColumns ? {
|
|
369
|
+
protectedColumns: buildProtectedColumnsVerifier(configs.protectedColumns)
|
|
370
|
+
} : {},
|
|
371
|
+
...configs.uniqueRow ? {
|
|
372
|
+
uniqueRow: buildUniqueRowVerifier(
|
|
373
|
+
_schema,
|
|
374
|
+
configs.uniqueRow
|
|
375
|
+
)
|
|
376
|
+
} : {},
|
|
377
|
+
...configs.uniqueColumn ? {
|
|
378
|
+
uniqueColumn: buildUniqueColumnVerifier(configs.uniqueColumn)
|
|
379
|
+
} : {}
|
|
384
380
|
};
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
},
|
|
389
|
-
|
|
390
|
-
|
|
381
|
+
const verify = {
|
|
382
|
+
...builtins.defaultValues ? { defaultValues: builtins.defaultValues.verify } : {},
|
|
383
|
+
...builtins.protectedColumns ? { protectedColumns: builtins.protectedColumns.verify } : {},
|
|
384
|
+
...builtins.uniqueRow ? { uniqueRow: builtins.uniqueRow.verify } : {},
|
|
385
|
+
...builtins.uniqueColumn ? { uniqueColumn: builtins.uniqueColumn.verify } : {}
|
|
386
|
+
};
|
|
387
|
+
const config = {
|
|
388
|
+
...configs.defaultValues ? { defaultValues: configs.defaultValues } : {},
|
|
389
|
+
...configs.protectedColumns ? { protectedColumns: configs.protectedColumns } : {},
|
|
390
|
+
...configs.uniqueRow ? { uniqueRow: configs.uniqueRow } : {},
|
|
391
|
+
...configs.uniqueColumn ? { uniqueColumn: configs.uniqueColumn } : {}
|
|
392
|
+
};
|
|
393
|
+
const customExtensions = configs.extensions ?? [];
|
|
394
|
+
const insert = async (ctx, tableName, data, options) => {
|
|
395
|
+
let verifiedData = data;
|
|
396
|
+
if (builtins.defaultValues) {
|
|
397
|
+
verifiedData = await builtins.defaultValues.verify({
|
|
398
|
+
ctx,
|
|
399
|
+
tableName,
|
|
400
|
+
operation: "insert",
|
|
401
|
+
onFail: options?.onFail,
|
|
402
|
+
schema: _schema,
|
|
403
|
+
data: verifiedData
|
|
404
|
+
});
|
|
391
405
|
}
|
|
392
|
-
|
|
406
|
+
if (customExtensions.length > 0) {
|
|
407
|
+
verifiedData = await runExtensions(customExtensions, {
|
|
408
|
+
ctx,
|
|
409
|
+
tableName,
|
|
410
|
+
operation: "insert",
|
|
411
|
+
onFail: options?.onFail,
|
|
412
|
+
schema: _schema,
|
|
413
|
+
data: verifiedData
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
if (builtins.uniqueRow) {
|
|
417
|
+
verifiedData = await builtins.uniqueRow.verify({
|
|
418
|
+
ctx,
|
|
419
|
+
tableName,
|
|
420
|
+
operation: "insert",
|
|
421
|
+
onFail: options?.onFail,
|
|
422
|
+
schema: _schema,
|
|
423
|
+
data: verifiedData
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
if (builtins.uniqueColumn) {
|
|
427
|
+
verifiedData = await builtins.uniqueColumn.verify({
|
|
428
|
+
ctx,
|
|
429
|
+
tableName,
|
|
430
|
+
operation: "insert",
|
|
431
|
+
onFail: options?.onFail,
|
|
432
|
+
schema: _schema,
|
|
433
|
+
data: verifiedData
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
return await ctx.db.insert(tableName, verifiedData);
|
|
437
|
+
};
|
|
438
|
+
const patch = async (ctx, tableName, id, data, options) => {
|
|
439
|
+
let verifiedData = data;
|
|
440
|
+
const removedProtectedColumns = /* @__PURE__ */ new Set();
|
|
441
|
+
const stripProtectedColumns = () => {
|
|
442
|
+
if (!builtins.protectedColumns) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
const filtered = stripProtectedPatchColumns(
|
|
446
|
+
builtins.protectedColumns.config,
|
|
447
|
+
tableName,
|
|
448
|
+
verifiedData
|
|
449
|
+
);
|
|
450
|
+
for (const column of filtered.removedColumns) {
|
|
451
|
+
removedProtectedColumns.add(column);
|
|
452
|
+
}
|
|
453
|
+
verifiedData = filtered.filteredData;
|
|
454
|
+
};
|
|
455
|
+
stripProtectedColumns();
|
|
456
|
+
if (customExtensions.length > 0) {
|
|
457
|
+
verifiedData = await runExtensions(customExtensions, {
|
|
458
|
+
ctx,
|
|
459
|
+
tableName,
|
|
460
|
+
operation: "patch",
|
|
461
|
+
patchId: id,
|
|
462
|
+
onFail: options?.onFail,
|
|
463
|
+
schema: _schema,
|
|
464
|
+
data: verifiedData
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
if (builtins.uniqueRow) {
|
|
468
|
+
verifiedData = await builtins.uniqueRow.verify({
|
|
469
|
+
ctx,
|
|
470
|
+
tableName,
|
|
471
|
+
operation: "patch",
|
|
472
|
+
patchId: id,
|
|
473
|
+
onFail: options?.onFail,
|
|
474
|
+
schema: _schema,
|
|
475
|
+
data: verifiedData
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
if (builtins.uniqueColumn) {
|
|
479
|
+
verifiedData = await builtins.uniqueColumn.verify({
|
|
480
|
+
ctx,
|
|
481
|
+
tableName,
|
|
482
|
+
operation: "patch",
|
|
483
|
+
patchId: id,
|
|
484
|
+
onFail: options?.onFail,
|
|
485
|
+
schema: _schema,
|
|
486
|
+
data: verifiedData
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
stripProtectedColumns();
|
|
490
|
+
if (removedProtectedColumns.size > 0) {
|
|
491
|
+
options?.onFail?.({
|
|
492
|
+
editableColumn: {
|
|
493
|
+
removedColumns: [...removedProtectedColumns],
|
|
494
|
+
filteredData: verifiedData
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
await ctx.db.patch(id, verifiedData);
|
|
499
|
+
};
|
|
500
|
+
const dangerouslyPatch = async (ctx, tableName, id, data, options) => {
|
|
501
|
+
let verifiedData = data;
|
|
502
|
+
if (customExtensions.length > 0) {
|
|
503
|
+
verifiedData = await runExtensions(customExtensions, {
|
|
504
|
+
ctx,
|
|
505
|
+
tableName,
|
|
506
|
+
operation: "patch",
|
|
507
|
+
patchId: id,
|
|
508
|
+
onFail: options?.onFail,
|
|
509
|
+
schema: _schema,
|
|
510
|
+
data: verifiedData
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
if (builtins.uniqueRow) {
|
|
514
|
+
verifiedData = await builtins.uniqueRow.verify({
|
|
515
|
+
ctx,
|
|
516
|
+
tableName,
|
|
517
|
+
operation: "patch",
|
|
518
|
+
patchId: id,
|
|
519
|
+
onFail: options?.onFail,
|
|
520
|
+
schema: _schema,
|
|
521
|
+
data: verifiedData
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
if (builtins.uniqueColumn) {
|
|
525
|
+
verifiedData = await builtins.uniqueColumn.verify({
|
|
526
|
+
ctx,
|
|
527
|
+
tableName,
|
|
528
|
+
operation: "patch",
|
|
529
|
+
patchId: id,
|
|
530
|
+
onFail: options?.onFail,
|
|
531
|
+
schema: _schema,
|
|
532
|
+
data: verifiedData
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
await ctx.db.patch(id, verifiedData);
|
|
536
|
+
};
|
|
537
|
+
return {
|
|
538
|
+
insert,
|
|
539
|
+
patch,
|
|
540
|
+
dangerouslyPatch,
|
|
541
|
+
verify,
|
|
542
|
+
config
|
|
543
|
+
};
|
|
393
544
|
};
|
|
545
|
+
|
|
546
|
+
// src/core/index.ts
|
|
547
|
+
var createExtension2 = createExtension;
|
|
548
|
+
var isExtension2 = isExtension;
|
|
549
|
+
var runExtensions2 = runExtensions;
|
|
550
|
+
|
|
551
|
+
// src/index.ts
|
|
552
|
+
var createExtension3 = createExtension2;
|
|
553
|
+
var isExtension3 = isExtension2;
|
|
554
|
+
var runExtensions3 = runExtensions2;
|
|
394
555
|
export {
|
|
395
556
|
constructColumnData,
|
|
396
557
|
constructIndexData,
|
|
397
|
-
|
|
398
|
-
defaultValuesConfig,
|
|
558
|
+
createExtension3 as createExtension,
|
|
399
559
|
getTableIndexes,
|
|
400
|
-
|
|
560
|
+
isExtension3 as isExtension,
|
|
401
561
|
normalizeIndexConfigEntry,
|
|
402
|
-
|
|
403
|
-
runValidatePlugins,
|
|
404
|
-
uniqueColumnConfig,
|
|
405
|
-
uniqueRowConfig,
|
|
562
|
+
runExtensions3 as runExtensions,
|
|
406
563
|
verifyConfig
|
|
407
564
|
};
|
|
408
565
|
//# sourceMappingURL=index.mjs.map
|