forj 0.0.1 → 0.0.3

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 ADDED
@@ -0,0 +1,260 @@
1
+ import z from 'zod'
2
+ import QueryBuilder from './query-builder'
3
+
4
+ export type text = string
5
+ export type real = number
6
+ export type integer = number
7
+ export type bool = boolean | 0 | 1
8
+
9
+ export type Primitive = null | number | string | boolean
10
+ export type Primitives = Primitive[]
11
+
12
+ export type Value = Primitive | undefined
13
+ export type Values = Value[]
14
+
15
+ // export type WriteType = Primitive | ArrayBuffer | ArrayBufferView | undefined
16
+ // export type ReadType = Primitive | any[]
17
+
18
+ export type Operator = '=' | '!=' | '<' | '>' | '<=' | '>=' | 'LIKE' // | 'IN' | 'NOT IN' | 'BETWEEN' | 'IS NULL' | 'IS NOT NULL'
19
+ export type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc'
20
+
21
+ export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'CROSS'
22
+
23
+ export type DBSchema = z.ZodObject<any>
24
+
25
+ export type SchemaObject = Record<string, z.ZodTypeAny>
26
+ export type SchemaKeys<TSchema extends DBSchema> =
27
+ TSchema extends z.ZodObject<infer TShape>
28
+ ? keyof TShape
29
+ : TSchema extends SchemaObject
30
+ ? keyof TSchema
31
+ : never
32
+
33
+ // TODO: transform QueryBuilder<S, T, C> into a interface
34
+ export type RunFn<S, T, C extends keyof T = keyof T> = (qb: QueryBuilder<S, T, C>) => Promise<Result<T, C>>
35
+ // export type RunBatchFn<S, T, C extends keyof T = keyof T> = (qb: QueryBuilder<S, T, C>[]) => Promise<Result<T, C>>[]
36
+
37
+ export type Pipe<S, T, C extends keyof T = keyof T> = {
38
+ run: RunFn<S, T, C>,
39
+ // batch: RunBatchFn<S, T, C>,
40
+ }
41
+
42
+ export type Result<T, C extends keyof T> = {
43
+ changes: number,
44
+ duration: number,
45
+ lastId?: number | string,
46
+ rowsRead: number,
47
+ rowsWritten: number,
48
+ success: boolean,
49
+ results: Item<T, C>[],
50
+ }
51
+
52
+ export type Item<B, S extends keyof B, T = Pick<B, S>> = { [K in keyof T]: T[K] } & {}
53
+
54
+ export type ClauseOperator = 'AND' | 'OR'
55
+
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]]
58
+
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
64
+
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
69
+
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
74
+
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
79
+
80
+ whereIn(column: C, values: T[C][]): this
81
+ in(column: C, values: T[C][]): this
82
+
83
+ whereNotIn(column: C, values: T[C][]): this
84
+ notIn(column: C, values: T[C][]): this
85
+
86
+ orWhereIn(column: C, values: T[C][]): this
87
+ orIn(column: C, values: T[C][]): this
88
+
89
+ orWhereNotIn(column: C, values: T[C][]): this
90
+ orNotIn(column: C, values: T[C][]): this
91
+
92
+ whereBetween(column: C, one: T[C], two: T[C]): this
93
+ between(column: C, one: T[C], two: T[C]): this
94
+
95
+ orWhereBetween(column: C, one: T[C], two: T[C]): this
96
+ orBetween(column: C, one: T[C], two: T[C]): this
97
+
98
+ whereNotBetween(column: C, one: T[C], two: T[C]): this
99
+ notBetween(column: C, one: T[C], two: T[C]): this
100
+
101
+ orWhereNotBetween(column: C, one: T[C], two: T[C]): this
102
+ orNotBetween(column: C, one: T[C], two: T[C]): this
103
+
104
+ whereNull(column: C): this
105
+ onNull(column: C): this
106
+
107
+ orWhereNull(column: C): this
108
+ orOnNull(column: C): this
109
+
110
+ whereNotNull(column: C): this
111
+ onNotNull(column: C): this
112
+
113
+ orWhereNotNull(column: C): this
114
+ orNotNull(column: C): this
115
+ }
116
+
117
+ export type JoinArgs<S, J extends keyof S> =
118
+ [WhereFn<S[J]>]
119
+ | [keyof S[J], S[J][keyof S[J]]]
120
+ | [keyof S[J], Operator, S[J][keyof S[J]]]
121
+ | [keyof S[J], keyof S, keyof S[keyof S]]
122
+ | [keyof S[J], Operator, S[J][keyof S[J]]]
123
+ | [keyof S[J], Operator, keyof S, keyof S[keyof S]]
124
+
125
+ export interface IJoinBuilder<S> {
126
+ join<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
127
+ join<
128
+ J extends keyof S,
129
+ T extends S[J],
130
+ C extends keyof T
131
+ >(table: J, column: C, value: T[C]): this
132
+ join<
133
+ J extends keyof S,
134
+ T extends S[J],
135
+ C extends keyof T
136
+ >(table: J, column: C, operator: Operator, value: T[C]): this
137
+ join<
138
+ J extends keyof S,
139
+ T extends S[J],
140
+ C extends keyof T,
141
+ J2 extends keyof S,
142
+ C2 extends keyof S[J2],
143
+ >(table: J, column: C, table2: J2, column2: C2): this
144
+ join<
145
+ J extends keyof S,
146
+ T extends S[J],
147
+ C extends keyof T,
148
+ J2 extends keyof S,
149
+ C2 extends keyof S[J2],
150
+ >(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
+
153
+ innerJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
154
+ innerJoin<
155
+ J extends keyof S,
156
+ T extends S[J],
157
+ C extends keyof T
158
+ >(table: J, column: C, value: T[C]): this
159
+ innerJoin<
160
+ J extends keyof S,
161
+ T extends S[J],
162
+ C extends keyof T
163
+ >(table: J, column: C, operator: Operator, value: T[C]): this
164
+ innerJoin<
165
+ J extends keyof S,
166
+ T extends S[J],
167
+ C extends keyof T,
168
+ J2 extends keyof S,
169
+ C2 extends keyof S[J2],
170
+ >(table: J, column: C, table2: J2, column2: C2): this
171
+ innerJoin<
172
+ J extends keyof S,
173
+ T extends S[J],
174
+ C extends keyof T,
175
+ J2 extends keyof S,
176
+ C2 extends keyof S[J2],
177
+ >(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
+
180
+ leftJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
181
+ leftJoin<
182
+ J extends keyof S,
183
+ T extends S[J],
184
+ C extends keyof T
185
+ >(table: J, column: C, value: T[C]): this
186
+ leftJoin<
187
+ J extends keyof S,
188
+ T extends S[J],
189
+ C extends keyof T
190
+ >(table: J, column: C, operator: Operator, value: T[C]): this
191
+ leftJoin<
192
+ J extends keyof S,
193
+ T extends S[J],
194
+ C extends keyof T,
195
+ J2 extends keyof S,
196
+ C2 extends keyof S[J2],
197
+ >(table: J, column: C, table2: J2, column2: C2): this
198
+ leftJoin<
199
+ J extends keyof S,
200
+ T extends S[J],
201
+ C extends keyof T,
202
+ J2 extends keyof S,
203
+ C2 extends keyof S[J2],
204
+ >(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
+
207
+ rightJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
208
+ rightJoin<
209
+ J extends keyof S,
210
+ T extends S[J],
211
+ C extends keyof T
212
+ >(table: J, column: C, value: T[C]): this
213
+ rightJoin<
214
+ J extends keyof S,
215
+ T extends S[J],
216
+ C extends keyof T
217
+ >(table: J, column: C, operator: Operator, value: T[C]): this
218
+ rightJoin<
219
+ J extends keyof S,
220
+ T extends S[J],
221
+ C extends keyof T,
222
+ J2 extends keyof S,
223
+ C2 extends keyof S[J2],
224
+ >(table: J, column: C, table2: J2, column2: C2): this
225
+ rightJoin<
226
+ J extends keyof S,
227
+ T extends S[J],
228
+ C extends keyof T,
229
+ J2 extends keyof S,
230
+ C2 extends keyof S[J2],
231
+ >(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
+
234
+ crossJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
235
+ crossJoin<
236
+ J extends keyof S,
237
+ T extends S[J],
238
+ C extends keyof T
239
+ >(table: J, column: C, value: T[C]): this
240
+ crossJoin<
241
+ J extends keyof S,
242
+ T extends S[J],
243
+ C extends keyof T
244
+ >(table: J, column: C, operator: Operator, value: T[C]): this
245
+ crossJoin<
246
+ J extends keyof S,
247
+ T extends S[J],
248
+ C extends keyof T,
249
+ J2 extends keyof S,
250
+ C2 extends keyof S[J2],
251
+ >(table: J, column: C, table2: J2, column2: C2): this
252
+ crossJoin<
253
+ J extends keyof S,
254
+ T extends S[J],
255
+ C extends keyof T,
256
+ J2 extends keyof S,
257
+ C2 extends keyof S[J2],
258
+ >(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
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,134 @@
1
+ import pluralize from 'pluralize'
2
+ import type { ZodTypeAny } from 'zod'
3
+ import type { DBSchema } from './types'
4
+
5
+ const operators = ['=', '!=', '>', '<', '>=', '<=', 'LIKE', 'IN', 'NOT IN', 'IS', 'IS NOT', 'BETWEEN']
6
+
7
+ export function isOperator(o: any) {
8
+ return typeof o == 'string' && operators.includes(o)
9
+ }
10
+
11
+ export function parseSelectColumn(
12
+ col: string,
13
+ baseTable: string,
14
+ hasJoin: boolean
15
+ ): string {
16
+ if (col.toLowerCase().includes(' as '))
17
+ return col
18
+
19
+ const explicit = col.includes('.')
20
+ if (!hasJoin && !explicit)
21
+ return col
22
+
23
+ const [table, column] = explicit ? col.split('.') : [baseTable, col]
24
+ return `${table}.${column} AS ${pluralize(table, 1)}_${column}`
25
+ }
26
+
27
+ export function parseColumn(name: string, table: string, hasJoin: boolean = true) {
28
+ return !hasJoin || name.includes('.') ? name : table +'.'+ name
29
+ }
30
+
31
+ export function formatValue(value: any): string {
32
+ if (value == null || value == undefined)
33
+ return 'NULL'
34
+
35
+ const type = typeof value
36
+ if (type == 'number' || type == 'bigint')
37
+ return String(value)
38
+
39
+ if (type == 'boolean')
40
+ return value ? '1' : '0'
41
+
42
+ if (value instanceof Date)
43
+ return `'${value.toISOString().slice(0, 19).replace('T', ' ')}'`
44
+
45
+ return `'${String(value).replace(/'/g, "''")}'`
46
+ }
47
+
48
+ const zodTypeMap: Record<string, string> = {
49
+ 'ZodString': 'string',
50
+ 'ZodNumber': 'number',
51
+ 'ZodBoolean': 'boolean',
52
+ 'ZodObject': 'object',
53
+ 'ZodArray': 'array',
54
+ 'ZodDate': 'object',
55
+ 'ZodNull': 'object',
56
+ 'ZodUndefined': 'undefined',
57
+ 'ZodSymbol': 'symbol',
58
+ 'ZodBigInt': 'bigint',
59
+ 'ZodFunction': 'function',
60
+ }
61
+
62
+ export const isZod = (obj: any): obj is ZodTypeAny => obj && typeof obj == 'object' && '_def' in obj
63
+
64
+ 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>))
65
+
66
+ export const zGet = (key: string, schema?: any): [string, ZodTypeAny] | false => {
67
+ const keys = key.split('.')
68
+ for (const i in keys) {
69
+ if (typeof schema != 'object') return false
70
+
71
+ const k = keys[i]
72
+ if ('shape' in schema && k in schema.shape) {
73
+ schema = schema.shape[k]
74
+ continue
75
+ } else if (k in schema) {
76
+ schema = schema[k]
77
+ continue
78
+ }
79
+
80
+ return false
81
+ }
82
+
83
+ return [keys[keys.length - 1], schema]
84
+ }
85
+
86
+ export const zType = (key: string, schema?: any): string => {
87
+ const _ = zGet(key, schema)
88
+ if (!_ || !('_def' in _[1]))
89
+ return 'unknown'
90
+ key = _[0]
91
+ schema = _[1]
92
+
93
+ return ((schema?._def?.innerType?._def || schema?._def)?.typeName || '').split('Zod').pop().toLowerCase()
94
+ }
95
+
96
+ export const zSame = (key: string, val: any, schema?: any, deep: boolean = false): boolean => {
97
+ if (!deep) {
98
+ const _ = zGet(key, schema)
99
+ if (!_) return _
100
+ key = _[0]
101
+ schema = _[1]
102
+ }
103
+
104
+ if (!('_def' in schema))
105
+ return false // typeof val == typeof schema[key] // TODO: improv it
106
+
107
+ let def = schema?._def || {}
108
+ if (schema?._def?.typeName == 'ZodOptional')
109
+ def = def?.innerType?._def || {}
110
+
111
+ const zType = def?.typeName || ''
112
+
113
+ if (!zType) return false
114
+
115
+ if (zType == 'ZodUnion' && def?.options?.length)
116
+ return def?.options?.some((z: any) => zSame(key, val, z, true))
117
+
118
+ else if (zType == 'ZodArray')
119
+ return Array.isArray(val)
120
+
121
+ else if (zType == 'ZodDate')
122
+ return val instanceof Date
123
+
124
+ return typeof val == zodTypeMap[zType]
125
+ }
126
+
127
+ export function isJoinCompare(val: any, schema?: DBSchema) {
128
+ // if (!schema) return typeof val == 'string' && val?.includes('.')
129
+ if (!schema || typeof val != 'string' || !val?.includes('.'))
130
+ return false
131
+
132
+ const keys = zGet(val, schema)
133
+ return keys && keys?.length
134
+ }
@@ -1 +0,0 @@
1
- import e from"pluralize";var t=["=","!=",">","<",">=","<=","LIKE","IN","NOT IN","IS","IS NOT","BETWEEN"];function r(e,t,r=!0){return!r||e.includes(".")?e:t+"."+e}function s(e){if(null==e||null==e)return"NULL";const t=typeof e;return"number"==t||"bigint"==t?String(e):"boolean"==t?e?"1":"0":e instanceof Date?`'${e.toISOString().slice(0,19).replace("T"," ")}'`:`'${String(e).replace(/'/g,"''")}'`}var n={ZodString:"string",ZodNumber:"number",ZodBoolean:"boolean",ZodObject:"object",ZodArray:"array",ZodDate:"object",ZodNull:"object",ZodUndefined:"undefined",ZodSymbol:"symbol",ZodBigInt:"bigint",ZodFunction:"function"},i=(e,t)=>{const r=e.split(".");for(const e in r){if("object"!=typeof t)return!1;const s=r[e];if("shape"in t&&s in t.shape)t=t.shape[s];else{if(!(s in t))return!1;t=t[s]}}return[r[r.length-1],t]},h=(e,t)=>{const r=i(e,t);return r&&"_def"in r[1]?(e=r[0],t=r[1],((t?._def?.innerType?._def||t?._def)?.typeName||"").split("Zod").pop().toLowerCase()):"unknown"},o=(e,t,r,s=!1)=>{if(!s){const t=i(e,r);if(!t)return t;e=t[0],r=t[1]}if(!("_def"in r))return!1;let h=r?._def||{};"ZodOptional"==r?._def?.typeName&&(h=h?.innerType?._def||{});const u=h?.typeName||"";return!!u&&("ZodUnion"==u&&h?.options?.length?h?.options?.some(r=>o(e,t,r,!0)):"ZodArray"==u?Array.isArray(t):"ZodDate"==u?t instanceof Date:typeof t==n[u])};function u(e,t){if(!t||"string"!=typeof e||!e?.includes("."))return!1;const r=i(e,t);return r&&r?.length}var l=class e{#e;#t;#r=[];#s=[];get clauses(){return this.#r}set clauses(e){this.#r.push(...e)}get args(){return this.#s}set args(e){this.#s.push(...e)}get length(){return this.#r.length}constructor(e,t){this.#e=e,this.#t=t}#n(t,r="AND"){const s=new e(this.#e,this.#t);return t(s),s.length&&(this.#r.push(`${this.length?r+" ":""}(${s.clauses.join(" ")})`),this.#s.push(...s.args)),this}#i(e,t=[],r="AND"){return this.length&&(e=r+" "+e),this.#r.push(e),t?.length&&this.#s.push(...t),this}#h(e,...t){if("function"==typeof t[0])return this.#n(t[0],e);const s=t.length;let[n,i,l]=t;if(2==s&&(l=i,i="="),n=r(String(n),this.#e),this.#t&&!o(n,l,this.#t))throw new Error(`Table column '${String(n)}' of type '${h(n,this.#t)}' is not assignable as type of '${typeof l}'.`);return u(l,this.#t)?this.#i(`${n} ${i} ${l}`,[],e):this.#i(`${n} ${i} ?`,[l],e)}where(...e){return this.#h("AND",...e)}on(...e){return this.where(...e)}orWhere(...e){return this.#h("OR",...e)}orOn(...e){return this.orWhere(...e)}#o(e,t,s,n="AND"){return t?.length?this.#i(r(e,this.#e)+` ${s} (${t.map(()=>"?").join(", ")})`,t,n):this}whereIn(e,t){return this.#o(e,t,"IN")}in(e,t){return this.whereIn(e,t)}whereNotIn(e,t){return this.#o(e,t,"NOT IN")}notIn(e,t){return this.whereNotIn(e,t)}orWhereIn(e,t){return this.#o(e,t,"IN","OR")}orIn(e,t){return this.orWhereIn(e,t)}orWhereNotIn(e,t){return this.#o(e,t,"NOT IN","OR")}orNotIn(e,t){return this.orWhereNotIn(e,t)}#u(e,t,s,n,i="AND"){return this.#i(r(e,this.#e)+` ${n} ? AND ?`,[t,s],i)}whereBetween(e,t,r){return this.#u(e,t,r,"BETWEEN")}between(e,t,r){return this.whereBetween(e,t,r)}orWhereBetween(e,t,r){return this.#u(e,t,r,"BETWEEN","OR")}orBetween(e,t,r){return this.orWhereBetween(e,t,r)}whereNotBetween(e,t,r){return this.#u(e,t,r,"NOT BETWEEN")}notBetween(e,t,r){return this.whereNotBetween(e,t,r)}orWhereNotBetween(e,t,r){return this.#u(e,t,r,"NOT BETWEEN","OR")}orNotBetween(e,t,r){return this.orWhereNotBetween(e,t,r)}#l(e,t="IS",s="AND"){return this.#i(r(e,this.#e)+` ${t} NULL`,[],s)}whereNull(e){return this.#l(e)}onNull(e){return this.whereNull(e)}orWhereNull(e){return this.#l(e,"IS","OR")}orOnNull(e){return this.orWhereNull(e)}whereNotNull(e){return this.#l(e,"IS NOT")}onNotNull(e){return this.whereNotNull(e)}orWhereNotNull(e){return this.#l(e,"IS NOT","OR")}orNotNull(e){return this.orWhereNotNull(e)}},a=class{#e;#t;#a=[];#r;#c=[];#N=[];#w=!1;#g=!1;#f;#p;#d=[];#I;constructor(e,t,r){this.#e=e,this.#t=t,this.#I=r,this.#r=new l(e,t)}async run(){if(!this.#I?.run)throw new Error("No database connection.");return await(this.#I?.run(this))}async first(...e){e?.length&&this.select(...e);const t=await this.run();return t.results?.length?t.results[0]:null}async all(...e){e?.length&&this.select(...e);return(await this.run()).results}select(...e){return this.#a.push(...e.flat(1/0)),this}distinct(){return this.#w=!0,this}#b(e,s,...n){this.#g=!0;const i=(e?e+" ":"")+`JOIN ${s} ON `;if("function"==typeof n[0]){const e=new l(s,this.#t);return n[0](e),this.#d.push(i+e.clauses.join(" ")),this.#r.args=e.args,this}const a=n.length;let[c,N,w,g]=n;if(2==a)w=N,N="=";else if(3!=a||"string"==typeof(f=N)&&t.includes(f))4==a&&(w=r(g,w),N="=");else{if(w=r(w,N),this.#t&&!u(w,this.#t))throw new Error(`Table column '${w}' doesn't exists.`);N="="}var f;const p=r(String(c),String(s));if(this.#t&&!o(p,w,this.#t))throw new Error(`Table column '${p}' of type '${h(p,this.#t)}' is not assignable as type of '${typeof w}'.`);return u(w,this.#t)||(this.#r.args=[w],w="?"),this.#d.push(i+p+` ${N} ${w}`),this}join(e,...t){return this.#b(void 0,e,...t)}innerJoin(e,...t){return this.#b("INNER",e,...t)}leftJoin(e,...t){return this.#b("LEFT",e,...t)}rightJoin(e,...t){return this.#b("RIGHT",e,...t)}crossJoin(e,...t){return this.#b("CROSS",e,...t)}where(...e){return this.#r.where(...e),this}on(...e){return this.where(...e)}orWhere(...e){return this.#r.orWhere(...e),this}orOn(...e){return this.orWhere(...e)}whereIn(e,t){return this.#r.whereIn(e,t),this}in(e,t){return this.whereIn(e,t)}whereNotIn(e,t){return this.#r.whereNotIn(e,t),this}notIn(e,t){return this.whereNotIn(e,t)}orWhereIn(e,t){return this.#r.orWhereIn(e,t),this}orIn(e,t){return this.orWhereIn(e,t)}orWhereNotIn(e,t){return this.#r.orWhereNotIn(e,t),this}orNotIn(e,t){return this.orWhereNotIn(e,t)}whereBetween(e,t,r){return this.#r.whereBetween(e,t,r),this}between(e,t,r){return this.whereBetween(e,t,r)}orWhereBetween(e,t,r){return this.#r.orWhereBetween(e,t,r),this}orBetween(e,t,r){return this.orWhereBetween(e,t,r)}whereNotBetween(e,t,r){return this.#r.whereNotBetween(e,t,r),this}notBetween(e,t,r){return this.whereNotBetween(e,t,r)}orWhereNotBetween(e,t,r){return this.#r.orWhereNotBetween(e,t,r),this}orNotBetween(e,t,r){return this.orWhereNotBetween(e,t,r)}whereNull(e){return this.#r.whereNull(e),this}onNull(e){return this.whereNull(e)}orWhereNull(e){return this.#r.orWhereNull(e),this}orOnNull(e){return this.orWhereNull(e)}whereNotNull(e){return this.#r.whereNotNull(e),this}onNotNull(e){return this.whereNotNull(e)}orWhereNotNull(e){return this.#r.orWhereNotNull(e),this}orNotNull(e){return this.orWhereNotNull(e)}groupBy(...e){return this.#c.push(...e),this}order(e,t="ASC"){return this.#N.push(r(e,this.#e,this.#g)+" "+t.toUpperCase()),this}orderBy(e,t="ASC"){return this.order(e,t)}asc(e){return this.order(e,"ASC")}desc(e){return this.order(e,"DESC")}limit(e){return(e=parseInt(String(e))||0)&&(this.#f=e),this}offset(e){return this.#p=parseInt(String(e))||0,this}#W(e,t){let r=0,n="",i=0;for(let h=e.indexOf("?");-1!==h;h=e.indexOf("?",h+1)){if(r>=t.length)throw new Error(`Missing bind value at position ${r}`);n+=e.slice(i,h),n+=s(t[r++]),i=h+1}if(r<t.length)throw new Error(`Too many bind values: expected ${r}, got ${t.length}`);return n+e.slice(i)}get args(){return this.#r.args}get arguments(){return this.args}get bindings(){return this.args}get query(){let t="";const r=new Set;return this.#a.forEach(t=>{t=function(t,r,s){if(t.toLowerCase().includes(" as "))return t;const n=t.includes(".");if(!s&&!n)return t;const[i,h]=n?t.split("."):[r,t];return`${i}.${h} AS ${e(i,1)}_${h}`}(t,this.#e,this.#g),!r.has(t)&&r.add(t)}),t+=`SELECT ${this.#w?"DISTINCT ":""}${r.size?[...r].join(", "):"*"}`,t+=" FROM "+this.#e,this.#d.length&&(t+=" "+this.#d.join(" ")),this.#r.length&&(t+=" WHERE "+this.#r.clauses.join(" ")),this.#c.length&&(t+=" GROUP BY "+this.#c.join(", ")),this.#N.length&&(t+=" ORDER BY "+this.#N.join(", ")),null!=this.#f&&(t+=" LIMIT "+this.#f),null!=this.#p&&(t+=" OFFSET "+this.#p),t}get sql(){return this.#W(this.query,this.#r.args)}get raw(){return this.sql}};export{a as QueryBuilder};
package/dist/d1.d.ts DELETED
@@ -1,113 +0,0 @@
1
- import { D as DBSchema, P as Pipe, Q as QueryBuilder, W as WhereFn, O as Operator, a as OrderDirection, S as SchemaKeys, R as RunFn } from './index-CwrzXlna.js';
2
- import { D1Database } from '@cloudflare/workers-types';
3
- import z from 'zod';
4
-
5
- declare abstract class Model$1<TB extends keyof DB, DB> {
6
- readonly $DBShape: DB;
7
- readonly $TShape: DB[TB];
8
- static $table: string;
9
- static $schema?: DBSchema;
10
- static pipe<S, T>(): Pipe<S, T>;
11
- static builder<S, T>(): QueryBuilder<S, T, keyof T>;
12
- static select<// @ts-ignore
13
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, ...columns: C[] | C[][]): QueryBuilder<I["$DBShape"], T, C>;
14
- static distinct<// @ts-ignore
15
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape']>(this: M): QueryBuilder<I["$DBShape"], T, keyof T>;
16
- static where<// @ts-ignore
17
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, fn: WhereFn<T>): QueryBuilder<I['$DBShape'], T, C>;
18
- static where<// @ts-ignore
19
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, value: T[C]): QueryBuilder<I['$DBShape'], T, C>;
20
- static where<// @ts-ignore
21
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I['$DBShape'], T, C>;
22
- static on<// @ts-ignore
23
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, fn: WhereFn<T>): QueryBuilder<I['$DBShape'], T, C>;
24
- static on<// @ts-ignore
25
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, value: T[C]): QueryBuilder<I['$DBShape'], T, C>;
26
- static on<// @ts-ignore
27
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I['$DBShape'], T, C>;
28
- static whereIn<// @ts-ignore
29
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
30
- static in<// @ts-ignore
31
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
32
- static whereNotIn<// @ts-ignore
33
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
34
- static notIn<// @ts-ignore
35
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
36
- static whereBetween<// @ts-ignore
37
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
38
- static between<// @ts-ignore
39
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
40
- static whereNotBetween<// @ts-ignore
41
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
42
- static notBetween<// @ts-ignore
43
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
44
- static whereNull<// @ts-ignore
45
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
46
- static onNull<// @ts-ignore
47
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
48
- static whereNotNull<// @ts-ignore
49
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
50
- static onNotNull<// @ts-ignore
51
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
52
- static order<// @ts-ignore
53
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, direction?: OrderDirection): QueryBuilder<I["$DBShape"], T, keyof T>;
54
- static orderBy<// @ts-ignore
55
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C, direction?: OrderDirection): QueryBuilder<I["$DBShape"], T, keyof T>;
56
- static asc<// @ts-ignore
57
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
58
- static desc<// @ts-ignore
59
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape'], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
60
- static limit<// @ts-ignore
61
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape']>(this: M, val: number | string): QueryBuilder<I["$DBShape"], T, keyof T>;
62
- static offset<// @ts-ignore
63
- M extends typeof Model$1<TB, DB>, I extends InstanceType<M>, T extends I['$TShape']>(this: M, val: number | string): QueryBuilder<I["$DBShape"], T, keyof T>;
64
- }
65
-
66
- declare function ModelBuilder<TSchema extends DBSchema, TBase extends SchemaKeys<TSchema>>(schema: TSchema, base: TBase): {
67
- new (): {
68
- readonly $DBShape: z.TypeOf<TSchema>;
69
- readonly $TShape: z.TypeOf<TSchema>[TBase];
70
- };
71
- $table: string;
72
- $schema: TSchema;
73
- $db: string | D1Database;
74
- pipe<S, T>(): Pipe<S, T>;
75
- DB(): D1Database;
76
- run<S, T>(db: D1Database): RunFn<S, T>;
77
- builder<S, T>(): QueryBuilder<S, T, keyof T>;
78
- select<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, ...columns: C[] | C[][]): QueryBuilder<I["$DBShape"], T, C>;
79
- distinct<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"]>(this: M): QueryBuilder<I["$DBShape"], T, keyof T>;
80
- where<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, fn: WhereFn<T>): QueryBuilder<I["$DBShape"], T, C>;
81
- where<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, value: T[C]): QueryBuilder<I["$DBShape"], T, C>;
82
- where<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I["$DBShape"], T, C>;
83
- on<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, fn: WhereFn<T>): QueryBuilder<I["$DBShape"], T, C>;
84
- on<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, value: T[C]): QueryBuilder<I["$DBShape"], T, C>;
85
- on<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I["$DBShape"], T, C>;
86
- whereIn<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
87
- in<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
88
- whereNotIn<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
89
- notIn<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, value: T[C][]): QueryBuilder<I["$DBShape"], T, keyof T>;
90
- whereBetween<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
91
- between<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
92
- whereNotBetween<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
93
- notBetween<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, one: T[C], two: T[C]): QueryBuilder<I["$DBShape"], T, keyof T>;
94
- whereNull<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
95
- onNull<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
96
- whereNotNull<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
97
- onNotNull<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
98
- order<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, direction?: OrderDirection): QueryBuilder<I["$DBShape"], T, keyof T>;
99
- orderBy<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C, direction?: OrderDirection): QueryBuilder<I["$DBShape"], T, keyof T>;
100
- asc<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
101
- desc<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"], C extends keyof T>(this: M, column: C): QueryBuilder<I["$DBShape"], T, keyof T>;
102
- limit<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"]>(this: M, val: number | string): QueryBuilder<I["$DBShape"], T, keyof T>;
103
- offset<M extends typeof Model$1, I extends InstanceType<M>, T extends I["$TShape"]>(this: M, val: number | string): QueryBuilder<I["$DBShape"], T, keyof T>;
104
- };
105
-
106
- declare abstract class Model<TB extends keyof DB, DB> extends Model$1<TB, DB> {
107
- static $db: string | D1Database;
108
- static pipe<S, T>(): Pipe<S, T>;
109
- static DB(): D1Database;
110
- static run<S, T>(db: D1Database): RunFn<S, T>;
111
- }
112
-
113
- export { Model, ModelBuilder };
package/dist/d1.js DELETED
@@ -1 +0,0 @@
1
- import{QueryBuilder as t}from"./chunk-NVO75XBO.js";import e from"pluralize";var r=class{static{this.$table=""}static pipe(){throw new Error("Database connection not provided.")}static builder(){const r=this.$table||e(this.name.toLowerCase());return new t(r,this.$schema,this.pipe())}static select(...t){return this.builder().select(...t)}static distinct(){return this.builder().distinct()}static where(...t){return this.builder().where(...t)}static on(...t){return this.builder().where(...t)}static whereIn(t,e){return this.builder().whereIn(t,e)}static in(t,e){return this.builder().whereIn(t,e)}static whereNotIn(t,e){return this.builder().whereNotIn(t,e)}static notIn(t,e){return this.builder().whereNotIn(t,e)}static whereBetween(t,e,r){return this.builder().whereBetween(t,e,r)}static between(t,e,r){return this.builder().whereBetween(t,e,r)}static whereNotBetween(t,e,r){return this.builder().whereNotBetween(t,e,r)}static notBetween(t,e,r){return this.builder().whereNotBetween(t,e,r)}static whereNull(t){return this.builder().whereNull(t)}static onNull(t){return this.builder().whereNull(t)}static whereNotNull(t){return this.builder().whereNotNull(t)}static onNotNull(t){return this.builder().whereNotNull(t)}static order(t,e="ASC"){return this.builder().order(t,e)}static orderBy(t,e="ASC"){return this.builder().order(t,e)}static asc(t){return this.builder().asc(t)}static desc(t){return this.builder().desc(t)}static limit(t){return this.builder().limit(t)}static offset(t){return this.builder().offset(t)}};function i(t,e){return class extends s{static{this.$table=String(e)}static{this.$schema=t}}}var s=class extends r{static{this.$db="DB"}static pipe(){const t=this.DB();return{run:this.run(t)}}static DB(){if("string"==typeof this.$db){if(!(this.$db in process.env))throw new Error(`Database '${this.$db}' instance not provided.`);return process.env[this.$db]}return this.$db}static run(t){return async e=>{let r=t.prepare(e.query);e.args?.length&&(r=r.bind(...e.args));const i=await r.run(),s=i.meta;return{changes:s?.changes,duration:s?.duration,lastId:s?.last_row_id,rowsRead:s?.rows_read,rowsWritten:s?.rows_written,success:i.success,results:i.results}}}};export{s as Model,i as ModelBuilder};