-
Notifications
You must be signed in to change notification settings - Fork 2
/
20-reference-field-one2many-many2many.js
91 lines (82 loc) · 2.27 KB
/
20-reference-field-one2many-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
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 Book = pipe(
withBase(),
withName("Book"),
withFields({
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.");
}
}
}),
favoriteBooks: ref({
list: true,
instanceOf: Book
})
})
)();
const book1 = new Book();
book1.populate({
title: "First book"
});
const book2 = new Book();
book2.populate({
title: "Second book"
});
const author = new Author();
author.populate({
nickname: "AwesomeAuthor",
favoriteBooks: [book1, book2]
});
console.log("woah");
await author.save();
let foundBooks = await book1
.getStorageDriver()
.getDatabase()
.collection("Book")
.find();
console.log(`Before delete, we have ${foundBooks.length} in the database.`);
await author.delete();
foundBooks = await book1
.getStorageDriver()
.getDatabase()
.collection("Book")
.find();
console.log(`After delete, we have ${foundBooks.length} in the database.`);
} catch (e) {
console.log("Error message: ", e.message);
console.log("Error data: ", JSON.stringify(e.data, null, 2));
}
})();