Skip to content

Commit

Permalink
fix: add missing attribute parameters and loosen keyword restrictions (
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored May 5, 2023
1 parent 4d7699f commit ef7acd7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/language/src/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function isReferenceTarget(item: unknown): item is ReferenceTarget {
return reflection.isInstance(item, ReferenceTarget);
}

export type RegularID = 'in' | string;
export type RegularID = 'abstract' | 'attribute' | 'datasource' | 'enum' | 'in' | 'model' | 'plugin' | 'sort' | string;

export type TypeDeclaration = DataModel | Enum;

Expand Down
28 changes: 28 additions & 0 deletions packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2449,9 +2449,37 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
},
"arguments": []
},
{
"$type": "Keyword",
"value": "model"
},
{
"$type": "Keyword",
"value": "enum"
},
{
"$type": "Keyword",
"value": "attribute"
},
{
"$type": "Keyword",
"value": "datasource"
},
{
"$type": "Keyword",
"value": "plugin"
},
{
"$type": "Keyword",
"value": "abstract"
},
{
"$type": "Keyword",
"value": "in"
},
{
"$type": "Keyword",
"value": "sort"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion packages/language/src/zmodel.langium
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ QualifiedName returns string:
// https://github.com/langium/langium/discussions/1012
RegularID returns string:
// include keywords that we'd like to work as ID in most places
ID | 'in';
ID | 'model' | 'enum' | 'attribute' | 'datasource' | 'plugin' | 'abstract' | 'in' | 'sort';

// attribute-level attribute
AttributeAttributeName returns string:
Expand Down
22 changes: 17 additions & 5 deletions packages/schema/src/res/stdlib.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ attribute @@@prisma()
/*
* Defines an ID on the model.
*/
attribute @id(map: String?) @@@prisma
attribute @id(map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma

/*
* Defines a default value for a field.
Expand All @@ -170,22 +170,34 @@ attribute @default(_ value: ContextType) @@@prisma
/*
* Defines a unique constraint for this field.
*/
attribute @unique(map: String?) @@@prisma
attribute @unique(map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma

/*
* Defines a multi-field ID (composite ID) on the model.
*/
attribute @@id(_ fields: FieldReference[], name: String?, map: String?) @@@prisma
attribute @@id(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma

/*
* Defines a compound unique constraint for the specified fields.
*/
attribute @@unique(_ fields: FieldReference[], name: String?, map: String?) @@@prisma
attribute @@unique(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: String?, clustered: Boolean?) @@@prisma

/*
* Index types
*/
enum IndexType {
BTree
Hash
Gist
Gin
SpGist
Brin
}

/*
* Defines an index in the database.
*/
attribute @@index(_ fields: FieldReference[], map: String?) @@@prisma
attribute @@index(_ fields: FieldReference[], name: String?, map: String?, length: Int?, sort: String?, clustered: Boolean?, type: IndexType?) @@@prisma

/*
* Defines meta information about the relation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ describe('Attribute tests', () => {
model A {
x Int
y String
@@id([x, y], name: 'x_y', map: '_x_y')
@@id([x, y], name: 'x_y', map: '_x_y', length: 10, sort: 'Asc', clustered: true)
}
model B {
id String @id(map: '_id', length: 10, sort: 'Asc', clustered: true)
}
`);

Expand All @@ -169,7 +173,7 @@ describe('Attribute tests', () => {
id String @id
x Int
y String
@@unique([x, y])
@@unique([x, y], name: 'x_y', map: '_x_y', length: 10, sort: 'Asc', clustered: true)
}
`);

Expand All @@ -183,6 +187,14 @@ describe('Attribute tests', () => {
}
`);

await loadModel(`
${prelude}
model A {
id String @id
x Int @unique(map: '_x', length: 10, sort: 'Asc', clustered: true)
}
`);

expect(
await loadModelWithError(`
${prelude}
Expand All @@ -203,6 +215,13 @@ describe('Attribute tests', () => {
y String
@@index([x, y])
}
model B {
id String @id
x Int
y String
@@index([x(sort: Asc), y(sort: Desc)], name: 'myindex', map: '_myindex', length: 10, sort: 'asc', clustered: true, type: BTree)
}
`);

await loadModel(`
Expand Down

0 comments on commit ef7acd7

Please sign in to comment.