@paralect/hive 0.1.12 → 0.1.13

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@paralect/hive",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -1,21 +1,10 @@
1
1
  import _ from "lodash";
2
2
  import db from "db";
3
3
  import ifUpdated from "helpers/db/ifUpdated";
4
- import schemaMappings from "./schemaMappings.js";
4
+ import schemaMappings from "./schemaMappings";
5
+ import getDependentFields from './getDependentFields';
5
6
  import { ZodArray } from 'zod';
6
7
 
7
- const getDependentFields = (schema, dependentFieldName) => {
8
- let targetSchema = schema.shape[dependentFieldName];
9
-
10
- if (targetSchema instanceof ZodArray) {
11
- targetSchema = targetSchema.element;
12
- }
13
-
14
- return Object.keys(targetSchema.shape).filter(
15
- (key) => !_.includes(['_id', 'createdOn', 'updatedOn'], key)
16
- );
17
- };
18
-
19
8
  const updatedSchemaMappings = (() => {
20
9
  const result = {};
21
10
  const schemaNames = Object.keys(schemaMappings);
@@ -0,0 +1,33 @@
1
+ import { z } from 'zod';
2
+
3
+ const getZodKeys = schema => {
4
+ // make sure schema is not null or undefined
5
+ if (schema === null || schema === undefined) return [];
6
+ // check if schema is nullable or optional
7
+ if (schema instanceof z.ZodNullable || schema instanceof z.ZodOptional) return getZodKeys(schema.unwrap());
8
+ // check if schema is an array
9
+ if (schema instanceof z.ZodArray) return getZodKeys(schema.element);
10
+ // check if schema is an object
11
+ if (schema instanceof z.ZodObject) {
12
+ // get key/value pairs from schema
13
+ const entries = Object.entries(schema.shape);
14
+ // loop through key/value pairs
15
+ return entries.flatMap(([key, value]) => {
16
+ // get nested keys
17
+ const nested = value instanceof z.ZodType ? getZodKeys(value).map(subKey => `${key}.${subKey}`) : [];
18
+ // return nested keys
19
+ return nested.length ? nested : key;
20
+ });
21
+ }
22
+ // return empty array
23
+ return [];
24
+ };
25
+
26
+ export default (schema, dependentFieldName) => {
27
+ let targetSchema = schema.shape[dependentFieldName];
28
+ let zodKeys = getZodKeys(targetSchema)
29
+
30
+ return zodKeys.filter(
31
+ (key) => !_.includes(['_id', 'createdOn', 'updatedOn'], key)
32
+ );
33
+ };
@@ -1,20 +1,13 @@
1
1
  import _ from "lodash";
2
2
  import db from "db";
3
- import schemaMappings from "./schemaMappings.js";
3
+ import fs from 'fs';
4
+ import { z, ZodArray } from 'zod';
4
5
 
5
- const schemaMappingService = db.services.schemaMappings;
6
-
7
- const getDependentFields = (schema, dependentFieldName) => {
8
- let targetSchema = schema.shape[dependentFieldName].shape;
6
+ import schemaMappings from "./schemaMappings";
7
+ import getDependentFields from './getDependentFields';
9
8
 
10
- if (targetSchema instanceof ZodArray) {
11
- targetSchema = targetSchema.element;
12
- }
9
+ const schemaMappingService = db.services.schemaMappings;
13
10
 
14
- return Object.keys(targetSchema).filter(
15
- (key) => !_.includes(['_id', 'createdOn', 'updatedOn'], key)
16
- );
17
- };
18
11
  const zodSchemaToSchemaMappings = () => {
19
12
  const newSchemaMappings = {};
20
13
 
@@ -30,7 +23,7 @@ const zodSchemaToSchemaMappings = () => {
30
23
  };
31
24
  });
32
25
  });
33
-
26
+ console.log('newSchemaMappings', newSchemaMappings)
34
27
  return newSchemaMappings;
35
28
  };
36
29
 
@@ -70,7 +63,7 @@ export default async () => {
70
63
  );
71
64
  await Promise.all(
72
65
  uniqueDependentEntities.map(async (entity) => {
73
- if (schema[fieldName].type === "array") {
66
+ if (schema[fieldName] instanceof ZodArray) {
74
67
  await db.services[schemaName].atomic.update(
75
68
  { [`${fieldName}._id`]: entity._id },
76
69
  {
@@ -1,7 +1,13 @@
1
- export default {
2
- // contacts: {
3
- // master: {
4
- // schema: 'users',
5
- // },
6
- // },
7
- };
1
+ import fs from 'fs';
2
+
3
+ let schemaMappings = {};
4
+
5
+ if (fs.existsSync('./schemaMappings.json')) {
6
+ schemaMappings = JSON.parse(fs.readFileSync('./schemaMappings.json', 'utf8') || '{}');
7
+ }
8
+
9
+ if (process.env.HIVE_SRC && fs.existsSync(`${process.env.HIVE_SRC}/automap/schemaMappings.json`)) {
10
+ schemaMappings = JSON.parse(fs.readFileSync(`${process.env.HIVE_SRC}/automap/schemaMappings.json`, 'utf8') || '{}');
11
+ }
12
+
13
+ export default schemaMappings;
@@ -0,0 +1,3 @@
1
+ {
2
+
3
+ }