wip: draft system with mongodb driver

This commit is contained in:
Supan Adit Pratama 2024-11-20 23:42:03 +07:00
parent b38f1eadc3
commit a396407cd4
7 changed files with 82 additions and 11 deletions

View File

@ -0,0 +1,36 @@
import { MongoClient } from 'mongodb';
import { DraftDriver } from '../interfaces/draft-driver.interface';
export class DraftMongoDBDriver implements DraftDriver {
private client: MongoClient;
private dbName: string;
private collectionName: string;
constructor(uri: string, dbName: string, collectionName: string) {
this.client = new MongoClient(uri);
this.dbName = dbName;
this.collectionName = collectionName;
}
async connect() {
await this.client.connect();
}
async saveDraft(data: any): Promise<void> {
const db = this.client.db(this.dbName);
const collection = db.collection(this.collectionName);
await collection.insertOne(data);
}
async loadDraft(id: string): Promise<any> {
const db = this.client.db(this.dbName);
const collection = db.collection(this.collectionName);
return await collection.findOne({ _id: id });
}
async deleteDraft(id: string): Promise<void> {
const db = this.client.db(this.dbName);
const collection = db.collection(this.collectionName);
await collection.deleteOne({ _id: id });
}
}

View File

@ -1,15 +1,17 @@
import { CreateProcess } from '../processes';
import { DefaultExecutor } from './default.executor';
import { DraftDriver } from '../interfaces/draft-driver.interface';
export class CreateExcutor extends DefaultExecutor {
constructor(process: CreateProcess, data) {
constructor(process: CreateProcess, data, isDraft: boolean = false, draftDriver: DraftDriver) {
super(process);
// Set data to process
process.payload = data;
process.isDraft = isDraft;
process.draftDriver = draftDriver;
}
static async bootstrap(process: CreateProcess, data) {
const executor = new CreateExcutor(process, data);
static async bootstrap(process: CreateProcess, data, isDraft: boolean = false, draftDriver: DraftDriver) {
const executor = new CreateExcutor(process, data, isDraft, draftDriver);
await executor.execute();
return executor.getOutput();
}

View File

@ -1,19 +1,23 @@
import { UpdateProcess } from '../processes';
import { DefaultExecutor } from './default.executor';
import { DraftDriver } from '../interfaces/draft-driver.interface';
// @TODO: This executor should be able to extend from ReadExecutor and CreateExecutor
export class UpdateExecutor extends DefaultExecutor {
constructor(
process: UpdateProcess,
identityData,
data,
identityKey: string = 'id',
isDraft: boolean = false,
draftDriver: DraftDriver,
) {
super(process);
// Set the id and data to process
process.identityData = identityData;
process.payload = data;
process.identityKey = identityKey;
process.isDraft = isDraft;
process.draftDriver = draftDriver;
}
static async bootstrap(
@ -21,12 +25,16 @@ export class UpdateExecutor extends DefaultExecutor {
identityData,
data,
identityKey: string = 'id',
isDraft: boolean = false,
draftDriver: DraftDriver,
) {
const executor = new UpdateExecutor(
process,
identityData,
data,
identityKey,
isDraft,
draftDriver,
);
await executor.execute();
return executor.getOutput();

View File

@ -0,0 +1,5 @@
export interface DraftDriver {
saveDraft(data: any): Promise<void>;
loadDraft(id: string): Promise<any>;
deleteDraft(id: string): Promise<void>;
}

View File

@ -1,5 +1,5 @@
import { DefaultProcess } from './default.process';
import { DraftProcess } from './draft.process';
export class CreateProcess extends DefaultProcess {
export class CreateProcess extends DraftProcess {
public payload;
}

View File

@ -0,0 +1,22 @@
import { DefaultProcess } from './default.process';
import { DraftDriver } from '../interfaces/draft-driver.interface';
export class DraftProcess extends DefaultProcess {
public isDraft: boolean = false;
private draftDriver: DraftDriver;
constructor(draftDriver: DraftDriver) {
super();
this.draftDriver = draftDriver;
}
async process() {
if (this.isDraft) {
// Handle draft-specific logic
console.log('Processing draft...');
await this.draftDriver.saveDraft(this.payload);
} else {
await super.process();
}
}
}

View File

@ -1,9 +1,7 @@
import { DefaultProcess } from './default.process';
import { DraftProcess } from './draft.process';
export class UpdateProcess extends DefaultProcess {
// @TODO: The property of id can be take from ReadProcess which is extended
export class UpdateProcess extends DraftProcess {
public identityData;
public identityKey: string = 'id';
// @TODO: The property of data can be take from CreateProcess which is extended
public payload;
}