-
Notifications
You must be signed in to change notification settings - Fork 2
/
15-storage-findbyid-and-findone.js
113 lines (93 loc) · 2.87 KB
/
15-storage-findbyid-and-findone.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
const { withFields, string, number } = require("@commodo/fields");
const { pipe } = require("ramda");
const { withStorage } = require("@commodo/fields-storage");
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 () => {
try {
const Book = pipe(
withName("Book"),
withId(),
withFields({
title: string(),
numberOfPages: number({
value: 0,
validation: (value) => {
if (value > 1000) {
throw new Error("A book cannot have more than 1000 pages.");
}
}
})
}),
withStorage({
driver: new NeDbDriver()
})
)();
const book1 = new Book();
book1.populate({
title: "First book",
numberOfPages: 100
});
await book1.save();
const book2 = new Book();
book2.populate({
title: "Second book",
numberOfPages: 200
});
await book2.save();
book2.numberOfPages = 250;
await book2.save();
// findById
const foundBook1 = await Book.findById(book1.id);
const unknownBook1 = await Book.findById("xyz");
console.log("Found book 1 title:", foundBook1.title);
console.log("Unknown book: ", unknownBook1);
// findOne
await Book.findOne({ query: { numberOfPages: { $gte: 200 } } });
const foundBook2 = await Book.findOne({ query: { title: "First book" } });
const unknownBook2 = await Book.findOne({
query: { title: "Unknown book" }
});
console.log("foundBook2.title:", foundBook2.title);
console.log("unknownBook2: ", unknownBook2);
// findOne - with sorting
const foundBook3 = await Book.findOne({
sort: { title: -1 }
});
console.log("foundBook3.title:", foundBook3.title);
// find
const foundBooks1 = await Book.find({
sort: { title: -1 },
totalCount: true,
limit: 1a
});
console.log(
`Found ${foundBooks1.length} books with titles:`,
foundBooks1.map((item) => item.title).join(", ")
);
console.log(foundBooks1.getMeta());
// // find
// let books = await Book.find({
// query: {
// numberOfPages: { $gte: 200 }
// },
// sort: { id: 1 }
// });
// console.log("Found books:", books.length);
// console.log("Book 1 title:", books[0].title);
// books = await Book.find({
// query: {
// numberOfPages: { $gte: 100 }
// },
// sort: { id: 1 }
// });
// console.log("Found books:", books.length);
// console.log("Book 1 title:", books[0].title);
// console.log("Book 2 title:", books[1].title);
} catch (e) {
console.log("Error message: ", e.message);
console.log("Error data: ", e.data);
}
})();