Skip to content

Commit 72ef8b7

Browse files
committed
feat(sqlite-demo): add Post↔Tag M:N relation + emit
Adds Tag model, PostTag junction (composite PK post_tag_pkey), and rel.manyToMany() declarations on both Post.tags and Tag.posts. Re-emits contract.json / contract.d.ts; cardinality 'N:M' + through descriptor present in the generated artifacts. Signed-off-by: Alexey Orlenko's AI Agent <robot@aqrln.net>
1 parent 3e1c908 commit 72ef8b7

3 files changed

Lines changed: 546 additions & 4 deletions

File tree

examples/prisma-next-demo-sqlite/prisma/contract.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ export const contract = defineContract({}, ({ field, model }) => {
2020
},
2121
});
2222

23+
const Tag = model('Tag', {
24+
fields: {
25+
id: field.id.uuidv4(),
26+
label: field.column(textColumn),
27+
},
28+
});
29+
30+
const PostTag = model('PostTag', {
31+
fields: {
32+
postId: field.uuid(),
33+
tagId: field.uuid(),
34+
},
35+
}).attributes(({ fields, constraints }) => ({
36+
id: constraints.id([fields.postId, fields.tagId], { name: 'post_tag_pkey' }),
37+
}));
38+
2339
return {
2440
models: {
2541
User: User.relations({
@@ -29,6 +45,11 @@ export const contract = defineContract({}, ({ field, model }) => {
2945
}),
3046
Post: Post.relations({
3147
user: rel.belongsTo(User, { from: 'userId', to: 'id' }),
48+
tags: rel.manyToMany(() => Tag, {
49+
through: () => PostTag,
50+
from: 'postId',
51+
to: 'tagId',
52+
}),
3253
}).sql(({ cols, constraints }) => ({
3354
table: 'post',
3455
foreignKeys: [
@@ -37,6 +58,22 @@ export const contract = defineContract({}, ({ field, model }) => {
3758
}),
3859
],
3960
})),
61+
Tag: Tag.relations({
62+
posts: rel.manyToMany(() => Post, {
63+
through: () => PostTag,
64+
from: 'tagId',
65+
to: 'postId',
66+
}),
67+
}).sql({
68+
table: 'tag',
69+
}),
70+
PostTag: PostTag.sql(({ cols, constraints }) => ({
71+
table: 'post_tag',
72+
foreignKeys: [
73+
constraints.foreignKey(cols.postId, Post.refs.id, { name: 'post_tag_postId_fkey' }),
74+
constraints.foreignKey(cols.tagId, Tag.refs.id, { name: 'post_tag_tagId_fkey' }),
75+
],
76+
})),
4077
},
4178
};
4279
});

0 commit comments

Comments
 (0)