@cuboapp/database 1.0.2 → 1.0.4

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.2",
3
+ "version": "1.0.4",
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) {
@@ -114,7 +122,11 @@ export class Database {
114
122
 
115
123
  switch (this.connectOptions.dialect) {
116
124
  case 'postgres':
117
- sql = `update ${table} set ${prepared.query} where ${where} returning ${pkKey}`
125
+ if (pkKey) {
126
+ sql = `update ${table} set ${prepared.query} where ${where} returning ${pkKey}`
127
+ } else {
128
+ sql = `update ${table} set ${prepared.query} where ${where}`
129
+ }
118
130
  break
119
131
  case 'mysql':
120
132
  sql = `update ${table} set ${prepared.query} where ${where}`
@@ -140,7 +152,7 @@ export class Database {
140
152
  case 'postgres':
141
153
  const res = result as unknown as [{ [pkKey]: T }[], T]
142
154
 
143
- return res[0].map((r) => (pkType === 'number' ? +r[pkKey] : r[pkKey])) as T[]
155
+ return res[0].map((r) => (!!pkKey ? (pkType === 'number' ? +r[pkKey] : r[pkKey]) : r)) as T[]
144
156
  case 'mysql':
145
157
  return res as T[]
146
158
  }
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 (value?.constructor?.name === 'Literal') {
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 (value?.constructor?.name === 'Literal') {
65
+ val = value
66
+ } else {
67
+ val = value ? JSON.stringify(value) : null
68
+ }
59
69
  }
60
70
  }
61
71