forj 0.1.6 → 0.1.8

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/types.ts CHANGED
@@ -1,5 +1,7 @@
1
- import z from 'zod'
2
- import QueryBuilder from './query-builder'
1
+ import type * as z from 'zod'
2
+ import type zm from 'zod/mini'
3
+ import type QueryBuilder from './query-builder'
4
+ import { types } from './utils'
3
5
 
4
6
  export type text = string
5
7
  export type real = number
@@ -15,21 +17,57 @@ export type Values = Value[]
15
17
  // export type WriteType = Primitive | ArrayBuffer | ArrayBufferView | undefined
16
18
  // export type ReadType = Primitive | any[]
17
19
 
20
+ export type QueryType = typeof types[keyof typeof types]
21
+ export type TableOpts = {
22
+ timestamps?: boolean,
23
+ createdAt?: boolean,
24
+ updatedAt?: boolean,
25
+ }
26
+
18
27
  export type Operator = '=' | '!=' | '<' | '>' | '<=' | '>=' | 'LIKE' // | 'IN' | 'NOT IN' | 'BETWEEN' | 'IS NULL' | 'IS NOT NULL'
19
28
  export type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc'
20
29
 
21
30
  export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'CROSS'
22
31
 
23
- export type DBSchema = z.ZodObject<any>
32
+ // type ZodRawShapeCompat = Record<string, z.ZodTypeAny>
33
+
34
+ // export type DBSchema = z.ZodObject<ZodRawShapeCompat>
35
+
36
+ // export type SchemaObject = ZodRawShapeCompat
37
+
38
+ // export type SchemaKeys<TSchema extends DBSchema | SchemaObject> =
39
+ // TSchema extends z.ZodObject<infer TShape extends ZodRawShapeCompat>
40
+ // ? keyof TShape
41
+ // : TSchema extends ZodRawShapeCompat
42
+ // ? keyof TSchema
43
+ // : never
44
+
45
+
46
+ export type DBSchema = z.ZodTypeAny | zm.ZodMiniObject
24
47
 
25
48
  export type SchemaObject = Record<string, z.ZodTypeAny>
26
- export type SchemaKeys<TSchema extends DBSchema> =
27
- TSchema extends z.ZodObject<infer TShape>
49
+
50
+ export type SchemaKeys<TSchema extends DBSchema | SchemaObject> =
51
+ TSchema extends { shape: infer TShape }
28
52
  ? keyof TShape
29
53
  : TSchema extends SchemaObject
30
54
  ? keyof TSchema
31
55
  : never
32
56
 
57
+
58
+ //////////////////////////
59
+ // funcionando no zod/v4
60
+ // export type DBSchema = z.ZodObject<z.ZodRawShape>
61
+
62
+ // export type SchemaObject = z.ZodRawShape
63
+
64
+ // export type SchemaKeys<TSchema extends DBSchema | SchemaObject> =
65
+ // TSchema extends z.ZodObject<infer TShape extends z.ZodRawShape>
66
+ // ? keyof TShape
67
+ // : TSchema extends z.ZodRawShape
68
+ // ? keyof TSchema
69
+ // : never
70
+
33
71
  // TODO: transform QueryBuilder<S, T, C> into a interface
34
72
  export type RunFn<S, T, C extends keyof T = keyof T> = (qb: QueryBuilder<S, T, C>) => Promise<Result<T, C>>
35
73
  // export type RunBatchFn<S, T, C extends keyof T = keyof T> = (qb: QueryBuilder<S, T, C>[]) => Promise<Result<T, C>>[]
@@ -53,65 +91,57 @@ export type Item<B, S extends keyof B, T = Pick<B, S>> = { [K in keyof T]: T[K]
53
91
 
54
92
  export type ClauseOperator = 'AND' | 'OR'
55
93
 
56
- export type WhereFn<T, C extends keyof T = keyof T> = (q: IClauseBuilder<T, C>) => void
57
- export type WhereArgs<T, C extends keyof T = keyof T> = [WhereFn<T, C>] | [C, T[C]] | [C, Operator, T[C]]
94
+ export type WhereFn<T> = (q: IClauseBuilder<T>) => void
95
+ export type WhereArgs<T, C extends keyof T = keyof T> = [WhereFn<T>] | [C, T[C]] | [C, Operator, T[C]]
58
96
 
59
- export interface IClauseBuilder<T, C extends keyof T = keyof T> {
60
- where(fn: WhereFn<T, C>): this
61
- where(column: C, value: T[C]): this
62
- where(column: C, operator: Operator, value: T[C]): this
63
- where(...args: WhereArgs<T>): this
97
+ export interface IClauseBuilder<T> {
98
+ where<K extends keyof T>(column: K, value: T[K]): this
99
+ where<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
64
100
 
65
- on(fn: WhereFn<T, C>): this
66
- on(column: C, value: T[C]): this
67
- on(column: C, operator: Operator, value: T[C]): this
68
- on(...args: WhereArgs<T>): this
101
+ on<K extends keyof T>(column: K, value: T[K]): this
102
+ on<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
69
103
 
70
- orWhere(fn: WhereFn<T, C>): this
71
- orWhere(column: C, value: T[C]): this
72
- orWhere(column: C, operator: Operator, value: T[C]): this
73
- orWhere(...args: WhereArgs<T>): this
104
+ orWhere<K extends keyof T>(column: K, value: T[K]): this
105
+ orWhere<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
74
106
 
75
- orOn(fn: WhereFn<T, C>): this
76
- orOn(column: C, value: T[C]): this
77
- orOn(column: C, operator: Operator, value: T[C]): this
78
- orOn(...args: WhereArgs<T>): this
107
+ orOn<K extends keyof T>(column: K, value: T[K]): this
108
+ orOn<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
79
109
 
80
- whereIn(column: C, values: T[C][]): this
81
- in(column: C, values: T[C][]): this
110
+ whereIn<K extends keyof T>(column: K, values: T[K][]): this
111
+ in<K extends keyof T>(column: K, values: T[K][]): this
82
112
 
83
- whereNotIn(column: C, values: T[C][]): this
84
- notIn(column: C, values: T[C][]): this
113
+ whereNotIn<K extends keyof T>(column: K, values: T[K][]): this
114
+ notIn<K extends keyof T>(column: K, values: T[K][]): this
85
115
 
86
- orWhereIn(column: C, values: T[C][]): this
87
- orIn(column: C, values: T[C][]): this
116
+ orWhereIn<K extends keyof T>(column: K, values: T[K][]): this
117
+ orIn<K extends keyof T>(column: K, values: T[K][]): this
88
118
 
89
- orWhereNotIn(column: C, values: T[C][]): this
90
- orNotIn(column: C, values: T[C][]): this
119
+ orWhereNotIn<K extends keyof T>(column: K, values: T[K][]): this
120
+ orNotIn<K extends keyof T>(column: K, values: T[K][]): this
91
121
 
92
- whereBetween(column: C, one: T[C], two: T[C]): this
93
- between(column: C, one: T[C], two: T[C]): this
122
+ whereBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
123
+ between<K extends keyof T>(column: K, one: T[K], two: T[K]): this
94
124
 
95
- orWhereBetween(column: C, one: T[C], two: T[C]): this
96
- orBetween(column: C, one: T[C], two: T[C]): this
125
+ orWhereBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
126
+ orBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
97
127
 
98
- whereNotBetween(column: C, one: T[C], two: T[C]): this
99
- notBetween(column: C, one: T[C], two: T[C]): this
128
+ whereNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
129
+ notBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
100
130
 
101
- orWhereNotBetween(column: C, one: T[C], two: T[C]): this
102
- orNotBetween(column: C, one: T[C], two: T[C]): this
131
+ orWhereNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
132
+ orNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
103
133
 
104
- whereNull(column: C): this
105
- onNull(column: C): this
134
+ whereNull<K extends keyof T>(column: K): this
135
+ onNull<K extends keyof T>(column: K): this
106
136
 
107
- orWhereNull(column: C): this
108
- orOnNull(column: C): this
137
+ orWhereNull<K extends keyof T>(column: K): this
138
+ orOnNull<K extends keyof T>(column: K): this
109
139
 
110
- whereNotNull(column: C): this
111
- onNotNull(column: C): this
140
+ whereNotNull<K extends keyof T>(column: K): this
141
+ onNotNull<K extends keyof T>(column: K): this
112
142
 
113
- orWhereNotNull(column: C): this
114
- orNotNull(column: C): this
143
+ orWhereNotNull<K extends keyof T>(column: K): this
144
+ orNotNull<K extends keyof T>(column: K): this
115
145
  }
116
146
 
117
147
  export type JoinArgs<S, J extends keyof S> =
@@ -123,7 +153,6 @@ export type JoinArgs<S, J extends keyof S> =
123
153
  | [keyof S[J], Operator, keyof S, keyof S[keyof S]]
124
154
 
125
155
  export interface IJoinBuilder<S> {
126
- join<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
127
156
  join<
128
157
  J extends keyof S,
129
158
  T extends S[J],
@@ -148,9 +177,7 @@ export interface IJoinBuilder<S> {
148
177
  J2 extends keyof S,
149
178
  C2 extends keyof S[J2],
150
179
  >(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
151
- join<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
152
180
 
153
- innerJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
154
181
  innerJoin<
155
182
  J extends keyof S,
156
183
  T extends S[J],
@@ -175,9 +202,7 @@ export interface IJoinBuilder<S> {
175
202
  J2 extends keyof S,
176
203
  C2 extends keyof S[J2],
177
204
  >(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
178
- innerJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
179
205
 
180
- leftJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
181
206
  leftJoin<
182
207
  J extends keyof S,
183
208
  T extends S[J],
@@ -202,9 +227,7 @@ export interface IJoinBuilder<S> {
202
227
  J2 extends keyof S,
203
228
  C2 extends keyof S[J2],
204
229
  >(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
205
- leftJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
206
230
 
207
- rightJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
208
231
  rightJoin<
209
232
  J extends keyof S,
210
233
  T extends S[J],
@@ -229,9 +252,7 @@ export interface IJoinBuilder<S> {
229
252
  J2 extends keyof S,
230
253
  C2 extends keyof S[J2],
231
254
  >(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
232
- rightJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
233
255
 
234
- crossJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
235
256
  crossJoin<
236
257
  J extends keyof S,
237
258
  T extends S[J],
@@ -256,5 +277,4 @@ export interface IJoinBuilder<S> {
256
277
  J2 extends keyof S,
257
278
  C2 extends keyof S[J2],
258
279
  >(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
259
- crossJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
260
280
  }
package/src/utils.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  import pluralize from 'pluralize'
2
- import type { ZodTypeAny } from 'zod'
2
+ import type * as z 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',
71
+ }
72
+
73
+ export const isZod = (obj: any): obj is z.ZodTypeAny => obj && typeof obj == 'object' && '_def' in obj
74
+
75
+ const getDef = (schema: any) => schema?._def ?? 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 ''
62
91
  }
63
92
 
64
- export const isZod = (obj: any): obj is ZodTypeAny => obj && typeof obj == 'object' && '_def' in obj
93
+ const unwrap = (schema: any): any => {
94
+ let current = schema
95
+ let allowNull = false
96
+ let allowUndefined = false
97
+
98
+ while (current?._def || current?.def) {
99
+ const def = current._def || current?.def
100
+ const type = getTypeName(def)
101
+
102
+ if (type == 'ZodNullable')
103
+ allowNull = true
65
104
 
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>))
105
+ if (type == 'ZodOptional' || type == 'ZodDefault')
106
+ allowUndefined = true
67
107
 
68
- export const zGet = (key: string, schema?: any): [string, ZodTypeAny] | false => {
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
+ }
148
+
149
+ export const zGet = (key: string, schema?: any): [string, z.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
+ const def = getDef(_schema.schema)
198
+ if (!def) return false
114
199
 
115
- if (!zType) return false
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) {