-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
namespace-member.js
76 lines (65 loc) 路 1.56 KB
/
namespace-member.js
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
'use strict';
const orm = require('ormnomnom');
const joi = require('@hapi/joi');
module.exports = class NamespaceMember {
#user = null;
#namespace = null;
constructor({
id,
user_id,
user,
namespace_id,
namespace,
created,
modified,
active,
accepted
}) {
this.id = id;
this.user_id = user_id;
this.#user = user ? Promise.resolve(user) : null;
this.namespace_id = namespace_id;
this.#namespace = namespace ? Promise.resolve(namespace) : null;
this.created = created;
this.modified = modified;
this.active = active;
this.accepted = accepted;
}
get user() {
if (this.#user === null) {
this.#user = User.objects.get({ id: this.user_id });
this.#user.catch(() => {});
}
return this.#user;
}
set user(u) {
this.#user = Promise.resolve(u);
this.user_id = this.#user.id;
}
get namespace() {
if (this.#namespace === null) {
this.#namespace = Namespace.objects.get({ id: this.namespace_id });
this.#namespace.catch(() => {});
}
return this.#namespace;
}
set namespace(u) {
this.#namespace = Promise.resolve(u);
this.namespace_id = this.#namespace.id;
}
};
const User = require('./user');
const Namespace = require('./namespace');
module.exports.objects = orm(module.exports, {
id: joi
.number()
.integer()
.greater(-1)
.required(),
user: orm.fk(User),
namespace: orm.fk(Namespace),
created: joi.date(),
modified: joi.date(),
active: joi.boolean().default(true),
accepted: joi.boolean().default(true)
});