Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/cli/test/prisma-schema-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,47 @@ model User {
expect(prismaSchemaText.includes('@ds.JsonB')).toBe(true);
expect(prismaSchemaText.includes('@ds.ByteA')).toBe(true);
});

it('renames primitive type defs to match the base type', async () => {
const model = await loadSchema(`
model User {
id String @id
name UserName
}

type UserName with String {
this String
}
`);

const generator = new PrismaSchemaGenerator(model);
const prismaSchemaText = await generator.generate();

expect(prismaSchemaText.includes('name UserName')).toBe(false);
expect(prismaSchemaText.includes('name String')).toBe(true);
});

it('renames primitive type defs to match the base type when used with lists', async () => {
const model = await loadSchema(`
datasource db {
provider = 'postgresql'
url = env('DATABASE_URL')
}

model User {
id String @id
name UserName[]
}

type UserName with String {
this String
}
`);

const generator = new PrismaSchemaGenerator(model);
const prismaSchemaText = await generator.generate();

expect(prismaSchemaText.includes('name UserName[]')).toBe(false);
expect(prismaSchemaText.includes('name String[]')).toBe(true);
});
});
156 changes: 156 additions & 0 deletions packages/cli/test/ts-schema-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,160 @@ type Profile with Strict {
},
});
});

it('supports primitive type defs', async () => {
const { schema } = await generateTsSchema(`
model User {
id Int @id
name UserName
}

type UserName with String {
this String
}
`);

expect(schema).toMatchObject({
models: {
User: {
name: 'User',
fields: {
id: {
name: 'id',
type: 'Int',
id: true,
attributes: [
{
name: '@id',
},
],
},
name: {
name: 'name',
type: 'UserName',
},
},
idFields: ['id'],
uniqueFields: {
id: {
type: 'Int',
},
},
},
},
typeDefs: {
UserName: {
name: 'UserName',
base: 'String',
fields: {
this: {
name: 'this',
type: 'String',
},
},
},
},
authType: 'User',
plugins: {},
});
});

it('supports primitive type defs with validation attributes', async () => {
const { schema } = await generateTsSchema(`
model User {
id Int @id
name UserName
}

type UserName with String {
this String @length(2, 16)
}
`);

expect(schema).toMatchObject({
models: {
User: {
name: 'User',
fields: {
id: {
name: 'id',
type: 'Int',
id: true,
attributes: [
{
name: '@id',
},
],
},
name: {
name: 'name',
type: 'UserName',
attributes: [
{
name: '@length',
args: [
{
name: 'min',
value: {
kind: 'literal',
value: 2,
},
},
{
name: 'max',
value: {
kind: 'literal',
value: 16,
},
},
],
},
],
},
},
idFields: ['id'],
uniqueFields: {
id: {
type: 'Int',
},
},
},
},
typeDefs: {
UserName: {
name: 'UserName',
base: 'String',
fields: {
this: {
name: 'this',
type: 'String',
attributes: [
{
name: '@length',
args: [
{
name: 'min',
value: {
kind: 'literal',
value: 2,
},
},
{
name: 'max',
value: {
kind: 'literal',
value: 16,
},
},
],
},
],
},
},
},
},
authType: 'User',
plugins: {},
});
});
});
14 changes: 12 additions & 2 deletions packages/language/src/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export interface DataModel extends langium.AstNode {
readonly $container: Model;
readonly $type: 'DataModel';
attributes: Array<DataModelAttribute>;
base?: BuiltinType;
baseModel?: langium.Reference<DataModel>;
comments: Array<string>;
fields: Array<DataField>;
Expand All @@ -448,6 +449,7 @@ export interface DataModel extends langium.AstNode {
export const DataModel = {
$type: 'DataModel',
attributes: 'attributes',
base: 'base',
baseModel: 'baseModel',
comments: 'comments',
fields: 'fields',
Expand Down Expand Up @@ -912,10 +914,10 @@ export function isReferenceTarget(item: unknown): item is ReferenceTarget {
return reflection.isInstance(item, ReferenceTarget.$type);
}

export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'type' | 'view' | string;
export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'import' | 'in' | 'model' | 'plugin' | 'this' | 'type' | 'view' | string;

export function isRegularID(item: unknown): item is RegularID {
return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || item === 'type' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item)));
return item === 'model' || item === 'enum' || item === 'attribute' || item === 'datasource' || item === 'plugin' || item === 'abstract' || item === 'in' || item === 'view' || item === 'import' || item === 'type' || item === 'this' || (typeof item === 'string' && (/[_a-zA-Z][\w_]*/.test(item)));
}

export type RegularIDWithTypeNames = 'Any' | 'BigInt' | 'Boolean' | 'Bytes' | 'DateTime' | 'Decimal' | 'Float' | 'Int' | 'Json' | 'Null' | 'Object' | 'String' | 'Unsupported' | 'Void' | RegularID;
Expand Down Expand Up @@ -968,6 +970,7 @@ export interface TypeDef extends langium.AstNode {
readonly $container: Model;
readonly $type: 'TypeDef';
attributes: Array<DataModelAttribute>;
base?: BuiltinType;
comments: Array<string>;
fields: Array<DataField>;
mixins: Array<langium.Reference<TypeDef>>;
Expand All @@ -977,6 +980,7 @@ export interface TypeDef extends langium.AstNode {
export const TypeDef = {
$type: 'TypeDef',
attributes: 'attributes',
base: 'base',
comments: 'comments',
fields: 'fields',
mixins: 'mixins',
Expand Down Expand Up @@ -1354,6 +1358,9 @@ export class ZModelAstReflection extends langium.AbstractAstReflection {
name: DataModel.attributes,
defaultValue: []
},
base: {
name: DataModel.base
},
baseModel: {
name: DataModel.baseModel,
referenceType: DataModel.$type
Expand Down Expand Up @@ -1765,6 +1772,9 @@ export class ZModelAstReflection extends langium.AbstractAstReflection {
name: TypeDef.attributes,
defaultValue: []
},
base: {
name: TypeDef.base
},
comments: {
name: TypeDef.comments,
defaultValue: []
Expand Down
41 changes: 31 additions & 10 deletions packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2013,17 +2013,34 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"value": "with"
},
{
"$type": "Assignment",
"feature": "mixins",
"operator": "+=",
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/rules@44"
"$type": "Alternatives",
"elements": [
{
"$type": "Assignment",
"feature": "mixins",
"operator": "+=",
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/rules@44"
},
"deprecatedSyntax": false,
"isMulti": false
}
},
"deprecatedSyntax": false,
"isMulti": false
}
{
"$type": "Assignment",
"feature": "base",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@64"
},
"arguments": []
}
}
]
},
{
"$type": "Group",
Expand Down Expand Up @@ -3087,6 +3104,10 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
{
"$type": "Keyword",
"value": "type"
},
{
"$type": "Keyword",
"value": "this"
}
]
},
Expand Down
20 changes: 20 additions & 0 deletions packages/language/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ export function isLiteAttribute(node: AstNode): node is Attribute {
return isAttribute(node) && hasAttribute(node, '@@@lite');
}

/**
* Returns if the given node is primitive type def.
*/
export function isPrimitiveTypeDef(node: AstNode): node is TypeDef {
return isTypeDef(node) && !!node.base;
}

/**
* Returns the datasource provider literal (e.g. `'postgresql'`) declared in the schema, or undefined
* if no datasource is found or its provider is not a literal.
Expand Down Expand Up @@ -689,6 +696,19 @@ export function getAllFields(
return fields;
}

export function getAllFieldAttributes(field: DataField) {
const attributes: DataFieldAttribute[] = [...field.attributes];
if (isTypeDef(field.type?.reference?.ref) && isPrimitiveTypeDef(field.type.reference.ref)) {
const thisField = getPrimitiveTypeDefThisField(field.type.reference.ref);
attributes.push(...(thisField?.attributes ?? []));
}
return attributes;
}

export function getPrimitiveTypeDefThisField(td: TypeDef) {
return td.fields.find((f) => f.name === 'this');
}

/**
* Gets all attributes of a data model or type def, including inherited attributes
* from base models and mixins.
Expand Down
Loading
Loading