|
1 | | -/* eslint-disable radix */ |
2 | | -/* eslint-disable no-unused-vars */ |
3 | | -/* eslint-disable no-undef */ |
4 | | -/* eslint-disable class-methods-use-this */ |
5 | | -/* eslint-disable no-param-reassign */ |
6 | | -/* eslint-disable no-use-before-define */ |
7 | | -/* eslint-disable prefer-destructuring */ |
8 | | -/* eslint-disable no-underscore-dangle */ |
9 | | -/* eslint-disable consistent-return */ |
10 | 1 | const Datastore = require('@google-cloud/datastore'); |
11 | 2 | const util = require('util'); |
12 | 3 | const Connector = require('loopback-connector').Connector; |
@@ -47,8 +38,13 @@ class GoogleCloudDatastore { |
47 | 38 | * @param {String} key The id value for this document |
48 | 39 | */ |
49 | 40 | createEntity(obj, key) { |
50 | | - const data = Object.assign(obj, { id: key.name }); |
51 | | - return { data, key }; |
| 41 | + const data = Object.assign(obj, { |
| 42 | + id: key.name, |
| 43 | + }); |
| 44 | + return { |
| 45 | + data, |
| 46 | + key, |
| 47 | + }; |
52 | 48 | } |
53 | 49 |
|
54 | 50 | /** |
@@ -164,12 +160,14 @@ class GoogleCloudDatastore { |
164 | 160 | * @param {Object} where The filter |
165 | 161 | */ |
166 | 162 | buildQuery(model, where) { |
167 | | - const filters = Object.keys(where).map(key => ({ [key]: where[key] })); |
| 163 | + const filters = Object.keys(where).map((key) => ({ |
| 164 | + [key]: where[key], |
| 165 | + })); |
168 | 166 |
|
169 | 167 | let query = this.db.createQuery(model); |
170 | 168 |
|
171 | 169 | for (let i = 0; i < filters.length; i += 1) { |
172 | | - query = this.addFilterToQuery(query, filter[i]); |
| 170 | + query = this.addFilterToQuery(query, filters[i]); |
173 | 171 | } |
174 | 172 |
|
175 | 173 | return query; |
@@ -240,8 +238,13 @@ class GoogleCloudDatastore { |
240 | 238 | */ |
241 | 239 | async findOrCreate(model, filter, data, options, callback) { |
242 | 240 | try { |
243 | | - const newData = Object.assign(data, { id: filter.where.id }); |
244 | | - const find = await this.findById(model, newData.id); |
| 241 | + const where = filter ? filter.where : undefined; |
| 242 | + const id = where ? where.id : 0; |
| 243 | + |
| 244 | + const newData = Object.assign(data, { |
| 245 | + id, |
| 246 | + }); |
| 247 | + const find = await this.findById(model, id); |
245 | 248 |
|
246 | 249 | if (find.length > 0) { |
247 | 250 | callback(null, find); |
@@ -321,7 +324,9 @@ class GoogleCloudDatastore { |
321 | 324 |
|
322 | 325 | if (!exist) throw new Error('Entity not found'); |
323 | 326 |
|
324 | | - const newData = Object.assign(data, { id }); |
| 327 | + const newData = Object.assign(data, { |
| 328 | + id, |
| 329 | + }); |
325 | 330 |
|
326 | 331 | await this.addOrUpdateEntity(model, newData, 'update'); |
327 | 332 |
|
@@ -376,6 +381,7 @@ class GoogleCloudDatastore { |
376 | 381 | */ |
377 | 382 | async updateOrCreate(model, data, options, callback) { |
378 | 383 | try { |
| 384 | + const id = data ? data.id : undefined; |
379 | 385 | const result = await this.updateInternal(model, id, data); |
380 | 386 |
|
381 | 387 | callback(null, result); |
@@ -445,7 +451,7 @@ class GoogleCloudDatastore { |
445 | 451 | */ |
446 | 452 | async destroyById(model, id, options, callback) { |
447 | 453 | try { |
448 | | - const result = await this.deleteData(model, entityId, null); |
| 454 | + const result = await this.deleteData(model, id, null); |
449 | 455 |
|
450 | 456 | callback(null, result); |
451 | 457 | } catch (error) { |
|
0 commit comments