@starbemtech/star-db-query-builder 1.0.0 → 1.0.2

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.
Files changed (48) hide show
  1. package/README.md +1 -0
  2. package/coverage/clover.xml +6 -0
  3. package/coverage/coverage-final.json +1 -0
  4. package/coverage/lcov-report/base.css +224 -0
  5. package/coverage/lcov-report/block-navigation.js +87 -0
  6. package/coverage/lcov-report/favicon.png +0 -0
  7. package/coverage/lcov-report/index.html +101 -0
  8. package/coverage/lcov-report/prettify.css +1 -0
  9. package/coverage/lcov-report/prettify.js +2 -0
  10. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  11. package/coverage/lcov-report/sorter.js +196 -0
  12. package/coverage/lcov.info +0 -0
  13. package/dist/index.d.ts +4 -0
  14. package/dist/index.js.map +1 -1
  15. package/index.d.ts +5 -1
  16. package/index.ts +6 -0
  17. package/package.json +3 -2
  18. package/.eslintrc.json +0 -32
  19. package/.husky/pre-commit +0 -1
  20. package/dist/.eslintrc.json +0 -32
  21. package/dist/src/db/IDatabaseClient.d.ts +0 -5
  22. package/dist/src/db/IDatabaseClient.js +0 -3
  23. package/dist/src/db/IDatabaseClient.js.map +0 -1
  24. package/dist/src/db/initDb.d.ts +0 -6
  25. package/dist/src/db/initDb.js +0 -30
  26. package/dist/src/db/initDb.js.map +0 -1
  27. package/dist/src/db/mysqlClient.d.ts +0 -3
  28. package/dist/src/db/mysqlClient.js +0 -12
  29. package/dist/src/db/mysqlClient.js.map +0 -1
  30. package/dist/src/db/pgClient.d.ts +0 -3
  31. package/dist/src/db/pgClient.js +0 -12
  32. package/dist/src/db/pgClient.js.map +0 -1
  33. package/dist/src/default/genericRepository.d.ts +0 -14
  34. package/dist/src/default/genericRepository.js +0 -97
  35. package/dist/src/default/genericRepository.js.map +0 -1
  36. package/dist/src/default/types.d.ts +0 -35
  37. package/dist/src/default/types.js +0 -3
  38. package/dist/src/default/types.js.map +0 -1
  39. package/dist/src/default/utils.d.ts +0 -8
  40. package/dist/src/default/utils.js +0 -91
  41. package/dist/src/default/utils.js.map +0 -1
  42. package/src/db/IDatabaseClient.ts +0 -6
  43. package/src/db/initDb.ts +0 -27
  44. package/src/db/mysqlClient.ts +0 -10
  45. package/src/db/pgClient.ts +0 -10
  46. package/src/default/genericRepository.ts +0 -143
  47. package/src/default/types.ts +0 -53
  48. package/src/default/utils.ts +0 -107
@@ -1,143 +0,0 @@
1
- import { QueryParams } from './types'
2
- import {
3
- createGroupByClause,
4
- createLimitClause,
5
- createOrderByClause,
6
- generatePlaceholders,
7
- createSelectFields,
8
- createWhereClause,
9
- generateSetClause,
10
- } from './utils'
11
-
12
- export const findFirst = async <T>({
13
- tableName,
14
- dbClient,
15
- select,
16
- where,
17
- groupBy,
18
- orderBy,
19
- }: QueryParams): Promise<T> => {
20
- if (!tableName) throw new Error('Table name is required')
21
- if (!dbClient) throw new Error('DB client is required')
22
-
23
- const fields = createSelectFields(select)
24
- const [whereClause, params] = createWhereClause(where)
25
- const orderByClause = createOrderByClause(orderBy)
26
- const groupByClause = createGroupByClause(groupBy)
27
-
28
- const rows = await dbClient.query<T>(
29
- `SELECT ${fields} FROM ${tableName}
30
- ${whereClause.length > 7 ? whereClause : ''}
31
- ${groupByClause}
32
- ${orderByClause}
33
- `,
34
- [params]
35
- )
36
-
37
- return rows[0] || null
38
- }
39
-
40
- export const findMany = async <T>({
41
- tableName,
42
- dbClient,
43
- select,
44
- where,
45
- groupBy,
46
- orderBy,
47
- limit,
48
- }: QueryParams): Promise<T[]> => {
49
- if (!tableName) throw new Error('Table name is required')
50
- if (!dbClient) throw new Error('DB client is required')
51
-
52
- const fields = createSelectFields(select)
53
- const [whereClause, params] = createWhereClause(where)
54
- const orderByClause = createOrderByClause(orderBy)
55
- const groupByClause = createGroupByClause(groupBy)
56
- const limitClause = createLimitClause(limit)
57
-
58
- const rows = await dbClient.query<T[]>(
59
- `SELECT ${fields} FROM ${tableName}
60
- ${whereClause.length > 7 ? whereClause : ''}
61
- ${groupByClause}
62
- ${orderByClause}
63
- ${limitClause}
64
- `,
65
- [params]
66
- )
67
-
68
- return rows || []
69
- }
70
-
71
- export const insert = async <T>({
72
- tableName,
73
- dbClient,
74
- data,
75
- returning,
76
- }: QueryParams & { data: T; returning?: string[] }): Promise<T | void> => {
77
- if (!tableName) throw new Error('Table name is required')
78
- if (!dbClient) throw new Error('DB client is required')
79
- if (!data) throw new Error('Data object is required')
80
-
81
- const keys = Object.keys(data)
82
- const values = Object.values(data)
83
-
84
- const placeholders = generatePlaceholders(keys, dbClient.clientType)
85
- let query = `INSERT INTO ${tableName} (${keys.join(', ')}) VALUES (${placeholders})`
86
-
87
- if (dbClient.clientType === 'pg') {
88
- if (returning && returning.length > 0) {
89
- query += ` RETURNING ${createSelectFields(returning)}`
90
- }
91
- }
92
-
93
- const inserted = await dbClient.query<T[]>(query, values)
94
-
95
- if (dbClient.clientType === 'pg') return inserted[0]
96
- }
97
-
98
- export const update = async <T>({
99
- tableName,
100
- dbClient,
101
- id,
102
- data,
103
- returning,
104
- }: QueryParams & { data: T; returning?: string[] }): Promise<T | void> => {
105
- if (!tableName) throw new Error('Table name is required')
106
- if (!dbClient) throw new Error('DB client is required')
107
- if (!id) throw new Error('ID is required')
108
- if (!data) throw new Error('Data object is required')
109
-
110
- const keys = Object.keys(data)
111
- const values: any[] = Object.values(data)
112
-
113
- const setClause = generateSetClause(keys, dbClient.clientType)
114
- let query = `UPDATE ${tableName} SET ${setClause} WHERE ${id}`
115
-
116
- if (dbClient.clientType === 'pg') {
117
- if (returning && returning.length > 0) {
118
- query += ` RETURNIN ${createSelectFields(returning)}`
119
- }
120
- }
121
-
122
- const updated = await dbClient.query<T[]>(query, values)
123
-
124
- if (dbClient.clientType === 'pg') return updated[0]
125
- }
126
-
127
- export const deleteOne = async ({
128
- tableName,
129
- dbClient,
130
- id,
131
- permanently = false,
132
- }: QueryParams & { permanently?: boolean }): Promise<void> => {
133
- if (!tableName) throw new Error('Table name is required')
134
- if (!dbClient) throw new Error('DB client is required')
135
- if (!id) throw new Error('ID is required')
136
-
137
- await dbClient.query(
138
- permanently
139
- ? `DELETE FROM ${tableName} WHERE id = ${dbClient.clientType === 'pg' ? '$1' : '?'}`
140
- : `UPDATE ${tableName} SET status = 'deleted' WHERE id = ${dbClient.clientType === 'pg' ? '$1' : '?'}`,
141
- [id]
142
- )
143
- }
@@ -1,53 +0,0 @@
1
- import { IDatabaseClient } from '../db/IDatabaseClient'
2
-
3
- export interface QueryExec {
4
- text: string
5
- values?: any[]
6
- }
7
-
8
- type SimpleValue = string | number | boolean | Date
9
-
10
- type ConditionOperator = 'AND' | 'OR'
11
-
12
- interface OperatorCondition {
13
- operator:
14
- | 'LIKE'
15
- | '='
16
- | '>'
17
- | '<'
18
- | 'IN'
19
- | 'BETWEEN'
20
- | '!='
21
- | '<='
22
- | '>='
23
- | 'NOT IN'
24
- | 'LIKE'
25
- | 'NOT LIKE'
26
- value: SimpleValue | SimpleValue[]
27
- }
28
-
29
- export type DBClients = 'pg' | 'mysql'
30
-
31
- export interface CompositeCondition {
32
- type: ConditionOperator
33
- conditions: Condition[]
34
- }
35
-
36
- export type Condition = SimpleValue | OperatorCondition | CompositeCondition
37
-
38
- export interface Conditions {
39
- [key: string]: Condition
40
- }
41
-
42
- export type OrderBy = { field: string; direction: 'ASC' | 'DESC' }[]
43
-
44
- export interface QueryParams {
45
- tableName: string
46
- dbClient: IDatabaseClient
47
- id?: string
48
- select?: string[]
49
- where?: Conditions
50
- orderBy?: OrderBy
51
- groupBy?: string[]
52
- limit?: number
53
- }
@@ -1,107 +0,0 @@
1
- import {
2
- Conditions,
3
- Condition,
4
- CompositeCondition,
5
- OrderBy,
6
- DBClients,
7
- } from './types'
8
-
9
- const arrayToStringWithQuotes = (items: string[]): string => {
10
- const itemsWithQuotes = items.map((item) => `"${item}"`)
11
- return itemsWithQuotes.join(', ')
12
- }
13
-
14
- export const createSelectFields = (fields?: string[]): string => {
15
- return fields && fields.length > 0 ? arrayToStringWithQuotes(fields) : '*'
16
- }
17
-
18
- export const generatePlaceholders = (
19
- keys: any[],
20
- clientType: DBClients
21
- ): string => {
22
- return keys
23
- .map((_, index) => (clientType === 'pg' ? `$${index + 1}` : '?'))
24
- .join(', ')
25
- }
26
-
27
- export const generateSetClause = (
28
- keys: any[],
29
- clientType: DBClients
30
- ): string => {
31
- return keys
32
- .map((key, index) =>
33
- clientType === 'pg' ? `${key} = $${index + 1}` : `${key} = ?`
34
- )
35
- .join(', ')
36
- }
37
-
38
- export const createWhereClause = (
39
- conditions: Conditions = {},
40
- startIndex: number = 1
41
- ): [string, any[], number] => {
42
- let index = startIndex
43
- const whereParts: string[] = []
44
- const values: any[] = []
45
-
46
- const processCondition = (key: string, condition: Condition): number => {
47
- if (typeof condition === 'object' && condition !== null) {
48
- if ('operator' in condition && 'value' in condition) {
49
- const { operator, value } = condition
50
- if (Array.isArray(value)) {
51
- const placeholders = value.map(() => `$${index++}`).join(', ')
52
- if (operator === 'BETWEEN') {
53
- whereParts.push(
54
- `"${key}" ${operator} ${placeholders.replace(', ', ' AND ')}`
55
- )
56
- } else {
57
- whereParts.push(`"${key}" ${operator} (${placeholders})`)
58
- }
59
- values.push(...value)
60
- } else {
61
- whereParts.push(`"${key}" ${operator} $${index++}`)
62
- values.push(value)
63
- }
64
- } else if ('type' in condition && 'conditions' in condition) {
65
- const compositeCondition: CompositeCondition = condition
66
- const subWhereParts: string[] = []
67
- compositeCondition.conditions.forEach((subCondition) => {
68
- // Recurso: criar uma chave fictícia, pois as condições compostas não necessitam de chave
69
- index = processCondition('', subCondition)
70
- })
71
- whereParts.push(
72
- `(${subWhereParts.join(` ${compositeCondition.type} `)})`
73
- )
74
- }
75
- } else if (key) {
76
- // Condição simples
77
- whereParts.push(`"${key}" = $${index}`)
78
- values.push(condition)
79
- index++
80
- }
81
- return index
82
- }
83
-
84
- Object.entries(conditions).forEach(([key, value]) => {
85
- index = processCondition(key, value)
86
- })
87
-
88
- const whereClause =
89
- whereParts.length > 0 ? ` WHERE ${whereParts.join(' AND ')}` : ''
90
- return [whereClause, values, index]
91
- }
92
-
93
- export const createOrderByClause = (orderBy?: OrderBy) => {
94
- if (!orderBy || orderBy.length === 0) return ''
95
- const clause = orderBy.map((o) => `${o.field} ${o.direction}`).join(', ')
96
- return ` ORDER BY ${clause}`
97
- }
98
-
99
- export const createGroupByClause = (groupBy?: string[]) => {
100
- if (!groupBy || groupBy.length === 0) return ''
101
- return ` GROUP BY ${groupBy.join(', ')}`
102
- }
103
-
104
- export const createLimitClause = (limit?: number) => {
105
- if (!limit) return ''
106
- return ` LIMIT ${limit}`
107
- }