Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 0 additions & 5 deletions core/index.js

This file was deleted.

1 change: 1 addition & 0 deletions core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/index.js';
78 changes: 78 additions & 0 deletions core/lib/controller/ExpressController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Router, Request, Response } from 'express';
import { PreshaControllerInterface } from './PreshaControllerInterface';
import { PreshaServiceInterface } from '../service/PreshaServiceInterface.js';

export class ExpressController<T> implements PreshaControllerInterface<T> {
public readonly router: Router;

constructor(private service: PreshaServiceInterface<T>) {
this.router = Router();

this.router.get('/', this.getAllHandler);
this.router.get('/:id', this.getHandler);
this.router.post('/', this.createHandler);
this.router.put('/:id', this.updateHandler);
this.router.delete('/:id', this.deleteHandler);
}

public async getAll(): Promise<T[]> {
return this.service.getAll();
}

public async get(id: string): Promise<T | null> {
return this.service.get(id);
}

public async create(data: Partial<T>): Promise<T> {
return this.service.create(data);
}

public async update(id: string, data: Partial<T>): Promise<T> {
return this.service.update(id, data);
}

public async delete(id: string): Promise<void> {
await this.service.delete(id);
}

private getAllHandler = async (
req: Request,
res: Response
): Promise<void> => {
const result = await this.getAll();
res.json(result);
};

private getHandler = async (req: Request, res: Response): Promise<void> => {
const result = await this.get(req.params.id);
if (!result) {
res.status(404).json({ message: 'Not found' });
return;
}
res.json(result);
};

private createHandler = async (
req: Request,
res: Response
): Promise<void> => {
const result = await this.create(req.body);
res.status(201).json(result);
};

private updateHandler = async (
req: Request,
res: Response
): Promise<void> => {
const result = await this.update(req.params.id, req.body);
res.json(result);
};

private deleteHandler = async (
req: Request,
res: Response
): Promise<void> => {
await this.delete(req.params.id);
res.status(204).send();
};
}
7 changes: 7 additions & 0 deletions core/lib/controller/PreshaControllerInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface PreshaControllerInterface<T> {
create(data: Partial<T>): Promise<T>;
get(id: string): Promise<T | null>;
getAll(): Promise<T[]>;
update(id: string, data: Partial<T>): Promise<T>;
delete(id: string): Promise<void>;
}
9 changes: 9 additions & 0 deletions core/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export * from './controller/PreshaControllerInterface.js';
export * from './controller/ExpressController.js';
export * from './model/PreshaModelInterface.js';
export * from './model/DrizzleModel.js';
export * from './repository/PreshaRepositoryInterface.js';
export * from './repository/PreshaRepository.js';
export * from './service/PreshaServiceInterface.js';
export * from './service/PreshaService.js';
export * from './kit/PreshaKit.js';
29 changes: 29 additions & 0 deletions core/lib/kit/PreshaKit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ExpressController } from '../controller/ExpressController.js';
import { PreshaControllerInterface } from '../controller/PreshaControllerInterface.js';
import { PreshaModelInterface } from '../model/PreshaModelInterface.js';
import { PreshaRepository } from '../repository/PreshaRepository.js';
import { PreshaRepositoryInterface } from '../repository/PreshaRepositoryInterface.js';
import { PreshaService } from '../service/PreshaService.js';
import { PreshaServiceInterface } from '../service/PreshaServiceInterface.js';
import { PreshaKitInterface } from './PreshaKitInterface.js';

export abstract class PreshaKit<T> implements PreshaKitInterface<T> {
public readonly model: PreshaModelInterface<T>;
public readonly repository: PreshaRepositoryInterface<T>;
public readonly service: PreshaServiceInterface<T>;
public readonly controller: PreshaControllerInterface<T>;

constructor(
model: PreshaModelInterface<T>,
repo?: PreshaRepositoryInterface<T>,
svc?: PreshaServiceInterface<T>,
ctrl?: PreshaControllerInterface<T>
) {
this.model = model;
this.repository = repo ?? new PreshaRepository<T>(model);
this.service = svc ?? new PreshaService<T>(this.repository);
this.controller = ctrl ?? new ExpressController<T>(this.service);
}

// TODO: add support for hooks
}
11 changes: 11 additions & 0 deletions core/lib/kit/PreshaKitInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PreshaControllerInterface } from '../controller/PreshaControllerInterface.js';
import { PreshaModelInterface } from '../model/PreshaModelInterface.js';
import { PreshaRepositoryInterface } from '../repository/PreshaRepositoryInterface.js';
import { PreshaServiceInterface } from '../service/PreshaServiceInterface.js';

export interface PreshaKitInterface<T> {
readonly model: PreshaModelInterface<T>;
readonly repository: PreshaRepositoryInterface<T>;
readonly service: PreshaServiceInterface<T>;
readonly controller: PreshaControllerInterface<T>;
}
158 changes: 158 additions & 0 deletions core/lib/model/DrizzleModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// core/DrizzleModel.ts

import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import {
pgTable,
text,
timestamp,
uuid,
boolean,
integer,
json
} from 'drizzle-orm/pg-core';
import { eq } from 'drizzle-orm';
import { PreshaModelInterface } from './PreshaModelInterface.js';
import { PreshaField, PreshaModelSchema } from '../types/PreshaSchema';

// Database connection
const pool = new Pool({
connectionString:
process.env.DATABASE_URL ||
'postgresql://gliba:test@localhost:5432/test-app'
});

const db = drizzle(pool);

// Helper function to convert PreshaField to Drizzle column
function createDrizzleColumn(fieldName: string, field: PreshaField) {
// const isNullable = field.nullable !== false;

switch (field.type) {
case 'string':
return text(fieldName).notNull();
case 'number':
return integer(fieldName).notNull();
case 'boolean':
return boolean(fieldName).notNull();
case 'uuid':
return uuid(fieldName).notNull();
case 'timestamp':
return timestamp(fieldName).notNull();
case 'json':
return json(fieldName).notNull();
default:
return text(fieldName).notNull();
}
}

// TODO: Need to introduce ts type for each model in order to get rid of any type, and accomplish full type safety.
// Possible solutions:
// based on schema

export class DrizzleModel<T> implements PreshaModelInterface<T> {
name: string;
fields: Record<string, PreshaField>;
private table: any;

constructor(schema: PreshaModelSchema) {
this.name = schema.name;
this.fields = schema.fields;
this.table = this.createTable();
}

private createTable() {
const columns: Record<string, any> = {};

// Add all fields as columns
Object.entries(this.fields).forEach(([fieldName, field]) => {
columns[fieldName] = createDrizzleColumn(fieldName, field);
});

return pgTable(this.name.toLowerCase(), columns);
}

getPrimaryKey(): string | undefined {
return Object.entries(this.fields).find(([, meta]) => meta.primary)?.[0];
}

getFieldNames(): string[] {
return Object.keys(this.fields);
}

getFieldMetadata(field: string): PreshaField | undefined {
return this.fields[field];
}

async create(data: Partial<T>): Promise<T> {
try {
const result = await db.insert(this.table).values(data).returning();
return result[0];
} catch (error) {
console.error(`Error creating ${this.name}:`, error);
throw error;
}
}

async findById(id: string): Promise<T | null> {
try {
const primaryKey = this.getPrimaryKey();
if (!primaryKey) {
throw new Error('No primary key defined for this model');
}

const result = await db
.select()
.from(this.table)
.where(eq(this.table[primaryKey], id));
return result[0] || null;
} catch (error) {
console.error(`Error finding ${this.name} by id:`, error);
throw error;
}
}

async findAll(): Promise<T[]> {
try {
const result = await db.select().from(this.table);
return result;
} catch (error) {
console.error(`Error finding all ${this.name}:`, error);
throw error;
}
}

async update(id: string, data: Partial<T>): Promise<T> {
try {
const primaryKey = this.getPrimaryKey();
if (!primaryKey) {
throw new Error('No primary key defined for this model');
}

const result = await db
.update(this.table)
.set(data)
.where(eq(this.table[primaryKey], id))
.returning();

return result[0];
} catch (error) {
console.error(`Error updating ${this.name}:`, error);
throw error;
}
}

async delete(id: string): Promise<void> {
try {
const primaryKey = this.getPrimaryKey();
if (!primaryKey) {
throw new Error('No primary key defined for this model');
}

await db.delete(this.table).where(eq(this.table[primaryKey], id));
} catch (error) {
console.error(`Error deleting ${this.name}:`, error);
throw error;
}
}
}
11 changes: 11 additions & 0 deletions core/lib/model/PreshaModelInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PreshaField } from '../types/PreshaSchema';

export interface PreshaModelInterface<T> {
name: string;
fields: Record<string, PreshaField>;
create(data: Partial<T>): Promise<T>;
findById(id: string): Promise<T | null>;
findAll(): Promise<T[]>;
update(id: string, data: Partial<T>): Promise<T>;
delete(id: string): Promise<void>;
}
26 changes: 26 additions & 0 deletions core/lib/repository/PreshaRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PreshaModelInterface } from '../model/PreshaModelInterface.js';
import { PreshaRepositoryInterface } from './PreshaRepositoryInterface.js';

export class PreshaRepository<T> implements PreshaRepositoryInterface<T> {
constructor(private model: PreshaModelInterface<T>) {}

async findById(id: string): Promise<T | null> {
return this.model.findById(id);
}

async findAll(): Promise<T[]> {
return this.model.findAll();
}

async create(data: Partial<T>): Promise<T> {
return this.model.create(data);
}

async update(id: string, data: Partial<T>): Promise<T> {
return this.model.update(id, data);
}

async delete(id: string): Promise<void> {
return this.model.delete(id);
}
}
7 changes: 7 additions & 0 deletions core/lib/repository/PreshaRepositoryInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface PreshaRepositoryInterface<T> {
findById(id: string): Promise<T | null>;
findAll(): Promise<T[]>;
create(data: Partial<T>): Promise<T>;
update(id: string, data: Partial<T>): Promise<T>;
delete(id: string): Promise<void>;
}
26 changes: 26 additions & 0 deletions core/lib/service/PreshaService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PreshaRepositoryInterface } from '../repository/PreshaRepositoryInterface.js';
import { PreshaServiceInterface } from './PreshaServiceInterface.js';

export class PreshaService<T> implements PreshaServiceInterface<T> {
constructor(protected repository: PreshaRepositoryInterface<T>) {}

async get(id: string): Promise<T | null> {
return this.repository.findById(id);
}

async getAll(): Promise<T[]> {
return this.repository.findAll();
}

async create(data: Partial<T>): Promise<T> {
return this.repository.create(data);
}

async update(id: string, data: Partial<T>): Promise<T> {
return this.repository.update(id, data);
}

async delete(id: string): Promise<void> {
return this.repository.delete(id);
}
}
7 changes: 7 additions & 0 deletions core/lib/service/PreshaServiceInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface PreshaServiceInterface<T> {
get(id: string): Promise<T | null>;
getAll(): Promise<T[]>;
create(data: Partial<T>): Promise<T>;
update(id: string, data: Partial<T>): Promise<T>;
delete(id: string): Promise<void>;
}
Loading