-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataWriter.js
More file actions
164 lines (125 loc) · 4.7 KB
/
dataWriter.js
File metadata and controls
164 lines (125 loc) · 4.7 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
const { DbMigration } = require('larvitdbmigration');
const { slugify } = require('larvitslugify');
const { Utils, Log } = require('larvitutils');
const topLogPrefix = 'larvitcms: dataWriter.js: ';
class DataWriter {
constructor(options) {
if (!options.log) {
options.log = new Log();
}
this.options = options;
for (const key of Object.keys(options)) {
this[key] = options[key];
}
this.lUtils = new Utils({log: this.log});
}
async runDbMigrations() {
const options = {};
options.dbType = 'mariadb';
options.dbDriver = this.db;
options.tableName = 'cms_db_version';
options.migrationScriptPath = __dirname + '/dbmigration';
const dbMigration = new DbMigration(options);
await dbMigration.run();
}
async rmPage(uuid) {
const logPrefix = topLogPrefix + 'rmPage() - ';
const uuidBuffer = this.lUtils.uuidToBuffer(uuid);
if (uuid === undefined) {
const err = new Error('pageUuid not provided');
this.log.warn(logPrefix + err.message);
throw err;
}
if (uuidBuffer === false) {
const err = new Error('Inavlid pageUuid provided');
this.log.warn(logPrefix + err.message);
throw err;
}
await this.db.query('DELETE FROM cms_pagesData WHERE pageUuid = ?', [uuidBuffer]);
await this.db.query('DELETE FROM cms_pages WHERE uuid = ?', [uuidBuffer]);
}
async rmSnippet(name) {
const logPrefix = topLogPrefix + 'rmSnippet() - ';
if (!name) {
const err = new Error('snippet name not provided');
this.log.warn(logPrefix + err.message);
throw err;
}
await this.db.query('DELETE FROM cms_snippets WHERE name = ?', [name]);
}
async savePage(options) {
const logPrefix = topLogPrefix + 'savePage() - ';
const uuidBuffer = this.lUtils.uuidToBuffer(options.uuid);
if (options.uuid === undefined) {
const err = new Error('pageUuid not provided');
this.log.warn(logPrefix + err.message);
throw err;
}
if (uuidBuffer === false) {
const err = new Error('Inavlid pageUuid provided');
this.log.warn(logPrefix + err.message);
throw err;
}
this.log.debug(logPrefix + 'Running with data. "' + JSON.stringify(options) + '"');
await this.db.query('DELETE FROM cms_pagesData WHERE pageUuid = ?', [uuidBuffer]);
await this.db.query('DELETE FROM cms_pages WHERE uuid = ?', [uuidBuffer]);
// Insert page
const dbFields = [uuidBuffer, options.name];
let sql = 'INSERT INTO cms_pages (uuid,name';
if (options.published) {
sql += ', published';
}
if (options.template) {
sql += ', template';
}
sql += ') VALUES(?,?';
if (options.published) {
sql += ',?';
dbFields.push(options.published);
}
if (options.template) {
sql += ',?';
dbFields.push(options.template);
}
sql += ');';
await this.db.query(sql, dbFields);
// We need to declare this outside the loop because of async operations
const addEntryData = async (lang, htmlTitle, body1, body2, body3, body4, body5, body6, slug) => {
const dbFields = [uuidBuffer, lang, htmlTitle, body1, body2, body3, body4, body5, body6, slug];
const sql = 'INSERT INTO cms_pagesData (pageUuid, lang, htmlTitle, body1, body2, body3, body4, body5, body6, slug) VALUES(?,?,?,?,?,?,?,?,?,?);';
await this.db.query(sql, dbFields);
};
// Add content data
if (options.langs) {
for (const lang in options.langs) {
if (options.langs[lang].slug) {
options.langs[lang].slug = slugify(options.langs[lang].slug, {save: ['_', '-', '/']});
}
if (!options.langs[lang].slug) {
options.langs[lang].slug = slugify(options.langs[lang].htmlTitle, {save: ['_', '-', '/']});
}
if (!options.langs[lang].htmlTitle) options.langs[lang].htmlTitle = '';
if (!options.langs[lang].body1) options.langs[lang].body1 = '';
if (!options.langs[lang].body2) options.langs[lang].body2 = '';
if (!options.langs[lang].body3) options.langs[lang].body3 = '';
if (!options.langs[lang].body4) options.langs[lang].body4 = '';
if (!options.langs[lang].body5) options.langs[lang].body5 = '';
if (!options.langs[lang].body6) options.langs[lang].body6 = '';
await addEntryData(lang, options.langs[lang].htmlTitle, options.langs[lang].body1, options.langs[lang].body2, options.langs[lang].body3, options.langs[lang].body4, options.langs[lang].body5, options.langs[lang].body6, options.langs[lang].slug);
}
}
}
async saveSnippet(options) {
const logPrefix = topLogPrefix + 'saveSnippet() - ';
const sql = 'REPLACE INTO cms_snippets (body, name, lang) VALUES(?,?,?);';
const dbFields = [options.body, options.name, options.lang];
if (options.name === undefined) {
const err = new Error('Name not provided');
this.log.warn(logPrefix + err.message);
throw err;
}
await this.db.query(sql, dbFields);
}
}
module.exports = exports = DataWriter;