@paralect/hive 0.1.4 → 0.1.6
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 +1 -1
- package/starter/src/app-config/index.js +4 -0
- package/starter/src/autoMap/addHandlers.js +16 -12
- package/starter/src/autoMap/mapSchema.js +17 -11
- package/starter/src/db.js +3 -2
- package/starter/src/helpers/schema/db.schema.js +10 -0
- package/starter/src/helpers/schema/pagination.schema.js +14 -0
- package/starter/src/migrations/migrations-log/migration-log.schema.js +2 -5
- package/starter/src/migrations/migrations.schema.js +2 -6
- package/starter/src/resources/schemaMappings/schemaMappings.schema.js +3 -6
- package/starter/src/resources/tokens/tokens.schema.js +2 -1
- package/starter/src/resources/users/users.schema.js +2 -5
- package/starter/src/scheduler.js +23 -12
package/package.json
CHANGED
|
@@ -8,6 +8,10 @@ dotenv.config({ path: `${__dirname}/.env.app` });
|
|
|
8
8
|
console.log('process.env.HIVE_SRC', process.env.HIVE_SRC)
|
|
9
9
|
|
|
10
10
|
if (process.env.HIVE_SRC) {
|
|
11
|
+
dotenv.config({
|
|
12
|
+
path: `${process.env.HIVE_SRC}/app-config/.env`,
|
|
13
|
+
});
|
|
14
|
+
|
|
11
15
|
dotenv.config({
|
|
12
16
|
path: `${process.env.HIVE_SRC}/app-config/.env.app`,
|
|
13
17
|
});
|
|
@@ -2,14 +2,17 @@ import _ from "lodash";
|
|
|
2
2
|
import db from "db";
|
|
3
3
|
import ifUpdated from "helpers/db/ifUpdated";
|
|
4
4
|
import schemaMappings from "./schemaMappings.js";
|
|
5
|
+
import { ZodArray } from 'zod';
|
|
5
6
|
|
|
6
7
|
const getDependentFields = (schema, dependentFieldName) => {
|
|
7
|
-
let targetSchema = schema[dependentFieldName];
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
let targetSchema = schema.shape[dependentFieldName];
|
|
9
|
+
|
|
10
|
+
if (targetSchema instanceof ZodArray) {
|
|
11
|
+
targetSchema = targetSchema.element;
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
|
|
14
|
+
return Object.keys(targetSchema.shape).filter(
|
|
15
|
+
(key) => !_.includes(['_id', 'createdOn', 'updatedOn'], key)
|
|
13
16
|
);
|
|
14
17
|
};
|
|
15
18
|
|
|
@@ -18,7 +21,7 @@ const updatedSchemaMappings = (() => {
|
|
|
18
21
|
const schemaNames = Object.keys(schemaMappings);
|
|
19
22
|
schemaNames.forEach((schemaName) => {
|
|
20
23
|
const dependentFieldNames = Object.keys(schemaMappings[schemaName]);
|
|
21
|
-
const schema = db.schemas[schemaName]
|
|
24
|
+
const schema = db.schemas[schemaName];
|
|
22
25
|
dependentFieldNames.forEach((dependentFieldName) => {
|
|
23
26
|
const dependentFields = getDependentFields(schema, dependentFieldName);
|
|
24
27
|
if (!_.isEmpty(dependentFields)) {
|
|
@@ -29,6 +32,7 @@ const updatedSchemaMappings = (() => {
|
|
|
29
32
|
};
|
|
30
33
|
}
|
|
31
34
|
});
|
|
35
|
+
|
|
32
36
|
});
|
|
33
37
|
return result;
|
|
34
38
|
})();
|
|
@@ -104,29 +108,29 @@ const addOnDependentEntitiesUpdatedHandlers = ({ schemaName }) => {
|
|
|
104
108
|
dependentFieldNames.forEach((dependentFieldName) => {
|
|
105
109
|
const dependentFieldSchemaName =
|
|
106
110
|
schemaMappings[schemaName][dependentFieldName].schema;
|
|
107
|
-
const schema = db.schemas[schemaName]
|
|
111
|
+
const schema = db.schemas[schemaName];
|
|
108
112
|
const dependentFields = getDependentFields(schema, dependentFieldName);
|
|
109
113
|
db.services[dependentFieldSchemaName].on(
|
|
110
114
|
"updated",
|
|
111
115
|
ifUpdated(dependentFields, async ({ doc }) => {
|
|
112
116
|
const toUpdate = _.pick(doc, ["_id", ...dependentFields]);
|
|
113
|
-
if (schema[dependentFieldName]
|
|
114
|
-
db.services[schemaName].atomic.
|
|
117
|
+
if (schema.shape[dependentFieldName] instanceof ZodArray) {
|
|
118
|
+
db.services[schemaName].atomic.updateOne(
|
|
115
119
|
{ [`${dependentFieldName}._id`]: doc._id },
|
|
116
120
|
{
|
|
117
121
|
$set: {
|
|
118
122
|
[`${dependentFieldName}.$`]: toUpdate,
|
|
119
123
|
},
|
|
120
|
-
}
|
|
124
|
+
},
|
|
121
125
|
);
|
|
122
126
|
} else {
|
|
123
|
-
db.services[schemaName].atomic.
|
|
127
|
+
db.services[schemaName].atomic.updateOne(
|
|
124
128
|
{ [`${dependentFieldName}._id`]: doc._id },
|
|
125
129
|
{
|
|
126
130
|
$set: {
|
|
127
131
|
[`${dependentFieldName}`]: toUpdate,
|
|
128
132
|
},
|
|
129
|
-
}
|
|
133
|
+
},
|
|
130
134
|
);
|
|
131
135
|
}
|
|
132
136
|
})
|
|
@@ -5,20 +5,24 @@ import schemaMappings from "./schemaMappings.js";
|
|
|
5
5
|
const schemaMappingService = db.services.schemaMappings;
|
|
6
6
|
|
|
7
7
|
const getDependentFields = (schema, dependentFieldName) => {
|
|
8
|
-
let targetSchema = schema[dependentFieldName];
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
let targetSchema = schema.shape[dependentFieldName].shape;
|
|
9
|
+
|
|
10
|
+
if (targetSchema instanceof ZodArray) {
|
|
11
|
+
targetSchema = targetSchema.element;
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
return Object.keys(targetSchema).filter(
|
|
15
|
+
(key) => !_.includes(['_id', 'createdOn', 'updatedOn'], key)
|
|
14
16
|
);
|
|
15
17
|
};
|
|
16
|
-
|
|
17
|
-
const joiSchemaToSchemaMappings = () => {
|
|
18
|
+
const zodSchemaToSchemaMappings = () => {
|
|
18
19
|
const newSchemaMappings = {};
|
|
20
|
+
|
|
19
21
|
Object.keys(schemaMappings).forEach((schemaName) => {
|
|
20
|
-
const schema = db.schemas[schemaName]
|
|
22
|
+
const schema = db.schemas[schemaName];
|
|
23
|
+
|
|
21
24
|
newSchemaMappings[schemaName] = {};
|
|
25
|
+
|
|
22
26
|
Object.keys(schemaMappings[schemaName]).forEach((fieldName) => {
|
|
23
27
|
newSchemaMappings[schemaName][fieldName] = {
|
|
24
28
|
schema: schemaMappings[schemaName][fieldName].schema,
|
|
@@ -26,6 +30,7 @@ const joiSchemaToSchemaMappings = () => {
|
|
|
26
30
|
};
|
|
27
31
|
});
|
|
28
32
|
});
|
|
33
|
+
|
|
29
34
|
return newSchemaMappings;
|
|
30
35
|
};
|
|
31
36
|
|
|
@@ -33,13 +38,14 @@ export default async () => {
|
|
|
33
38
|
const prevSchema = await schemaMappingService.findOne({});
|
|
34
39
|
const prevSchemaMappings = prevSchema?.mappings;
|
|
35
40
|
if (!prevSchemaMappings) {
|
|
36
|
-
const initialSchemaMappings =
|
|
41
|
+
const initialSchemaMappings = zodSchemaToSchemaMappings();
|
|
37
42
|
schemaMappingService.create({ mappings: initialSchemaMappings });
|
|
38
43
|
} else {
|
|
39
44
|
const schemaNames = Object.keys(schemaMappings);
|
|
40
45
|
await Promise.all(
|
|
41
46
|
schemaNames.map(async (schemaName) => {
|
|
42
|
-
const schema = db.schemas[schemaName]
|
|
47
|
+
const schema = db.schemas[schemaName];
|
|
48
|
+
|
|
43
49
|
const fieldNames = Object.keys(schemaMappings[schemaName]);
|
|
44
50
|
await Promise.all(
|
|
45
51
|
fieldNames.map(async (fieldName) => {
|
|
@@ -88,7 +94,7 @@ export default async () => {
|
|
|
88
94
|
);
|
|
89
95
|
schemaMappingService.atomic.update(
|
|
90
96
|
{ _id: prevSchema._id },
|
|
91
|
-
{ $set: { mappings:
|
|
97
|
+
{ $set: { mappings: zodSchemaToSchemaMappings() } }
|
|
92
98
|
);
|
|
93
99
|
}
|
|
94
100
|
};
|
package/starter/src/db.js
CHANGED
|
@@ -11,7 +11,6 @@ const db = connect(config.mongoUri);
|
|
|
11
11
|
db.services = {};
|
|
12
12
|
db.schemas = {};
|
|
13
13
|
|
|
14
|
-
|
|
15
14
|
db.init = async () => {
|
|
16
15
|
const schemaPaths = await getSchemas();
|
|
17
16
|
|
|
@@ -29,7 +28,9 @@ db.init = async () => {
|
|
|
29
28
|
}
|
|
30
29
|
db.schemas[schemaName] = schema;
|
|
31
30
|
db.services[schemaName] = db.createService(`${resourceName}`, {
|
|
32
|
-
validate: (obj) =>
|
|
31
|
+
validate: (obj) => {
|
|
32
|
+
return { value: schema.passthrough().parse(obj) };
|
|
33
|
+
},
|
|
33
34
|
});
|
|
34
35
|
|
|
35
36
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
export const paginationSchema = z.object({
|
|
4
|
+
page: z.coerce.number().default(1),
|
|
5
|
+
perPage: z.coerce.number().default(10),
|
|
6
|
+
|
|
7
|
+
searchValue: z.string().optional(),
|
|
8
|
+
|
|
9
|
+
sort: z
|
|
10
|
+
.object({
|
|
11
|
+
createdOn: z.enum(['asc', 'desc']).default('asc'),
|
|
12
|
+
})
|
|
13
|
+
.default({}),
|
|
14
|
+
});
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import dbSchema from 'helpers/schema/db.schema';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
_id: z.string(),
|
|
5
|
-
createdOn: z.date(),
|
|
6
|
-
updatedOn: z.date(),
|
|
4
|
+
export default dbSchema.extend({
|
|
7
5
|
startTime: z.date(),
|
|
8
6
|
finishTime: z.date().optional(),
|
|
9
7
|
status: z.string(),
|
|
@@ -13,4 +11,3 @@ const schema = z.object({
|
|
|
13
11
|
migrationVersion: z.number(),
|
|
14
12
|
});
|
|
15
13
|
|
|
16
|
-
export default schema;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import dbSchema from 'helpers/schema/db.schema';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
_id: z.string(),
|
|
5
|
-
createdOn: z.date(),
|
|
6
|
-
updatedOn: z.date(),
|
|
4
|
+
export default dbSchema.extend({
|
|
7
5
|
version: z.number(),
|
|
8
6
|
});
|
|
9
|
-
|
|
10
|
-
export default schema;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import dbSchema from 'helpers/schema/db.schema';
|
|
2
3
|
|
|
3
|
-
export default
|
|
4
|
-
|
|
5
|
-
createdOn: z.date(),
|
|
6
|
-
updatedOn: z.date(),
|
|
7
|
-
|
|
8
|
-
mappings: z.object(),
|
|
4
|
+
export default dbSchema.extend({
|
|
5
|
+
mappings: z.object({}),
|
|
9
6
|
});
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import dbSchema from 'helpers/schema/db.schema';
|
|
2
3
|
|
|
3
|
-
const users =
|
|
4
|
-
_id: z.string(),
|
|
5
|
-
createdOn: z.date(),
|
|
6
|
-
updatedOn: z.date(),
|
|
7
|
-
|
|
4
|
+
const users = dbSchema.extend({
|
|
8
5
|
email: z.string().email(),
|
|
9
6
|
fullName: z.string(),
|
|
10
7
|
avatarUrl: z.string().url().optional(),
|
package/starter/src/scheduler.js
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
1
3
|
import moment from 'moment';
|
|
2
4
|
import schedule from 'node-schedule';
|
|
3
5
|
import requireDir from 'require-dir';
|
|
4
6
|
|
|
5
7
|
export default () => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
);
|
|
8
|
+
const paths = [path.resolve(__dirname, './scheduler/handlers')];
|
|
9
|
+
if (process.env.HIVE_SRC) {
|
|
10
|
+
paths.push(path.resolve(process.env.HIVE_SRC, './scheduler/handlers'))
|
|
11
|
+
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
paths.forEach((pathName) => {
|
|
14
|
+
if (fs.existsSync(pathName)) {
|
|
15
|
+
requireDir(pathName, {
|
|
16
|
+
mapValue: (handler, handlerName) => {
|
|
17
|
+
console.log(
|
|
18
|
+
`[scheduler] Registering handler ${handlerName} with cron ${handler.cron}`
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
schedule.scheduleJob(handler.cron, () => {
|
|
22
|
+
console.log(
|
|
23
|
+
`[scheduler] ${moment().format()} executing ${handlerName} with cron ${handler.cron
|
|
24
|
+
}`
|
|
25
|
+
);
|
|
26
|
+
handler.handler();
|
|
27
|
+
});
|
|
28
|
+
},
|
|
18
29
|
});
|
|
19
|
-
}
|
|
30
|
+
}
|
|
20
31
|
});
|
|
21
32
|
}
|