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