-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (132 loc) · 4.46 KB
/
index.js
File metadata and controls
161 lines (132 loc) · 4.46 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const { miniql } = require("miniql");
const { createQueryResolver } = require("@miniql/csv");
async function main() {
//
// Configures CSV files to be loaded and how they relate to each other.
//
const csvFilesConfig = {
species: {
primaryKey: "name",
csvFilePath: "./data/species.csv",
nested: {
homeworld: {
parentKey: "homeworld",
from: "planet",
},
},
},
planet: {
primaryKey: "name",
csvFilePath: "./data/planets.csv",
nested: {
species: {
foreignKey: "homeworld",
},
},
},
};
//
// Loads CSV files and creates a MiniQL query resolver.
//
const queryResolver = await createQueryResolver(csvFilesConfig);
//
// Check out each function for example queries.
//
await example_query_get_all_species(queryResolver);
await example_query_get_one_species(queryResolver);
await example_query_get_one_species_with_nested_entity_lookup(queryResolver);
await example_query_get_all_species_with_nested_entity_lookup(queryResolver);
await example_query_get_all_planets_with_nested_entity_lookup(queryResolver);
}
//
// Example query to get all species.
//
async function example_query_get_all_species(queryResolver) {
const query = {
get: {
species: { // Query for "species" entity.
// No arguments gets all entities.
},
},
};
const result = await miniql(query, queryResolver, {}); // Invokes MiniQL.
console.log(JSON.stringify(result, null, 4)); // Displays the query result.
}
//
// Example query to get one individual species.
//
async function example_query_get_one_species(queryResolver) {
const query = {
get: {
species: { // Query for "species" entity.
args: {
name: "Hutt", // Gets the one species that matches this name.
},
},
},
};
const result = await miniql(query, queryResolver, {}); // Invokes MiniQL.
console.log(JSON.stringify(result, null, 4)); // Displays the query result.
}
//
// Example query to get a single species with nested entity lookup.
//
async function example_query_get_one_species_with_nested_entity_lookup(queryResolver) {
const query = {
get: {
species: { // Query for "species" entity.
args: {
name: "Hutt", // Gets the one species that matches this name.
},
resolve: {
homeworld: { // Resolves the homeworld of the species as a nested lookup.
},
},
},
},
};
const result = await miniql(query, queryResolver, {}); // Invokes MiniQL.
console.log(JSON.stringify(result, null, 4)); // Displays the query result.
}
//
// Example query to get all species with nested entity lookup.
//
async function example_query_get_all_species_with_nested_entity_lookup(queryResolver) {
const query = {
get: {
species: { // Query for "species" entity.
// No arguments gets all entities.
resolve: {
homeworld: { // Resolves the homeworld of each species as a nested lookup.
},
},
},
},
};
const result = await miniql(query, queryResolver, {}); // Invokes MiniQL.
console.log(JSON.stringify(result, null, 4)); // Displays the query result.
}
//
// Example query to get all planets with nested species lookup.
//
async function example_query_get_all_planets_with_nested_entity_lookup(queryResolver) {
const query = {
get: {
planet: { // Query for "planet" entity.
// No arguments gets all entities.
resolve: {
species: { // Gets all the species related to to each planet.
},
},
},
},
};
const result = await miniql(query, queryResolver, {}); // Invokes MiniQL.
console.log(JSON.stringify(result, null, 4)); // Displays the query result.
}
main()
.then(() => console.log("Done"))
.catch(err => {
console.error("Error!");
console.error(err && err.stack || err);
})