Skip to content

Commit 9f45358

Browse files
committed
chore: ugrade webpack 5
1 parent cb0c5b6 commit 9f45358

File tree

14 files changed

+359
-13497
lines changed

14 files changed

+359
-13497
lines changed

app/extend/application.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const factory_1 = require("../lib/db/factory");
4+
const DBSymbol = Symbol('Application#db');
5+
exports.default = {
6+
get db() {
7+
return factory_1.default();
8+
}
9+
};

app/extend/context.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.default = {
4+
get db() {
5+
return this.app.db;
6+
}
7+
};

app/lib/condition.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const tslib_1 = require("tslib");
4+
const json_typescript_mapper_1 = require("@hubcarl/json-typescript-mapper");
5+
class Condition {
6+
constructor() {
7+
this.where = {};
8+
this.like = {};
9+
this.orderByField = 'createTime';
10+
this.orderBy = 'desc';
11+
this.title = undefined;
12+
this.categoryId = undefined;
13+
this.status = undefined;
14+
this.tag = undefined;
15+
this.pageIndex = 1;
16+
this.pageSize = 10;
17+
this.where = {};
18+
this.like = {};
19+
}
20+
}
21+
tslib_1.__decorate([
22+
json_typescript_mapper_1.JsonProperty('title'),
23+
tslib_1.__metadata("design:type", String)
24+
], Condition.prototype, "title", void 0);
25+
tslib_1.__decorate([
26+
json_typescript_mapper_1.JsonProperty('categoryId'),
27+
tslib_1.__metadata("design:type", Number)
28+
], Condition.prototype, "categoryId", void 0);
29+
tslib_1.__decorate([
30+
json_typescript_mapper_1.JsonProperty('status'),
31+
tslib_1.__metadata("design:type", Number)
32+
], Condition.prototype, "status", void 0);
33+
tslib_1.__decorate([
34+
json_typescript_mapper_1.JsonProperty('tag'),
35+
tslib_1.__metadata("design:type", String)
36+
], Condition.prototype, "tag", void 0);
37+
tslib_1.__decorate([
38+
json_typescript_mapper_1.JsonProperty('pageIndex'),
39+
tslib_1.__metadata("design:type", Number)
40+
], Condition.prototype, "pageIndex", void 0);
41+
tslib_1.__decorate([
42+
json_typescript_mapper_1.JsonProperty('pageSize'),
43+
tslib_1.__metadata("design:type", Number)
44+
], Condition.prototype, "pageSize", void 0);
45+
exports.default = Condition;

app/lib/db/base.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const path = require("path");
4+
const shortid = require("shortid");
5+
const dbpath = path.resolve(__dirname, 'blog.json');
6+
class DB {
7+
constructor(name = dbpath) {
8+
this.name = name;
9+
}
10+
getUniqueId() {
11+
return shortid.generate();
12+
}
13+
get(collectionName) {
14+
return null;
15+
}
16+
query(collectionName, json) {
17+
return null;
18+
}
19+
add(collectionName, json) {
20+
return null;
21+
}
22+
update(collectionName, where, json) {
23+
return null;
24+
}
25+
delete(collectionName, field) {
26+
return null;
27+
}
28+
getPager(collectionName, condition) {
29+
return null;
30+
}
31+
}
32+
exports.default = DB;

app/lib/db/collection.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
class Collection {
4+
constructor(db, name) {
5+
this.db = db;
6+
this.name = name;
7+
}
8+
get() {
9+
return this.db.get(this.name);
10+
}
11+
query(json) {
12+
return this.db.query(this.name, json);
13+
}
14+
add(json) {
15+
return this.db.add(this.name, json);
16+
}
17+
update(where, json) {
18+
return this.db.update(this.name, where, json);
19+
}
20+
delete(field) {
21+
return this.db.delete(this.name, field);
22+
}
23+
getPager(condition) {
24+
return this.db.getPager(this.name, condition);
25+
}
26+
getOrderAscByField(field) {
27+
return this.get().orderBy(field, 'asc').value();
28+
}
29+
getOrderDescByField(field) {
30+
return this.get().orderBy(field, 'desc').value();
31+
}
32+
}
33+
exports.default = Collection;

app/lib/db/factory.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const file_1 = require("./file");
4+
const mysql_1 = require("./mysql");
5+
function default_1(type) {
6+
switch (type) {
7+
case 'mysql':
8+
return new mysql_1.default();
9+
case 'mongo':
10+
return new mysql_1.default();
11+
default:
12+
return new file_1.default();
13+
}
14+
}
15+
exports.default = default_1;

app/lib/db/file.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const lowdb = require("lowdb");
4+
const lodashid = require("lodash-id");
5+
const FileSync = require("lowdb/adapters/FileSync");
6+
const base_1 = require("./base");
7+
class FileDB extends base_1.default {
8+
constructor(name) {
9+
super(name);
10+
const file = new FileSync(this.name);
11+
this.instance = lowdb(file);
12+
this.instance._.mixin(lodashid);
13+
this.create();
14+
}
15+
create() {
16+
this.instance.defaults({ article: [], user: {} }).write();
17+
}
18+
get(collectionName) {
19+
return this.instance.get(collectionName);
20+
}
21+
query(collectionName, json) {
22+
return this.get(collectionName)
23+
.find(json)
24+
.write();
25+
}
26+
add(collectionName, json) {
27+
return this.get(collectionName)
28+
.push(json)
29+
.write();
30+
}
31+
update(collectionName, where, json) {
32+
return this.get(collectionName).find(where).assign(json).write();
33+
}
34+
delete(collectionName, json) {
35+
return this.get(collectionName).remove(json).write();
36+
}
37+
getPager(collectionName, condition) {
38+
const { where, like, pageIndex, pageSize, orderByField, orderBy } = condition;
39+
const start = (pageIndex - 1) * pageSize;
40+
const end = pageIndex * pageSize;
41+
const result = this.get(collectionName)
42+
.filter(where)
43+
.filter(item => {
44+
return Object.keys(like).reduce((isLike, key) => {
45+
return isLike && item[key] && item[key].indexOf(like[key]) > -1;
46+
}, true);
47+
})
48+
.orderBy(orderByField, orderBy);
49+
const total = result.size().value();
50+
const list = result.slice(start, end).value();
51+
return { total, list };
52+
}
53+
}
54+
exports.default = FileDB;

app/lib/db/mongo.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const base_1 = require("./base");
4+
class MongoDB extends base_1.default {
5+
constructor(name) {
6+
super(name);
7+
}
8+
}
9+
exports.default = MongoDB;

app/lib/db/mysql.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const base_1 = require("./base");
4+
class MySQLDB extends base_1.default {
5+
constructor(name) {
6+
super(name);
7+
}
8+
}
9+
exports.default = MySQLDB;

app/model/article.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const tslib_1 = require("tslib");
4+
const json_typescript_mapper_1 = require("@hubcarl/json-typescript-mapper");
5+
class Article {
6+
constructor() {
7+
this.id = '';
8+
this.title = '';
9+
this.summary = '';
10+
this.categoryId = -1;
11+
this.tag = '';
12+
this.authorId = -1;
13+
this.url = '';
14+
this.status = 0;
15+
this.hits = 0;
16+
this.content = '';
17+
this.createTime = Date.now();
18+
}
19+
}
20+
tslib_1.__decorate([
21+
json_typescript_mapper_1.JsonProperty('id'),
22+
tslib_1.__metadata("design:type", String)
23+
], Article.prototype, "id", void 0);
24+
tslib_1.__decorate([
25+
json_typescript_mapper_1.JsonProperty('title'),
26+
tslib_1.__metadata("design:type", String)
27+
], Article.prototype, "title", void 0);
28+
tslib_1.__decorate([
29+
json_typescript_mapper_1.JsonProperty('summary'),
30+
tslib_1.__metadata("design:type", String)
31+
], Article.prototype, "summary", void 0);
32+
tslib_1.__decorate([
33+
json_typescript_mapper_1.JsonProperty('categoryId'),
34+
tslib_1.__metadata("design:type", Number)
35+
], Article.prototype, "categoryId", void 0);
36+
tslib_1.__decorate([
37+
json_typescript_mapper_1.JsonProperty('tag'),
38+
tslib_1.__metadata("design:type", String)
39+
], Article.prototype, "tag", void 0);
40+
tslib_1.__decorate([
41+
json_typescript_mapper_1.JsonProperty('categoryId'),
42+
tslib_1.__metadata("design:type", Number)
43+
], Article.prototype, "authorId", void 0);
44+
tslib_1.__decorate([
45+
json_typescript_mapper_1.JsonProperty('createTime'),
46+
tslib_1.__metadata("design:type", Number)
47+
], Article.prototype, "createTime", void 0);
48+
tslib_1.__decorate([
49+
json_typescript_mapper_1.JsonProperty('hits'),
50+
tslib_1.__metadata("design:type", Number)
51+
], Article.prototype, "hits", void 0);
52+
tslib_1.__decorate([
53+
json_typescript_mapper_1.JsonProperty('url'),
54+
tslib_1.__metadata("design:type", String)
55+
], Article.prototype, "url", void 0);
56+
tslib_1.__decorate([
57+
json_typescript_mapper_1.JsonProperty('status'),
58+
tslib_1.__metadata("design:type", Number)
59+
], Article.prototype, "status", void 0);
60+
tslib_1.__decorate([
61+
json_typescript_mapper_1.JsonProperty('content'),
62+
tslib_1.__metadata("design:type", String)
63+
], Article.prototype, "content", void 0);
64+
exports.default = Article;

app/service/article.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const egg_1 = require("egg");
4+
const json_typescript_mapper_1 = require("@hubcarl/json-typescript-mapper");
5+
const collection_1 = require("../lib/db/collection");
6+
const article_1 = require("../model/article");
7+
class ArticeService extends egg_1.Service {
8+
constructor(ctx) {
9+
super(ctx);
10+
this.context = ctx;
11+
this.colllection = new collection_1.default(ctx.db, 'article');
12+
}
13+
async getArtilceList(condition) {
14+
if (condition.categoryId) {
15+
condition.where.categoryId = condition.categoryId;
16+
}
17+
if (condition.status) {
18+
condition.where.status = condition.status;
19+
}
20+
if (condition.title) {
21+
condition.like.title = condition.title;
22+
}
23+
return this.colllection.getPager(condition);
24+
}
25+
async saveArticle(data) {
26+
const article = json_typescript_mapper_1.deserialize(article_1.default, data);
27+
if (article.id) {
28+
return this.colllection.update({ id: article.id }, article);
29+
}
30+
article.id = this.context.db.getUniqueId();
31+
this.colllection.add(article);
32+
return article;
33+
}
34+
async query(json) {
35+
return this.colllection.query(json);
36+
}
37+
async deleteArticle(id) {
38+
return this.colllection.delete({ id });
39+
}
40+
}
41+
exports.default = ArticeService;

0 commit comments

Comments
 (0)