1 | import 'babel-core/register'
|
2 | import 'babel-polyfill'
|
3 | import Promise from 'bluebird'
|
4 | import _ from 'lodash'
|
5 | import inspect from 'object-inspect'
|
6 | import log from 'loglevel'
|
7 |
|
8 | const nodeArgs = require('node-args')
|
9 |
|
10 |
|
11 | const originalFactory = log.methodFactory
|
12 | log.methodFactory = function (methodName, logLevel, loggerName) {
|
13 | const rawMethod = originalFactory(methodName, logLevel, loggerName)
|
14 | return (message, optionalData = null) => {
|
15 | let msg = message
|
16 | if (optionalData) {
|
17 | msg += ': ' + inspect(optionalData)
|
18 | }
|
19 | rawMethod(msg)
|
20 | }
|
21 | }
|
22 | log.setLevel(log.getLevel())
|
23 | export const logger = log
|
24 |
|
25 |
|
26 | export const initFlexSdk = defineService => {
|
27 | const sdk = require('kinvey-flex-sdk')
|
28 | const opts = nodeArgs.port ? {port: nodeArgs.port} : null
|
29 | sdk.service(opts, defineService)
|
30 | }
|
31 |
|
32 | export const getAsyncDataStore = (collectionName, modules) => {
|
33 | const store = modules.dataStore()
|
34 | const collection = store.collection(collectionName)
|
35 | return Promise.promisifyAll(collection)
|
36 | }
|
37 |
|
38 | export const getAsyncUserStore = modules => {
|
39 | const store = modules.userStore()
|
40 | return Promise.promisifyAll(store)
|
41 | }
|
42 |
|
43 | export const setLowercaseFields = (data, fields) => {
|
44 | if (_.isArray(fields) && _.isPlainObject(data)) {
|
45 | const result = {...data}
|
46 | _.forEach(fields, field => {
|
47 | if (_.isString(data[field])) {
|
48 | result[`${field}LC`] = _.toLower(data[field])
|
49 | }
|
50 | })
|
51 | return result
|
52 | }
|
53 | }
|
54 |
|
55 | export const findEntityById = (entityType, entityId, modules) => {
|
56 | const store = entityType === 'user'
|
57 | ? getAsyncUserStore(modules)
|
58 | : getAsyncDataStore(entityType, modules)
|
59 | return store.findByIdAsync(entityId)
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | export const getPotentialDenormalizedData = ({collectionName, newData, denormMap}) => _.chain(denormMap)
|
70 | .mapValues((val, key) => _.get(newData, key))
|
71 | .pickBy(val => !_.isEmpty(val))
|
72 | .value()
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | export const getFinalDenormalizedData = ({oldData, potentialDenormalizedData, denormMap}) => _.chain(potentialDenormalizedData)
|
82 | .pickBy((val, key) => val !== _.get(oldData, key))
|
83 | .mapKeys((val, key) => denormMap[key])
|
84 | .value()
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | export const getDenormalizedData = async ({collectionName, entityId, newData, denormMap, kinveyModules}) => {
|
99 | const relevantData = getPotentialDenormalizedData({collectionName, newData, denormMap})
|
100 | if (_.isEmpty(relevantData)) {
|
101 | return null
|
102 | }
|
103 | const oldData = await findEntityById(collectionName, entityId, kinveyModules)
|
104 | const denormData = getFinalDenormalizedData({oldData, relevantData, denormMap})
|
105 | return {
|
106 | oldData,
|
107 | denormData: _.isEmpty(denormData) ? null : denormData
|
108 | }
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | export const entityList = {
|
115 | findIndex: (arr, item) => _.findIndex(arr, existingItem => existingItem.id === item.id),
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | set: (arr = [], item) => {
|
123 | const result = Array.from(arr)
|
124 | const idx = entityList.findIndex(arr, item)
|
125 | if (idx === -1) {
|
126 | result.push(item)
|
127 | } else {
|
128 | result.splice(idx, 1, item)
|
129 | }
|
130 | return result
|
131 | },
|
132 | |
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | del: (arr = [], item) => {
|
139 | const result = Array.from(arr)
|
140 | const idx = entityList.findIndex(arr, item)
|
141 | if (idx !== -1) {
|
142 | result.splice(idx, 1)
|
143 | }
|
144 | return result
|
145 | }
|
146 | }
|