我目前有一个 Node 应用,它有这样的 Typescript 类:
class Client{
private _id : string;
public get id() {
return this._id;
}
public set id(newId) {
if isValidId(newId) {
this._id = newId;
}
}
private _orders : Array<Order>;
public get orders(){
return this._orders;
}
public addOrder = (newOrder : Order) => this._orders.push(newOrder);
private isValidId = (id: string) => {
//Some validation logic;
return true;
}
constructor (id) {
this._id = id;
this._orders = new Array<Order>();
}
}
client Order{
private _id : string;
public get id() {
return this._id;
}
private _item : string;
public get item() {
return this._item;
}
public doSomething = () => {
//some things are done;
}
constructor(id : string, item : string){
this._id = id;
this._item = item;
}
}
这适用于内存中,但对于持久存储,我必须使用像 MongoDB 或 Postgresql 这样的数据库。现在,我的问题是,如何将我当前的模型转化为可以与数据库一起使用的模型?例如,我目前可以使用 someClient.orders
获取特定客户的订单。这不适用于数据库。那么,我该如何设计一个适用于 DB 的模型呢?我是否放弃了所有当前的模型并从头开始使用 ORM?由于我已经有一个可以工作的业务层,我非常想扩展当前代码以使用数据库。