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/package.json +5 -5
- package/src/clause-builder.ts +26 -26
- package/src/d1/model.ts +6 -5
- package/src/dynamodb/schema.ts +45 -17
- package/src/model.ts +270 -14
- package/src/query-builder.ts +245 -43
- package/src/types.ts +49 -58
- package/src/utils.ts +127 -37
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
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
|
-
|
|
151
|
+
|
|
152
|
+
for (const k of keys) {
|
|
153
|
+
schema = unwrap(schema).schema
|
|
154
|
+
|
|
71
155
|
if (typeof schema != 'object') return false
|
|
72
156
|
|
|
73
|
-
|
|
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
|
-
}
|
|
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
|
|
90
|
-
if (!
|
|
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
|
|
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
|
|
101
|
-
if (!
|
|
102
|
-
|
|
103
|
-
schema = _[1]
|
|
187
|
+
const result = zGet(key, schema)
|
|
188
|
+
if (!result) return false
|
|
189
|
+
schema = result[1]
|
|
104
190
|
}
|
|
105
191
|
|
|
106
|
-
|
|
107
|
-
return false // typeof val == typeof schema[key] // TODO: improv it
|
|
192
|
+
const _schema = unwrap(schema)
|
|
108
193
|
|
|
109
|
-
|
|
110
|
-
if (
|
|
111
|
-
def = def?.innerType?._def || {}
|
|
194
|
+
if (val === undefined) return _schema.allowUndefined
|
|
195
|
+
if (val === null) return _schema.allowNull
|
|
112
196
|
|
|
113
|
-
|
|
197
|
+
if (!_schema.schema?._def) return false
|
|
114
198
|
|
|
115
|
-
|
|
199
|
+
const def = _schema.schema._def
|
|
200
|
+
const type = getTypeName(def)
|
|
116
201
|
|
|
117
|
-
if (
|
|
118
|
-
return def?.options?.some((z: any) => zSame(key, val, z, true))
|
|
202
|
+
if (!type) return false
|
|
119
203
|
|
|
120
|
-
|
|
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
|
-
|
|
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[
|
|
216
|
+
return typeof val == zodTypeMap[type]
|
|
127
217
|
}
|
|
128
218
|
|
|
129
219
|
export function isJoinCompare(val: any, schema?: DBSchema) {
|