@coderich/autograph 0.10.14 → 0.10.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/autograph",
3
3
  "author": "Richard Livolsi (coderich)",
4
- "version": "0.10.14",
4
+ "version": "0.10.15",
5
5
  "description": "AutoGraph",
6
6
  "keywords": [
7
7
  "graphql",
@@ -30,11 +30,11 @@
30
30
  "ratchet": "ratchet"
31
31
  },
32
32
  "dependencies": {
33
+ "@coderich/util": "^0.1.0",
33
34
  "@hapi/boom": "^9.1.0",
34
35
  "dataloader": "^2.0.0",
35
36
  "deepmerge": "^4.2.2",
36
37
  "fill-range": "^7.0.1",
37
- "flat": "^5.0.2",
38
38
  "glob": "^7.1.6",
39
39
  "graphql-fields": "^2.0.3",
40
40
  "lodash": "^4.17.21",
package/src/data/Model.js CHANGED
@@ -1,4 +1,6 @@
1
1
  const Stream = require('stream');
2
+ const { get } = require('lodash');
3
+ const { flatten } = require('@coderich/util');
2
4
  const Field = require('./Field');
3
5
  const Model = require('../graphql/ast/Model');
4
6
  const { eventEmitter } = require('../service/event.service');
@@ -129,13 +131,14 @@ module.exports = class extends Model {
129
131
  shape.model = this;
130
132
  shape.serdes = serdes;
131
133
  shape.target = target;
134
+ // console.log(shape.modelRef);
132
135
 
133
136
  // Cache and return
134
137
  this.shapesCache.set(cacheKey, shape);
135
138
  return shape;
136
139
  }
137
140
 
138
- shapeObject(shape, obj, query, root, base) {
141
+ shapeObject(shape, obj, query, root, base, toFlat = false) {
139
142
  const { serdes, model } = shape;
140
143
  const { context, resolver, doc = {}, flags = {} } = query.toObject();
141
144
  const { pipeline } = flags;
@@ -170,7 +173,13 @@ module.exports = class extends Model {
170
173
  if (!instructed && subShape && typeof transformedValue !== 'object') return prev;
171
174
 
172
175
  // Rename key & assign value
173
- prev[to] = (!subShape || transformedValue == null) ? transformedValue : this.shapeObject(subShape, transformedValue, query, root, base);
176
+ prev[to] = (!subShape || transformedValue == null) ? transformedValue : this.shapeObject(subShape, transformedValue, query, root, base, toFlat);
177
+
178
+ if (toFlat && get(doc, to) && field.getModelRef()) {
179
+ const val = prev[to];
180
+ delete prev[to];
181
+ Object.assign(prev, flatten({ [to]: val }, { safe: true }));
182
+ }
174
183
 
175
184
  return prev;
176
185
  }, {});
@@ -1,6 +1,6 @@
1
1
  const Util = require('util');
2
- const Flat = require('flat');
3
2
  const { get } = require('lodash');
3
+ const { unflatten } = require('@coderich/util');
4
4
  const { MongoClient, ObjectId } = require('mongodb');
5
5
  const { map, ensureArray, proxyDeep, toKeyObj, globToRegex, proxyPromise, isScalarDataType, promiseRetry } = require('../service/app.service');
6
6
 
@@ -71,8 +71,8 @@ module.exports = class MongoDriver {
71
71
  }
72
72
 
73
73
  updateOne({ model, where, $doc, options, flags }) {
74
- const $update = { $set: Flat.flatten($doc, { safe: true }) };
75
- return this.query(model, 'updateOne', where, $update, options, flags).then(() => $doc);
74
+ const $update = { $set: $doc };
75
+ return this.query(model, 'updateOne', where, $update, options, flags).then(() => unflatten($doc));
76
76
  }
77
77
 
78
78
  deleteOne({ model, where, options, flags }) {
@@ -86,7 +86,8 @@ module.exports = class QueryResolver {
86
86
  return createSystemEvent('Mutation', { query: query.doc(doc).merged(merged) }, async () => {
87
87
  const payload = model.shapeObject(inputShape, merged, query);
88
88
  await model.validateObject(inputShape, payload, query.payload(payload));
89
- return this.resolver.resolve(query.$doc(model.shapeObject(docShape, payload, query)));
89
+ const $doc = model.shapeObject(docShape, payload, query, undefined, undefined, true);
90
+ return this.resolver.resolve(query.$doc($doc));
90
91
  });
91
92
  });
92
93
  }