@drax/settings-back 0.11.5 → 0.12.2
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/dist/factory/SettingServiceFactory.js +2 -1
- package/dist/model/SettingsModel.js +4 -4
- package/dist/repository/sqlite/SettingSqliteRepository.js +68 -96
- package/dist/routes/SettingRoutes.js +20 -4
- package/dist/services/SettingService.js +4 -4
- package/package.json +8 -9
- package/src/factory/SettingServiceFactory.ts +2 -1
- package/src/interfaces/ISettingRepository.ts +2 -2
- package/src/model/SettingsModel.ts +4 -7
- package/src/repository/sqlite/SettingSqliteRepository.ts +68 -138
- package/src/routes/SettingRoutes.ts +20 -4
- package/src/services/SettingService.ts +4 -4
- package/tsconfig.tsbuildinfo +1 -1
- package/types/factory/SettingServiceFactory.d.ts.map +1 -1
- package/types/interfaces/ISettingRepository.d.ts +2 -2
- package/types/interfaces/ISettingRepository.d.ts.map +1 -1
- package/types/model/SettingsModel.d.ts +9 -9
- package/types/model/SettingsModel.d.ts.map +1 -1
- package/types/repository/sqlite/SettingSqliteRepository.d.ts +13 -13
- package/types/repository/sqlite/SettingSqliteRepository.d.ts.map +1 -1
- package/types/routes/SettingRoutes.d.ts.map +1 -1
|
@@ -12,7 +12,8 @@ const SettingServiceFactory = (verbose = false) => {
|
|
|
12
12
|
break;
|
|
13
13
|
case COMMON.DB_ENGINES.SQLITE:
|
|
14
14
|
const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile);
|
|
15
|
-
settingRepository = new SettingSqliteRepository(dbFile,
|
|
15
|
+
settingRepository = new SettingSqliteRepository(dbFile, true);
|
|
16
|
+
settingRepository.build();
|
|
16
17
|
break;
|
|
17
18
|
default:
|
|
18
19
|
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { mongoose } from '@drax/common-back';
|
|
2
2
|
import uniqueValidator from 'mongoose-unique-validator';
|
|
3
|
+
import mongooseLeanVirtuals from 'mongoose-lean-virtuals';
|
|
3
4
|
const SettingSchema = new mongoose.Schema({
|
|
4
5
|
key: { type: String, required: true, unique: true },
|
|
5
6
|
value: { type: mongoose.Schema.Types.Mixed, required: false, unique: false },
|
|
6
7
|
//valueList: [{type: String, required: false, unique: false}],
|
|
7
8
|
label: { type: String, required: false },
|
|
8
|
-
|
|
9
|
+
category: { type: String, required: true },
|
|
9
10
|
type: { type: String, default: "string", enum: ['string', 'longString', 'number', 'enum', 'boolean', 'password', 'stringList', 'numberList', 'enumList', 'ref', 'secret'], required: false, unique: false },
|
|
10
11
|
options: [{ type: String }],
|
|
11
12
|
regex: { type: String },
|
|
@@ -14,13 +15,12 @@ const SettingSchema = new mongoose.Schema({
|
|
|
14
15
|
entityText: { type: String, required: false, unique: false },
|
|
15
16
|
prefix: { type: String, required: false },
|
|
16
17
|
suffix: { type: String, required: false },
|
|
17
|
-
});
|
|
18
|
+
}, { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } });
|
|
18
19
|
SettingSchema.virtual("id").get(function () {
|
|
19
20
|
return this._id.toString();
|
|
20
21
|
});
|
|
21
|
-
SettingSchema.set('toJSON', { getters: true, virtuals: true });
|
|
22
|
-
SettingSchema.set('toObject', { getters: true, virtuals: true });
|
|
23
22
|
SettingSchema.plugin(uniqueValidator, { message: 'validation.unique' });
|
|
23
|
+
SettingSchema.plugin(mongooseLeanVirtuals);
|
|
24
24
|
const MODEL_NAME = 'Setting';
|
|
25
25
|
const COLLECTION_NAME = 'settings';
|
|
26
26
|
const SettingModel = mongoose.model(MODEL_NAME, SettingSchema, COLLECTION_NAME);
|
|
@@ -1,93 +1,77 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
];
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { AbstractSqliteRepository } from "@drax/crud-back";
|
|
2
|
+
class SettingSqliteRepository extends AbstractSqliteRepository {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
this.tableName = 'settings';
|
|
6
|
+
this.searchFields = [];
|
|
7
|
+
this.booleanFields = [];
|
|
8
|
+
this.identifier = '_id';
|
|
9
|
+
this.populateFields = [];
|
|
10
|
+
this.tableFields = [
|
|
11
|
+
{ name: "key", type: "TEXT", unique: true, primary: false },
|
|
12
|
+
{ name: "value", type: "TEXT", unique: false, primary: false },
|
|
13
|
+
{ name: "label", type: "TEXT", unique: false, primary: false },
|
|
14
|
+
{ name: "category", type: "TEXT", unique: false, primary: false },
|
|
15
|
+
{ name: "type", type: "TEXT", unique: false, primary: false },
|
|
16
|
+
{ name: "options", type: "TEXT", unique: false, primary: false },
|
|
17
|
+
{ name: "regex", type: "TEXT", unique: false, primary: false },
|
|
18
|
+
{ name: "entity", type: "TEXT", unique: false, primary: false },
|
|
19
|
+
{ name: "entityValue", type: "TEXT", unique: false, primary: false },
|
|
20
|
+
{ name: "entityText", type: "TEXT", unique: false, primary: false },
|
|
21
|
+
{ name: "prefix", type: "TEXT", unique: false, primary: false },
|
|
22
|
+
{ name: "suffix", type: "TEXT", unique: false, primary: false },
|
|
23
|
+
];
|
|
15
24
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
async create(data) {
|
|
21
|
-
try {
|
|
22
|
-
if (!data.id) {
|
|
23
|
-
data.id = randomUUID();
|
|
24
|
-
}
|
|
25
|
-
// data.createdAt = (new Date().toISOString())
|
|
26
|
-
const fields = Object.keys(data)
|
|
27
|
-
.map(field => `${field}`)
|
|
28
|
-
.join(', ');
|
|
29
|
-
const values = Object.keys(data)
|
|
30
|
-
.map(field => `@${field}`)
|
|
31
|
-
.join(', ');
|
|
32
|
-
const stmt = this.db.prepare(`INSERT INTO settings (${fields}) VALUES (${values})`);
|
|
33
|
-
stmt.run(data);
|
|
34
|
-
return this.findById(data.id);
|
|
35
|
-
}
|
|
36
|
-
catch (e) {
|
|
37
|
-
console.log(e);
|
|
38
|
-
throw SqliteErrorToValidationError(e, data);
|
|
25
|
+
async updatePartial(id, data) {
|
|
26
|
+
let item = await this.findById(id);
|
|
27
|
+
if (!item) {
|
|
28
|
+
throw new Error('Setting not found');
|
|
39
29
|
}
|
|
30
|
+
data = { ...item, ...data };
|
|
31
|
+
return await this.update(id, data);
|
|
40
32
|
}
|
|
41
|
-
async
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
33
|
+
async prepareData(data) {
|
|
34
|
+
if (data && data.options) {
|
|
35
|
+
data.options = JSON.stringify(data.options);
|
|
36
|
+
}
|
|
37
|
+
if (data.type === 'boolean') {
|
|
38
|
+
data.value = data.value ? 1 : 0;
|
|
39
|
+
}
|
|
40
|
+
if (data.type === 'stringList') {
|
|
41
|
+
data.value = data.value ? data.value.join(',') : '';
|
|
42
|
+
}
|
|
43
|
+
if (data.type === 'numberList') {
|
|
44
|
+
data.value = data.value ? data.value.join(',') : '';
|
|
45
|
+
}
|
|
46
|
+
if (data.type === 'enumList') {
|
|
47
|
+
data.value = data.value ? data.value.join(',') : '';
|
|
48
|
+
}
|
|
49
|
+
if (data.type === 'ref') {
|
|
50
|
+
}
|
|
51
|
+
if (data.type === 'secret') {
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
|
-
async
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
stmt.run(id);
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
async deleteAll() {
|
|
81
|
-
const stmt = this.db.prepare('DELETE FROM settings');
|
|
82
|
-
stmt.run();
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
async findById(id) {
|
|
86
|
-
const setting = this.db.prepare('SELECT * FROM settings WHERE id = ?').get(id);
|
|
87
|
-
if (setting) {
|
|
88
|
-
return setting;
|
|
54
|
+
async prepareItem(item) {
|
|
55
|
+
console.log('prepareItem', item);
|
|
56
|
+
if (item && item.options) {
|
|
57
|
+
item.options = JSON.parse(item.options);
|
|
58
|
+
}
|
|
59
|
+
if (item.type === 'boolean') {
|
|
60
|
+
item.value = item.value === 1 || item.value === '1' || item.value === '1.0';
|
|
61
|
+
}
|
|
62
|
+
if (item.type === 'stringList') {
|
|
63
|
+
item.value = item.value ? item.value.split(',') : [];
|
|
64
|
+
}
|
|
65
|
+
if (item.type === 'numberList') {
|
|
66
|
+
item.value = item.value ? item.value.split(',') : [];
|
|
67
|
+
}
|
|
68
|
+
if (item.type === 'enumList') {
|
|
69
|
+
item.value = item.value ? item.value.split(',') : [];
|
|
70
|
+
}
|
|
71
|
+
if (item.type === 'ref') {
|
|
72
|
+
}
|
|
73
|
+
if (item.type === 'secret') {
|
|
89
74
|
}
|
|
90
|
-
return undefined;
|
|
91
75
|
}
|
|
92
76
|
async findByKey(key) {
|
|
93
77
|
const setting = this.db.prepare('SELECT * FROM settings WHERE key = ?').get(key);
|
|
@@ -96,18 +80,6 @@ class SettingSqliteRepository {
|
|
|
96
80
|
}
|
|
97
81
|
return undefined;
|
|
98
82
|
}
|
|
99
|
-
async fetchAll() {
|
|
100
|
-
const settings = this.db.prepare('SELECT * FROM settings').all();
|
|
101
|
-
return settings;
|
|
102
|
-
}
|
|
103
|
-
async search(value, limit = 1000) {
|
|
104
|
-
let where = "";
|
|
105
|
-
if (value && this._searchFields.length > 0) {
|
|
106
|
-
where = ` WHERE ${this._searchFields.map(field => `${field} LIKE '%${value}%'`).join(" OR ")}`;
|
|
107
|
-
}
|
|
108
|
-
const items = this.db.prepare(`SELECT * FROM settings ${where}`).all();
|
|
109
|
-
return items;
|
|
110
|
-
}
|
|
111
83
|
}
|
|
112
84
|
export default SettingSqliteRepository;
|
|
113
85
|
export { SettingSqliteRepository };
|
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
import SettingController from "../controller/SettingController.js";
|
|
2
2
|
async function SettingsRoutes(fastify, options) {
|
|
3
3
|
const controller = new SettingController();
|
|
4
|
-
fastify.get('/api/settings',
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
fastify.get('/api/settings', {
|
|
5
|
+
schema: {
|
|
6
|
+
tags: ['Settings'],
|
|
7
|
+
}
|
|
8
|
+
}, (req, rep) => controller.fetchAll(req, rep));
|
|
9
|
+
fastify.get('/api/settings/grouped', {
|
|
10
|
+
schema: {
|
|
11
|
+
tags: ['Settings'],
|
|
12
|
+
}
|
|
13
|
+
}, (req, rep) => controller.fetchGrouped(req, rep));
|
|
14
|
+
fastify.get('/api/settings/:key', {
|
|
15
|
+
schema: {
|
|
16
|
+
tags: ['Settings'],
|
|
17
|
+
}
|
|
18
|
+
}, (req, rep) => controller.findByKey(req, rep));
|
|
19
|
+
fastify.patch('/api/settings/:id', {
|
|
20
|
+
schema: {
|
|
21
|
+
tags: ['Settings'],
|
|
22
|
+
}
|
|
23
|
+
}, (req, rep) => controller.updateValue(req, rep));
|
|
8
24
|
}
|
|
9
25
|
export default SettingsRoutes;
|
|
10
26
|
export { SettingsRoutes };
|
|
@@ -27,10 +27,10 @@ class SettingService extends AbstractService {
|
|
|
27
27
|
async fetchGrouped() {
|
|
28
28
|
const settings = await this._repository.fetchAll();
|
|
29
29
|
return settings.reduce((acc, setting) => {
|
|
30
|
-
if (!acc[setting.
|
|
31
|
-
acc[setting.
|
|
30
|
+
if (!acc[setting.category]) {
|
|
31
|
+
acc[setting.category] = [];
|
|
32
32
|
}
|
|
33
|
-
acc[setting.
|
|
33
|
+
acc[setting.category].push(setting);
|
|
34
34
|
return acc;
|
|
35
35
|
}, {});
|
|
36
36
|
}
|
|
@@ -76,7 +76,7 @@ class SettingService extends AbstractService {
|
|
|
76
76
|
const setting = await this._repository.findOneBy("key", data.key);
|
|
77
77
|
if (setting) {
|
|
78
78
|
delete data.value;
|
|
79
|
-
return await this._repository.updatePartial(setting.
|
|
79
|
+
return await this._repository.updatePartial(setting._id, data);
|
|
80
80
|
}
|
|
81
81
|
else {
|
|
82
82
|
return await this._repository.create(data);
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.12.2",
|
|
7
7
|
"description": "Setting module for nice management options.",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "types/index.d.ts",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"author": "Cristian Incarnato & Drax Team",
|
|
29
29
|
"license": "ISC",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@drax/common-back": "^0.
|
|
32
|
-
"@drax/crud-back": "^0.
|
|
33
|
-
"@drax/crud-share": "^0.
|
|
34
|
-
"@drax/email-back": "^0.
|
|
35
|
-
"@drax/identity-share": "^0.
|
|
31
|
+
"@drax/common-back": "^0.12.1",
|
|
32
|
+
"@drax/crud-back": "^0.12.2",
|
|
33
|
+
"@drax/crud-share": "^0.12.1",
|
|
34
|
+
"@drax/email-back": "^0.12.1",
|
|
35
|
+
"@drax/identity-share": "^0.12.1",
|
|
36
36
|
"bcryptjs": "^2.4.3",
|
|
37
37
|
"express-jwt": "^8.4.1",
|
|
38
38
|
"graphql": "^16.8.2",
|
|
@@ -41,9 +41,8 @@
|
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"better-sqlite3": "^11.0.0",
|
|
44
|
-
"fastify": "^
|
|
44
|
+
"fastify": "^5.2.1",
|
|
45
45
|
"mongoose": "^8.6.3",
|
|
46
|
-
"mongoose-paginate-v2": "^1.8.3",
|
|
47
46
|
"mongoose-unique-validator": "^5.0.1"
|
|
48
47
|
},
|
|
49
48
|
"devDependencies": {
|
|
@@ -62,5 +61,5 @@
|
|
|
62
61
|
"debug": "0"
|
|
63
62
|
}
|
|
64
63
|
},
|
|
65
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "521b1a2066aaab3d6d6c062f36428effd517ad32"
|
|
66
65
|
}
|
|
@@ -17,7 +17,8 @@ const SettingServiceFactory = (verbose: boolean = false): SettingService => {
|
|
|
17
17
|
break;
|
|
18
18
|
case COMMON.DB_ENGINES.SQLITE:
|
|
19
19
|
const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile)
|
|
20
|
-
settingRepository = new SettingSqliteRepository(dbFile,
|
|
20
|
+
settingRepository = new SettingSqliteRepository(dbFile, true)
|
|
21
|
+
settingRepository.build()
|
|
21
22
|
break;
|
|
22
23
|
default:
|
|
23
24
|
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {ISetting, ISettingBase} from "@drax/settings-share";
|
|
2
|
-
import {
|
|
3
|
-
interface ISettingRepository extends
|
|
2
|
+
import {IDraxCrudRepository} from "@drax/crud-share";
|
|
3
|
+
interface ISettingRepository extends IDraxCrudRepository<ISetting,ISettingBase,ISettingBase>{
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
export {ISettingRepository}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {mongoose} from '@drax/common-back';
|
|
2
2
|
import uniqueValidator from 'mongoose-unique-validator';
|
|
3
|
+
import mongooseLeanVirtuals from 'mongoose-lean-virtuals'
|
|
3
4
|
import {ISetting} from "@drax/settings-share";
|
|
4
5
|
|
|
5
6
|
|
|
@@ -9,7 +10,7 @@ const SettingSchema = new mongoose.Schema<ISetting>({
|
|
|
9
10
|
value: {type: mongoose.Schema.Types.Mixed, required: false, unique: false},
|
|
10
11
|
//valueList: [{type: String, required: false, unique: false}],
|
|
11
12
|
label: {type: String, required: false},
|
|
12
|
-
|
|
13
|
+
category: {type: String, required: true},
|
|
13
14
|
type: {type: String, default: "string", enum: ['string','longString','number','enum','boolean', 'password', 'stringList','numberList', 'enumList', 'ref', 'secret'], required: false, unique: false},
|
|
14
15
|
options: [{type: String}],
|
|
15
16
|
regex: {type: String},
|
|
@@ -18,18 +19,14 @@ const SettingSchema = new mongoose.Schema<ISetting>({
|
|
|
18
19
|
entityText: {type: String, required: false, unique: false},
|
|
19
20
|
prefix: {type: String, required: false},
|
|
20
21
|
suffix: {type: String, required: false},
|
|
21
|
-
})
|
|
22
|
+
}, {timestamps: true, toJSON: { virtuals: true}, toObject: {virtuals: true} })
|
|
22
23
|
|
|
23
24
|
SettingSchema.virtual("id").get(function () {
|
|
24
25
|
return this._id.toString();
|
|
25
26
|
});
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
SettingSchema.set('toJSON', {getters: true, virtuals: true});
|
|
29
|
-
|
|
30
|
-
SettingSchema.set('toObject', {getters: true, virtuals: true});
|
|
31
|
-
|
|
32
28
|
SettingSchema.plugin(uniqueValidator, {message: 'validation.unique'});
|
|
29
|
+
SettingSchema.plugin(mongooseLeanVirtuals);
|
|
33
30
|
|
|
34
31
|
|
|
35
32
|
|
|
@@ -1,146 +1,94 @@
|
|
|
1
1
|
import {ISettingRepository} from '../../interfaces/ISettingRepository'
|
|
2
|
-
import {UUID} from "crypto";
|
|
3
|
-
import sqlite from "better-sqlite3";
|
|
4
|
-
import {randomUUID} from "node:crypto";
|
|
5
|
-
import {IDraxPaginateResult, IDraxPaginateOptions} from "@drax/crud-share";
|
|
6
2
|
import {ISetting, ISettingBase} from "@drax/settings-share";
|
|
7
3
|
import {
|
|
8
|
-
SqliteErrorToValidationError,
|
|
9
|
-
SqliteTableBuilder,
|
|
10
4
|
SqliteTableField,
|
|
11
|
-
SqlQueryFilter,
|
|
12
|
-
SqlSort
|
|
13
5
|
} from "@drax/common-back";
|
|
6
|
+
import {AbstractSqliteRepository} from "@drax/crud-back";
|
|
14
7
|
|
|
15
|
-
const tableFields: SqliteTableField[] = [
|
|
16
|
-
{name: "key", type: "TEXT", unique: true, primary: false},
|
|
17
|
-
{name: "value", type: "TEXT", unique: false, primary: false},
|
|
18
|
-
]
|
|
19
8
|
|
|
20
|
-
|
|
21
|
-
class SettingSqliteRepository implements ISettingRepository{
|
|
9
|
+
class SettingSqliteRepository extends AbstractSqliteRepository<ISetting,ISettingBase, ISettingBase> implements ISettingRepository{
|
|
22
10
|
|
|
23
11
|
protected db: any;
|
|
12
|
+
protected tableName: string = 'settings';
|
|
24
13
|
protected dataBaseFile: string;
|
|
25
|
-
protected
|
|
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
|
-
// data.createdAt = (new Date().toISOString())
|
|
53
|
-
|
|
54
|
-
const fields = Object.keys(data)
|
|
55
|
-
.map(field => `${field}`)
|
|
56
|
-
.join(', ');
|
|
57
|
-
|
|
58
|
-
const values = Object.keys(data)
|
|
59
|
-
.map(field => `@${field}`)
|
|
60
|
-
.join(', ');
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const stmt = this.db.prepare(`INSERT INTO settings (${fields}) VALUES (${values})`);
|
|
64
|
-
stmt.run(data)
|
|
65
|
-
return this.findById(data.id as UUID)
|
|
66
|
-
}catch (e){
|
|
67
|
-
console.log(e)
|
|
68
|
-
throw SqliteErrorToValidationError(e, data)
|
|
14
|
+
protected searchFields: string[] = [];
|
|
15
|
+
protected booleanFields: string[] = [];
|
|
16
|
+
protected identifier: string = '_id';
|
|
17
|
+
protected populateFields = []
|
|
18
|
+
protected tableFields: SqliteTableField[] = [
|
|
19
|
+
{name: "key", type: "TEXT", unique: true, primary: false},
|
|
20
|
+
{name: "value", type: "TEXT", unique: false, primary: false},
|
|
21
|
+
{name: "label", type: "TEXT", unique: false, primary: false},
|
|
22
|
+
{name: "category", type: "TEXT", unique: false, primary: false},
|
|
23
|
+
{name: "type", type: "TEXT", unique: false, primary: false},
|
|
24
|
+
{name: "options", type: "TEXT", unique: false, primary: false},
|
|
25
|
+
{name: "regex", type: "TEXT", unique: false, primary: false},
|
|
26
|
+
{name: "entity", type: "TEXT", unique: false, primary: false},
|
|
27
|
+
{name: "entityValue", type: "TEXT", unique: false, primary: false},
|
|
28
|
+
{name: "entityText", type: "TEXT", unique: false, primary: false},
|
|
29
|
+
{name: "prefix", type: "TEXT", unique: false, primary: false},
|
|
30
|
+
{name: "suffix", type: "TEXT", unique: false, primary: false},
|
|
31
|
+
]
|
|
32
|
+
protected verbose: boolean;
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
async updatePartial(id: string, data: any) {
|
|
36
|
+
let item = await this.findById(id)
|
|
37
|
+
if(!item){
|
|
38
|
+
throw new Error('Setting not found')
|
|
69
39
|
}
|
|
40
|
+
data = {...item,...data}
|
|
41
|
+
return await this.update(id, data)
|
|
70
42
|
}
|
|
71
43
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
try{
|
|
76
|
-
// data.updatedAt = (new Date().toISOString())
|
|
77
|
-
|
|
78
|
-
const setClauses = Object.keys(data)
|
|
79
|
-
.map(field => `${field} = @${field}`)
|
|
80
|
-
.join(', ');
|
|
81
|
-
|
|
82
|
-
data.id = id
|
|
83
|
-
|
|
84
|
-
const stmt = this.db.prepare( `UPDATE settings SET ${setClauses} WHERE id = @id `);
|
|
85
|
-
|
|
86
|
-
stmt.run(data);
|
|
87
|
-
|
|
88
|
-
return this.findById(id)
|
|
89
|
-
}catch (e){
|
|
90
|
-
console.log(e)
|
|
91
|
-
throw SqliteErrorToValidationError(e, data)
|
|
44
|
+
async prepareData(data: any){
|
|
45
|
+
if(data && data.options){
|
|
46
|
+
data.options = JSON.stringify(data.options)
|
|
92
47
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
async paginate({
|
|
97
|
-
page= 1,
|
|
98
|
-
limit= 5,
|
|
99
|
-
orderBy= '',
|
|
100
|
-
order= false,
|
|
101
|
-
search= '',
|
|
102
|
-
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<ISetting>>{
|
|
103
|
-
const offset = page > 1 ? (page - 1) * limit : 0
|
|
104
|
-
|
|
105
|
-
let where=""
|
|
106
|
-
if (search) {
|
|
107
|
-
where = ` WHERE name LIKE '%${search}%'`
|
|
48
|
+
if(data.type === 'boolean'){
|
|
49
|
+
data.value = data.value ? 1 : 0
|
|
108
50
|
}
|
|
51
|
+
if(data.type === 'stringList'){
|
|
52
|
+
data.value = data.value ? data.value.join(',') : ''
|
|
53
|
+
}
|
|
54
|
+
if(data.type === 'numberList'){
|
|
55
|
+
data.value = data.value ? data.value.join(',') : ''
|
|
56
|
+
}
|
|
57
|
+
if(data.type === 'enumList'){
|
|
58
|
+
data.value = data.value ? data.value.join(',') : ''
|
|
59
|
+
}
|
|
60
|
+
if(data.type === 'ref'){
|
|
109
61
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const rCount = this.db.prepare('SELECT COUNT(*) as count FROM settings'+where).get();
|
|
114
|
-
where += sort
|
|
115
|
-
const settings = this.db.prepare('SELECT * FROM settings ' + where + ' LIMIT ? OFFSET ?').all([limit, offset]);
|
|
116
|
-
|
|
62
|
+
}
|
|
63
|
+
if(data.type === 'secret'){
|
|
117
64
|
|
|
118
|
-
return {
|
|
119
|
-
page: page,
|
|
120
|
-
limit: limit,
|
|
121
|
-
total: rCount.count,
|
|
122
|
-
items: settings
|
|
123
65
|
}
|
|
124
66
|
}
|
|
125
67
|
|
|
126
|
-
async
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
68
|
+
async prepareItem(item: any){
|
|
69
|
+
console.log('prepareItem', item)
|
|
70
|
+
if(item && item.options){
|
|
71
|
+
item.options = JSON.parse(item.options)
|
|
72
|
+
}
|
|
73
|
+
if(item.type === 'boolean'){
|
|
74
|
+
item.value = item.value === 1 || item.value === '1' || item.value === '1.0'
|
|
75
|
+
}
|
|
76
|
+
if(item.type === 'stringList'){
|
|
77
|
+
item.value = item.value ? item.value.split(',') : []
|
|
78
|
+
}
|
|
79
|
+
if(item.type === 'numberList'){
|
|
80
|
+
item.value = item.value ? item.value.split(',') : []
|
|
81
|
+
}
|
|
82
|
+
if(item.type === 'enumList'){
|
|
83
|
+
item.value = item.value ? item.value.split(',') : []
|
|
84
|
+
}
|
|
85
|
+
if(item.type === 'ref'){
|
|
131
86
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
stmt.run();
|
|
135
|
-
return true
|
|
136
|
-
}
|
|
87
|
+
}
|
|
88
|
+
if(item.type === 'secret'){
|
|
137
89
|
|
|
138
|
-
async findById(id: string): Promise<ISetting | null>{
|
|
139
|
-
const setting = this.db.prepare('SELECT * FROM settings WHERE id = ?').get(id);
|
|
140
|
-
if(setting){
|
|
141
|
-
return setting
|
|
142
90
|
}
|
|
143
|
-
|
|
91
|
+
|
|
144
92
|
}
|
|
145
93
|
|
|
146
94
|
async findByKey(key: string): Promise<ISetting | null>{
|
|
@@ -151,24 +99,6 @@ class SettingSqliteRepository implements ISettingRepository{
|
|
|
151
99
|
return undefined
|
|
152
100
|
}
|
|
153
101
|
|
|
154
|
-
async fetchAll(): Promise<ISetting[]>{
|
|
155
|
-
const settings = this.db.prepare('SELECT * FROM settings').all();
|
|
156
|
-
return settings
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
async search(value: any, limit: number = 1000): Promise<any[]>{
|
|
161
|
-
let where=""
|
|
162
|
-
if (value && this._searchFields.length > 0) {
|
|
163
|
-
where = ` WHERE ${this._searchFields.map(field => `${field} LIKE '%${value}%'`).join(" OR ")}`
|
|
164
|
-
}
|
|
165
|
-
const items = this.db.prepare(`SELECT * FROM settings ${where}`).all();
|
|
166
|
-
return items
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
102
|
|
|
173
103
|
}
|
|
174
104
|
|
|
@@ -4,13 +4,29 @@ import SettingController from "../controller/SettingController.js";
|
|
|
4
4
|
async function SettingsRoutes(fastify, options) {
|
|
5
5
|
const controller: SettingController = new SettingController()
|
|
6
6
|
|
|
7
|
-
fastify.get('/api/settings',
|
|
7
|
+
fastify.get('/api/settings', {
|
|
8
|
+
schema: {
|
|
9
|
+
tags: ['Settings'],
|
|
10
|
+
}
|
|
11
|
+
}, (req,rep) => controller.fetchAll(req,rep))
|
|
8
12
|
|
|
9
|
-
fastify.get('/api/settings/grouped',
|
|
13
|
+
fastify.get('/api/settings/grouped', {
|
|
14
|
+
schema: {
|
|
15
|
+
tags: ['Settings'],
|
|
16
|
+
}
|
|
17
|
+
}, (req,rep) => controller.fetchGrouped(req,rep))
|
|
10
18
|
|
|
11
|
-
fastify.get('/api/settings/:key',
|
|
19
|
+
fastify.get('/api/settings/:key', {
|
|
20
|
+
schema: {
|
|
21
|
+
tags: ['Settings'],
|
|
22
|
+
}
|
|
23
|
+
}, (req,rep) => controller.findByKey(req,rep))
|
|
12
24
|
|
|
13
|
-
fastify.patch('/api/settings/:id',
|
|
25
|
+
fastify.patch('/api/settings/:id', {
|
|
26
|
+
schema: {
|
|
27
|
+
tags: ['Settings'],
|
|
28
|
+
}
|
|
29
|
+
}, (req,rep) => controller.updateValue(req,rep))
|
|
14
30
|
}
|
|
15
31
|
|
|
16
32
|
export default SettingsRoutes
|