Schema definition without using the decorators

Viewed 280

I'm trying to define a schema in pure code, without using the "experimental" decorators. Everything achievable with decorators should be achievable in pure code, no?

Here's an example of what I have got up and running so far, I will pose my questions afterwards:

// define the TypeScript type
export type ProjectRecord = {
    project_id: string

    name: string
    created: Date
}

// instantiate the schema in pure code
export const ProjectSchema = new EntitySchema<ProjectRecord>({
    name: "PROJECT",
    tableName: "project",
    columns: {
        project_id: {primary: true, type: "varchar", length: 32},

        name: {type: "varchar", length: 128},
        created: {type: "datetime"},
    },
});

This has been working great for doing very primitive CRUD operations. What I haven't been able to do is to define relations between schemas, in order to transparently do JOIN operations. Assuming the ProjectSchema defined above, and a UserSchema defined elsewhere (one user has many projects), how would I define the relations configuration option?

I tinkered with the code by using the TypeScript type hints and I managed to get as far as the following configuration in the EntitySchema constructor, as a starting point, but it's woefully incomplete.

// relations: {
//     user_id: {
//         target: {
//             type: UserSchema,
//             name: "u"
//         },
//         type: "one-to-many"
//     }
// }

Ideally, I'd be able to just do: project.user and user.projects to access the linked objects. If you could also show me how the cascading operations configuration would look like (for both the cascading and not cascading case), I would very much appreciate it.

Thank you!

0 Answers
Related