@cuboapp/database 1.0.3 → 1.0.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuboapp/database",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Database methods",
5
5
  "main": "src/index.ts",
6
6
  "repository": "git@cuboapp.gitlab.yandexcloud.net:cubo/database.git",
package/src/db/index.ts CHANGED
@@ -59,7 +59,11 @@ export class Database {
59
59
 
60
60
  switch (this.connectOptions.dialect) {
61
61
  case 'postgres':
62
- sql = `insert into ${table} (${keys}) values (${values}) returning ${pkKey}`
62
+ if (!!pkKey) {
63
+ sql = `insert into ${table} (${keys}) values (${values}) returning ${pkKey}`
64
+ } else {
65
+ sql = `insert into ${table} (${keys}) values (${values})`
66
+ }
63
67
  break
64
68
  case 'mysql':
65
69
  sql = `insert into ${table} (${keys}) values (${values})`
@@ -79,6 +83,10 @@ export class Database {
79
83
  transaction: opts?.transaction
80
84
  })
81
85
  .then((res: any) => {
86
+ if (!pkKey) {
87
+ return []
88
+ }
89
+
82
90
  let pk = []
83
91
 
84
92
  switch (this.connectOptions.dialect) {
package/src/db/utils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Options, QueryTypes, Sequelize, Transaction } from 'sequelize'
1
+ import { literal, Options, QueryTypes, Sequelize, Transaction } from 'sequelize'
2
2
 
3
3
  import { Database } from '.'
4
4
  import { DatabaseOptions } from './types'
@@ -22,9 +22,13 @@ export function dbPrepareUpdateQueryString(obj: Record<string, any>, prefix = ''
22
22
  val = value
23
23
  } else if (Array.isArray(value) || typeof value === 'object') {
24
24
  if (Buffer.isBuffer(value)) {
25
- val = value
25
+ val = literal(value.toString('utf-8'))
26
26
  } else {
27
- val = value ? JSON.stringify(value) : null
27
+ if (['Literal', 'Date'].includes(value?.constructor?.name)) {
28
+ val = value
29
+ } else {
30
+ val = value ? JSON.stringify(value) : null
31
+ }
28
32
  }
29
33
  }
30
34
 
@@ -45,6 +49,8 @@ export function dbPrepareInsertQueryString(obj: Record<string, any>, prefix = ''
45
49
 
46
50
  let val = null
47
51
 
52
+ // console.log(key, typeof value, value, value?.constructor?.name, value?.name)
53
+
48
54
  if (typeof value === 'string') {
49
55
  val = !!value ? value : null
50
56
  } else if (typeof value === 'number') {
@@ -53,9 +59,13 @@ export function dbPrepareInsertQueryString(obj: Record<string, any>, prefix = ''
53
59
  val = value
54
60
  } else if (Array.isArray(value) || typeof value === 'object') {
55
61
  if (Buffer.isBuffer(value)) {
56
- val = value
62
+ val = literal(value.toString('utf-8'))
57
63
  } else {
58
- val = value ? JSON.stringify(value) : null
64
+ if (['Literal', 'Date'].includes(value?.constructor?.name)) {
65
+ val = value
66
+ } else {
67
+ val = value ? JSON.stringify(value) : null
68
+ }
59
69
  }
60
70
  }
61
71