forj 0.1.6 → 0.1.7

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/src/utils.ts CHANGED
@@ -2,6 +2,13 @@ import pluralize from 'pluralize'
2
2
  import type { ZodTypeAny } from 'zod'
3
3
  import type { DBSchema } from './types'
4
4
 
5
+ export const types = {
6
+ SELECT: 1,
7
+ INSERT: 2,
8
+ UPDATE: 3,
9
+ DELETE: 4,
10
+ } as const
11
+
5
12
  const operators = ['=', '!=', '>', '<', '>=', '<=', 'LIKE', 'IN', 'NOT IN', 'IS', 'IS NOT', 'BETWEEN']
6
13
 
7
14
  export function isOperator(o: any) {
@@ -21,6 +28,8 @@ export function parseSelectColumn(
21
28
  return col
22
29
 
23
30
  const [table, column] = explicit ? col.split('.') : [baseTable, col]
31
+ if (column == '*') return col
32
+
24
33
  return `${table}.${column} AS ${pluralize(table, 1)}_${column}`
25
34
  }
26
35
 
@@ -48,33 +57,109 @@ export function formatValue(value: any): string {
48
57
  }
49
58
 
50
59
  const zodTypeMap: Record<string, string> = {
51
- 'ZodString': 'string',
52
- 'ZodNumber': 'number',
53
- 'ZodBoolean': 'boolean',
54
- 'ZodObject': 'object',
55
- 'ZodArray': 'array',
56
- 'ZodDate': 'object',
57
- 'ZodNull': 'object',
58
- 'ZodUndefined': 'undefined',
59
- 'ZodSymbol': 'symbol',
60
- 'ZodBigInt': 'bigint',
61
- 'ZodFunction': 'function',
60
+ ZodString: 'string',
61
+ ZodNumber: 'number',
62
+ ZodBoolean: 'boolean',
63
+ ZodObject: 'object',
64
+ ZodArray: 'array',
65
+ ZodDate: 'object',
66
+ ZodNull: 'object',
67
+ ZodUndefined: 'undefined',
68
+ ZodSymbol: 'symbol',
69
+ ZodBigInt: 'bigint',
70
+ ZodFunction: 'function',
62
71
  }
63
72
 
64
73
  export const isZod = (obj: any): obj is ZodTypeAny => obj && typeof obj == 'object' && '_def' in obj
65
74
 
66
- export const zHas = (key: string, schema?: any) => schema != null && typeof schema == 'object' && !Array.isArray(schema) && (key in schema || 'shape' in schema && key in (schema.shape as Record<string, ZodTypeAny>))
75
+ const getDef = (schema: any) => schema?._def ?? {}
76
+
77
+ const getTypeName = (def: any): string => {
78
+ if (!def) return ''
79
+ if (def.typeName) return def.typeName // zod v3
80
+ if (def.type) { // zod v4
81
+ if (typeof def.type == 'string') {
82
+ if (def.type.startsWith('Zod')) return def.type
83
+ return 'Zod'+ def.type[0].toUpperCase() + def.type.slice(1)
84
+ // return zodTypeMap[def.type] || def.type
85
+ }
86
+
87
+ if (def.type?.name) return def.type.name
88
+ }
89
+
90
+ return ''
91
+ }
92
+
93
+ const unwrap = (schema: any): any => {
94
+ let current = schema
95
+ let allowNull = false
96
+ let allowUndefined = false
97
+
98
+ while (current?._def) {
99
+ const def = current._def
100
+ const type = getTypeName(def)
101
+
102
+ if (type == 'ZodNullable')
103
+ allowNull = true
104
+
105
+ if (type == 'ZodOptional' || type == 'ZodDefault')
106
+ allowUndefined = true
107
+
108
+ if (['ZodOptional', 'ZodNullable', 'ZodDefault', 'ZodReadonly'].includes(type)) {
109
+ current = def.innerType
110
+ continue
111
+ }
112
+
113
+ if (type == 'ZodEffects' || type == 'ZodPipeline') {
114
+ current = def.schema || def.innerType || def.out
115
+ continue
116
+ }
117
+
118
+ break
119
+ }
120
+
121
+ return { schema: current, allowNull, allowUndefined }
122
+ // return current
123
+ }
124
+
125
+ export const zHas = (key: string, schema?: any): boolean => {
126
+ if (!schema || typeof schema != 'object' || Array.isArray(schema))
127
+ return false
128
+
129
+ const keys = key.split('.')
130
+
131
+ for (const k of keys) {
132
+ schema = unwrap(schema).schema
133
+
134
+ if (!schema || typeof schema != 'object')
135
+ return false
136
+
137
+ if (schema.shape && k in schema.shape) {
138
+ schema = schema.shape[k]
139
+ } else if (k in schema) {
140
+ schema = schema[k]
141
+ } else {
142
+ return false
143
+ }
144
+ }
145
+
146
+ return true
147
+ }
67
148
 
68
149
  export const zGet = (key: string, schema?: any): [string, ZodTypeAny] | false => {
69
150
  const keys = key.split('.')
70
- for (const i in keys) {
151
+
152
+ for (const k of keys) {
153
+ schema = unwrap(schema).schema
154
+
71
155
  if (typeof schema != 'object') return false
72
156
 
73
- const k = keys[i]
74
- if ('shape' in schema && k in schema.shape) {
157
+ if (schema?.shape && k in schema.shape) {
75
158
  schema = schema.shape[k]
76
159
  continue
77
- } else if (k in schema) {
160
+ }
161
+
162
+ if (k in schema) {
78
163
  schema = schema[k]
79
164
  continue
80
165
  }
@@ -86,44 +171,49 @@ export const zGet = (key: string, schema?: any): [string, ZodTypeAny] | false =>
86
171
  }
87
172
 
88
173
  export const zType = (key: string, schema?: any): string => {
89
- const _ = zGet(key, schema)
90
- if (!_ || !('_def' in _[1]))
174
+ const result = zGet(key, schema)
175
+ if (!result)
176
+ return 'unknown'
177
+
178
+ const type = getTypeName(getDef(unwrap(result[1]).schema))
179
+ if (!type)
91
180
  return 'unknown'
92
- key = _[0]
93
- schema = _[1]
94
181
 
95
- return ((schema?._def?.innerType?._def || schema?._def)?.typeName || '').split('Zod').pop().toLowerCase()
182
+ return type.replace('Zod', '').toLowerCase()
96
183
  }
97
184
 
98
185
  export const zSame = (key: string, val: any, schema?: any, deep: boolean = false): boolean => {
99
186
  if (!deep) {
100
- const _ = zGet(key, schema)
101
- if (!_) return _
102
- key = _[0]
103
- schema = _[1]
187
+ const result = zGet(key, schema)
188
+ if (!result) return false
189
+ schema = result[1]
104
190
  }
105
191
 
106
- if (!('_def' in schema))
107
- return false // typeof val == typeof schema[key] // TODO: improv it
192
+ const _schema = unwrap(schema)
108
193
 
109
- let def = schema?._def || {}
110
- if (schema?._def?.typeName == 'ZodOptional')
111
- def = def?.innerType?._def || {}
194
+ if (val === undefined) return _schema.allowUndefined
195
+ if (val === null) return _schema.allowNull
112
196
 
113
- const zType = def?.typeName || ''
197
+ if (!_schema.schema?._def) return false
114
198
 
115
- if (!zType) return false
199
+ const def = _schema.schema._def
200
+ const type = getTypeName(def)
116
201
 
117
- if (zType == 'ZodUnion' && def?.options?.length)
118
- return def?.options?.some((z: any) => zSame(key, val, z, true))
202
+ if (!type) return false
119
203
 
120
- else if (zType == 'ZodArray')
204
+ if (type == 'ZodUnion' && def.options)
205
+ return def.options.some((z: any) => zSame(key, val, z, true))
206
+
207
+ if (type == 'ZodArray')
121
208
  return Array.isArray(val)
122
209
 
123
- else if (zType == 'ZodDate')
210
+ if (type == 'ZodObject')
211
+ return typeof val == 'object' && val != null && !Array.isArray(val)
212
+
213
+ if (type == 'ZodDate')
124
214
  return val instanceof Date
125
215
 
126
- return typeof val == zodTypeMap[zType]
216
+ return typeof val == zodTypeMap[type]
127
217
  }
128
218
 
129
219
  export function isJoinCompare(val: any, schema?: DBSchema) {