Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fa051dd
feat(language): primitive custom types
sanny-io Sep 23, 2026
4600449
chore(language): add tests
sanny-io Sep 23, 2026
5b4131c
feat(orm): primitive custom types
sanny-io Sep 23, 2026
60018b3
feat(sdk): primitive custom types
sanny-io Sep 23, 2026
c7ca5c4
feat(schema): primitive custom types
sanny-io Sep 23, 2026
98e2a24
feat(zod): primitive custom types
sanny-io Sep 23, 2026
74f9883
chore(cli): add tests
sanny-io Sep 23, 2026
e53b856
chore: regenerate schemas
sanny-io Sep 23, 2026
172d563
chore(zod): add tests
sanny-io Sep 23, 2026
d736dfa
chore: add e2e tests
sanny-io Sep 23, 2026
8839367
fix(orm): do not convert json to array
sanny-io Sep 24, 2026
094a2b7
fix(language): remove parent primitive type def check
sanny-io Sep 24, 2026
3309af7
fix(sdk): fix primitive type def arrays for prisma schema gen
sanny-io Sep 24, 2026
f7e9bda
chore: add default age for procedures test
sanny-io Sep 24, 2026
3aa959c
fix(orm): add missing custom validations
sanny-io Sep 24, 2026
bc9f8f5
fix(orm): json arrays
sanny-io Sep 24, 2026
8743f4f
fix(zod): apply consuming field attributes for `makeTypeSchema`
sanny-io Sep 24, 2026
812f2a5
chore(zod): regenerate schema
sanny-io Sep 24, 2026
905476a
fix(language): map to expression type
sanny-io Sep 24, 2026
d43de69
Merge remote-tracking branch 'upstream/dev' into feat/primitive-custo…
sanny-io Oct 1, 2026
d71eb56
Merge branch 'dev' into feat/primitive-custom-types
ymc9 Oct 10, 2026
6d9ab06
test(orm): add counter tests for primitive custom type review findings
ymc9 Oct 10, 2026
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 @@ -1015,4 +1015,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 @@ -249,6 +249,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 @@ -737,6 +744,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