mirror of
https://github.com/aditama-labs/nest-autocrud.git
synced 2024-11-22 03:16:21 +00:00
wip: draft system with mongodb driver
This commit is contained in:
parent
b38f1eadc3
commit
a396407cd4
36
libs/skeleton/src/drivers/draft-mongodb-driver.ts
Normal file
36
libs/skeleton/src/drivers/draft-mongodb-driver.ts
Normal 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 });
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
|
5
libs/skeleton/src/interfaces/draft-driver.interface.ts
Normal file
5
libs/skeleton/src/interfaces/draft-driver.interface.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface DraftDriver {
|
||||
saveDraft(data: any): Promise<void>;
|
||||
loadDraft(id: string): Promise<any>;
|
||||
deleteDraft(id: string): Promise<void>;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
22
libs/skeleton/src/processes/draft.process.ts
Normal file
22
libs/skeleton/src/processes/draft.process.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user