Skip to content

Refactor - use Inheritance over templating #23

@DimitrijeGlibic

Description

@DimitrijeGlibic

Currently i am trying to refactor how presha works, i want to try to make it via inheritance.

In future, when inheritance is implemented we will have something like this presha definitions:

  • PreshaModel - interface for now only class that will implement this will be Drizzle but we can extend this to others like Sequlize or Prisma
  • PreshaRepository - repository implementation
  • PreshaService - service implementation
  • PreshaController - interface, for now we will only implement ExpressController
  • PreshaUnit - placehodler name, self-contained module that bundles a model, repository, service, and controller into a single unit, streamlining domain logic and reducing boilerplate for each database model in a Node.js application

Those presha definitions will be exported from our library and our domain/user.ts can look like this:

import { DrizzleModel } from "../presha-definitions/DrizzleModel.js";
import { PreshaUnit } from "../presha-definitions/PreshaUnit.js";

export interface UserModel {
  id: string;
  email: string;
  createdAt: Date;
}

const userModel = new DrizzleModel({
  name: 'User',
  fields: {
    id: { type: 'uuid', primary: true },
    email: { type: 'string', unique: true },
    createdAt: { type: 'timestamp' },
  },
});

export class User extends PreshaUnit<UserModel> {
  constructor() {
    super(userModel);
  }


  // TODO: add support for hooks
  // optional
//   protected async beforeCreate(data: Partial<UserModel>) {
//     console.log('🔍 Validating user before create:', data.email);
//     if (!data.email?.includes('@')) {
//       throw new Error('Invalid email');
//     }
//     return data;
//   }

  // optional
//   protected async afterCreate(user: UserModel) {
//     console.log('✅ Created user:', user.id);
//   }
}

Why inheritance over code generation?

Via inheritance we will have less magic in how our framework works and more flexibility.

Can this be library instead of framework then?

Well no, because we still want to generate migrations, sdk, and maybe docs later on without any coding. So this part will still be implemented in presha via templating.

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions