@websy/websy-designs 0.0.114 → 0.0.118
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/server/helpers/v1/pgHelper.js +73 -10
- package/dist/server/routes/v1/api.js +43 -19
- package/dist/server/utils.js +6 -0
- package/dist/server/websy-designs-server.js +18 -10
- package/dist/websy-designs.debug.js +244 -37
- package/dist/websy-designs.js +338 -89
- package/dist/websy-designs.min.css +1 -1
- package/dist/websy-designs.min.js +1 -1
- package/package.json +1 -1
|
@@ -19,7 +19,11 @@ const sql = {
|
|
|
19
19
|
id SERIAL PRIMARY KEY,
|
|
20
20
|
text text,
|
|
21
21
|
tags text,
|
|
22
|
-
label text
|
|
22
|
+
label text,
|
|
23
|
+
createuser text,
|
|
24
|
+
edituser text,
|
|
25
|
+
createdate timestamp without time zone DEFAULT now(),
|
|
26
|
+
editdate timestamp without time zone DEFAULT now()
|
|
23
27
|
);
|
|
24
28
|
`,
|
|
25
29
|
translations: `
|
|
@@ -29,7 +33,11 @@ const sql = {
|
|
|
29
33
|
entity_id integer NOT NULL,
|
|
30
34
|
field_name text,
|
|
31
35
|
language character varying(28),
|
|
32
|
-
text text
|
|
36
|
+
text text,
|
|
37
|
+
createuser text,
|
|
38
|
+
edituser text,
|
|
39
|
+
createdate timestamp without time zone DEFAULT now(),
|
|
40
|
+
editdate timestamp without time zone DEFAULT now()
|
|
33
41
|
);
|
|
34
42
|
`
|
|
35
43
|
}
|
|
@@ -93,15 +101,43 @@ class PGHelper {
|
|
|
93
101
|
sql += `
|
|
94
102
|
LEFT JOIN (
|
|
95
103
|
SELECT entity_id, json_object_agg(field_name, text) as translation
|
|
96
|
-
FROM translations
|
|
104
|
+
FROM translations as t
|
|
105
|
+
WHERE table_name = '${entity}' AND language = '${lang}'
|
|
106
|
+
GROUP BY entity_id
|
|
107
|
+
) t2
|
|
108
|
+
ON ${entity}.${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = t2.entity_id
|
|
109
|
+
`
|
|
110
|
+
}
|
|
111
|
+
sql += `
|
|
112
|
+
WHERE ${this.buildWhere(query.where, entity)}
|
|
113
|
+
${this.buildOrderBy(query)}
|
|
114
|
+
`
|
|
115
|
+
if (query.limit) {
|
|
116
|
+
sql += ` LIMIT ${query.limit}`
|
|
117
|
+
}
|
|
118
|
+
if (query.offset) {
|
|
119
|
+
sql += ` OFFSET ${query.offset}`
|
|
120
|
+
}
|
|
121
|
+
return sql
|
|
122
|
+
}
|
|
123
|
+
buildSelectWithId (entity, id, query, columns, lang) {
|
|
124
|
+
let sql = `
|
|
125
|
+
SELECT ${columns || '*'}
|
|
126
|
+
FROM ${entity}
|
|
127
|
+
`
|
|
128
|
+
if ((process.env.TRANSLATE === true || process.env.TRANSLATE === 'true') && lang !== null) {
|
|
129
|
+
sql += `
|
|
130
|
+
LEFT JOIN (
|
|
131
|
+
SELECT entity_id, json_object_agg(field_name, text) as translation
|
|
132
|
+
FROM translations as t
|
|
97
133
|
WHERE table_name = '${entity}' AND language = '${lang}'
|
|
98
134
|
GROUP BY entity_id
|
|
99
|
-
)
|
|
100
|
-
ON ${entity}.${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} =
|
|
135
|
+
) t2
|
|
136
|
+
ON ${entity}.${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = t2.entity_id
|
|
101
137
|
`
|
|
102
138
|
}
|
|
103
139
|
sql += `
|
|
104
|
-
WHERE ${this.
|
|
140
|
+
WHERE ${this.buildWhereWithId(entity, id)}
|
|
105
141
|
${this.buildOrderBy(query)}
|
|
106
142
|
`
|
|
107
143
|
return sql
|
|
@@ -119,18 +155,37 @@ class PGHelper {
|
|
|
119
155
|
WHERE ${this.buildWhere(where)}
|
|
120
156
|
`
|
|
121
157
|
}
|
|
122
|
-
|
|
158
|
+
buildUpdateWithId (entity, id, data) {
|
|
159
|
+
let updates = []
|
|
160
|
+
for (let key in data) {
|
|
161
|
+
if (this.updateIgnores.indexOf(key) === -1) {
|
|
162
|
+
updates.push(`${key} = ${(data[key] === null ? data[key] : `'${data[key]}'`)}`)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return `
|
|
166
|
+
UPDATE ${entity}
|
|
167
|
+
SET ${updates.join(',')}
|
|
168
|
+
WHERE ${this.buildWhereWithId(entity, id)}
|
|
169
|
+
`
|
|
170
|
+
}
|
|
171
|
+
buildWhere (input, entity) {
|
|
123
172
|
if (typeof input === 'undefined') {
|
|
124
173
|
return '1=1'
|
|
125
174
|
}
|
|
126
175
|
else {
|
|
176
|
+
try {
|
|
177
|
+
input = decodeURI(input)
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
// console.log(error)
|
|
181
|
+
}
|
|
127
182
|
let list = input.split(';').map(d => {
|
|
128
183
|
let parts = d.split(':')
|
|
129
184
|
if (parts[1].indexOf('%') !== -1) {
|
|
130
|
-
return `${parts[0]} LIKE '${parts[1]}'`
|
|
185
|
+
return `${entity ? entity + '.' : ''}${parts[0]} LIKE '${parts[1]}'`
|
|
131
186
|
}
|
|
132
187
|
else {
|
|
133
|
-
return `${parts[0]} = '${parts[1]}'`
|
|
188
|
+
return `${entity ? entity + '.' : ''}${parts[0]} = '${parts[1]}'`
|
|
134
189
|
}
|
|
135
190
|
})
|
|
136
191
|
return `
|
|
@@ -138,6 +193,14 @@ class PGHelper {
|
|
|
138
193
|
`
|
|
139
194
|
}
|
|
140
195
|
}
|
|
196
|
+
buildWhereWithId (entity, id) {
|
|
197
|
+
if (typeof id === 'undefined') {
|
|
198
|
+
return '1=1'
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
return `${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = ${id}`
|
|
202
|
+
}
|
|
203
|
+
}
|
|
141
204
|
checkTables () {
|
|
142
205
|
this.createContentTable().then(() => {
|
|
143
206
|
this.createTranslationTable()
|
|
@@ -210,7 +273,7 @@ class PGHelper {
|
|
|
210
273
|
rollback (err, callbackFn) {
|
|
211
274
|
console.log('Rolling Back')
|
|
212
275
|
console.log(err)
|
|
213
|
-
this.client.query('ROLLBACK', function (
|
|
276
|
+
this.client.query('ROLLBACK', function (rollbackErr) {
|
|
214
277
|
console.log(err)
|
|
215
278
|
// done()
|
|
216
279
|
callbackFn(err)
|
|
@@ -5,35 +5,32 @@ const router = express.Router()
|
|
|
5
5
|
// const AuthHelper = require('../helpers/auth')
|
|
6
6
|
|
|
7
7
|
function APIRoutes (dbHelper) {
|
|
8
|
+
router.delete('/:entity/:id', (req, res) => {
|
|
9
|
+
const sql = `DELETE FROM ${req.params.entity} WHERE ${dbHelper.buildWhereWithId(req.params.entity, req.params.id)}`
|
|
10
|
+
dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
|
|
11
|
+
})
|
|
8
12
|
router.delete('/:entity', (req, res) => {
|
|
9
13
|
const sql = dbHelper.buildDelete(req.params.entity, req.query.where)
|
|
10
14
|
dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
|
|
11
15
|
})
|
|
12
|
-
|
|
16
|
+
router.get('/:entity/:id', (req, res) => {
|
|
17
|
+
let lang = null
|
|
18
|
+
if (req.session && req.session.language && req.query.notranslate !== 'true') {
|
|
19
|
+
lang = req.session.language
|
|
20
|
+
}
|
|
21
|
+
const sql = dbHelper.buildSelectWithId(req.params.entity, req.params.id, req.query, req.query.columns, lang)
|
|
22
|
+
dbHelper.execute(sql).then(response => {
|
|
23
|
+
res.json(translate(response))
|
|
24
|
+
}, err => res.json(err))
|
|
25
|
+
})
|
|
13
26
|
router.get('/:entity', (req, res) => {
|
|
14
27
|
let lang = null
|
|
15
|
-
if (req.session && req.session.language) {
|
|
28
|
+
if (req.session && req.session.language && req.query.notranslate !== 'true') {
|
|
16
29
|
lang = req.session.language
|
|
17
30
|
}
|
|
18
31
|
const sql = dbHelper.buildSelect(req.params.entity, req.query, req.query.columns, lang)
|
|
19
32
|
dbHelper.execute(sql).then(response => {
|
|
20
|
-
|
|
21
|
-
for (let r = 0; r < response.rows.length; r++) {
|
|
22
|
-
const row = response.rows[r]
|
|
23
|
-
for (let key in row) {
|
|
24
|
-
try {
|
|
25
|
-
if (row.translation) {
|
|
26
|
-
row.translation = JSON.parse(row.translation)
|
|
27
|
-
row[key] = row.translation[key] || row[key]
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
row[key] = row.translation[key] || row[key]
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
res.json(response)
|
|
33
|
+
res.json(translate(response))
|
|
37
34
|
}, err => res.json(err))
|
|
38
35
|
})
|
|
39
36
|
router.post('/:entity', (req, res) => {
|
|
@@ -41,6 +38,13 @@ function APIRoutes (dbHelper) {
|
|
|
41
38
|
console.log(req.body)
|
|
42
39
|
const sql = dbHelper.buildInsert(req.params.entity, req.body)
|
|
43
40
|
console.log(sql)
|
|
41
|
+
dbHelper.execute(sql).then(response => res.json(response), err => {
|
|
42
|
+
res.statusCode = 404
|
|
43
|
+
res.json({err})
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
router.put('/:entity/:id', (req, res) => {
|
|
47
|
+
const sql = dbHelper.buildUpdateWithId(req.params.entity, req.params.id, req.body)
|
|
44
48
|
dbHelper.execute(sql).then(response => res.json(response), err => res.json(err))
|
|
45
49
|
})
|
|
46
50
|
router.put('/:entity', (req, res) => {
|
|
@@ -50,4 +54,24 @@ function APIRoutes (dbHelper) {
|
|
|
50
54
|
return router
|
|
51
55
|
}
|
|
52
56
|
|
|
57
|
+
function translate (response) {
|
|
58
|
+
if (process.env.TRANSLATE && response.rows) {
|
|
59
|
+
for (let r = 0; r < response.rows.length; r++) {
|
|
60
|
+
const row = response.rows[r]
|
|
61
|
+
for (let key in row) {
|
|
62
|
+
try {
|
|
63
|
+
if (row.translation) {
|
|
64
|
+
row.translation = JSON.parse(row.translation)
|
|
65
|
+
row[key] = row.translation[key] || row[key]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
row[key] = row.translation[key] || row[key]
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return response
|
|
75
|
+
}
|
|
76
|
+
|
|
53
77
|
module.exports = APIRoutes
|
package/dist/server/utils.js
CHANGED
|
@@ -7,5 +7,11 @@ module.exports = {
|
|
|
7
7
|
text += possible.charAt(Math.floor(Math.random() * possible.length))
|
|
8
8
|
}
|
|
9
9
|
return text
|
|
10
|
+
},
|
|
11
|
+
getElementPos: el => {
|
|
12
|
+
const rect = el.getBoundingClientRect()
|
|
13
|
+
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft
|
|
14
|
+
const scrollTop = window.pageYOffset || document.documentElement.scrollTop
|
|
15
|
+
return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
|
|
10
16
|
}
|
|
11
17
|
}
|
|
@@ -44,20 +44,17 @@ module.exports = function (options) {
|
|
|
44
44
|
})
|
|
45
45
|
if (options.useRecaptcha === true) {
|
|
46
46
|
app.use('/google', require(`./routes/${version}/recaptcha`))
|
|
47
|
-
}
|
|
48
|
-
console.log('setting pdf route')
|
|
47
|
+
}
|
|
49
48
|
app.use('/pdf', require(`./routes/${version}/pdf`))
|
|
50
49
|
if (options.useDB === true) {
|
|
51
50
|
const dbHelper = require(`./helpers/${version}/${options.dbEngine}Helper`)
|
|
52
|
-
dbHelper.init(options.dbOptions).then(() => {
|
|
53
|
-
console.log('initializing session')
|
|
54
|
-
console.log(dbHelper.pool)
|
|
51
|
+
dbHelper.init(options.dbOptions || {}).then(() => {
|
|
52
|
+
console.log('initializing session')
|
|
55
53
|
// const store = new DBSession({
|
|
56
54
|
// pool: dbHelper.pool, // Connection pool, need to make dynamic to accommodate mySql
|
|
57
55
|
// tableName: 'sessions' // Use another table-name than the default "session" one
|
|
58
56
|
// })
|
|
59
|
-
console.log('setting up session')
|
|
60
|
-
console.log(process.env)
|
|
57
|
+
console.log('setting up session')
|
|
61
58
|
app.set('trust proxy', 1)
|
|
62
59
|
app.use(cookieParser(process.env.SESSION_SECRET))
|
|
63
60
|
let cookieConfig = {
|
|
@@ -73,15 +70,26 @@ module.exports = function (options) {
|
|
|
73
70
|
}
|
|
74
71
|
if (process.env.COOKIE_SECURE === 'true' || process.env.COOKIE_SECURE === true) {
|
|
75
72
|
cookieConfig.secure = true
|
|
73
|
+
}
|
|
74
|
+
let DBSession = null
|
|
75
|
+
let store = null
|
|
76
|
+
if (options.useDBStore === true) {
|
|
77
|
+
console.log('using db store')
|
|
78
|
+
if (options.dbEngine === 'pg') {
|
|
79
|
+
DBSession = require('connect-pg-simple')(expressSession)
|
|
80
|
+
store = new DBSession({
|
|
81
|
+
pool: dbHelper.pool, // Connection pool, need to make dynamic to accommodate mySql
|
|
82
|
+
tableName: 'sessions' // Use another table-name than the default "session" one
|
|
83
|
+
})
|
|
84
|
+
}
|
|
76
85
|
}
|
|
77
|
-
console.log(cookieConfig)
|
|
78
86
|
app.use(expressSession({
|
|
79
87
|
secret: process.env.SESSION_SECRET,
|
|
80
88
|
resave: false,
|
|
81
89
|
saveUninitialized: true,
|
|
82
90
|
cookie: cookieConfig,
|
|
83
|
-
name: process.env.COOKIE_NAME
|
|
84
|
-
|
|
91
|
+
name: process.env.COOKIE_NAME,
|
|
92
|
+
store: store
|
|
85
93
|
}))
|
|
86
94
|
// app.use(sessionHelper.checkSession(dbHelper))
|
|
87
95
|
// app.use(function (req, res, next) {
|