-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpets-dao-example.js
53 lines (47 loc) · 1.4 KB
/
pets-dao-example.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
import config from 'config';
import _ from 'lodash';
import { serializePets, serializePet } from 'api/v1/serializers/pets-serializer';
import { getConnection } from './connection';
import { contrib } from './contrib/contrib';
const { endpointUri } = config.get('server');
/**
* Return a list of pets
*
* @returns {Promise<object[]>} Promise object represents a list of pets
*/
const getPets = async () => {
const connection = await getConnection();
try {
const { rawPets } = await connection.execute(contrib.getPets());
const serializedPets = serializePets(rawPets, endpointUri);
return serializedPets;
} finally {
connection.close();
}
};
/**
* Return a specific pet by unique ID
*
* @param {string} id Unique pet ID
* @returns {Promise<object>} Promise object represents a specific pet or return undefined if term
* is not found
*/
const getPetById = async (id) => {
const connection = await getConnection();
try {
const { rawPets } = await connection.execute(contrib.getPetById(id), id);
if (_.isEmpty(rawPets)) {
return undefined;
}
if (rawPets.length > 1) {
throw new Error('Expect a single object but got multiple results.');
} else {
const [rawPet] = rawPets;
const serializedPet = serializePet(rawPet);
return serializedPet;
}
} finally {
connection.close();
}
};
export { getPets, getPetById };