From a396407cd41a423fde934c3ceb2977b1a362d712 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Wed, 20 Nov 2024 23:42:03 +0700 Subject: [PATCH] wip: draft system with mongodb driver --- .../src/drivers/draft-mongodb-driver.ts | 36 +++++++++++++++++++ .../skeleton/src/executors/create.executor.ts | 10 +++--- .../skeleton/src/executors/update.executor.ts | 10 +++++- .../src/interfaces/draft-driver.interface.ts | 5 +++ libs/skeleton/src/processes/create.process.ts | 4 +-- libs/skeleton/src/processes/draft.process.ts | 22 ++++++++++++ libs/skeleton/src/processes/update.process.ts | 6 ++-- 7 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 libs/skeleton/src/drivers/draft-mongodb-driver.ts create mode 100644 libs/skeleton/src/interfaces/draft-driver.interface.ts create mode 100644 libs/skeleton/src/processes/draft.process.ts diff --git a/libs/skeleton/src/drivers/draft-mongodb-driver.ts b/libs/skeleton/src/drivers/draft-mongodb-driver.ts new file mode 100644 index 0000000..b923501 --- /dev/null +++ b/libs/skeleton/src/drivers/draft-mongodb-driver.ts @@ -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 { + const db = this.client.db(this.dbName); + const collection = db.collection(this.collectionName); + await collection.insertOne(data); + } + + async loadDraft(id: string): Promise { + const db = this.client.db(this.dbName); + const collection = db.collection(this.collectionName); + return await collection.findOne({ _id: id }); + } + + async deleteDraft(id: string): Promise { + const db = this.client.db(this.dbName); + const collection = db.collection(this.collectionName); + await collection.deleteOne({ _id: id }); + } +} diff --git a/libs/skeleton/src/executors/create.executor.ts b/libs/skeleton/src/executors/create.executor.ts index 7e932a5..ac2ce09 100644 --- a/libs/skeleton/src/executors/create.executor.ts +++ b/libs/skeleton/src/executors/create.executor.ts @@ -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(); } diff --git a/libs/skeleton/src/executors/update.executor.ts b/libs/skeleton/src/executors/update.executor.ts index 9e3c35b..cecda6f 100644 --- a/libs/skeleton/src/executors/update.executor.ts +++ b/libs/skeleton/src/executors/update.executor.ts @@ -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(); diff --git a/libs/skeleton/src/interfaces/draft-driver.interface.ts b/libs/skeleton/src/interfaces/draft-driver.interface.ts new file mode 100644 index 0000000..9b0fa2e --- /dev/null +++ b/libs/skeleton/src/interfaces/draft-driver.interface.ts @@ -0,0 +1,5 @@ +export interface DraftDriver { + saveDraft(data: any): Promise; + loadDraft(id: string): Promise; + deleteDraft(id: string): Promise; +} diff --git a/libs/skeleton/src/processes/create.process.ts b/libs/skeleton/src/processes/create.process.ts index 02e41c1..23b574f 100644 --- a/libs/skeleton/src/processes/create.process.ts +++ b/libs/skeleton/src/processes/create.process.ts @@ -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; } diff --git a/libs/skeleton/src/processes/draft.process.ts b/libs/skeleton/src/processes/draft.process.ts new file mode 100644 index 0000000..ac2c1f3 --- /dev/null +++ b/libs/skeleton/src/processes/draft.process.ts @@ -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(); + } + } +} diff --git a/libs/skeleton/src/processes/update.process.ts b/libs/skeleton/src/processes/update.process.ts index 72c2bfb..4d059bb 100644 --- a/libs/skeleton/src/processes/update.process.ts +++ b/libs/skeleton/src/processes/update.process.ts @@ -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; }