1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const types_1 = __importDefault(require("../types"));
|
7 | const datalayer_libs_1 = require("./datalayer-libs");
|
8 | const middleware_component_1 = __importDefault(require("../middleware/middleware-component"));
|
9 | const graphql_1 = require("graphql");
|
10 | exports.ENTRY_INSTANCE_TYPE = "EntryComponent";
|
11 | ;
|
12 | exports.createEntryProps = (props) => {
|
13 | const entryProps = {
|
14 | createEntryFields: () => {
|
15 | const fields = Object.keys(props.data).reduce((result, key) => {
|
16 | result[key] = { type: props.data[key] };
|
17 | return result;
|
18 | }, {});
|
19 | if (props.primaryKey) {
|
20 | fields[props.primaryKey] = { type: graphql_1.GraphQLString };
|
21 | }
|
22 | if (props.rangeKey) {
|
23 | fields[props.rangeKey] = { type: graphql_1.GraphQLString };
|
24 | }
|
25 | return fields;
|
26 | },
|
27 | createEntryType: (prefix) => {
|
28 | return new graphql_1.GraphQLObjectType({
|
29 | name: prefix + props.id,
|
30 | fields: () => entryProps.createEntryFields()
|
31 | });
|
32 | },
|
33 | createKeyArgs: () => {
|
34 | const args = {};
|
35 | if (props.primaryKey) {
|
36 | args[props.primaryKey] = { name: props.primaryKey, type: graphql_1.GraphQLString };
|
37 | }
|
38 | if (props.rangeKey) {
|
39 | args[props.rangeKey] = { name: props.rangeKey, type: graphql_1.GraphQLString };
|
40 | }
|
41 | return args;
|
42 | },
|
43 | createEntryArgs: () => {
|
44 | const args = Object.keys(props.data).reduce((result, key) => {
|
45 | result[key] = { name: key, type: graphql_1.GraphQLString };
|
46 | return result;
|
47 | }, {});
|
48 | if (props.primaryKey) {
|
49 | args[props.primaryKey] = { name: props.primaryKey, type: graphql_1.GraphQLString };
|
50 | }
|
51 | if (props.rangeKey) {
|
52 | args[props.rangeKey] = { name: props.rangeKey, type: graphql_1.GraphQLString };
|
53 | }
|
54 | return args;
|
55 | },
|
56 | getEntryListQuery: (dictKey) => {
|
57 | const fields = entryProps.createEntryFields();
|
58 |
|
59 | return datalayer_libs_1.getEntryListQuery(props.id, dictKey, fields);
|
60 | },
|
61 | getEntryQuery: (dictKey) => {
|
62 | const fields = entryProps.createEntryFields();
|
63 |
|
64 | return datalayer_libs_1.getEntryQuery(props.id, dictKey, fields);
|
65 | },
|
66 | getEntryScanQuery: (dictKey) => {
|
67 | const fields = entryProps.createEntryFields();
|
68 |
|
69 | return datalayer_libs_1.getEntryScanQuery(props.id, dictKey, fields);
|
70 | },
|
71 | setEntryMutation: (values) => {
|
72 | const fields = entryProps.createEntryFields();
|
73 |
|
74 | return datalayer_libs_1.setEntryMutation(props.id, values, fields);
|
75 | },
|
76 | setEntry: (args, context, tableName, isOffline) => {
|
77 |
|
78 | return datalayer_libs_1.setEntry(tableName,
|
79 | props.primaryKey,
|
80 | args[props.primaryKey],
|
81 | props.rangeKey,
|
82 | args[props.rangeKey],
|
83 | Object.keys(args).reduce((result, key) => {
|
84 | if (Object.keys(props.data).find(datakey => datakey === key) !== undefined) {
|
85 | result[key] = args[key];
|
86 | }
|
87 | return result;
|
88 | }, {}),
|
89 | isOffline
|
90 | );
|
91 | },
|
92 | listEntries: (args, context, tableName, key, isOffline) => {
|
93 | const entity = key === "pk" ? props.primaryKey : props.rangeKey;
|
94 | const range = key === "pk" ? props.rangeKey : props.primaryKey;
|
95 |
|
96 | return datalayer_libs_1.ddbListEntries(tableName,
|
97 | key,
|
98 | entity,
|
99 | args[entity],
|
100 | range,
|
101 | isOffline).then(results => {
|
102 |
|
103 | return results.map(item => {
|
104 | const data = item.jsonData !== undefined ? JSON.parse(item.jsonData) : {};
|
105 | data[props.primaryKey] = item.pk.substring(item.pk.indexOf("|") + 1);
|
106 | data[props.rangeKey] = item.sk.substring(item.sk.indexOf("|") + 1);
|
107 | return data;
|
108 | });
|
109 | });
|
110 | },
|
111 | getEntry: (args, context, tableName, isOffline) => {
|
112 | return datalayer_libs_1.ddbGetEntry(tableName,
|
113 | props.primaryKey,
|
114 | args[props.primaryKey],
|
115 | props.rangeKey,
|
116 | args[props.rangeKey],
|
117 | isOffline).then((result) => {
|
118 |
|
119 | const data = result.jsonData !== undefined ? JSON.parse(result.jsonData) : {};
|
120 | if (result && result.pk && result.sk) {
|
121 | data[props.primaryKey] = result.pk.substring(result.pk.indexOf("|") + 1);
|
122 | data[props.rangeKey] = result.sk.substring(result.sk.indexOf("|") + 1);
|
123 | }
|
124 | return data;
|
125 | });
|
126 | },
|
127 | scan: (args, context, tableName, key, isOffline) => {
|
128 |
|
129 | return datalayer_libs_1.ddbScan(tableName,
|
130 | key,
|
131 | key === "pk" ? props.primaryKey : props.rangeKey,
|
132 | args.scanall ? undefined : args[`start_${key === "pk" ? props.primaryKey : props.rangeKey}`],
|
133 | args.scanall ? undefined : args[`end_${key === "pk" ? props.primaryKey : props.rangeKey}`],
|
134 | key === "pk" ? props.rangeKey : props.primaryKey,
|
135 | isOffline).then((result) => {
|
136 |
|
137 | return result.map(entry => {
|
138 |
|
139 | const data = entry.jsonData !== undefined ? JSON.parse(entry.jsonData) : {};
|
140 | if (entry && entry.pk && entry.sk) {
|
141 | data[props.primaryKey] = entry.pk.substring(entry.pk.indexOf("|") + 1);
|
142 | data[props.rangeKey] = entry.sk.substring(entry.sk.indexOf("|") + 1);
|
143 | }
|
144 |
|
145 | return data;
|
146 | });
|
147 | });
|
148 | },
|
149 | deleteEntryMutation: (values) => {
|
150 | const fields = entryProps.createEntryFields();
|
151 |
|
152 |
|
153 | return datalayer_libs_1.deleteEntryMutation(props.id, values, fields);
|
154 | },
|
155 | deleteEntry: (args, context, tableName, isOffline) => {
|
156 | return datalayer_libs_1.deleteEntry(tableName,
|
157 | props.primaryKey,
|
158 | args[props.primaryKey],
|
159 | props.rangeKey,
|
160 | args[props.rangeKey],
|
161 | isOffline);
|
162 | },
|
163 | middleware: middleware_component_1.default({ callback: (req, res, next) => {
|
164 |
|
165 | return next();
|
166 | } }),
|
167 | getPrimaryListQueryName: () => "list_" + props.id + "_" + props.primaryKey,
|
168 | getRangeListQueryName: () => "list_" + props.id + "_" + props.rangeKey,
|
169 | getGetQueryName: () => "get_" + props.id,
|
170 | getSetMutationName: () => "set_" + props.id,
|
171 | getDeleteMutationName: () => "delete_" + props.id,
|
172 | getPrimaryScanName: () => "scan_" + props.id + "_" + props.primaryKey,
|
173 | getRangeScanName: () => "scan_" + props.id + "_" + props.rangeKey,
|
174 | getScanName: () => "scan_" + props.id,
|
175 | |
176 |
|
177 |
|
178 |
|
179 | providesQuery: (name) => {
|
180 | const result = name === entryProps.getPrimaryListQueryName() ||
|
181 | name === entryProps.getRangeListQueryName() ||
|
182 | name === entryProps.getSetMutationName() ||
|
183 | name === entryProps.getDeleteMutationName() ||
|
184 | name === entryProps.getPrimaryScanName() ||
|
185 | name === entryProps.getRangeScanName() ||
|
186 | name === entryProps.getScanName();
|
187 |
|
188 | return result;
|
189 | }
|
190 | };
|
191 | return Object.assign({}, props, entryProps);
|
192 | };
|
193 |
|
194 |
|
195 |
|
196 | exports.default = (props) => {
|
197 |
|
198 | const componentProps = {
|
199 | infrastructureType: types_1.default.INFRASTRUCTURE_TYPE_COMPONENT,
|
200 | instanceType: exports.ENTRY_INSTANCE_TYPE,
|
201 | instanceId: props.id
|
202 | };
|
203 | return exports.createEntryProps(Object.assign({}, props, componentProps));
|
204 | };
|
205 | exports.isEntry = (component) => {
|
206 | return component !== undefined && component.instanceType === exports.ENTRY_INSTANCE_TYPE;
|
207 | };
|
208 |
|
\ | No newline at end of file |