@zenstackhq/sdk 3.4.4 → 3.4.6
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/index.cjs +32 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +15 -3
- package/dist/index.d.ts +15 -3
- package/dist/index.js +32 -7
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _zenstackhq_language_ast from '@zenstackhq/language/ast';
|
|
2
|
-
import { DataFieldAttribute, DataModelAttribute, Model, AstNode, DataModel, TypeDef,
|
|
2
|
+
import { DataFieldAttribute, DataModelAttribute, Model, AstNode, DataField, DataModel, TypeDef, Enum, EnumField, FunctionDecl, Attribute, AttributeParam, Reference, InvocationExpr } from '@zenstackhq/language/ast';
|
|
3
3
|
import { AttributeTarget } from '@zenstackhq/language/utils';
|
|
4
4
|
import { MaybePromise } from 'langium';
|
|
5
5
|
|
|
@@ -7,6 +7,17 @@ declare function isIdField(field: DataField, contextModel: DataModel): boolean;
|
|
|
7
7
|
declare function hasAttribute(decl: DataModel | TypeDef | DataField | Enum | EnumField | FunctionDecl | Attribute | AttributeParam, name: string): boolean;
|
|
8
8
|
declare function getAttribute(decl: AttributeTarget, name: string): DataFieldAttribute | DataModelAttribute | undefined;
|
|
9
9
|
declare function isDelegateModel(node: AstNode): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Returns all fields that physically belong to a model's table: its directly declared
|
|
12
|
+
* fields plus fields from its mixins (recursively).
|
|
13
|
+
*/
|
|
14
|
+
declare function getOwnedFields(model: DataModel | TypeDef): DataField[];
|
|
15
|
+
/**
|
|
16
|
+
* Returns the name of the delegate base model that "owns" the given field in the context of
|
|
17
|
+
* `contextModel`. This handles both direct fields of delegate models and mixin fields that
|
|
18
|
+
* belong to a mixin used by a delegate base model.
|
|
19
|
+
*/
|
|
20
|
+
declare function getDelegateOriginModel(field: DataField, contextModel: DataModel): string | undefined;
|
|
10
21
|
declare function isUniqueField(field: DataField): boolean;
|
|
11
22
|
declare function isFromStdlib(node: AstNode): boolean;
|
|
12
23
|
declare function getContainingModel(node: AstNode | undefined): Model | null;
|
|
@@ -22,7 +33,9 @@ declare const modelUtils_DELEGATE_AUX_RELATION_PREFIX: typeof DELEGATE_AUX_RELAT
|
|
|
22
33
|
declare const modelUtils_getAttribute: typeof getAttribute;
|
|
23
34
|
declare const modelUtils_getAuthDecl: typeof getAuthDecl;
|
|
24
35
|
declare const modelUtils_getContainingModel: typeof getContainingModel;
|
|
36
|
+
declare const modelUtils_getDelegateOriginModel: typeof getDelegateOriginModel;
|
|
25
37
|
declare const modelUtils_getIdFields: typeof getIdFields;
|
|
38
|
+
declare const modelUtils_getOwnedFields: typeof getOwnedFields;
|
|
26
39
|
declare const modelUtils_hasAttribute: typeof hasAttribute;
|
|
27
40
|
declare const modelUtils_isDelegateModel: typeof isDelegateModel;
|
|
28
41
|
declare const modelUtils_isFromStdlib: typeof isFromStdlib;
|
|
@@ -30,7 +43,7 @@ declare const modelUtils_isIdField: typeof isIdField;
|
|
|
30
43
|
declare const modelUtils_isUniqueField: typeof isUniqueField;
|
|
31
44
|
declare const modelUtils_resolved: typeof resolved;
|
|
32
45
|
declare namespace modelUtils {
|
|
33
|
-
export { modelUtils_DELEGATE_AUX_RELATION_PREFIX as DELEGATE_AUX_RELATION_PREFIX, modelUtils_getAttribute as getAttribute, modelUtils_getAuthDecl as getAuthDecl, modelUtils_getContainingModel as getContainingModel, modelUtils_getIdFields as getIdFields, modelUtils_hasAttribute as hasAttribute, modelUtils_isDelegateModel as isDelegateModel, modelUtils_isFromStdlib as isFromStdlib, modelUtils_isIdField as isIdField, modelUtils_isUniqueField as isUniqueField, modelUtils_resolved as resolved };
|
|
46
|
+
export { modelUtils_DELEGATE_AUX_RELATION_PREFIX as DELEGATE_AUX_RELATION_PREFIX, modelUtils_getAttribute as getAttribute, modelUtils_getAuthDecl as getAuthDecl, modelUtils_getContainingModel as getContainingModel, modelUtils_getDelegateOriginModel as getDelegateOriginModel, modelUtils_getIdFields as getIdFields, modelUtils_getOwnedFields as getOwnedFields, modelUtils_hasAttribute as hasAttribute, modelUtils_isDelegateModel as isDelegateModel, modelUtils_isFromStdlib as isFromStdlib, modelUtils_isIdField as isIdField, modelUtils_isUniqueField as isUniqueField, modelUtils_resolved as resolved };
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
/**
|
|
@@ -108,7 +121,6 @@ declare class PrismaSchemaGenerator {
|
|
|
108
121
|
private getUnsupportedFieldType;
|
|
109
122
|
private generateModelField;
|
|
110
123
|
private isDefaultWithAuthInvocation;
|
|
111
|
-
private isInheritedFromDelegate;
|
|
112
124
|
private makeFieldAttribute;
|
|
113
125
|
private makeAttributeArg;
|
|
114
126
|
private makeAttributeArgValue;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _zenstackhq_language_ast from '@zenstackhq/language/ast';
|
|
2
|
-
import { DataFieldAttribute, DataModelAttribute, Model, AstNode, DataModel, TypeDef,
|
|
2
|
+
import { DataFieldAttribute, DataModelAttribute, Model, AstNode, DataField, DataModel, TypeDef, Enum, EnumField, FunctionDecl, Attribute, AttributeParam, Reference, InvocationExpr } from '@zenstackhq/language/ast';
|
|
3
3
|
import { AttributeTarget } from '@zenstackhq/language/utils';
|
|
4
4
|
import { MaybePromise } from 'langium';
|
|
5
5
|
|
|
@@ -7,6 +7,17 @@ declare function isIdField(field: DataField, contextModel: DataModel): boolean;
|
|
|
7
7
|
declare function hasAttribute(decl: DataModel | TypeDef | DataField | Enum | EnumField | FunctionDecl | Attribute | AttributeParam, name: string): boolean;
|
|
8
8
|
declare function getAttribute(decl: AttributeTarget, name: string): DataFieldAttribute | DataModelAttribute | undefined;
|
|
9
9
|
declare function isDelegateModel(node: AstNode): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Returns all fields that physically belong to a model's table: its directly declared
|
|
12
|
+
* fields plus fields from its mixins (recursively).
|
|
13
|
+
*/
|
|
14
|
+
declare function getOwnedFields(model: DataModel | TypeDef): DataField[];
|
|
15
|
+
/**
|
|
16
|
+
* Returns the name of the delegate base model that "owns" the given field in the context of
|
|
17
|
+
* `contextModel`. This handles both direct fields of delegate models and mixin fields that
|
|
18
|
+
* belong to a mixin used by a delegate base model.
|
|
19
|
+
*/
|
|
20
|
+
declare function getDelegateOriginModel(field: DataField, contextModel: DataModel): string | undefined;
|
|
10
21
|
declare function isUniqueField(field: DataField): boolean;
|
|
11
22
|
declare function isFromStdlib(node: AstNode): boolean;
|
|
12
23
|
declare function getContainingModel(node: AstNode | undefined): Model | null;
|
|
@@ -22,7 +33,9 @@ declare const modelUtils_DELEGATE_AUX_RELATION_PREFIX: typeof DELEGATE_AUX_RELAT
|
|
|
22
33
|
declare const modelUtils_getAttribute: typeof getAttribute;
|
|
23
34
|
declare const modelUtils_getAuthDecl: typeof getAuthDecl;
|
|
24
35
|
declare const modelUtils_getContainingModel: typeof getContainingModel;
|
|
36
|
+
declare const modelUtils_getDelegateOriginModel: typeof getDelegateOriginModel;
|
|
25
37
|
declare const modelUtils_getIdFields: typeof getIdFields;
|
|
38
|
+
declare const modelUtils_getOwnedFields: typeof getOwnedFields;
|
|
26
39
|
declare const modelUtils_hasAttribute: typeof hasAttribute;
|
|
27
40
|
declare const modelUtils_isDelegateModel: typeof isDelegateModel;
|
|
28
41
|
declare const modelUtils_isFromStdlib: typeof isFromStdlib;
|
|
@@ -30,7 +43,7 @@ declare const modelUtils_isIdField: typeof isIdField;
|
|
|
30
43
|
declare const modelUtils_isUniqueField: typeof isUniqueField;
|
|
31
44
|
declare const modelUtils_resolved: typeof resolved;
|
|
32
45
|
declare namespace modelUtils {
|
|
33
|
-
export { modelUtils_DELEGATE_AUX_RELATION_PREFIX as DELEGATE_AUX_RELATION_PREFIX, modelUtils_getAttribute as getAttribute, modelUtils_getAuthDecl as getAuthDecl, modelUtils_getContainingModel as getContainingModel, modelUtils_getIdFields as getIdFields, modelUtils_hasAttribute as hasAttribute, modelUtils_isDelegateModel as isDelegateModel, modelUtils_isFromStdlib as isFromStdlib, modelUtils_isIdField as isIdField, modelUtils_isUniqueField as isUniqueField, modelUtils_resolved as resolved };
|
|
46
|
+
export { modelUtils_DELEGATE_AUX_RELATION_PREFIX as DELEGATE_AUX_RELATION_PREFIX, modelUtils_getAttribute as getAttribute, modelUtils_getAuthDecl as getAuthDecl, modelUtils_getContainingModel as getContainingModel, modelUtils_getDelegateOriginModel as getDelegateOriginModel, modelUtils_getIdFields as getIdFields, modelUtils_getOwnedFields as getOwnedFields, modelUtils_hasAttribute as hasAttribute, modelUtils_isDelegateModel as isDelegateModel, modelUtils_isFromStdlib as isFromStdlib, modelUtils_isIdField as isIdField, modelUtils_isUniqueField as isUniqueField, modelUtils_resolved as resolved };
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
/**
|
|
@@ -108,7 +121,6 @@ declare class PrismaSchemaGenerator {
|
|
|
108
121
|
private getUnsupportedFieldType;
|
|
109
122
|
private generateModelField;
|
|
110
123
|
private isDefaultWithAuthInvocation;
|
|
111
|
-
private isInheritedFromDelegate;
|
|
112
124
|
private makeFieldAttribute;
|
|
113
125
|
private makeAttributeArg;
|
|
114
126
|
private makeAttributeArgValue;
|
package/dist/index.js
CHANGED
|
@@ -12,7 +12,9 @@ __export(model_utils_exports, {
|
|
|
12
12
|
getAttribute: () => getAttribute,
|
|
13
13
|
getAuthDecl: () => getAuthDecl,
|
|
14
14
|
getContainingModel: () => getContainingModel,
|
|
15
|
+
getDelegateOriginModel: () => getDelegateOriginModel,
|
|
15
16
|
getIdFields: () => getIdFields,
|
|
17
|
+
getOwnedFields: () => getOwnedFields,
|
|
16
18
|
hasAttribute: () => hasAttribute,
|
|
17
19
|
isDelegateModel: () => isDelegateModel,
|
|
18
20
|
isFromStdlib: () => isFromStdlib,
|
|
@@ -57,6 +59,29 @@ function isDelegateModel(node) {
|
|
|
57
59
|
return isDataModel(node) && hasAttribute(node, "@@delegate");
|
|
58
60
|
}
|
|
59
61
|
__name(isDelegateModel, "isDelegateModel");
|
|
62
|
+
function getOwnedFields(model) {
|
|
63
|
+
const fields = [
|
|
64
|
+
...model.fields
|
|
65
|
+
];
|
|
66
|
+
for (const mixin of model.mixins) {
|
|
67
|
+
if (mixin.ref) {
|
|
68
|
+
fields.push(...getOwnedFields(mixin.ref));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return fields;
|
|
72
|
+
}
|
|
73
|
+
__name(getOwnedFields, "getOwnedFields");
|
|
74
|
+
function getDelegateOriginModel(field, contextModel) {
|
|
75
|
+
let base = contextModel.baseModel?.ref;
|
|
76
|
+
while (base) {
|
|
77
|
+
if (isDelegateModel(base) && getOwnedFields(base).some((f) => f.name === field.name)) {
|
|
78
|
+
return base.name;
|
|
79
|
+
}
|
|
80
|
+
base = base.baseModel?.ref;
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
__name(getDelegateOriginModel, "getDelegateOriginModel");
|
|
60
85
|
function isUniqueField(field) {
|
|
61
86
|
if (hasAttribute(field, "@unique")) {
|
|
62
87
|
return true;
|
|
@@ -610,7 +635,7 @@ var PrismaSchemaGenerator = class {
|
|
|
610
635
|
if (model_utils_exports.hasAttribute(field, "@computed")) {
|
|
611
636
|
continue;
|
|
612
637
|
}
|
|
613
|
-
if (model_utils_exports.isIdField(field, decl) || !
|
|
638
|
+
if (model_utils_exports.isIdField(field, decl) || !getDelegateOriginModel(field, decl)) {
|
|
614
639
|
this.generateModelField(model, field, decl);
|
|
615
640
|
}
|
|
616
641
|
}
|
|
@@ -682,7 +707,7 @@ var PrismaSchemaGenerator = class {
|
|
|
682
707
|
const type = new ModelFieldType(fieldType, isArray, field.type.optional);
|
|
683
708
|
const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithAuthInvocation(attr)).filter((attr) => (
|
|
684
709
|
// when building physical schema, exclude `@default` for id fields inherited from delegate base
|
|
685
|
-
!(model_utils_exports.isIdField(field, contextModel) &&
|
|
710
|
+
!(model_utils_exports.isIdField(field, contextModel) && getDelegateOriginModel(field, contextModel) && attr.decl.$refText === "@default")
|
|
686
711
|
)).map((attr) => this.makeFieldAttribute(attr));
|
|
687
712
|
const docs = [
|
|
688
713
|
...field.comments
|
|
@@ -700,9 +725,6 @@ var PrismaSchemaGenerator = class {
|
|
|
700
725
|
}
|
|
701
726
|
return AstUtils.streamAst(expr).some(isAuthInvocation);
|
|
702
727
|
}
|
|
703
|
-
isInheritedFromDelegate(field, contextModel) {
|
|
704
|
-
return field.$container !== contextModel && model_utils_exports.isDelegateModel(field.$container);
|
|
705
|
-
}
|
|
706
728
|
makeFieldAttribute(attr) {
|
|
707
729
|
const attrName = attr.decl.ref.name;
|
|
708
730
|
return new FieldAttribute(attrName, attr.args.map((arg) => this.makeAttributeArg(arg)));
|
|
@@ -1067,8 +1089,11 @@ var TsSchemaGenerator = class {
|
|
|
1067
1089
|
objectFields.push(ts.factory.createPropertyAssignment("omit", ts.factory.createTrue()));
|
|
1068
1090
|
}
|
|
1069
1091
|
if (contextModel && // id fields are duplicated in inherited models
|
|
1070
|
-
!isIdField(field, contextModel)
|
|
1071
|
-
|
|
1092
|
+
!isIdField(field, contextModel)) {
|
|
1093
|
+
const delegateOrigin = getDelegateOriginModel(field, contextModel);
|
|
1094
|
+
if (delegateOrigin) {
|
|
1095
|
+
objectFields.push(ts.factory.createPropertyAssignment("originModel", ts.factory.createStringLiteral(delegateOrigin)));
|
|
1096
|
+
}
|
|
1072
1097
|
}
|
|
1073
1098
|
if (this.isDiscriminatorField(field)) {
|
|
1074
1099
|
objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
|