-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempClass.ts
95 lines (90 loc) · 2.47 KB
/
TempClass.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* add headers here */
import { omit } from 'lodash';
import * as mongoose from 'mongoose';
import {
connectionSearch,
elasticCreateCross,
elasticUpdateCross,
} from '../ElasticSearch';
import { formatArgs } from '../Pagination/types/PaginationType';
import [TableName]Model from './[TableName]Model';
import { I[TableName]InputType } from './types/[TableName]InputType';
class [TableName] {
public static async find(_: Common.Viewer, args: object) {
return [TableName]Model.find(args);
}
public static async findOne(_: Common.Viewer, args: object) {
return [TableName]Model.findOne(args);
}
public static async findById(
_: Common.Viewer,
id: string | mongoose.Types.ObjectId,
) {
return [TableName]Model.findById(id);
}
public static async findRawById(id: string | mongoose.Types.ObjectId) {
const [tableName] = await [TableName]Model.findById(id, '-__v');
if (![tableName]) {
throw new Error(/*add error message here*/);
}
const [tableName]Json = {
...omit([tableName].toJSON(), '_id'),
id: [tableName].id,
};
return [tableName]Json;
}
public static async findCrossById(id: string | mongoose.Types.ObjectId) {
const [tableName]Json = await this.findRawById(id);
return {
...[tableName]Json,
};
}
public static async createOne(
viewer: Common.Viewer,
input: I[TableName]InputType,
) {
if (!viewer) {
throw new Error('Unauthorized');
}
try {
/* add restriction here */
const [tableName]Doc = new [TableName]Model({
...input,
});
try {
const [tableName] = await [tableName]Doc.save();
await elasticCreateCross('[tablename]', [tableName]Doc.id);
return [tableName];
} catch (e) {
throw e;
}
} catch (e) {
throw e;
}
}
public static async updateById(
viewer: Common.Viewer,
id: string | mongoose.Types.ObjectId,
[tableName]Input: I[TableName]InputType,
) {
const [tableName] = await [TableName]Model.findByIdAndUpdate(
id,
{ $set: [tableName]Input },
{ new: true },
);
await elasticUpdateCross('[tablename]', id);
return [tableName];
}
public static async connectionSearch(
viewer: Common.Viewer,
args: Common.PageArgs,
) {
const newArgs = formatArgs(args);
const data = await connectionSearch(newArgs, {
index: '[tablename]',
type: '[tablename]',
});
return data;
}
}
export default [TableName];