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 { CreateProcess } from '../processes';
|
||||||
import { DefaultExecutor } from './default.executor';
|
import { DefaultExecutor } from './default.executor';
|
||||||
|
import { DraftDriver } from '../interfaces/draft-driver.interface';
|
||||||
|
|
||||||
export class CreateExcutor extends DefaultExecutor {
|
export class CreateExcutor extends DefaultExecutor {
|
||||||
constructor(process: CreateProcess, data) {
|
constructor(process: CreateProcess, data, isDraft: boolean = false, draftDriver: DraftDriver) {
|
||||||
super(process);
|
super(process);
|
||||||
// Set data to process
|
|
||||||
process.payload = data;
|
process.payload = data;
|
||||||
|
process.isDraft = isDraft;
|
||||||
|
process.draftDriver = draftDriver;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async bootstrap(process: CreateProcess, data) {
|
static async bootstrap(process: CreateProcess, data, isDraft: boolean = false, draftDriver: DraftDriver) {
|
||||||
const executor = new CreateExcutor(process, data);
|
const executor = new CreateExcutor(process, data, isDraft, draftDriver);
|
||||||
await executor.execute();
|
await executor.execute();
|
||||||
return executor.getOutput();
|
return executor.getOutput();
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
import { UpdateProcess } from '../processes';
|
import { UpdateProcess } from '../processes';
|
||||||
import { DefaultExecutor } from './default.executor';
|
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 {
|
export class UpdateExecutor extends DefaultExecutor {
|
||||||
constructor(
|
constructor(
|
||||||
process: UpdateProcess,
|
process: UpdateProcess,
|
||||||
identityData,
|
identityData,
|
||||||
data,
|
data,
|
||||||
identityKey: string = 'id',
|
identityKey: string = 'id',
|
||||||
|
isDraft: boolean = false,
|
||||||
|
draftDriver: DraftDriver,
|
||||||
) {
|
) {
|
||||||
super(process);
|
super(process);
|
||||||
// Set the id and data to process
|
// Set the id and data to process
|
||||||
process.identityData = identityData;
|
process.identityData = identityData;
|
||||||
process.payload = data;
|
process.payload = data;
|
||||||
process.identityKey = identityKey;
|
process.identityKey = identityKey;
|
||||||
|
process.isDraft = isDraft;
|
||||||
|
process.draftDriver = draftDriver;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async bootstrap(
|
static async bootstrap(
|
||||||
@ -21,12 +25,16 @@ export class UpdateExecutor extends DefaultExecutor {
|
|||||||
identityData,
|
identityData,
|
||||||
data,
|
data,
|
||||||
identityKey: string = 'id',
|
identityKey: string = 'id',
|
||||||
|
isDraft: boolean = false,
|
||||||
|
draftDriver: DraftDriver,
|
||||||
) {
|
) {
|
||||||
const executor = new UpdateExecutor(
|
const executor = new UpdateExecutor(
|
||||||
process,
|
process,
|
||||||
identityData,
|
identityData,
|
||||||
data,
|
data,
|
||||||
identityKey,
|
identityKey,
|
||||||
|
isDraft,
|
||||||
|
draftDriver,
|
||||||
);
|
);
|
||||||
await executor.execute();
|
await executor.execute();
|
||||||
return executor.getOutput();
|
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;
|
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 {
|
export class UpdateProcess extends DraftProcess {
|
||||||
// @TODO: The property of id can be take from ReadProcess which is extended
|
|
||||||
public identityData;
|
public identityData;
|
||||||
public identityKey: string = 'id';
|
public identityKey: string = 'id';
|
||||||
// @TODO: The property of data can be take from CreateProcess which is extended
|
|
||||||
public payload;
|
public payload;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user