@simitgroup/simpleapp-generator 1.0.20 → 1.0.21
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/backend1/.env +5 -0
- package/backend1/package.json +6 -0
- package/backend1/pnpm-lock.yaml +428 -177
- package/backend1/src/class/SimpleAppController.js +38 -0
- package/backend1/src/class/SimpleAppController.js.map +1 -0
- package/backend1/src/class/SimpleAppController.ts +69 -0
- package/backend1/src/class/SimpleAppService.js +264 -0
- package/backend1/src/class/SimpleAppService.js.map +1 -0
- package/backend1/src/class/SimpleAppService.ts +213 -0
- package/backend1/src/main.js +13 -1
- package/backend1/src/main.js.map +1 -1
- package/backend1/src/main.ts +14 -2
- package/backend1/tsconfig.json +1 -21
- package/dist/framework.js +161 -0
- package/dist/framework.js.map +1 -0
- package/dist/generate.js +2 -2
- package/dist/generate.js.map +1 -1
- package/dist/index.js +44 -28
- package/dist/index.js.map +1 -1
- package/frontend1/README.md +63 -0
- package/frontend1/app.vue +8 -0
- package/frontend1/assets/css/tailwind.css +28 -0
- package/frontend1/components/CrudSimple.vue +112 -0
- package/frontend1/components/DebugDocumentData.vue +20 -0
- package/frontend1/components/EventMonitor.vue +79 -0
- package/frontend1/components/Menus.vue +18 -0
- package/frontend1/layouts/default.vue +10 -0
- package/frontend1/nuxt.config.js +42 -0
- package/frontend1/nuxt.config.js.map +1 -0
- package/frontend1/nuxt.config.ts +42 -0
- package/frontend1/package-lock.json +11877 -0
- package/frontend1/package.json +38 -0
- package/frontend1/pages/index.vue +3 -0
- package/frontend1/plugins/simpleapp.js +73 -0
- package/frontend1/plugins/simpleapp.js.map +1 -0
- package/frontend1/plugins/simpleapp.ts +73 -0
- package/frontend1/pnpm-lock.yaml +8339 -0
- package/frontend1/public/favicon.ico +0 -0
- package/frontend1/server/api/[...].js +117 -0
- package/frontend1/server/api/[...].js.map +1 -0
- package/frontend1/server/api/[...].ts +131 -0
- package/frontend1/server/tsconfig.json +3 -0
- package/frontend1/tailwind.config.js +10 -0
- package/frontend1/tailwind.config.js.map +1 -0
- package/frontend1/tailwind.config.ts +9 -0
- package/frontend1/tsconfig.json +4 -0
- package/package.json +2 -10
- package/src/framework.ts +133 -0
- package/src/generate.ts +2 -2
- package/src/index.ts +22 -42
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SimpleAppController = void 0;
|
|
4
|
+
const doctype = 'person'.toUpperCase();
|
|
5
|
+
// @ApiTags(doctype)
|
|
6
|
+
// @Controller(doctype.toLowerCase())
|
|
7
|
+
class SimpleAppController {
|
|
8
|
+
// protected apiSchemaClass=
|
|
9
|
+
constructor(service) {
|
|
10
|
+
this.service = service;
|
|
11
|
+
}
|
|
12
|
+
async _list() {
|
|
13
|
+
return this.service.list();
|
|
14
|
+
}
|
|
15
|
+
async _autocomplete(keyword) {
|
|
16
|
+
return this.service.getAutoComplete(keyword);
|
|
17
|
+
}
|
|
18
|
+
async _findOne(id) {
|
|
19
|
+
const result = (await this.service.findById(id));
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
async _create(data) {
|
|
23
|
+
//const newdata: persontype.Person = { ...data };
|
|
24
|
+
const newdata = {}; //= { ...data };
|
|
25
|
+
Object.assign(newdata, data); //
|
|
26
|
+
return this.service.setData(newdata).create();
|
|
27
|
+
}
|
|
28
|
+
async _update(id, data) {
|
|
29
|
+
const newdata = {}; //= { ...data };
|
|
30
|
+
Object.assign(newdata, data); //
|
|
31
|
+
return this.service.findIdThenUpdate(id, newdata);
|
|
32
|
+
}
|
|
33
|
+
async _delete(id) {
|
|
34
|
+
return this.service.findIdThenDelete(id);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.SimpleAppController = SimpleAppController;
|
|
38
|
+
//# sourceMappingURL=SimpleAppController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SimpleAppController.js","sour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AAgB,CAAC,EAAE,CAAC,CAAC;IAC3C,CAAC;CACF;AAzCD,kDAyCC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Controller,
|
|
3
|
+
Get,
|
|
4
|
+
Put,
|
|
5
|
+
Post,
|
|
6
|
+
Delete,
|
|
7
|
+
Body,
|
|
8
|
+
Param,
|
|
9
|
+
Type,
|
|
10
|
+
} from '@nestjs/common';
|
|
11
|
+
import { ApiTags, ApiBody, ApiResponse, ApiOperation } from '@nestjs/swagger';
|
|
12
|
+
|
|
13
|
+
const doctype = 'person'.toUpperCase();
|
|
14
|
+
type ServiceType = {
|
|
15
|
+
list: Function;
|
|
16
|
+
create: Function;
|
|
17
|
+
update: Function;
|
|
18
|
+
delete: Function;
|
|
19
|
+
findById: Function;
|
|
20
|
+
findIdThenDelete: Function;
|
|
21
|
+
findIdThenUpdate: Function;
|
|
22
|
+
setData: Function;
|
|
23
|
+
getAutoComplete: Function;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// @ApiTags(doctype)
|
|
27
|
+
// @Controller(doctype.toLowerCase())
|
|
28
|
+
export class SimpleAppController<TService extends ServiceType, TApiSchema, T> {
|
|
29
|
+
protected service: TService;
|
|
30
|
+
protected tryno: number;
|
|
31
|
+
// protected apiSchemaClass=
|
|
32
|
+
|
|
33
|
+
constructor(service: TService) {
|
|
34
|
+
this.service = service;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
async _list() {
|
|
39
|
+
return this.service.list();
|
|
40
|
+
}
|
|
41
|
+
async _autocomplete(keyword: string) {
|
|
42
|
+
return this.service.getAutoComplete(keyword);
|
|
43
|
+
}
|
|
44
|
+
async _findOne(id: string) {
|
|
45
|
+
const result = (await this.service.findById(id)) as TApiSchema;
|
|
46
|
+
|
|
47
|
+
return result as Type<TApiSchema>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
async _create(data: TApiSchema) {
|
|
52
|
+
//const newdata: persontype.Person = { ...data };
|
|
53
|
+
const newdata: T = {} as T; //= { ...data };
|
|
54
|
+
Object.assign(newdata, data); //
|
|
55
|
+
return this.service.setData(newdata).create() as TApiSchema;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
async _update( id: string,data: TApiSchema) {
|
|
60
|
+
const newdata: T = {} as T; //= { ...data };
|
|
61
|
+
Object.assign(newdata, data); //
|
|
62
|
+
return this.service.findIdThenUpdate(id, newdata) as TApiSchema;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
async _delete(id: string) {
|
|
67
|
+
return this.service.findIdThenDelete(id);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.SimpleAppService = void 0;
|
|
45
|
+
const common_1 = require("@nestjs/common");
|
|
46
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
47
|
+
const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
48
|
+
const foreignkeys_json_1 = __importDefault(require("../dicts/foreignkeys.json"));
|
|
49
|
+
const exceptions_1 = require("@nestjs/common/exceptions");
|
|
50
|
+
let SimpleAppService = (() => {
|
|
51
|
+
let _classDecorators = [(0, common_1.Injectable)()];
|
|
52
|
+
let _classDescriptor;
|
|
53
|
+
let _classExtraInitializers = [];
|
|
54
|
+
let _classThis;
|
|
55
|
+
var SimpleAppService = _classThis = class {
|
|
56
|
+
constructor(newdoc) {
|
|
57
|
+
this.jsonschema = { type: 'object', properties: {}, required: [] };
|
|
58
|
+
this.documentIdentityCode = 'code';
|
|
59
|
+
this.documentIdentityName = 'label';
|
|
60
|
+
this.documentName = 'category';
|
|
61
|
+
this.documentType = 'CAT';
|
|
62
|
+
this.LIMITPERPAGE = 20;
|
|
63
|
+
this.data = { _id: '' };
|
|
64
|
+
this.errorlist = [];
|
|
65
|
+
this.getRecordId = () => this.data._id;
|
|
66
|
+
this.setSchema = (newschema) => (this.jsonschema = newschema);
|
|
67
|
+
this.getSchema = () => this.doc.schema.obj;
|
|
68
|
+
this.getData = () => {
|
|
69
|
+
//console.log('thisdata', this.data);
|
|
70
|
+
return this.data;
|
|
71
|
+
};
|
|
72
|
+
this.setData = (newdata) => {
|
|
73
|
+
delete newdata._id;
|
|
74
|
+
this.data = Object.assign({}, newdata);
|
|
75
|
+
return this;
|
|
76
|
+
};
|
|
77
|
+
this.doc = newdoc;
|
|
78
|
+
}
|
|
79
|
+
async list() {
|
|
80
|
+
try {
|
|
81
|
+
const products = await this.doc.find();
|
|
82
|
+
const productlist = products.map((p) => {
|
|
83
|
+
return p;
|
|
84
|
+
});
|
|
85
|
+
// console.log(products);
|
|
86
|
+
return productlist;
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
throw new exceptions_1.InternalServerErrorException(err.message);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async getAutoComplete(keyword) {
|
|
93
|
+
try {
|
|
94
|
+
const filter1 = {};
|
|
95
|
+
const filter2 = {};
|
|
96
|
+
filter1[this.documentIdentityCode] = { $regex: keyword };
|
|
97
|
+
filter2[this.documentIdentityName] = { $regex: keyword };
|
|
98
|
+
const filterobj = { $or: [filter1, filter2] };
|
|
99
|
+
const projections = {
|
|
100
|
+
id: `\$_id`,
|
|
101
|
+
label: `\$${this.documentIdentityCode}`,
|
|
102
|
+
name: `\$${this.documentIdentityName}`,
|
|
103
|
+
};
|
|
104
|
+
const products = await this.doc.find(filterobj, projections, {
|
|
105
|
+
limit: this.LIMITPERPAGE,
|
|
106
|
+
});
|
|
107
|
+
const productlist = products.map((p) => {
|
|
108
|
+
return p;
|
|
109
|
+
});
|
|
110
|
+
return productlist;
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
throw new exceptions_1.InternalServerErrorException(err.message);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
async findById(id) {
|
|
117
|
+
try {
|
|
118
|
+
//console.log('findById', id);
|
|
119
|
+
this.data = await this.doc.findById(id);
|
|
120
|
+
//console.log('findById done', this.data);
|
|
121
|
+
}
|
|
122
|
+
catch (err) {
|
|
123
|
+
//error
|
|
124
|
+
throw new exceptions_1.InternalServerErrorException(err.message);
|
|
125
|
+
}
|
|
126
|
+
if (!this.data) {
|
|
127
|
+
//data not found
|
|
128
|
+
throw new exceptions_1.NotFoundException('Document Not found:');
|
|
129
|
+
}
|
|
130
|
+
//console.log('this.data', this.data);
|
|
131
|
+
return this.data;
|
|
132
|
+
// return this;
|
|
133
|
+
}
|
|
134
|
+
async create() {
|
|
135
|
+
let result;
|
|
136
|
+
try {
|
|
137
|
+
delete this.data._id;
|
|
138
|
+
this.validateData(this.data);
|
|
139
|
+
const newdoc = new this.doc(this.data);
|
|
140
|
+
result = await newdoc.save();
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
throw new exceptions_1.InternalServerErrorException(err.message);
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
async update() {
|
|
148
|
+
const id = this.getRecordId();
|
|
149
|
+
this.findIdThenUpdate(id, this.data);
|
|
150
|
+
}
|
|
151
|
+
async delete() {
|
|
152
|
+
const id = this.getRecordId();
|
|
153
|
+
const dependency = await this.getRelatedRecords(id);
|
|
154
|
+
if (!dependency) {
|
|
155
|
+
return await this.findIdThenDelete(id);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
return dependency;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
hook(type, data) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
validateData(data) {
|
|
165
|
+
const ajv = new ajv_1.default({ allErrors: true });
|
|
166
|
+
(0, ajv_formats_1.default)(ajv);
|
|
167
|
+
ajv.addFormat('field-autocomplete-code', /.*$/);
|
|
168
|
+
ajv.addFormat('field-autocomplete-name', /.*$/);
|
|
169
|
+
ajv.addFormat('x-text', /.*$/);
|
|
170
|
+
ajv.addFormat('x-html', /.*$/);
|
|
171
|
+
ajv.addKeyword({
|
|
172
|
+
keyword: 'x-foreignkey',
|
|
173
|
+
type: 'string',
|
|
174
|
+
});
|
|
175
|
+
if (!this.hook('pre-validation', data)) {
|
|
176
|
+
const erromsg = [];
|
|
177
|
+
for (let i = 0; i < this.errorlist.length; i++) {
|
|
178
|
+
erromsg.push(this.errorlist[i].message);
|
|
179
|
+
}
|
|
180
|
+
throw new exceptions_1.InternalServerErrorException(erromsg.join('\n'));
|
|
181
|
+
}
|
|
182
|
+
const validate = ajv.compile(this.jsonschema);
|
|
183
|
+
const valid = validate(data);
|
|
184
|
+
if (!valid) {
|
|
185
|
+
//console.log(validate.errors);
|
|
186
|
+
const erromsg = [];
|
|
187
|
+
for (let i = 0; i < validate.errors.length; i++) {
|
|
188
|
+
erromsg.push(validate.errors[i].message);
|
|
189
|
+
}
|
|
190
|
+
throw new exceptions_1.InternalServerErrorException(erromsg.join('\n'));
|
|
191
|
+
}
|
|
192
|
+
this.hook('post-validation', data);
|
|
193
|
+
}
|
|
194
|
+
async findIdThenDelete(id) {
|
|
195
|
+
// const data = await this.findById(id);
|
|
196
|
+
try {
|
|
197
|
+
//console.log('deletedeletedeletedelete');
|
|
198
|
+
const dependency = await this.getRelatedRecords(id);
|
|
199
|
+
if (!dependency) {
|
|
200
|
+
const deleteresult = await this.doc.findByIdAndDelete(id);
|
|
201
|
+
return deleteresult;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
return Promise.reject(dependency);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
throw new exceptions_1.InternalServerErrorException(err.message);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async findIdThenUpdate(id, data) {
|
|
212
|
+
// const existingdata = await this.findById(id);
|
|
213
|
+
try {
|
|
214
|
+
//console.log('findIdThenUpdate', id);
|
|
215
|
+
this.validateData(data);
|
|
216
|
+
// const result = await existingdata.doc.updateOne(data);
|
|
217
|
+
const result = await this.doc.findByIdAndUpdate(id, data);
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
throw new exceptions_1.InternalServerErrorException(err.message);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
//find what foreign key constraint
|
|
225
|
+
async getRelatedRecords(id) {
|
|
226
|
+
const foreignkeysettings = foreignkeys_json_1.default[this.documentName];
|
|
227
|
+
//console.log('foreignkeysettings', foreignkeysettings);
|
|
228
|
+
const propkeys = Object.getOwnPropertyNames(foreignkeysettings);
|
|
229
|
+
if (propkeys.length > 0) {
|
|
230
|
+
//console.log('Have properties');
|
|
231
|
+
for (let i = 0; i < propkeys.length; i++) {
|
|
232
|
+
const collectionname = propkeys[i];
|
|
233
|
+
//console.log('run ', i, collectionname);
|
|
234
|
+
const fobjs = foreignkeysettings[propkeys[i]];
|
|
235
|
+
const collection = this.doc.db.collection(collectionname);
|
|
236
|
+
//single schema may have multiple foreign key link here, loop all
|
|
237
|
+
for (let j = 0; j < fobjs.length; j++) {
|
|
238
|
+
const fkey = fobjs[j];
|
|
239
|
+
const filter = {};
|
|
240
|
+
filter[fkey] = id;
|
|
241
|
+
//console.log('getRelatedRecords get filter', collectionname, filter);
|
|
242
|
+
const result = await collection.findOne(filter);
|
|
243
|
+
//console.log('getRelatedRecords', result);
|
|
244
|
+
if (result) {
|
|
245
|
+
return Promise.reject(result);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return Promise.resolve(null);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
__setFunctionName(_classThis, "SimpleAppService");
|
|
254
|
+
(() => {
|
|
255
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
256
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
257
|
+
SimpleAppService = _classThis = _classDescriptor.value;
|
|
258
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
259
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
260
|
+
})();
|
|
261
|
+
return SimpleAppService = _classThis;
|
|
262
|
+
})();
|
|
263
|
+
exports.SimpleAppService = SimpleAppService;
|
|
264
|
+
//# sourceMappingURL=SimpleAppService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SimpleAppService.js","sourceRoot":"","sources":["SimpleAppService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA4C;AAG5C,8CAAsB;AACtB,8DAAqC;AACrC,iFAAoD;AACpD,0DAGmC;IAEtB,gBAAgB;4BAD5B,IAAA,mBAAU,GAAE;;;;;QAWX,YAAY,MAAgB;YATlB,eAAU,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;YAC9D,yBAAoB,GAAG,MAAM,CAAC;YAC9B,yBAAoB,GAAG,OAAO,CAAC;YAC/B,iBAAY,GAAG,UAAU,CAA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EAAE;gBACvB,iCAAiC;gBACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;oBACxC,MAAM,cAAc,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;oBACnC,yCAAyC;oBACzC,MAAM,KAAK,GAAG,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC9C,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC;oBAC1D,iEAAiE;oBACjE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;wBACrC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;wBACtB,MAAM,MAAM,GAAG,EAAE,CAAC;wBAClB,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;wBAClB,sEAAsE;wBACtE,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;wBAChD,2CAA2C;wBAC3C,IAAI,MAAM,EAAE;4BACV,OAAO,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;yBAC/B;qBACF;iBACF;aACF;YACD,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC/B,CAAC;;;;;QAxMH,6KAyMC;;;QAzMY,uDAAgB;;;;AAAhB,4CAAgB"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { InjectModel } from '@nestjs/mongoose';
|
|
3
|
+
import { Model } from 'mongoose';
|
|
4
|
+
import Ajv from 'ajv';
|
|
5
|
+
import addFormats from 'ajv-formats';
|
|
6
|
+
import foreignkeys from '../dicts/foreignkeys.json';
|
|
7
|
+
import {
|
|
8
|
+
NotFoundException,
|
|
9
|
+
InternalServerErrorException,
|
|
10
|
+
} from '@nestjs/common/exceptions';
|
|
11
|
+
@Injectable()
|
|
12
|
+
export class SimpleAppService<T extends { _id?: string }> {
|
|
13
|
+
protected jsonschema = { type: 'object', properties: {}, required: [] };
|
|
14
|
+
protected documentIdentityCode = 'code';
|
|
15
|
+
protected documentIdentityName = 'label';
|
|
16
|
+
protected documentName = 'category';
|
|
17
|
+
protected documentType = 'CAT';
|
|
18
|
+
protected LIMITPERPAGE = 20;
|
|
19
|
+
protected data: T = { _id: '' } as T;
|
|
20
|
+
protected doc: Model<T>;
|
|
21
|
+
protected errorlist = [];
|
|
22
|
+
constructor(newdoc: Model<T>) {
|
|
23
|
+
this.doc = newdoc;
|
|
24
|
+
}
|
|
25
|
+
getRecordId = (): string => this.data._id;
|
|
26
|
+
setSchema = (newschema) => (this.jsonschema = newschema);
|
|
27
|
+
getSchema = () => this.doc.schema.obj;
|
|
28
|
+
getData = () => {
|
|
29
|
+
//console.log('thisdata', this.data);
|
|
30
|
+
return this.data;
|
|
31
|
+
};
|
|
32
|
+
setData = (newdata: T) => {
|
|
33
|
+
delete newdata._id;
|
|
34
|
+
this.data = { ...newdata };
|
|
35
|
+
return this;
|
|
36
|
+
};
|
|
37
|
+
async list() {
|
|
38
|
+
try {
|
|
39
|
+
const products = await this.doc.find();
|
|
40
|
+
const productlist = products.map((p: T) => {
|
|
41
|
+
return p;
|
|
42
|
+
});
|
|
43
|
+
// console.log(products);
|
|
44
|
+
return productlist;
|
|
45
|
+
} catch (err) {
|
|
46
|
+
throw new InternalServerErrorException(err.message);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async getAutoComplete(keyword: string) {
|
|
50
|
+
try {
|
|
51
|
+
const filter1 = {};
|
|
52
|
+
const filter2 = {};
|
|
53
|
+
filter1[this.documentIdentityCode] = { $regex: keyword };
|
|
54
|
+
filter2[this.documentIdentityName] = { $regex: keyword };
|
|
55
|
+
const filterobj = { $or: [filter1, filter2] };
|
|
56
|
+
const projections = {
|
|
57
|
+
id: `\$_id`,
|
|
58
|
+
label: `\$${this.documentIdentityCode}`,
|
|
59
|
+
name: `\$${this.documentIdentityName}`,
|
|
60
|
+
};
|
|
61
|
+
const products = await this.doc.find(filterobj, projections, {
|
|
62
|
+
limit: this.LIMITPERPAGE,
|
|
63
|
+
});
|
|
64
|
+
const productlist = products.map((p: T) => {
|
|
65
|
+
return p;
|
|
66
|
+
});
|
|
67
|
+
return productlist;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
throw new InternalServerErrorException(err.message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async findById(id: string) {
|
|
73
|
+
try {
|
|
74
|
+
//console.log('findById', id);
|
|
75
|
+
this.data = await this.doc.findById(id);
|
|
76
|
+
//console.log('findById done', this.data);
|
|
77
|
+
} catch (err) {
|
|
78
|
+
//error
|
|
79
|
+
throw new InternalServerErrorException(err.message);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!this.data) {
|
|
83
|
+
//data not found
|
|
84
|
+
throw new NotFoundException('Document Not found:');
|
|
85
|
+
}
|
|
86
|
+
//console.log('this.data', this.data);
|
|
87
|
+
return this.data;
|
|
88
|
+
// return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async create() {
|
|
92
|
+
let result;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
delete this.data._id;
|
|
96
|
+
this.validateData(this.data);
|
|
97
|
+
const newdoc = new this.doc(this.data);
|
|
98
|
+
result = await newdoc.save();
|
|
99
|
+
} catch (err) {
|
|
100
|
+
throw new InternalServerErrorException(err.message);
|
|
101
|
+
}
|
|
102
|
+
return result as T;
|
|
103
|
+
}
|
|
104
|
+
async update() {
|
|
105
|
+
const id: string = this.getRecordId();
|
|
106
|
+
this.findIdThenUpdate(id, this.data);
|
|
107
|
+
}
|
|
108
|
+
async delete() {
|
|
109
|
+
|
|
110
|
+
const id: string = this.getRecordId();
|
|
111
|
+
const dependency = await this.getRelatedRecords(id);
|
|
112
|
+
if (!dependency) {
|
|
113
|
+
return await this.findIdThenDelete(id);
|
|
114
|
+
} else {
|
|
115
|
+
return dependency;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
hook(type: string, data: T) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
validateData(data: T) {
|
|
122
|
+
const ajv = new Ajv({ allErrors: true });
|
|
123
|
+
addFormats(ajv);
|
|
124
|
+
ajv.addFormat('field-autocomplete-code', /.*$/);
|
|
125
|
+
ajv.addFormat('field-autocomplete-name', /.*$/);
|
|
126
|
+
ajv.addFormat('x-text',/.*$/)
|
|
127
|
+
ajv.addFormat('x-html',/.*$/)
|
|
128
|
+
ajv.addKeyword({
|
|
129
|
+
keyword: 'x-foreignkey',
|
|
130
|
+
type: 'string',
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (!this.hook('pre-validation', data)) {
|
|
134
|
+
const erromsg: string[] = [];
|
|
135
|
+
for (let i = 0; i < this.errorlist.length; i++) {
|
|
136
|
+
erromsg.push(this.errorlist[i].message);
|
|
137
|
+
}
|
|
138
|
+
throw new InternalServerErrorException(erromsg.join('\n'));
|
|
139
|
+
}
|
|
140
|
+
const validate = ajv.compile(this.jsonschema);
|
|
141
|
+
const valid = validate(data);
|
|
142
|
+
if (!valid) {
|
|
143
|
+
//console.log(validate.errors);
|
|
144
|
+
const erromsg: string[] = [];
|
|
145
|
+
for (let i = 0; i < validate.errors.length; i++) {
|
|
146
|
+
erromsg.push(validate.errors[i].message);
|
|
147
|
+
}
|
|
148
|
+
throw new InternalServerErrorException(erromsg.join('\n'));
|
|
149
|
+
}
|
|
150
|
+
this.hook('post-validation', data);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async findIdThenDelete(id: string) {
|
|
154
|
+
// const data = await this.findById(id);
|
|
155
|
+
try {
|
|
156
|
+
//console.log('deletedeletedeletedelete');
|
|
157
|
+
const dependency = await this.getRelatedRecords(id);
|
|
158
|
+
if (!dependency) {
|
|
159
|
+
const deleteresult = await this.doc.findByIdAndDelete(id);
|
|
160
|
+
return deleteresult;
|
|
161
|
+
} else {
|
|
162
|
+
return Promise.reject(dependency);
|
|
163
|
+
}
|
|
164
|
+
} catch (err) {
|
|
165
|
+
throw new InternalServerErrorException(err.message);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async findIdThenUpdate(id: string, data: T) {
|
|
170
|
+
// const existingdata = await this.findById(id);
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
//console.log('findIdThenUpdate', id);
|
|
174
|
+
this.validateData(data);
|
|
175
|
+
// const result = await existingdata.doc.updateOne(data);
|
|
176
|
+
const result = await this.doc.findByIdAndUpdate(id, data);
|
|
177
|
+
|
|
178
|
+
return result;
|
|
179
|
+
} catch (err) {
|
|
180
|
+
throw new InternalServerErrorException(err.message);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
//find what foreign key constraint
|
|
185
|
+
async getRelatedRecords(id: string) {
|
|
186
|
+
const foreignkeysettings = foreignkeys[this.documentName];
|
|
187
|
+
//console.log('foreignkeysettings', foreignkeysettings);
|
|
188
|
+
const propkeys = Object.getOwnPropertyNames(foreignkeysettings);
|
|
189
|
+
|
|
190
|
+
if (propkeys.length > 0) {
|
|
191
|
+
//console.log('Have properties');
|
|
192
|
+
for (let i = 0; i < propkeys.length; i++) {
|
|
193
|
+
const collectionname = propkeys[i];
|
|
194
|
+
//console.log('run ', i, collectionname);
|
|
195
|
+
const fobjs = foreignkeysettings[propkeys[i]];
|
|
196
|
+
const collection = this.doc.db.collection(collectionname);
|
|
197
|
+
//single schema may have multiple foreign key link here, loop all
|
|
198
|
+
for (let j = 0; j < fobjs.length; j++) {
|
|
199
|
+
const fkey = fobjs[j];
|
|
200
|
+
const filter = {};
|
|
201
|
+
filter[fkey] = id;
|
|
202
|
+
//console.log('getRelatedRecords get filter', collectionname, filter);
|
|
203
|
+
const result = await collection.findOne(filter);
|
|
204
|
+
//console.log('getRelatedRecords', result);
|
|
205
|
+
if (result) {
|
|
206
|
+
return Promise.reject(result);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return Promise.resolve(null);
|
|
212
|
+
}
|
|
213
|
+
}
|
package/backend1/src/main.js
CHANGED
|
@@ -2,9 +2,21 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const core_1 = require("@nestjs/core");
|
|
4
4
|
const app_module_1 = require("./app.module");
|
|
5
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
5
6
|
async function bootstrap() {
|
|
7
|
+
var _a;
|
|
6
8
|
const app = await core_1.NestFactory.create(app_module_1.AppModule);
|
|
7
|
-
|
|
9
|
+
app.enableCors();
|
|
10
|
+
const config = new swagger_1.DocumentBuilder()
|
|
11
|
+
.setTitle(process.env.PROJECT_NAME)
|
|
12
|
+
.setDescription(process.env.PROJECT_DESCRIPTION)
|
|
13
|
+
.setVersion(process.env.PROJECT_VERSION)
|
|
14
|
+
.build();
|
|
15
|
+
const document = swagger_1.SwaggerModule.createDocument(app, config);
|
|
16
|
+
swagger_1.SwaggerModule.setup('api', app, document, {
|
|
17
|
+
swaggerOptions: { showExtensions: true },
|
|
18
|
+
});
|
|
19
|
+
await app.listen((_a = process.env.HTTP_PORT) !== null && _a !== void 0 ? _a : 8000); //listen which port
|
|
8
20
|
}
|
|
9
21
|
bootstrap();
|
|
10
22
|
//# sourceMappingURL=main.js.map
|
package/backend1/src/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["main.ts"],"names":[],"mappings":";;AAAA,uCAA2C;AAC3C,6CAAyC;
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["main.ts"],"names":[],"mappings":";;AAAA,uCAA2C;AAC3C,6CAAyC;AACzC,6CAAiE;AAEjE,KAAK,UAAU,SAAS;;IACtB,MAAM,GAAG,GAAG,MAAM,kBAAW,CAAC,MAAM,CAAC,sBAAS,CAAC,CAAC;IAChD,GAAG,CAAC,UAAU,EAAE,CAAC;IACjB,MAAM,MAAM,GAAG,IAAI,yBAAe,EAAE;SACjC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;SAClC,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;SAC/C,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC;SACvC,KAAK,EAAE,CAAC;IACX,MAAM,QAAQ,GAAG,uBAAa,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAC3D,uBAAa,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAE;QACxC,cAAc,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE;KACzC,CAAC,CAAC;IAEH,MAAM,GAAG,CAAC,MAAM,CAAC,MAAA,OAAO,CAAC,GAAG,CAAC,SAAS,mCAAI,IAAI,CAAC,CAAC,CAAC,mBAAmB;AACtE,CAAC;AACD,SAAS,EAAE,CAAC"}
|
package/backend1/src/main.ts
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { NestFactory } from '@nestjs/core';
|
|
2
2
|
import { AppModule } from './app.module';
|
|
3
|
+
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
3
4
|
|
|
4
5
|
async function bootstrap() {
|
|
5
6
|
const app = await NestFactory.create(AppModule);
|
|
6
|
-
|
|
7
|
+
app.enableCors();
|
|
8
|
+
const config = new DocumentBuilder()
|
|
9
|
+
.setTitle(process.env.PROJECT_NAME)
|
|
10
|
+
.setDescription(process.env.PROJECT_DESCRIPTION)
|
|
11
|
+
.setVersion(process.env.PROJECT_VERSION)
|
|
12
|
+
.build();
|
|
13
|
+
const document = SwaggerModule.createDocument(app, config);
|
|
14
|
+
SwaggerModule.setup('api', app, document, {
|
|
15
|
+
swaggerOptions: { showExtensions: true },
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
await app.listen(process.env.HTTP_PORT ?? 8000); //listen which port
|
|
7
19
|
}
|
|
8
|
-
bootstrap();
|
|
20
|
+
bootstrap();
|
package/backend1/tsconfig.json
CHANGED
|
@@ -1,21 +1 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "commonjs",
|
|
4
|
-
"declaration": true,
|
|
5
|
-
"removeComments": true,
|
|
6
|
-
"emitDecoratorMetadata": true,
|
|
7
|
-
"experimentalDecorators": true,
|
|
8
|
-
"allowSyntheticDefaultImports": true,
|
|
9
|
-
"target": "ES2021",
|
|
10
|
-
"sourceMap": true,
|
|
11
|
-
"outDir": "./dist",
|
|
12
|
-
"baseUrl": "./",
|
|
13
|
-
"incremental": true,
|
|
14
|
-
"skipLibCheck": true,
|
|
15
|
-
"strictNullChecks": false,
|
|
16
|
-
"noImplicitAny": false,
|
|
17
|
-
"strictBindCallApply": false,
|
|
18
|
-
"forceConsistentCasingInFileNames": false,
|
|
19
|
-
"noFallthroughCasesInSwitch": false
|
|
20
|
-
}
|
|
21
|
-
}
|
|
1
|
+
{"compilerOptions":{"module":"commonjs","declaration":true,"removeComments":true,"emitDecoratorMetadata":true,"experimentalDecorators":true,"allowSyntheticDefaultImports":true,"target":"ES2021","sourceMap":true,"outDir":"./dist","baseUrl":"./","incremental":true,"skipLibCheck":true,"strictNullChecks":false,"noImplicitAny":false,"strictBindCallApply":false,"forceConsistentCasingInFileNames":false,"noFallthroughCasesInSwitch":false,"esModuleInterop":true,"resolveJsonModule":true}}
|