-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path21-reference-field-many2many.js
120 lines (106 loc) · 2.99 KB
/
21-reference-field-many2many.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
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
const { withFields, string } = require("@commodo/fields");
const { pipe } = require("ramda");
const { withStorage } = require("@commodo/fields-storage");
const { ref } = require("@commodo/fields-storage-ref");
const { withName } = require("@commodo/name");
const { NeDbDriver, withId } = require("@commodo/fields-storage-nedb");
// We've wrapped this code sample into an async function, so we can
// make function calls with the await keyword, and make our code look nicer.
(async () => {
// With the base model factory, we can reduce the amount of copied code.
const withBase = () => (Model) => {
return pipe(
withId(),
withStorage({
driver: new NeDbDriver()
})
)(Model);
};
try {
const Author2Book = pipe(
withBase(),
withName("Author2Book"),
withFields(() => ({
author: ref({ instanceOf: Author }),
book: ref({ instanceOf: Book })
}))
)();
const Book = pipe(
withBase(),
withName("Book"),
withFields(() => ({
authors: ref({ list: true, instanceOf: Author, using: Author2Book }),
title: string({
validation: (value) => {
if (!value) {
throw new Error("A book must have a title.");
}
}
})
}))
)();
const Author = pipe(
withBase(),
withName("Author"),
withFields(() => ({
nickname: string({
validation: (value) => {
if (!value) {
throw new Error("An author must have a nickname.");
}
}
}),
books: ref({
list: true,
instanceOf: Book,
using: Author2Book
})
}))
)();
const author = new Author();
await author
.populate({
nickname: "AwesomeAuthor"
})
.save();
const book1 = new Book();
await book1
.populate({
title: "First book",
authors: [author]
})
.save();
const book2 = new Book();
await book2
.populate({
title: "Second book",
authors: [author]
})
.save();
const authorsBooks = await author.books;
console.log(
`Author1's books: `,
authorsBooks.map((item) => item.title).join(", ")
);
let foundAuthor2BookEntries = await Author2Book.getStorageDriver()
.getDatabase()
.collection("Author2Book")
.find();
console.log(
`There is a total of ${foundAuthor2BookEntries.length} entries in the "Author2Book" table:`
);
console.log(foundAuthor2BookEntries);
author.books = [];
await author.save();
foundAuthor2BookEntries = await Author2Book.getStorageDriver()
.getDatabase()
.collection("Author2Book")
.find();
console.log(
`After unlinking, there is a total of ${foundAuthor2BookEntries.length} entries in the "Author2Book" table.`
);
} catch (e) {
console.log("Error message: ", e.message);
console.log("Error data: ", JSON.stringify(e.data, null, 2));
}
})();