Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DDD和带有knexjs的typescript,用于域函数_Typescript_Knexjs - Fatal编程技术网

DDD和带有knexjs的typescript,用于域函数

DDD和带有knexjs的typescript,用于域函数,typescript,knexjs,Typescript,Knexjs,您好,我正在尝试实现DDD,但我对typescript等有一些疑问 我在employee表和department表之间有1:n关系,1个department有1个或n个员工 我的域员工: export interface IEmployeeProps { id: string; matricula: string; first_name: string; last_name: string; departament: Departament; } export class E

您好,我正在尝试实现DDD,但我对typescript等有一些疑问

我在employee表和department表之间有1:n关系,1个department有1个或n个员工

我的域员工:

export interface IEmployeeProps {
  id: string;
  matricula: string;
  first_name: string;
  last_name: string;
  departament: Departament;
}
export class Employee extends AggregateRoot<Omit<IEmployeeProps, 'id'>> {
  private constructor(props: IEmployeeProps, id?: string) {
    super(props, id);
  }
  public static create = (
    props: IEmployeeProps,
    id?: string,
  ): Promise<Either<Error, Employee>> => {
    return new Promise<Either<Error, Employee>>((resolve, reject) => {
      const guardedProps = [
        { argument: props.matricula, argumentName: 'matricula' },
        { argument: props.first_name, argumentName: 'first_name' },
        { argument: props.last_name, argumentName: 'last_name' },
        { argument: props.departament, argumentName: 'departament' },
      ];
      const guardResult = Guard.againstNullOrUndefinedBulk(guardedProps);
      if (!guardResult.succeeded) reject(left(new Error(guardResult.message)));
      if (!validate(props.departament.Identity)) {
        return reject(left(new Error(`Manager_id not is a valid UUID`)));
      }
      const employee = new Employee(props, id);
      const idWasProvided = !!id;
      if (!idWasProvided) {
        employee.when(new LocationCreatedEvent(employee));
      }
      return resolve(right(employee));
    }).catch((error: Error) => {
      throw error;
    });
  };
  public serialize() {
    return null;
  }
}
导出接口IEmployeeProps{
id:字符串;
母体:弦;
第一个名称:字符串;
姓氏:字符串;
出发:出发;
}
导出类Employee扩展AggregateRoot{
私有构造函数(props:IEmployeeProps,id?:string){
超级(道具,id);
}
公共静态创建=(
道具:IEmployeeProps,
id?:字符串,
):承诺=>{
返回新承诺((解决、拒绝)=>{
const guardedProps=[
{argument:props.matricula,argumentName:'matricula'},
{参数:props.first_name,argumentName:'first_name'},
{参数:props.last_name,argumentName:'last_name'},
{argument:props.department,argumentName:'department'},
];
const guardResult=针对Null或UndefinedBulk(guardedProps)的守卫;
如果(!guardResult.successed)拒绝(左侧(新错误(guardResult.message));
如果(!验证(道具部门标识)){
返回拒绝(左侧(新错误(`Manager\u id不是有效的UUID`));
}
const employee=新员工(道具、id);
const id提供=!!id;
如果(!idwas提供){
员工。当(新位置CreateDevent(员工));
}
返回解析(右(员工));
}).catch((错误:error)=>{
投掷误差;
});
};
公共序列化(){
返回null;
}
}
但是我有一个关于knex和typescript的问题。 基本上,在执行查询时,我可以使用以下类型:

const rawResult = await trx ()
        .insert (item)
        .into <Employee> (this.tableName)
        .returning ('*');
const rawResult=wait trx()
.插入(项目)
.into(this.tableName)
。返回(“*”);
我的问题如下,在我的repostorio中,返回必须来自我的域?以便:

public create = async (item: any): Promise<Employee> => {}
public create=async(项:any):Promise=>{}
如果这是正确的,我将不得不使用一些东西将我的答案从我的查询转换为一个函数,该函数将创建我的域的一个实例,如下所示:

  public create = async (item: any): Promise<Employee> => {
    const trx = await this.transactionProvider();
    try {
      const rawResult = await trx()
        .insert(item)
        .into<Employee>(this.tableName)
        .returning('*');
      const toDomainResult = await this.Mapper.toDomain(rawResult);
      await trx.commit();
      return toDomainResult;
    } catch (error) {
      trx.rollback();
      return Promise.reject('Error adding departament Name: ' + error);
    }
  };
public create=async(项:any):Promise=>{
const trx=wait this.transactionProvider();
试一试{
const rawResult=await trx()
.插入(项目)
.into(this.tableName)
。返回(“*”);
const toDomainResult=wait this.Mapper.toDomain(rawResult);
等待trx.commit();
返回到omainresult;
}捕获(错误){
trx.rollback();
return Promise.reject('添加部门名称时出错:'+错误);
}
};
下面是我创建实例的映射器:

  public toDomain = async (raw: any): Promise<Employee> => {
    if (raw instanceof Employee) return raw;
    const data: IEmployeeProps = raw;
    if (!data) return Promise.reject('data is not valid');
    const employee = await Employee.create(data, data.id ? data.id : uuid());
    if (employee.isLeft()) return Promise.reject(employee.value);
    return employee.value;
  };
public-toDomain=async(原始:任意):Promise=>{
如果(员工的原始实例)返回原始;
常量数据:IEmployeeProps=raw;
如果(!data)返回承诺.拒绝('数据无效');
const employee=await employee.create(data,data.id?data.id:uuid());
if(employee.isLeft())返回Promise.reject(employee.value);
返回员工价值;
};
我迷路了,因为在我看来这是一件非常辛苦的事:

将数据发送到存储库, 从我的查询的答案创建我的域的实例

我目前在存储库的所有功能中都这样做:

  public find = async (id: string): Promise<Employee> => {
    const rawDepartament = await this.db
      .select('*')
      .from<IEmployeeProps>(this.tableName)
      .where('id', id)
      .returning('*');
    const departamentDomain = await this.Mapper.toDomain(rawDepartament);
    return departamentDomain;
  };
  public findAll = async (): Promise<Employee[]> => {
    const rawResults = await this.db
      .select('*')
      .from<IEmployeeProps>(this.tableName)
      .where('deleted_at', null)
      .returning('*');
    const toDomainResults = [];
    for (let raw of rawResults) {
      const rawValue = await this.Mapper.toDomain(raw);
      toDomainResults.push(rawValue);
    }
    return toDomainResults;
  };
public find=async(id:string):Promise=>{
const rawdepartment=等待此消息。db
。选择(“*”)
.from(this.tableName)
.where('id',id)
。返回(“*”);
const departmentdomain=wait this.Mapper.toDomain(rawdepartmentdomain);
返回部门域;
};
public findAll=async():Promise=>{
const rawResults=等待this.db
。选择(“*”)
.from(this.tableName)
.where('deleted_at',null)
。返回(“*”);
常数toDomainResults=[];
for(让原始结果为零){
const rawValue=wait this.Mapper.toDomain(原始);
toDomainResults.push(原始值);
}
返回到omainresults;
};
我需要这样做吗?我的逻辑是失败的,还是有效的