@websy/websy-designs 2.0.1 → 2.1.0

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.
@@ -134,16 +134,8 @@ class PGHelper {
134
134
  const placeholders = []
135
135
  const values = []
136
136
  Object.values(data).forEach(d => {
137
- if (d === null) {
138
- // return d
139
- }
140
- else {
141
- if (typeof d === 'string') {
142
- d = d.replace(/'/g, `''`)
143
- }
144
- values.push(d)
145
- placeholders.push(`$${values.length}`)
146
- }
137
+ values.push(this.prepareWriteValue(d))
138
+ placeholders.push(`$${values.length}${Array.isArray(d) ? '::text[]' : ''}`)
147
139
  })
148
140
  sql += `
149
141
  ${placeholders.join(',')}
@@ -261,11 +253,8 @@ class PGHelper {
261
253
  let updateValues = []
262
254
  for (let key in data) {
263
255
  if (this.updateIgnores.indexOf(key) === -1) {
264
- if (typeof data[key] === 'string') {
265
- data[key] = data[key].replace(/''/gm, `'`).replace(/'/gm, `''`).replace(/\\\\/gm, '\\')
266
- }
267
- updateValues.push((data[key] === null ? data[key] : data[key]))
268
- updates.push(`${key} = $${updateValues.length}`)
256
+ updateValues.push(this.prepareWriteValue(data[key]))
257
+ updates.push(`${key} = $${updateValues.length}${Array.isArray(data[key]) ? '::text[]' : ''}`)
269
258
  }
270
259
  }
271
260
  if (process.env.CREATE_USER_FIELD && process.env.USERID_FIELD && user) {
@@ -308,11 +297,8 @@ class PGHelper {
308
297
  let updates = []
309
298
  for (let key in row) {
310
299
  if (this.updateIgnores.indexOf(key) === -1) {
311
- if (typeof row[key] === 'string') {
312
- row[key] = row[key].replace(/''/gm, `'`).replace(/'/gm, `''`).replace(/\\\\/gm, '\\')
313
- }
314
- allValues.push((row[key] === null ? row[key] : `'${row[key]}'`))
315
- updates.push(`${key} = $${allValues.length}`)
300
+ allValues.push(this.prepareWriteValue(row[key]))
301
+ updates.push(`${key} = $${allValues.length}${Array.isArray(row[key]) ? '::text[]' : ''}`)
316
302
  }
317
303
  }
318
304
  allValues.push(id)
@@ -326,16 +312,8 @@ class PGHelper {
326
312
  delete row[(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id']
327
313
  const placeholders = []
328
314
  Object.values(row).forEach(d => {
329
- if (d === null) {
330
- // return d
331
- }
332
- else {
333
- if (typeof d === 'string') {
334
- d = d.replace(/'/g, `''`)
335
- }
336
- allValues.push(d)
337
- placeholders.push(`$${allValues.length}`)
338
- }
315
+ allValues.push(this.prepareWriteValue(d))
316
+ placeholders.push(`$${allValues.length}${Array.isArray(d) ? '::text[]' : ''}`)
339
317
  })
340
318
  sql = `
341
319
  INSERT INTO ${entity} (${Object.keys(row).join(',')})
@@ -411,6 +389,34 @@ class PGHelper {
411
389
  return { where: `${(this.options.entityConfig[entity] && this.options.entityConfig[entity].idColumn) || 'id'} = $${startCount + 1}`, values: [id] }
412
390
  }
413
391
  }
392
+ prepareWriteValue (value) {
393
+ if (Array.isArray(value)) {
394
+ return value.map(v => this.prepareWriteValue(v))
395
+ }
396
+ return value
397
+ }
398
+ prepareResponseRows (rows = []) {
399
+ return rows.map(row => {
400
+ const output = {}
401
+ for (let key in row) {
402
+ output[key] = this.prepareResponseValue(row[key])
403
+ }
404
+ return output
405
+ })
406
+ }
407
+ prepareResponseValue (value) {
408
+ if (Array.isArray(value)) {
409
+ return value.map(v => this.prepareResponseValue(v))
410
+ }
411
+ if (value && value.constructor === Object) {
412
+ const output = {}
413
+ for (let key in value) {
414
+ output[key] = this.prepareResponseValue(value[key])
415
+ }
416
+ return output
417
+ }
418
+ return value
419
+ }
414
420
  checkTables () {
415
421
  this.createContentTable().then(() => {
416
422
  this.createTranslationTable().then(() => {
@@ -494,16 +500,21 @@ class PGHelper {
494
500
  })
495
501
  }
496
502
  execute (query, values) {
503
+ console.log('query')
504
+ console.log(query)
505
+ console.log(values)
497
506
  return new Promise((resolve, reject) => {
498
507
  const params = [query]
499
508
  if (values && values.length > 0) {
500
509
  params.push(values)
501
510
  }
502
511
  if (query !== null) {
503
- this.client.query(...params).then(queryResponse => {
512
+ this.client.query(...params).then(queryResponse => {
513
+ console.log('queryResponse.rows')
514
+ console.log(queryResponse.rows)
504
515
  resolve({
505
516
  rowCount: queryResponse.rowCount,
506
- rows: queryResponse.rows
517
+ rows: this.prepareResponseRows(queryResponse.rows)
507
518
  })
508
519
  }, err => {
509
520
  if (err) {
@@ -2293,7 +2293,7 @@ class WebsyForm {
2293
2293
  }
2294
2294
  for (let key in d) {
2295
2295
  this.options.fields.forEach(f => {
2296
- if (f.field === key && d[key]) {
2296
+ if (f.field === key && typeof d[key] !== 'undefined') {
2297
2297
  this.setValue(key, d[key])
2298
2298
  // f.value = d[key]
2299
2299
  // const el = document.getElementById(`${this.elementId}_input_${f.field}`)
@@ -2298,7 +2298,7 @@ var WebsyForm = /*#__PURE__*/function () {
2298
2298
  }
2299
2299
  var _loop = function _loop(key) {
2300
2300
  _this16.options.fields.forEach(function (f) {
2301
- if (f.field === key && d[key]) {
2301
+ if (f.field === key && typeof d[key] !== 'undefined') {
2302
2302
  _this16.setValue(key, d[key]);
2303
2303
  // f.value = d[key]
2304
2304
  // const el = document.getElementById(`${this.elementId}_input_${f.field}`)