express-sweet-generator 2.0.8 → 2.0.9
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/templates/cjs/.env +2 -1
- package/templates/cjs/errors/NotFoundError.js +1 -1
- package/templates/cjs/middlewares/checkValidationResult.js +5 -2
- package/templates/cjs/models/UserModel.js +42 -33
- package/templates/esm/.env +2 -1
- package/templates/esm/errors/NotFoundError.js +1 -1
- package/templates/esm/middlewares/checkValidationResult.js +5 -2
- package/templates/esm/models/UserModel.js +42 -33
package/package.json
CHANGED
package/templates/cjs/.env
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
NODE_ENV=development
|
|
1
|
+
NODE_ENV=development
|
|
2
|
+
LOG_VALIDATION_ERRORS=true
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
module.exports = class extends Error {
|
|
5
5
|
/**
|
|
6
6
|
* Creates a new NotFoundError instance.
|
|
7
|
-
* @param {string} [message='The requested record could not be found.']
|
|
7
|
+
* @param {string} [message='The requested record could not be found.'] The error message.
|
|
8
8
|
*/
|
|
9
9
|
constructor(message = 'The requested record could not be found.') {
|
|
10
10
|
super(message);
|
|
@@ -7,7 +7,10 @@ module.exports = (req, res, next) => {
|
|
|
7
7
|
const result = validationResult(req);
|
|
8
8
|
if (result.isEmpty())
|
|
9
9
|
next();
|
|
10
|
-
else
|
|
10
|
+
else {
|
|
11
|
+
// Log validation errors only if LOG_VALIDATION_ERRORS is enabled
|
|
12
|
+
if (process.env.LOG_VALIDATION_ERRORS === 'true')
|
|
13
|
+
console.error('Validation errors:', result.array());
|
|
11
14
|
res.status(400).end();
|
|
12
|
-
|
|
15
|
+
}
|
|
13
16
|
}
|
|
@@ -67,19 +67,21 @@ module.exports = class extends expressExtension.database.Model {
|
|
|
67
67
|
dir: 'asc',
|
|
68
68
|
search: null,
|
|
69
69
|
}, options);
|
|
70
|
+
|
|
70
71
|
const recordsTotal = await super.count();
|
|
71
|
-
|
|
72
|
+
|
|
73
|
+
const whereClause = {};
|
|
72
74
|
if (options.search)
|
|
73
|
-
|
|
74
|
-
[super.Op.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
whereClause[super.Op.or] = [
|
|
76
|
+
{email: {[super.Op.like]: `%${options.search}%`}},
|
|
77
|
+
{name: {[super.Op.like]: `%${options.search}%`}}
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
const recordsFiltered = await super.count({where: whereClause});
|
|
81
|
+
|
|
80
82
|
const data = await super.findAll({
|
|
81
83
|
attributes: ['id', 'name', 'email', 'icon', 'modified'],
|
|
82
|
-
where,
|
|
84
|
+
where: whereClause,
|
|
83
85
|
order: [[options.order, options.dir]],
|
|
84
86
|
offset: parseInt(options.start, 10),
|
|
85
87
|
limit: parseInt(options.length),
|
|
@@ -88,23 +90,27 @@ module.exports = class extends expressExtension.database.Model {
|
|
|
88
90
|
return {recordsTotal, recordsFiltered, data};
|
|
89
91
|
}
|
|
90
92
|
|
|
91
|
-
static async createUser(
|
|
93
|
+
static async createUser(userData) {
|
|
92
94
|
let transaction;
|
|
93
95
|
try {
|
|
94
|
-
|
|
96
|
+
userData = Object.assign({
|
|
95
97
|
email: null,
|
|
96
98
|
name: null,
|
|
97
99
|
password: null,
|
|
98
100
|
icon: null
|
|
99
|
-
},
|
|
101
|
+
}, userData);
|
|
100
102
|
transaction = await super.begin();
|
|
101
103
|
const user = await super.create({
|
|
102
|
-
name:
|
|
103
|
-
email:
|
|
104
|
-
password:
|
|
104
|
+
name: userData.name,
|
|
105
|
+
email: userData.email,
|
|
106
|
+
password: userData.password,
|
|
105
107
|
}, {transaction});
|
|
106
|
-
|
|
108
|
+
|
|
109
|
+
await this.#updateUserIcon(user, userData.icon, transaction);
|
|
110
|
+
|
|
107
111
|
await transaction.commit();
|
|
112
|
+
|
|
113
|
+
console.log('Successfully created a new user with ID:', user.id);
|
|
108
114
|
} catch (error) {
|
|
109
115
|
if (transaction)
|
|
110
116
|
await transaction.rollback();
|
|
@@ -112,27 +118,31 @@ module.exports = class extends expressExtension.database.Model {
|
|
|
112
118
|
}
|
|
113
119
|
}
|
|
114
120
|
|
|
115
|
-
static async updateUser(userId,
|
|
121
|
+
static async updateUser(userId, userData) {
|
|
116
122
|
let transaction;
|
|
117
123
|
try {
|
|
118
|
-
|
|
124
|
+
userData = Object.assign({
|
|
119
125
|
email: null,
|
|
120
126
|
name: null,
|
|
121
127
|
password: null,
|
|
122
128
|
icon: null
|
|
123
|
-
},
|
|
129
|
+
}, userData);
|
|
124
130
|
const user = await this.getUser(userId);
|
|
125
131
|
if (!user)
|
|
126
132
|
throw new NotFoundError();
|
|
127
133
|
transaction = await super.begin();
|
|
128
|
-
user.email =
|
|
129
|
-
user.name =
|
|
134
|
+
user.email = userData.email;
|
|
135
|
+
user.name = userData.name;
|
|
130
136
|
user.modified = super.literal('CURRENT_TIMESTAMP');
|
|
131
|
-
if (
|
|
132
|
-
user.password =
|
|
137
|
+
if (userData.password)
|
|
138
|
+
user.password = userData.password;
|
|
139
|
+
|
|
133
140
|
await user.save({transaction});
|
|
134
|
-
await this.#updateUserIcon(user,
|
|
141
|
+
await this.#updateUserIcon(user, userData.icon, transaction);
|
|
142
|
+
|
|
135
143
|
await transaction.commit();
|
|
144
|
+
|
|
145
|
+
console.log(`Successfully updated user with ID: ${userId}`);
|
|
136
146
|
} catch (error) {
|
|
137
147
|
if (transaction)
|
|
138
148
|
await transaction.rollback();
|
|
@@ -144,26 +154,25 @@ module.exports = class extends expressExtension.database.Model {
|
|
|
144
154
|
user.icon = `/upload/${user.id}.${Media.getExtensionFromDataUrl(dataUrl)}`;
|
|
145
155
|
await user.save({transaction});
|
|
146
156
|
const iconPath = `${global.APP_DIR}/public${user.icon}`;
|
|
147
|
-
console.log(`Write ${iconPath}`);
|
|
148
157
|
Media.writeDataUrlToFile(iconPath, dataUrl);
|
|
149
158
|
}
|
|
150
159
|
|
|
151
160
|
static async deleteUser(userId) {
|
|
152
|
-
|
|
153
|
-
|
|
161
|
+
await super.destroy({where: {id: userId}});
|
|
162
|
+
console.log(`Successfully deleted user with ID: ${userId}`);
|
|
154
163
|
}
|
|
155
164
|
|
|
156
165
|
static async emailExists(email, excludeUserId = null) {
|
|
157
|
-
const
|
|
166
|
+
const whereClause = {email};
|
|
158
167
|
if (excludeUserId)
|
|
159
|
-
|
|
160
|
-
return await super.count({where}) > 0;
|
|
168
|
+
whereClause.id = {[super.Op.ne]: excludeUserId};
|
|
169
|
+
return await super.count({where: whereClause}) > 0;
|
|
161
170
|
}
|
|
162
171
|
|
|
163
172
|
static async userIdExists(userId) {
|
|
164
|
-
const
|
|
173
|
+
const whereClause = {email};
|
|
165
174
|
if (excludeUserId)
|
|
166
|
-
|
|
167
|
-
return await super.count({where}) > 0;
|
|
175
|
+
whereClause.id = {[super.Op.ne]: excludeUserId};
|
|
176
|
+
return await super.count({where: whereClause}) > 0;
|
|
168
177
|
}
|
|
169
178
|
}
|
package/templates/esm/.env
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
NODE_ENV=development
|
|
1
|
+
NODE_ENV=development
|
|
2
|
+
LOG_VALIDATION_ERRORS=true
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
export default class extends Error {
|
|
5
5
|
/**
|
|
6
6
|
* Creates a new NotFoundError instance.
|
|
7
|
-
* @param {string} [message='The requested record could not be found.']
|
|
7
|
+
* @param {string} [message='The requested record could not be found.'] The error message.
|
|
8
8
|
*/
|
|
9
9
|
constructor(message = 'The requested record could not be found.') {
|
|
10
10
|
super(message);
|
|
@@ -7,7 +7,10 @@ export default (req, res, next) => {
|
|
|
7
7
|
const result = validationResult(req);
|
|
8
8
|
if (result.isEmpty())
|
|
9
9
|
next();
|
|
10
|
-
else
|
|
10
|
+
else {
|
|
11
|
+
// Log validation errors only if LOG_VALIDATION_ERRORS is enabled
|
|
12
|
+
if (process.env.LOG_VALIDATION_ERRORS === 'true')
|
|
13
|
+
console.error('Validation errors:', result.array());
|
|
11
14
|
res.status(400).end();
|
|
12
|
-
|
|
15
|
+
}
|
|
13
16
|
}
|
|
@@ -67,19 +67,21 @@ export default class extends expressExtension.database.Model {
|
|
|
67
67
|
dir: 'asc',
|
|
68
68
|
search: null,
|
|
69
69
|
}, options);
|
|
70
|
+
|
|
70
71
|
const recordsTotal = await super.count();
|
|
71
|
-
|
|
72
|
+
|
|
73
|
+
const whereClause = {};
|
|
72
74
|
if (options.search)
|
|
73
|
-
|
|
74
|
-
[super.Op.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
whereClause[super.Op.or] = [
|
|
76
|
+
{email: {[super.Op.like]: `%${options.search}%`}},
|
|
77
|
+
{name: {[super.Op.like]: `%${options.search}%`}}
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
const recordsFiltered = await super.count({where: whereClause});
|
|
81
|
+
|
|
80
82
|
const data = await super.findAll({
|
|
81
83
|
attributes: ['id', 'name', 'email', 'icon', 'modified'],
|
|
82
|
-
where,
|
|
84
|
+
where: whereClause,
|
|
83
85
|
order: [[options.order, options.dir]],
|
|
84
86
|
offset: parseInt(options.start, 10),
|
|
85
87
|
limit: parseInt(options.length),
|
|
@@ -88,23 +90,27 @@ export default class extends expressExtension.database.Model {
|
|
|
88
90
|
return {recordsTotal, recordsFiltered, data};
|
|
89
91
|
}
|
|
90
92
|
|
|
91
|
-
static async createUser(
|
|
93
|
+
static async createUser(userData) {
|
|
92
94
|
let transaction;
|
|
93
95
|
try {
|
|
94
|
-
|
|
96
|
+
userData = Object.assign({
|
|
95
97
|
email: null,
|
|
96
98
|
name: null,
|
|
97
99
|
password: null,
|
|
98
100
|
icon: null
|
|
99
|
-
},
|
|
101
|
+
}, userData);
|
|
100
102
|
transaction = await super.begin();
|
|
101
103
|
const user = await super.create({
|
|
102
|
-
name:
|
|
103
|
-
email:
|
|
104
|
-
password:
|
|
104
|
+
name: userData.name,
|
|
105
|
+
email: userData.email,
|
|
106
|
+
password: userData.password,
|
|
105
107
|
}, {transaction});
|
|
106
|
-
|
|
108
|
+
|
|
109
|
+
await this.#updateUserIcon(user, userData.icon, transaction);
|
|
110
|
+
|
|
107
111
|
await transaction.commit();
|
|
112
|
+
|
|
113
|
+
console.log('Successfully created a new user with ID:', user.id);
|
|
108
114
|
} catch (error) {
|
|
109
115
|
if (transaction)
|
|
110
116
|
await transaction.rollback();
|
|
@@ -112,27 +118,31 @@ export default class extends expressExtension.database.Model {
|
|
|
112
118
|
}
|
|
113
119
|
}
|
|
114
120
|
|
|
115
|
-
static async updateUser(userId,
|
|
121
|
+
static async updateUser(userId, userData) {
|
|
116
122
|
let transaction;
|
|
117
123
|
try {
|
|
118
|
-
|
|
124
|
+
userData = Object.assign({
|
|
119
125
|
email: null,
|
|
120
126
|
name: null,
|
|
121
127
|
password: null,
|
|
122
128
|
icon: null
|
|
123
|
-
},
|
|
129
|
+
}, userData);
|
|
124
130
|
const user = await this.getUser(userId);
|
|
125
131
|
if (!user)
|
|
126
132
|
throw new NotFoundError();
|
|
127
133
|
transaction = await super.begin();
|
|
128
|
-
user.email =
|
|
129
|
-
user.name =
|
|
134
|
+
user.email = userData.email;
|
|
135
|
+
user.name = userData.name;
|
|
130
136
|
user.modified = super.literal('CURRENT_TIMESTAMP');
|
|
131
|
-
if (
|
|
132
|
-
user.password =
|
|
137
|
+
if (userData.password)
|
|
138
|
+
user.password = userData.password;
|
|
139
|
+
|
|
133
140
|
await user.save({transaction});
|
|
134
|
-
await this.#updateUserIcon(user,
|
|
141
|
+
await this.#updateUserIcon(user, userData.icon, transaction);
|
|
142
|
+
|
|
135
143
|
await transaction.commit();
|
|
144
|
+
|
|
145
|
+
console.log(`Successfully updated user with ID: ${userId}`);
|
|
136
146
|
} catch (error) {
|
|
137
147
|
if (transaction)
|
|
138
148
|
await transaction.rollback();
|
|
@@ -144,26 +154,25 @@ export default class extends expressExtension.database.Model {
|
|
|
144
154
|
user.icon = `/upload/${user.id}.${Media.getExtensionFromDataUrl(dataUrl)}`;
|
|
145
155
|
await user.save({transaction});
|
|
146
156
|
const iconPath = `${global.APP_DIR}/public${user.icon}`;
|
|
147
|
-
console.log(`Write ${iconPath}`);
|
|
148
157
|
Media.writeDataUrlToFile(iconPath, dataUrl);
|
|
149
158
|
}
|
|
150
159
|
|
|
151
160
|
static async deleteUser(userId) {
|
|
152
|
-
|
|
153
|
-
|
|
161
|
+
await super.destroy({where: {id: userId}});
|
|
162
|
+
console.log(`Successfully deleted user with ID: ${userId}`);
|
|
154
163
|
}
|
|
155
164
|
|
|
156
165
|
static async emailExists(email, excludeUserId = null) {
|
|
157
|
-
const
|
|
166
|
+
const whereClause = {email};
|
|
158
167
|
if (excludeUserId)
|
|
159
|
-
|
|
160
|
-
return await super.count({where}) > 0;
|
|
168
|
+
whereClause.id = {[super.Op.ne]: excludeUserId};
|
|
169
|
+
return await super.count({where: whereClause}) > 0;
|
|
161
170
|
}
|
|
162
171
|
|
|
163
172
|
static async userIdExists(userId) {
|
|
164
|
-
const
|
|
173
|
+
const whereClause = {email};
|
|
165
174
|
if (excludeUserId)
|
|
166
|
-
|
|
167
|
-
return await super.count({where}) > 0;
|
|
175
|
+
whereClause.id = {[super.Op.ne]: excludeUserId};
|
|
176
|
+
return await super.count({where: whereClause}) > 0;
|
|
168
177
|
}
|
|
169
178
|
}
|