@vibeorm/schema 5.9.0 → 5.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/capabilities.d.ts +33 -0
- package/dist/errors.d.ts +11 -0
- package/dist/exclusion.d.ts +8 -0
- package/dist/extension.d.ts +0 -5
- package/dist/failure-kind.d.ts +72 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +523 -7
- package/dist/index.js.map +17 -11
- package/dist/native-rls-admission.d.ts +1 -1
- package/dist/partial-predicate.d.ts +18 -0
- package/dist/reserved.d.ts +0 -10
- package/dist/row-rules.d.ts +1 -22
- package/dist/sealed.d.ts +16 -0
- package/dist/suggest.d.ts +53 -0
- package/dist/unique-keys.d.ts +48 -0
- package/dist/validate.d.ts +4 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ var VIBE_ERROR_CODES = [
|
|
|
10
10
|
"VIBE_DATA_INVALID",
|
|
11
11
|
"VIBE_SCHEMA_INVALID",
|
|
12
12
|
"VIBE_UNSUPPORTED_CAPABILITY",
|
|
13
|
+
"VIBE_RESULT_TOO_LARGE",
|
|
13
14
|
"VIBE_STALE_CLIENT",
|
|
14
15
|
"VIBE_CONFIG",
|
|
15
16
|
"VIBE_MIGRATION",
|
|
@@ -222,9 +223,12 @@ var RESERVED_DELEGATE_NAMES = [
|
|
|
222
223
|
"delete",
|
|
223
224
|
"deleteMany",
|
|
224
225
|
"count",
|
|
226
|
+
"exists",
|
|
225
227
|
"aggregate",
|
|
226
228
|
"groupBy",
|
|
227
|
-
"fields"
|
|
229
|
+
"fields",
|
|
230
|
+
"uniques",
|
|
231
|
+
"exclusions"
|
|
228
232
|
];
|
|
229
233
|
var RESERVED_CLIENT_NAMES = [
|
|
230
234
|
"$connect",
|
|
@@ -254,7 +258,8 @@ var RESERVED_CLIENT_NAMES = [
|
|
|
254
258
|
"$withContext",
|
|
255
259
|
"$advisoryLock",
|
|
256
260
|
"$advisoryLocks",
|
|
257
|
-
"$tryAdvisoryLock"
|
|
261
|
+
"$tryAdvisoryLock",
|
|
262
|
+
"$sql"
|
|
258
263
|
];
|
|
259
264
|
var RESERVED_COLUMN_PREFIX = "__vibe_";
|
|
260
265
|
function isReservedDelegateName(params) {
|
|
@@ -479,7 +484,10 @@ var POSTGRES_CAPABILITIES = Object.freeze({
|
|
|
479
484
|
filteredAggregates: "filterClause",
|
|
480
485
|
groupingSets: "groupingSets",
|
|
481
486
|
rowCapture: true,
|
|
482
|
-
returningReadsJoinedSource: true
|
|
487
|
+
returningReadsJoinedSource: true,
|
|
488
|
+
upsertSkipUnchanged: true,
|
|
489
|
+
carrierRows: true,
|
|
490
|
+
sharedListDictionary: true
|
|
483
491
|
});
|
|
484
492
|
var SQLITE_CAPABILITIES = Object.freeze({
|
|
485
493
|
rowRuleAppendOnly: true,
|
|
@@ -526,7 +534,10 @@ var SQLITE_CAPABILITIES = Object.freeze({
|
|
|
526
534
|
filteredAggregates: "filterClause",
|
|
527
535
|
groupingSets: "none",
|
|
528
536
|
rowCapture: false,
|
|
529
|
-
returningReadsJoinedSource: false
|
|
537
|
+
returningReadsJoinedSource: false,
|
|
538
|
+
upsertSkipUnchanged: true,
|
|
539
|
+
carrierRows: false,
|
|
540
|
+
sharedListDictionary: false
|
|
530
541
|
});
|
|
531
542
|
var MYSQL_CAPABILITIES = Object.freeze({
|
|
532
543
|
rowRuleAppendOnly: true,
|
|
@@ -573,7 +584,10 @@ var MYSQL_CAPABILITIES = Object.freeze({
|
|
|
573
584
|
filteredAggregates: "caseWhen",
|
|
574
585
|
groupingSets: "rollup",
|
|
575
586
|
rowCapture: false,
|
|
576
|
-
returningReadsJoinedSource: false
|
|
587
|
+
returningReadsJoinedSource: false,
|
|
588
|
+
upsertSkipUnchanged: true,
|
|
589
|
+
carrierRows: true,
|
|
590
|
+
sharedListDictionary: true
|
|
577
591
|
});
|
|
578
592
|
var DIALECT_CAPABILITIES = Object.freeze({
|
|
579
593
|
postgres: POSTGRES_CAPABILITIES,
|
|
@@ -1566,12 +1580,81 @@ function assertSqlNamespace(params) {
|
|
|
1566
1580
|
function qualifiedSqlName(params) {
|
|
1567
1581
|
return { namespace: params.namespace ?? params.defaultNamespace ?? SQL_NAMESPACES.postgres, name: params.name };
|
|
1568
1582
|
}
|
|
1583
|
+
// packages/schema/src/sealed.ts
|
|
1584
|
+
var sealed = new WeakSet;
|
|
1585
|
+
var valid = new WeakSet;
|
|
1586
|
+
var admitted = new WeakMap;
|
|
1587
|
+
function sealSchemaIR(params) {
|
|
1588
|
+
if (sealed.has(params.schema))
|
|
1589
|
+
return params.schema;
|
|
1590
|
+
const copy = frozenPlainCopy({ value: params.schema });
|
|
1591
|
+
if (copy === undefined)
|
|
1592
|
+
return params.schema;
|
|
1593
|
+
sealed.add(copy);
|
|
1594
|
+
return copy;
|
|
1595
|
+
}
|
|
1596
|
+
function isKnownValidSchemaIR(params) {
|
|
1597
|
+
return valid.has(params.schema);
|
|
1598
|
+
}
|
|
1599
|
+
function rememberValidSchemaIR(params) {
|
|
1600
|
+
if (sealed.has(params.schema))
|
|
1601
|
+
valid.add(params.schema);
|
|
1602
|
+
}
|
|
1603
|
+
function isKnownAdmittedSchemaIR(params) {
|
|
1604
|
+
return admitted.get(params.schema)?.includes(params.dialect) === true;
|
|
1605
|
+
}
|
|
1606
|
+
function rememberAdmittedSchemaIR(params) {
|
|
1607
|
+
if (!sealed.has(params.schema))
|
|
1608
|
+
return;
|
|
1609
|
+
const dialects = admitted.get(params.schema);
|
|
1610
|
+
if (dialects === undefined)
|
|
1611
|
+
admitted.set(params.schema, [params.dialect]);
|
|
1612
|
+
else if (!dialects.includes(params.dialect))
|
|
1613
|
+
dialects.push(params.dialect);
|
|
1614
|
+
}
|
|
1615
|
+
function frozenPlainCopy(params) {
|
|
1616
|
+
const { value } = params;
|
|
1617
|
+
if (typeof value === "function" || typeof value === "symbol")
|
|
1618
|
+
return;
|
|
1619
|
+
if (typeof value !== "object" || value === null)
|
|
1620
|
+
return value;
|
|
1621
|
+
if (Array.isArray(value)) {
|
|
1622
|
+
if (Object.getPrototypeOf(value) !== Array.prototype)
|
|
1623
|
+
return;
|
|
1624
|
+
const out = [];
|
|
1625
|
+
for (let index = 0;index < value.length; index++) {
|
|
1626
|
+
const member = value[index];
|
|
1627
|
+
const item = frozenPlainCopy({ value: member });
|
|
1628
|
+
if (item === undefined && member !== undefined)
|
|
1629
|
+
return;
|
|
1630
|
+
out.push(item);
|
|
1631
|
+
}
|
|
1632
|
+
return Object.freeze(out);
|
|
1633
|
+
}
|
|
1634
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1635
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
1636
|
+
return;
|
|
1637
|
+
if (Object.getOwnPropertySymbols(value).length > 0)
|
|
1638
|
+
return;
|
|
1639
|
+
const out = prototype === null ? Object.create(null) : {};
|
|
1640
|
+
for (const key of Object.keys(value)) {
|
|
1641
|
+
const member = value[key];
|
|
1642
|
+
const item = frozenPlainCopy({ value: member });
|
|
1643
|
+
if (item === undefined && member !== undefined)
|
|
1644
|
+
return;
|
|
1645
|
+
Object.defineProperty(out, key, { value: item, enumerable: true, writable: true, configurable: true });
|
|
1646
|
+
}
|
|
1647
|
+
return Object.freeze(out);
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1569
1650
|
// packages/schema/src/native-rls-admission.ts
|
|
1570
1651
|
function assertNativeRlsAdmission(params) {
|
|
1571
1652
|
const { schema } = params;
|
|
1572
1653
|
if (schema.rls === undefined && !schema.models.some((model) => model.rls !== undefined))
|
|
1573
1654
|
return;
|
|
1574
1655
|
const dialect = params.dialect ?? providerToDialect({ provider: schema.datasource.provider });
|
|
1656
|
+
if (isKnownAdmittedSchemaIR({ schema, dialect }))
|
|
1657
|
+
return;
|
|
1575
1658
|
if (!DIALECT_CAPABILITIES[dialect].nativeRowLevelSecurity) {
|
|
1576
1659
|
throw unsupportedCapability({ dialect, feature: "nativeRowLevelSecurity" });
|
|
1577
1660
|
}
|
|
@@ -1589,6 +1672,7 @@ function assertNativeRlsAdmission(params) {
|
|
|
1589
1672
|
});
|
|
1590
1673
|
}
|
|
1591
1674
|
}
|
|
1675
|
+
rememberAdmittedSchemaIR({ schema, dialect });
|
|
1592
1676
|
}
|
|
1593
1677
|
// packages/schema/src/model-sql-namespace.ts
|
|
1594
1678
|
function modelSqlNamespace(params) {
|
|
@@ -2554,6 +2638,24 @@ function prepareDatabaseModuleSchema(params) {
|
|
|
2554
2638
|
enums: schema.enums.map((entry) => enumStorageOf({ enumDef: entry }) === "database" ? { ...entry, namespace } : entry)
|
|
2555
2639
|
};
|
|
2556
2640
|
}
|
|
2641
|
+
// packages/schema/src/exclusion.ts
|
|
2642
|
+
function exclusionFields(params) {
|
|
2643
|
+
const { source } = params.exclusion;
|
|
2644
|
+
if (source === undefined)
|
|
2645
|
+
return;
|
|
2646
|
+
const fields = new Set;
|
|
2647
|
+
for (const key of source.keys) {
|
|
2648
|
+
if ("field" in key)
|
|
2649
|
+
fields.add(key.field);
|
|
2650
|
+
else
|
|
2651
|
+
for (const field of key.fields)
|
|
2652
|
+
fields.add(field);
|
|
2653
|
+
}
|
|
2654
|
+
for (const endpoint of [source.range.lower, source.range.upper])
|
|
2655
|
+
if ("field" in endpoint)
|
|
2656
|
+
fields.add(endpoint.field);
|
|
2657
|
+
return [...fields];
|
|
2658
|
+
}
|
|
2557
2659
|
// packages/schema/src/check-rules.ts
|
|
2558
2660
|
var CHECK_RULE_GROUP_MAX = 8;
|
|
2559
2661
|
var field = (name) => ({ kind: "field", name });
|
|
@@ -2717,6 +2819,133 @@ function dialectCollation(params) {
|
|
|
2717
2819
|
return;
|
|
2718
2820
|
}
|
|
2719
2821
|
}
|
|
2822
|
+
// packages/schema/src/failure-kind.ts
|
|
2823
|
+
var CONNECTION_FAILURE_REASONS = Object.freeze([
|
|
2824
|
+
"connection-refused",
|
|
2825
|
+
"connection-timeout",
|
|
2826
|
+
"connection-lost",
|
|
2827
|
+
"connection-not-writable",
|
|
2828
|
+
"acquire-timeout",
|
|
2829
|
+
"pool-exhausted",
|
|
2830
|
+
"pool-closed",
|
|
2831
|
+
"too-many-connections",
|
|
2832
|
+
"server-shutdown"
|
|
2833
|
+
]);
|
|
2834
|
+
var CONNECTION_REASON_SET = new Set(CONNECTION_FAILURE_REASONS);
|
|
2835
|
+
function isConnectionFailureReason(params) {
|
|
2836
|
+
return typeof params.value === "string" && CONNECTION_REASON_SET.has(params.value);
|
|
2837
|
+
}
|
|
2838
|
+
var FAILURE_KINDS = Object.freeze([
|
|
2839
|
+
"connection",
|
|
2840
|
+
"conflict",
|
|
2841
|
+
"timeout",
|
|
2842
|
+
"constraint"
|
|
2843
|
+
]);
|
|
2844
|
+
var VIBE_CODE_SET = new Set(VIBE_ERROR_CODES);
|
|
2845
|
+
var CONFLICT_REASONS = new Set(["deadlock", "serialization-failure", "busy"]);
|
|
2846
|
+
var TIMEOUT_REASONS = new Set(["timeout", "transaction-deadline"]);
|
|
2847
|
+
var POSTGRES_CONSTRAINT_SQLSTATES = new Set(["23502", "23514"]);
|
|
2848
|
+
var SQLITE_CONSTRAINT_CODES = new Set([
|
|
2849
|
+
"SQLITE_CONSTRAINT_NOTNULL",
|
|
2850
|
+
"SQLITE_CONSTRAINT_CHECK",
|
|
2851
|
+
"SQLITE_CONSTRAINT_TRIGGER"
|
|
2852
|
+
]);
|
|
2853
|
+
var MYSQL_CONSTRAINT_ERRNOS = new Set([1048, 1364, 3819]);
|
|
2854
|
+
function constraintRefusal(params) {
|
|
2855
|
+
const { meta } = params;
|
|
2856
|
+
if (typeof meta.sqlstate === "string")
|
|
2857
|
+
return POSTGRES_CONSTRAINT_SQLSTATES.has(meta.sqlstate);
|
|
2858
|
+
if (typeof meta.sqlState === "string" && typeof meta.errno === "number")
|
|
2859
|
+
return MYSQL_CONSTRAINT_ERRNOS.has(meta.errno);
|
|
2860
|
+
return typeof meta.code === "string" && SQLITE_CONSTRAINT_CODES.has(meta.code);
|
|
2861
|
+
}
|
|
2862
|
+
function failureKindOf(params) {
|
|
2863
|
+
const { error } = params;
|
|
2864
|
+
if (typeof error !== "object" || error === null)
|
|
2865
|
+
return;
|
|
2866
|
+
const { code, meta } = error;
|
|
2867
|
+
if (typeof code !== "string" || !VIBE_CODE_SET.has(code))
|
|
2868
|
+
return;
|
|
2869
|
+
const facts = typeof meta === "object" && meta !== null ? meta : {};
|
|
2870
|
+
const reason = facts.reason;
|
|
2871
|
+
switch (code) {
|
|
2872
|
+
case "VIBE_ADAPTER":
|
|
2873
|
+
if (isConnectionFailureReason({ value: reason }))
|
|
2874
|
+
return "connection";
|
|
2875
|
+
return typeof reason === "string" && TIMEOUT_REASONS.has(reason) ? "timeout" : undefined;
|
|
2876
|
+
case "VIBE_TRANSACTION":
|
|
2877
|
+
if (typeof reason !== "string")
|
|
2878
|
+
return;
|
|
2879
|
+
if (CONFLICT_REASONS.has(reason))
|
|
2880
|
+
return "conflict";
|
|
2881
|
+
return TIMEOUT_REASONS.has(reason) ? "timeout" : undefined;
|
|
2882
|
+
case "VIBE_UNIQUE_VIOLATION":
|
|
2883
|
+
case "VIBE_FK_VIOLATION":
|
|
2884
|
+
case "VIBE_EXCLUSION_VIOLATION":
|
|
2885
|
+
return "constraint";
|
|
2886
|
+
case "VIBE_VALIDATION":
|
|
2887
|
+
return constraintRefusal({ meta: facts }) ? "constraint" : undefined;
|
|
2888
|
+
default:
|
|
2889
|
+
return;
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
// packages/schema/src/suggest.ts
|
|
2893
|
+
function editDistance(params) {
|
|
2894
|
+
const { from, to } = params;
|
|
2895
|
+
const rows = from.length + 1;
|
|
2896
|
+
const columns = to.length + 1;
|
|
2897
|
+
const table = new Array(rows * columns).fill(0);
|
|
2898
|
+
for (let row = 0;row < rows; row++)
|
|
2899
|
+
table[row * columns] = row;
|
|
2900
|
+
for (let column = 0;column < columns; column++)
|
|
2901
|
+
table[column] = column;
|
|
2902
|
+
for (let row = 1;row < rows; row++) {
|
|
2903
|
+
for (let column = 1;column < columns; column++) {
|
|
2904
|
+
const cost = from[row - 1] === to[column - 1] ? 0 : 1;
|
|
2905
|
+
let best = Math.min(table[(row - 1) * columns + column] + 1, table[row * columns + column - 1] + 1, table[(row - 1) * columns + column - 1] + cost);
|
|
2906
|
+
if (row > 1 && column > 1 && from[row - 1] === to[column - 2] && from[row - 2] === to[column - 1]) {
|
|
2907
|
+
best = Math.min(best, table[(row - 2) * columns + column - 2] + 1);
|
|
2908
|
+
}
|
|
2909
|
+
table[row * columns + column] = best;
|
|
2910
|
+
}
|
|
2911
|
+
}
|
|
2912
|
+
return table[rows * columns - 1];
|
|
2913
|
+
}
|
|
2914
|
+
function suggestName(params) {
|
|
2915
|
+
const { name } = params;
|
|
2916
|
+
let best;
|
|
2917
|
+
let bestDistance = Number.POSITIVE_INFINITY;
|
|
2918
|
+
let tied = false;
|
|
2919
|
+
for (const candidate of new Set(params.candidates)) {
|
|
2920
|
+
if (candidate === name)
|
|
2921
|
+
continue;
|
|
2922
|
+
const distance = editDistance({ from: name, to: candidate });
|
|
2923
|
+
const limit = Math.max(2, Math.floor(Math.max(name.length, candidate.length) / 3));
|
|
2924
|
+
if (distance > limit || distance >= name.length)
|
|
2925
|
+
continue;
|
|
2926
|
+
if (distance < bestDistance) {
|
|
2927
|
+
best = candidate;
|
|
2928
|
+
bestDistance = distance;
|
|
2929
|
+
tied = false;
|
|
2930
|
+
} else if (distance === bestDistance) {
|
|
2931
|
+
tied = true;
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
return tied ? undefined : best;
|
|
2935
|
+
}
|
|
2936
|
+
function didYouMean(params) {
|
|
2937
|
+
return params.suggestion === undefined ? "" : ` \u2014 did you mean ${JSON.stringify(params.suggestion)}?`;
|
|
2938
|
+
}
|
|
2939
|
+
var unknownNameMetas = new WeakSet;
|
|
2940
|
+
function withSuggestion(params) {
|
|
2941
|
+
const suggestion = suggestName({ name: params.name, candidates: params.candidates });
|
|
2942
|
+
const meta = suggestion === undefined ? params.meta : { ...params.meta, suggestion };
|
|
2943
|
+
unknownNameMetas.add(meta);
|
|
2944
|
+
return { message: suggestion === undefined ? params.message : `${params.message}${didYouMean({ suggestion })}`, meta };
|
|
2945
|
+
}
|
|
2946
|
+
function isUnknownNameRefusal(params) {
|
|
2947
|
+
return unknownNameMetas.has(params.meta);
|
|
2948
|
+
}
|
|
2720
2949
|
// packages/schema/src/computed.ts
|
|
2721
2950
|
function buildComputedManifest(params) {
|
|
2722
2951
|
const { computed } = params;
|
|
@@ -2755,6 +2984,8 @@ function validateComputedConfig(params) {
|
|
|
2755
2984
|
}
|
|
2756
2985
|
if (fieldName === "_count")
|
|
2757
2986
|
problems.push(`"${at}" collides with the _count projection`);
|
|
2987
|
+
if (fieldName === "_aggregate")
|
|
2988
|
+
problems.push(`"${at}" collides with the _aggregate projection`);
|
|
2758
2989
|
if (model.fields.some((field) => field.name === fieldName)) {
|
|
2759
2990
|
problems.push(`"${at}" collides with a schema field of the same name`);
|
|
2760
2991
|
}
|
|
@@ -3043,6 +3274,8 @@ var RESERVED_ENUM_NAMES = [
|
|
|
3043
3274
|
"BagKeys",
|
|
3044
3275
|
"__VibeBagKeysIn",
|
|
3045
3276
|
"__VibeCertain",
|
|
3277
|
+
"__VibeAllOn",
|
|
3278
|
+
"__VibeAggregateSelection",
|
|
3046
3279
|
"IncludedRelation",
|
|
3047
3280
|
"IncludedCount",
|
|
3048
3281
|
"ViewDelegateMap",
|
|
@@ -3051,8 +3284,21 @@ var RESERVED_ENUM_NAMES = [
|
|
|
3051
3284
|
"__VibeAt",
|
|
3052
3285
|
"__VibeItem",
|
|
3053
3286
|
"__VibeExact",
|
|
3287
|
+
"__VibeLiteral",
|
|
3054
3288
|
"__VibePreviousInput",
|
|
3055
3289
|
"__VibePrevious",
|
|
3290
|
+
"__VibeUpdated",
|
|
3291
|
+
"__VibeDeleted",
|
|
3292
|
+
"__VibeOrNullRow",
|
|
3293
|
+
"__VibeUpserted",
|
|
3294
|
+
"__VibeCreated",
|
|
3295
|
+
"__VibeAggregateValue",
|
|
3296
|
+
"__VibeSplitEntries",
|
|
3297
|
+
"__VibeViewAggregates",
|
|
3298
|
+
"__VibeViewBucket",
|
|
3299
|
+
"__VibeViewFiltered",
|
|
3300
|
+
"__VibeViewBy",
|
|
3301
|
+
"__VibeViewByKey",
|
|
3056
3302
|
"extensionManifest",
|
|
3057
3303
|
"extensionMounts",
|
|
3058
3304
|
"computedManifest",
|
|
@@ -3065,6 +3311,7 @@ var RESERVED_ENUM_NAMES = [
|
|
|
3065
3311
|
"Partial",
|
|
3066
3312
|
"Omit",
|
|
3067
3313
|
"NonNullable",
|
|
3314
|
+
"NoInfer",
|
|
3068
3315
|
"Promise",
|
|
3069
3316
|
"PromiseLike",
|
|
3070
3317
|
"Awaited",
|
|
@@ -3137,6 +3384,7 @@ var RESERVED_ENUM_NAMES = [
|
|
|
3137
3384
|
"TransactionLocalSettings",
|
|
3138
3385
|
"VibeUpdateManySources",
|
|
3139
3386
|
"VibeModelRows",
|
|
3387
|
+
"VibeUniqueKeys",
|
|
3140
3388
|
"VibeHandleTypes",
|
|
3141
3389
|
"ClientContext",
|
|
3142
3390
|
"QueryOperation",
|
|
@@ -3251,6 +3499,10 @@ var GENERATED_TYPE_NAMES = {
|
|
|
3251
3499
|
"OmitBase",
|
|
3252
3500
|
"CountResult",
|
|
3253
3501
|
"CountPayload",
|
|
3502
|
+
"AggregateEntries",
|
|
3503
|
+
"AggregateEntryArgs",
|
|
3504
|
+
"AggregateOf",
|
|
3505
|
+
"AggregatePayload",
|
|
3254
3506
|
"AggregateValues",
|
|
3255
3507
|
"AggregateSelection",
|
|
3256
3508
|
"CountOutput",
|
|
@@ -3267,6 +3519,7 @@ var GENERATED_TYPE_NAMES = {
|
|
|
3267
3519
|
"ViewNestedArgs",
|
|
3268
3520
|
"ViewFindManyArgs",
|
|
3269
3521
|
"ViewFindUniqueArgs",
|
|
3522
|
+
"ViewAggregateBag",
|
|
3270
3523
|
"ViewScalars",
|
|
3271
3524
|
"ViewSelectScalars",
|
|
3272
3525
|
"ViewReveal",
|
|
@@ -4531,6 +4784,12 @@ function assertMysqlCheckReferentialActions(params) {
|
|
|
4531
4784
|
}
|
|
4532
4785
|
}
|
|
4533
4786
|
function assertValidSchemaIR(params) {
|
|
4787
|
+
if (isKnownValidSchemaIR(params))
|
|
4788
|
+
return;
|
|
4789
|
+
assertSchemaIRNow(params);
|
|
4790
|
+
rememberValidSchemaIR(params);
|
|
4791
|
+
}
|
|
4792
|
+
function assertSchemaIRNow(params) {
|
|
4534
4793
|
const rowRules = createRowRuleValidationContext(params);
|
|
4535
4794
|
const dialect = providerToDialect({ provider: params.schema.datasource.provider });
|
|
4536
4795
|
assertIndexExpressions({ schema: params.schema, dialect });
|
|
@@ -4849,13 +5108,259 @@ function validRlsForeignKey(params) {
|
|
|
4849
5108
|
];
|
|
4850
5109
|
return validFields && uniqueSets.some((set) => set.length === refs.length && set.every((name) => refs.includes(name)));
|
|
4851
5110
|
}
|
|
5111
|
+
// packages/schema/src/partial-predicate.ts
|
|
5112
|
+
function normalizeWhere(params) {
|
|
5113
|
+
const { where } = params;
|
|
5114
|
+
if (where === undefined || where.trim() === "")
|
|
5115
|
+
return "";
|
|
5116
|
+
if (/\$|\\|--|\/\*/.test(where))
|
|
5117
|
+
return `raw:${where.trim()}`;
|
|
5118
|
+
const tokens = tokenizeWhere({ text: where });
|
|
5119
|
+
const parsed = parseOrExpression({ tokens, at: 0 });
|
|
5120
|
+
return parsed !== undefined && parsed.at === tokens.length ? parsed.text : tokens.join(" ");
|
|
5121
|
+
}
|
|
5122
|
+
function tokenizeWhere(params) {
|
|
5123
|
+
const { text } = params;
|
|
5124
|
+
const tokens = [];
|
|
5125
|
+
let i = 0;
|
|
5126
|
+
while (i < text.length) {
|
|
5127
|
+
const ch = text[i];
|
|
5128
|
+
if (/\s/.test(ch)) {
|
|
5129
|
+
i += 1;
|
|
5130
|
+
continue;
|
|
5131
|
+
}
|
|
5132
|
+
if (ch === "'") {
|
|
5133
|
+
let literal = "'";
|
|
5134
|
+
i += 1;
|
|
5135
|
+
while (i < text.length) {
|
|
5136
|
+
literal += text[i];
|
|
5137
|
+
if (text[i] === "'") {
|
|
5138
|
+
if (text[i + 1] === "'") {
|
|
5139
|
+
literal += "'";
|
|
5140
|
+
i += 2;
|
|
5141
|
+
continue;
|
|
5142
|
+
}
|
|
5143
|
+
i += 1;
|
|
5144
|
+
break;
|
|
5145
|
+
}
|
|
5146
|
+
i += 1;
|
|
5147
|
+
}
|
|
5148
|
+
tokens.push(literal);
|
|
5149
|
+
continue;
|
|
5150
|
+
}
|
|
5151
|
+
if (ch === '"' || ch === "`") {
|
|
5152
|
+
let name = "";
|
|
5153
|
+
i += 1;
|
|
5154
|
+
while (i < text.length) {
|
|
5155
|
+
if (text[i] === ch) {
|
|
5156
|
+
if (text[i + 1] !== ch) {
|
|
5157
|
+
i += 1;
|
|
5158
|
+
break;
|
|
5159
|
+
}
|
|
5160
|
+
name += ch;
|
|
5161
|
+
i += 2;
|
|
5162
|
+
} else {
|
|
5163
|
+
name += text[i];
|
|
5164
|
+
i += 1;
|
|
5165
|
+
}
|
|
5166
|
+
}
|
|
5167
|
+
tokens.push(JSON.stringify(name));
|
|
5168
|
+
continue;
|
|
5169
|
+
}
|
|
5170
|
+
if (ch === "[") {
|
|
5171
|
+
const end = text.indexOf("]", i + 1);
|
|
5172
|
+
const inner = end === -1 ? undefined : text.slice(i + 1, end);
|
|
5173
|
+
if (inner !== undefined && tokens[tokens.length - 1] !== "array" && /^[A-Za-z_][A-Za-z_0-9$]*$/.test(inner)) {
|
|
5174
|
+
tokens.push(JSON.stringify(inner));
|
|
5175
|
+
i = end + 1;
|
|
5176
|
+
continue;
|
|
5177
|
+
}
|
|
5178
|
+
tokens.push(ch);
|
|
5179
|
+
i += 1;
|
|
5180
|
+
continue;
|
|
5181
|
+
}
|
|
5182
|
+
const word = /^[A-Za-z_][A-Za-z_0-9$]*/.exec(text.slice(i));
|
|
5183
|
+
if (word !== null) {
|
|
5184
|
+
tokens.push(word[0].toLowerCase());
|
|
5185
|
+
i += word[0].length;
|
|
5186
|
+
continue;
|
|
5187
|
+
}
|
|
5188
|
+
const number = /^[0-9]+(?:\.[0-9]+)?/.exec(text.slice(i));
|
|
5189
|
+
if (number !== null) {
|
|
5190
|
+
tokens.push(number[0]);
|
|
5191
|
+
i += number[0].length;
|
|
5192
|
+
continue;
|
|
5193
|
+
}
|
|
5194
|
+
tokens.push(ch);
|
|
5195
|
+
i += 1;
|
|
5196
|
+
}
|
|
5197
|
+
return tokens;
|
|
5198
|
+
}
|
|
5199
|
+
function parseOrExpression(params) {
|
|
5200
|
+
const first = parseAndExpression({ tokens: params.tokens, at: params.at });
|
|
5201
|
+
if (first === undefined)
|
|
5202
|
+
return;
|
|
5203
|
+
let at = first.at;
|
|
5204
|
+
const parts = [first.text];
|
|
5205
|
+
while (params.tokens[at] === "or") {
|
|
5206
|
+
const next = parseAndExpression({ tokens: params.tokens, at: at + 1 });
|
|
5207
|
+
if (next === undefined)
|
|
5208
|
+
return;
|
|
5209
|
+
parts.push(next.text);
|
|
5210
|
+
at = next.at;
|
|
5211
|
+
}
|
|
5212
|
+
return parts.length === 1 ? { text: parts[0], at, kind: first.kind } : { text: parts.join(" or "), at, kind: "or" };
|
|
5213
|
+
}
|
|
5214
|
+
function parseAndExpression(params) {
|
|
5215
|
+
const first = parsePrimaryExpression({ tokens: params.tokens, at: params.at });
|
|
5216
|
+
if (first === undefined)
|
|
5217
|
+
return;
|
|
5218
|
+
let at = first.at;
|
|
5219
|
+
const operands = [first];
|
|
5220
|
+
while (params.tokens[at] === "and") {
|
|
5221
|
+
const next = parsePrimaryExpression({ tokens: params.tokens, at: at + 1 });
|
|
5222
|
+
if (next === undefined)
|
|
5223
|
+
return;
|
|
5224
|
+
operands.push(next);
|
|
5225
|
+
at = next.at;
|
|
5226
|
+
}
|
|
5227
|
+
return operands.length === 1 ? { text: first.text, at, kind: first.kind } : {
|
|
5228
|
+
text: operands.map((operand) => operand.kind === "or" ? `(${operand.text})` : operand.text).join(" and "),
|
|
5229
|
+
at,
|
|
5230
|
+
kind: "and"
|
|
5231
|
+
};
|
|
5232
|
+
}
|
|
5233
|
+
function parsePrimaryExpression(params) {
|
|
5234
|
+
const { tokens } = params;
|
|
5235
|
+
let at = params.at;
|
|
5236
|
+
if (tokens[at] === "not") {
|
|
5237
|
+
const operand = parsePrimaryExpression({ tokens, at: at + 1 });
|
|
5238
|
+
if (operand === undefined)
|
|
5239
|
+
return;
|
|
5240
|
+
const inner = operand.kind === "atom" ? operand.text : `(${operand.text})`;
|
|
5241
|
+
return { text: `not ${inner}`, at: operand.at, kind: "atom" };
|
|
5242
|
+
}
|
|
5243
|
+
if (tokens[at] === "(") {
|
|
5244
|
+
const inner = parseOrExpression({ tokens, at: at + 1 });
|
|
5245
|
+
if (inner === undefined || tokens[inner.at] !== ")")
|
|
5246
|
+
return;
|
|
5247
|
+
return { text: inner.text, at: inner.at + 1, kind: inner.kind };
|
|
5248
|
+
}
|
|
5249
|
+
const parts = [];
|
|
5250
|
+
let depth = 0;
|
|
5251
|
+
while (at < tokens.length) {
|
|
5252
|
+
const token = tokens[at];
|
|
5253
|
+
if (depth === 0 && (token === "and" || token === "or" || token === ")"))
|
|
5254
|
+
break;
|
|
5255
|
+
if (token === "(")
|
|
5256
|
+
depth += 1;
|
|
5257
|
+
if (token === ")")
|
|
5258
|
+
depth -= 1;
|
|
5259
|
+
parts.push(token);
|
|
5260
|
+
at += 1;
|
|
5261
|
+
}
|
|
5262
|
+
if (parts.length === 0)
|
|
5263
|
+
return;
|
|
5264
|
+
if (parts[1] === "is" && (parts.length === 3 && parts[2] === "null" || parts.length === 4 && parts[2] === "not" && parts[3] === "null")) {
|
|
5265
|
+
const quoted = /^"([a-z_][a-z_0-9]*)"$/.exec(parts[0]);
|
|
5266
|
+
if (quoted !== null && !/^(?:true|false|null|unknown|default|user|current_.*|session_user|system_user|localtime|localtimestamp|and|or|not|is)$/.test(quoted[1]))
|
|
5267
|
+
parts[0] = quoted[1];
|
|
5268
|
+
}
|
|
5269
|
+
return { text: parts.join(" "), at, kind: "atom" };
|
|
5270
|
+
}
|
|
5271
|
+
|
|
5272
|
+
// packages/schema/src/unique-keys.ts
|
|
5273
|
+
var RANK = { primaryKey: 0, field: 1, block: 2, partial: 3, expression: 4 };
|
|
5274
|
+
function comparisonKey(params) {
|
|
5275
|
+
const { model, index } = params;
|
|
5276
|
+
const common = [index.nullsNotDistinct === true, normalizeWhere({ where: index.where }), index.include ?? []];
|
|
5277
|
+
if (isExpressionIndex({ index }))
|
|
5278
|
+
return JSON.stringify(["expression", indexKeyDefinitionKey({ index, model }), ...common]);
|
|
5279
|
+
return JSON.stringify(["fields", index.fields.map((field) => [field.name, field.sort === "desc", field.operatorClass ?? null]), ...common]);
|
|
5280
|
+
}
|
|
5281
|
+
function modelUniqueKeys(params) {
|
|
5282
|
+
const { model, dialect } = params;
|
|
5283
|
+
if (model.isView)
|
|
5284
|
+
return [];
|
|
5285
|
+
const table = model.dbName ?? model.name;
|
|
5286
|
+
const columnOf = (name) => model.fields.find((field) => field.name === name)?.dbName ?? name;
|
|
5287
|
+
const candidates = [];
|
|
5288
|
+
const primaryKey = model.primaryKey?.fields ?? model.fields.filter((field) => field.isId).map((field) => field.name);
|
|
5289
|
+
if (primaryKey.length > 0) {
|
|
5290
|
+
const name = dialect === "mysql" ? "PRIMARY" : dialect === "sqlite" ? null : model.primaryKeyConstraintName ?? defaultPrimaryKeyName({ table, dialect });
|
|
5291
|
+
candidates.push({
|
|
5292
|
+
rank: RANK.primaryKey,
|
|
5293
|
+
order: 0,
|
|
5294
|
+
natural: model.primaryKey?.clientName ?? primaryKey.join("_"),
|
|
5295
|
+
fallback: model.primaryKeyConstraintName ?? defaultPrimaryKeyName({ table, dialect: "sqlite" }),
|
|
5296
|
+
entry: { name, names: name === null ? [] : [name], fields: primaryKey, primaryKey: true, partial: false }
|
|
5297
|
+
});
|
|
5298
|
+
}
|
|
5299
|
+
const seen = new Set;
|
|
5300
|
+
const add = (params) => {
|
|
5301
|
+
const { index } = params;
|
|
5302
|
+
const identity = comparisonKey({ model, index });
|
|
5303
|
+
if (seen.has(identity))
|
|
5304
|
+
return;
|
|
5305
|
+
seen.add(identity);
|
|
5306
|
+
const partial = index.where !== undefined;
|
|
5307
|
+
if (isExpressionIndex({ index })) {
|
|
5308
|
+
const sourceFields = indexKeyFieldNames({ index });
|
|
5309
|
+
const indexName = index.name ?? (sourceFields === undefined ? undefined : defaultExpressionIndexName({ table, model, index, dialect }));
|
|
5310
|
+
if (indexName === undefined)
|
|
5311
|
+
return;
|
|
5312
|
+
const names = [...new Set([index.constraintName ?? indexName, indexName])];
|
|
5313
|
+
candidates.push({
|
|
5314
|
+
rank: RANK.expression,
|
|
5315
|
+
order: params.order,
|
|
5316
|
+
natural: sourceFields?.join("_"),
|
|
5317
|
+
fallback: index.name ?? defaultExpressionIndexName({ table, model, index, dialect: "sqlite" }),
|
|
5318
|
+
entry: { name: names[0], names, ...sourceFields === undefined ? {} : { sourceFields }, primaryKey: false, partial }
|
|
5319
|
+
});
|
|
5320
|
+
return;
|
|
5321
|
+
}
|
|
5322
|
+
const fields = index.fields.map((field) => field.name);
|
|
5323
|
+
const indexName = index.name ?? defaultIndexName({ table, columns: fields.map(columnOf), unique: true, dialect });
|
|
5324
|
+
const names = [...new Set([index.constraintName ?? indexName, indexName])];
|
|
5325
|
+
candidates.push({
|
|
5326
|
+
rank: partial ? RANK.partial : params.rank,
|
|
5327
|
+
order: params.order,
|
|
5328
|
+
natural: index.clientName ?? fields.join("_"),
|
|
5329
|
+
fallback: index.constraintName ?? index.name ?? defaultIndexName({ table, columns: fields.map(columnOf), unique: true, dialect: "sqlite" }),
|
|
5330
|
+
entry: { name: names[0], names, fields, primaryKey: false, partial }
|
|
5331
|
+
});
|
|
5332
|
+
};
|
|
5333
|
+
const fieldKey = (name) => ({ kind: "unique", fields: [{ name }] });
|
|
5334
|
+
model.indexes.forEach((index, position) => {
|
|
5335
|
+
if (index.kind !== "unique")
|
|
5336
|
+
return;
|
|
5337
|
+
const own = isExpressionIndex({ index }) || index.fields.length !== 1 ? -1 : model.fields.findIndex((field) => field.isUnique && field.name === index.fields[0].name && comparisonKey({ model, index }) === comparisonKey({ model, index: fieldKey(field.name) }));
|
|
5338
|
+
add({ index, rank: own >= 0 ? RANK.field : RANK.block, order: own >= 0 ? own : position });
|
|
5339
|
+
});
|
|
5340
|
+
model.fields.forEach((field, position) => {
|
|
5341
|
+
if (field.isUnique)
|
|
5342
|
+
add({ index: fieldKey(field.name), rank: RANK.field, order: position });
|
|
5343
|
+
});
|
|
5344
|
+
const claimed = new Set;
|
|
5345
|
+
const keyed = new Map;
|
|
5346
|
+
for (const candidate of [...candidates].sort((left, right) => left.rank - right.rank || left.order - right.order)) {
|
|
5347
|
+
let key = candidate.natural !== undefined && !claimed.has(candidate.natural) ? candidate.natural : candidate.fallback;
|
|
5348
|
+
for (let suffix = 2;claimed.has(key); suffix++)
|
|
5349
|
+
key = `${candidate.fallback}_${suffix}`;
|
|
5350
|
+
claimed.add(key);
|
|
5351
|
+
keyed.set(candidate, key);
|
|
5352
|
+
}
|
|
5353
|
+
return candidates.sort((left, right) => left.rank - right.rank || left.order - right.order).map((candidate) => ({ key: keyed.get(candidate), ...candidate.entry }));
|
|
5354
|
+
}
|
|
4852
5355
|
export {
|
|
4853
5356
|
CHECK_RULE_GROUP_MAX,
|
|
5357
|
+
CONNECTION_FAILURE_REASONS,
|
|
4854
5358
|
DATE_TRUNCATION_UNITS,
|
|
4855
5359
|
DIALECTS,
|
|
4856
5360
|
DIALECT_CAPABILITIES,
|
|
4857
5361
|
EMPTY_EXTENSION_REGISTRY,
|
|
4858
5362
|
ENUM_STORAGES,
|
|
5363
|
+
FAILURE_KINDS,
|
|
4859
5364
|
IDENTIFIER_LIMITS,
|
|
4860
5365
|
INCLUDE_INDEX_KINDS,
|
|
4861
5366
|
MYSQL_CAPABILITIES,
|
|
@@ -4910,13 +5415,17 @@ export {
|
|
|
4910
5415
|
defaultRowRuleName,
|
|
4911
5416
|
defineExtension,
|
|
4912
5417
|
dialectCollation,
|
|
5418
|
+
didYouMean,
|
|
5419
|
+
editDistance,
|
|
4913
5420
|
encodePrismaString,
|
|
4914
5421
|
enumStorageOf,
|
|
4915
5422
|
exactDecimalLiteral,
|
|
4916
5423
|
exactIntegerLiteral,
|
|
5424
|
+
exclusionFields,
|
|
4917
5425
|
extensionDeclarations,
|
|
4918
5426
|
extensionManifestEntry,
|
|
4919
5427
|
extensionOptionsHash,
|
|
5428
|
+
failureKindOf,
|
|
4920
5429
|
fieldTypeIdentity,
|
|
4921
5430
|
fitDefaultName,
|
|
4922
5431
|
generatedColumnFromSql,
|
|
@@ -4939,6 +5448,7 @@ export {
|
|
|
4939
5448
|
internalError,
|
|
4940
5449
|
invalidStoredData,
|
|
4941
5450
|
isCanonicalLocalDate,
|
|
5451
|
+
isConnectionFailureReason,
|
|
4942
5452
|
isEnumType,
|
|
4943
5453
|
isExpressionIndex,
|
|
4944
5454
|
isImplicitManyToManyRelation,
|
|
@@ -4950,17 +5460,20 @@ export {
|
|
|
4950
5460
|
isSqlObjectDefinition,
|
|
4951
5461
|
isSqlObjectIdentity,
|
|
4952
5462
|
isToastStorageKey,
|
|
5463
|
+
isUnknownNameRefusal,
|
|
4953
5464
|
jsonSchemaRefIssue,
|
|
4954
5465
|
jsonTypeAnnotationIssue,
|
|
4955
5466
|
jsonTypeAnnotationRef,
|
|
4956
5467
|
localDateCheckName,
|
|
4957
5468
|
mergeExtensionDeclarations,
|
|
4958
5469
|
modelSqlNamespace,
|
|
5470
|
+
modelUniqueKeys,
|
|
4959
5471
|
mysqlIndexKeyWidth,
|
|
4960
5472
|
mysqlRowRuleMessageByteLength,
|
|
4961
5473
|
normalizeJsonSchemaRef,
|
|
4962
5474
|
normalizeRowRules,
|
|
4963
5475
|
normalizeValidatorRef,
|
|
5476
|
+
normalizeWhere,
|
|
4964
5477
|
parseTableStorageValue,
|
|
4965
5478
|
postgresGeneratedObjectName,
|
|
4966
5479
|
predicateCapabilityIssue,
|
|
@@ -4984,12 +5497,14 @@ export {
|
|
|
4984
5497
|
rowRuleTriggerName,
|
|
4985
5498
|
scalarListDefault,
|
|
4986
5499
|
schemaRowRuleIdentities,
|
|
5500
|
+
sealSchemaIR,
|
|
4987
5501
|
serializeJsonDefault,
|
|
4988
5502
|
setDefaultRelationOf,
|
|
4989
5503
|
sqlNamespaceIssue,
|
|
4990
5504
|
sqlObjectKey,
|
|
4991
5505
|
sqlObjectNamespace,
|
|
4992
5506
|
sqliteRowRuleConstraint,
|
|
5507
|
+
suggestName,
|
|
4993
5508
|
tableStorageEntries,
|
|
4994
5509
|
tableStorageKeyFromPrisma,
|
|
4995
5510
|
tableStoragePrismaKey,
|
|
@@ -5001,7 +5516,8 @@ export {
|
|
|
5001
5516
|
validateRlsContextIR,
|
|
5002
5517
|
validateRowRules,
|
|
5003
5518
|
validateSchemaIR,
|
|
5004
|
-
validatorRefIssue
|
|
5519
|
+
validatorRefIssue,
|
|
5520
|
+
withSuggestion
|
|
5005
5521
|
};
|
|
5006
5522
|
|
|
5007
|
-
//# debugId=
|
|
5523
|
+
//# debugId=9496E5998F6FBDFD64756E2164756E21
|