@wearesage/schema
Version:
A flexible schema definition and validation system for TypeScript with multi-database support
245 lines (197 loc) • 4.82 kB
text/typescript
export const generateSchema = (entity: string, description: string) => `
<INTRO>
You're an expert TypeScript engineer who excels at emulating patterns you see in code. You will be shown a set of TypeScript entity classes that define a schema using decorators, properties, and relationships. Then, using these patterns, you will create a brand new schema representing a ${entity} (${description}) with all necessary relationships and fields, including your own creative additions. Please match the style, structure, and exact usage of decorators, properties, and relationships shown in the example.
</INTRO>
<TYPESCRIPT_SCHEMA_ABSTRACTION_EXAMPLE>
import "reflect-metadata";
import { Entity, Id, Property, OneToMany, OneToOne, ManyToOne, ManyToMany } from "../..";
// --- BEGIN EXAMPLE SCHEMA ---
@Entity()
export class User {
@Id()
id: string;
@Property({ required: true })
name: string;
@Property({ required: true, unique: true })
email: string;
@ManyToMany({
target: () => Organization,
inverse: "members",
name: "MEMBER_OF",
})
organizations: Organization[] = [];
@ManyToOne({
target: () => Organization,
inverse: "owners",
name: "OWNS_ORGANIZATION",
})
ownedOrganization: Organization;
@OneToMany({
target: () => Task,
inverse: "assignee",
name: "ASSIGNED_TO",
})
tasks: Task[] = [];
@OneToMany({
target: () => Task,
inverse: "creator",
name: "CREATED_BY",
})
createdTasks: Task[] = [];
}
@Entity()
export class Organization {
@Id()
id: string;
@Property({ required: true })
name: string;
@Property()
description: string;
@OneToMany({
target: () => Project,
inverse: "organization",
name: "OWNS_PROJECT",
})
projects: Project[] = [];
@ManyToMany({
target: () => User,
inverse: "organizations",
name: "HAS_MEMBER",
})
members: User[] = [];
@OneToMany({
target: () => User,
inverse: "ownedOrganization",
name: "OWNED_BY",
})
owners: User[] = [];
}
@Entity()
export class Project {
@Id()
id: string;
@Property({ required: true })
name: string;
@Property()
description: string;
@ManyToOne({
target: () => Organization,
inverse: "projects",
name: "BELONGS_TO_ORGANIZATION",
})
organization: Organization;
@OneToMany({
target: () => TaskBucket,
inverse: "project",
name: "HAS_BUCKET",
})
buckets: TaskBucket[] = [];
}
@Entity()
export class TaskBucket {
@Id()
id: string;
@Property({ required: true })
name: string;
@Property()
order: number;
@ManyToOne({
target: () => Project,
inverse: "buckets",
name: "BELONGS_TO_PROJECT",
})
project: Project;
@OneToMany({
target: () => Task,
inverse: "bucket",
name: "CONTAINS",
})
tasks: Task[] = [];
}
@Entity()
export class Task {
@Id()
id: string;
@Property({ required: true })
title: string;
@Property()
description: string;
@Property()
dueDate: Date;
@Property()
completed: boolean = false;
@Property()
createdAt: Date;
@ManyToOne({
target: () => TaskBucket,
inverse: "tasks",
name: "BELONGS_TO_BUCKET",
})
bucket: TaskBucket;
@ManyToOne({
target: () => User,
inverse: "tasks",
name: "ASSIGNED_TO",
})
assignee: User;
@ManyToOne({
target: () => User,
inverse: "createdTasks",
name: "CREATED_BY",
})
creator: User;
@OneToMany({
target: () => Comment,
inverse: "task",
name: "HAS_COMMENT",
})
comments: Comment[] = [];
@ManyToMany({
target: () => Label,
inverse: "tasks",
name: "HAS_LABEL",
})
labels: Label[] = [];
}
@Entity()
export class Comment {
@Id()
id: string;
@Property({ required: true })
content: string;
@Property()
createdAt: Date;
@ManyToOne({
target: () => Task,
inverse: "comments",
name: "BELONGS_TO_TASK",
})
task: Task;
@ManyToOne({
target: () => User,
inverse: null,
name: "CREATED_BY",
})
author: User;
}
@Entity()
export class Label {
@Id()
id: string;
@Property({ required: true })
name: string;
@Property()
color: string;
@ManyToMany({
target: () => Task,
inverse: "labels",
name: "APPLIED_TO",
})
tasks: Task[] = [];
}
// --- END EXAMPLE SCHEMA ---
</TYPESCRIPT_SCHEMA_ABSTRACTION_EXAMPLE>
<TASK>
Using these same patterns and decorators (including @Entity, @Property, @Id, @OneToMany, @ManyToOne, @ManyToMany, etc.), create a new schema that accurately represents a ${entity} (${description}). Please be thorough and creative in mapping out the relationships between these entities. Retain consistency in naming patterns, usage of decorators, and property definitions. Your response should be a cohesive TypeScript code snippet that can be used as-is.
</TASK>
`;