forked from hyperledger-archives/composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.registries.js
123 lines (113 loc) · 4.6 KB
/
engine.registries.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
121
122
123
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const Logger = require('composer-common').Logger;
const util = require('util');
const LOG = Logger.getLog('EngineRegistries');
/**
* The JavaScript engine responsible for processing chaincode commands.
* @protected
* @memberof module:composer-runtime
*/
class EngineRegistries {
/**
* Get all registries.
* @param {Context} context The request context.
* @param {string[]} args The arguments to pass to the chaincode function.
* @return {Promise} A promise that will be resolved when complete, or rejected
* with an error.
*/
getAllRegistries(context, args) {
const method = 'getAllRegistries';
LOG.entry(method, context, args);
if (args.length !== 1) {
LOG.error(method, 'Invalid arguments', args);
throw new Error(util.format('Invalid arguments "%j" to function "%s", expecting "%j"', args, 'getAllRegistries', ['registryType']));
}
let registryType = args[0];
return context.getRegistryManager().getAll(registryType)
.then((result) => {
LOG.exit(method, result);
return result;
});
}
/**
* Get the specified registry.
* @param {Context} context The request context.
* @param {string[]} args The arguments to pass to the chaincode function.
* @return {Promise} A promise that will be resolved when complete, or rejected
* with an error.
*/
getRegistry(context, args) {
const method = 'getRegistry';
LOG.entry(method, context, args);
if (args.length !== 2) {
LOG.error(method, 'Invalid arguments', args);
throw new Error(util.format('Invalid arguments "%j" to function "%s", expecting "%j"', args, 'getRegistry', ['registryType', 'registryId']));
}
let registryType = args[0];
let registryId = args[1];
return context.getRegistryManager().get(registryType, registryId)
.then((result) => {
LOG.exit(method, result);
return result;
});
}
/**
* Determine whether an asset registry exists.
* @param {Context} context The request context.
* @param {string[]} args The arguments to pass to the chaincode function.
* @return {Promise} A promise that will be resolved with a boolean indicating
* whether the asset registry exists.
*/
existsRegistry(context, args) {
const method = 'existsRegistry';
LOG.entry(method, context, args);
if (args.length !== 2) {
LOG.error(method, 'Invalid arguments', args);
throw new Error(util.format('Invalid arguments "%j" to function "%s", expecting "%j"', args, 'existsRegistry', ['registryType', 'registryId']));
}
let registryType = args[0];
let registryId = args[1];
return context.getRegistryManager().exists(registryType, registryId)
.then((result) => {
LOG.exit(method, result);
return result;
});
}
/**
* Add a new registry.
* @param {Context} context The request context.
* @param {string[]} args The arguments to pass to the chaincode function.
* @return {Promise} A promise that will be resolved when complete, or rejected
* with an error.
*/
addRegistry(context, args) {
const method = 'addRegistry';
LOG.entry(method, context, args);
if (args.length !== 3) {
LOG.error(method, 'Invalid arguments', args);
throw new Error(util.format('Invalid arguments "%j" to function "%s", expecting "%j"', args, 'addRegistry', ['registryType', 'registryId', 'registryName']));
}
let registryType = args[0];
let registryId = args[1];
let registryName = args[2];
LOG.debug(method,'Adding registry',registryType,registryId,registryName);
return context.getRegistryManager().add(registryType, registryId, registryName)
.then(() => {
LOG.exit(method);
});
}
}
module.exports = EngineRegistries;