forked from louislam/redbean-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared-list.ts
135 lines (96 loc) · 3.43 KB
/
shared-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {LazyLoadArray} from "./lazy-load-array";
import {Bean} from "./bean";
export class SharedList extends LazyLoadArray {
via : string;
constructor(parentBean: Bean, type: string, via : string) {
super(parentBean, type);
this.via = via;
}
push(...items): number {
for (let item of items) {
if (this.type != item.beanMeta.type) {
throw new Error("The bean type does not match the shared list type");
}
}
return super.push(...items);
}
remove(...items) {
for (let item of items) {
if (this.type != item.beanMeta.type) {
throw new Error("The bean type does not match the shared list type");
}
}
super.remove(...items);
}
async toArray(force: boolean = false) : Promise<Bean[]> {
// Not yet save, must empty
if (! this.parentBean._id) {
this.devLog("Parent Bean no id", this.parentBean._id);
return this.list;
}
if (! this.loaded || force) {
this._list = [];
this.loaded = true;
let id1 = this.type + ".id"; // tag.id
let id2 = this.via + "." + this.type + "_id"; // product_tag.tag_id
let parentBeanFieldName = this.parentBean.beanMeta.type + "_id";
let queryPromise = this.R.knex.table(this.type).select(this.type + ".*")
.join(this.via, id1, "=", id2)
.where(parentBeanFieldName, this.parentBean._id);
if (this.withCondition) {
queryPromise.whereRaw(this.withCondition, this.withConditionData)
}
this.R.queryLog(queryPromise);
try {
let list = await queryPromise;
console.log("Result length: ", list.length)
list = this.R.convertToBeans(this.type, list);
for (let item of list) {
this.list.push(item);
}
} catch (error) {
try {
this.R.checkAllowedError(error);
} catch (e) {
this.loaded = false;
throw e;
}
}
}
return this.list;
}
async store() {
this.devLog("Store Shared List");
let id1 = this.parentBean.beanMeta.type + "_id";
let id2 = this.type + "_id";
// Add
while (this._pendingAddList.length > 0) {
let bean = this._pendingAddList.pop();
if (! bean) {
continue;
}
if (! bean.id) {
await this.R.store(bean);
}
let viaBean = this.R.dispense(this.via);
viaBean[id1] = this.parentBean._id;
viaBean[id2] = bean.id;
await this.R.store(viaBean);
}
let promiseList : Promise<any>[] = [];
// Remove
while (this._pendingRemoveList.length > 0) {
let bean = this._pendingRemoveList.pop();
if (! bean || ! bean.id) {
continue;
}
let queryPromise = this.R.knex(this.via)
.where(id1, this.parentBean._id)
.where(id2, bean.id)
.del()
this.R.queryLog(queryPromise);
promiseList.push(queryPromise);
}
await this.R.concurrent(promiseList);
}
}