forj 0.0.0 → 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present, ZUNQ.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,17 +1,24 @@
1
- ### Verify if name exists:
1
+ # `forj`
2
+
3
+ SQLite ORM and Query Builder whitout dependencies.
4
+
5
+ ## Install
6
+
7
+ To install the package, run:
2
8
 
3
9
  ```bash
4
- npm view PACKAGE_NAME
10
+ # bun
11
+ bun i forj
5
12
  ```
6
13
 
7
- or
14
+ ## Tests
15
+
16
+ To perform the tests, run:
8
17
 
9
18
  ```bash
10
- bun info PACKAGE_NAME
19
+ bun test
11
20
  ```
12
21
 
13
- ### Publish package
22
+ ## License
14
23
 
15
- ```bash
16
- bun publish
17
- ```
24
+ This package is licensed under the [MIT license](https://github.com/attla/forj/blob/main/LICENSE) © [Zunq](https://zunq.com)
@@ -0,0 +1 @@
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 ADDED
@@ -0,0 +1,113 @@
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 ADDED
@@ -0,0 +1 @@
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};
@@ -0,0 +1,163 @@
1
+ import z from 'zod';
2
+
3
+ declare class QueryBuilder<S, T, C extends keyof T = keyof T> implements IJoinBuilder<S>, IClauseBuilder<T> {
4
+ #private;
5
+ constructor(table: string, schema?: DBSchema, pipe?: Pipe<S, T, C>);
6
+ run(): Promise<Result<T, C>>;
7
+ first<K extends keyof T>(...columns: K[] | K[][]): Promise<null | Item<T, C>>;
8
+ all<K extends keyof T>(...columns: K[] | K[][]): Promise<Item<T, C>[]>;
9
+ select<K extends keyof T>(...columns: K[] | K[][]): QueryBuilder<S, T, K>;
10
+ distinct(): this;
11
+ join<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
12
+ innerJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
13
+ leftJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
14
+ rightJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
15
+ crossJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
16
+ where(...args: WhereArgs<T>): this;
17
+ on(...args: WhereArgs<T>): this;
18
+ orWhere(...args: WhereArgs<T>): this;
19
+ orOn(...args: WhereArgs<T>): this;
20
+ whereIn(column: C, values: T[C][]): this;
21
+ in(column: C, values: T[C][]): this;
22
+ whereNotIn(column: C, values: T[C][]): this;
23
+ notIn(column: C, values: T[C][]): this;
24
+ orWhereIn(column: C, values: T[C][]): this;
25
+ orIn(column: C, values: T[C][]): this;
26
+ orWhereNotIn(column: C, values: T[C][]): this;
27
+ orNotIn(column: C, values: T[C][]): this;
28
+ whereBetween(column: C, one: T[C], two: T[C]): this;
29
+ between(column: C, one: T[C], two: T[C]): this;
30
+ orWhereBetween(column: C, one: T[C], two: T[C]): this;
31
+ orBetween(column: C, one: T[C], two: T[C]): this;
32
+ whereNotBetween(column: C, one: T[C], two: T[C]): this;
33
+ notBetween(column: C, one: T[C], two: T[C]): this;
34
+ orWhereNotBetween(column: C, one: T[C], two: T[C]): this;
35
+ orNotBetween(column: C, one: T[C], two: T[C]): this;
36
+ whereNull(column: C): this;
37
+ onNull(column: C): this;
38
+ orWhereNull(column: C): this;
39
+ orOnNull(column: C): this;
40
+ whereNotNull(column: C): this;
41
+ onNotNull(column: C): this;
42
+ orWhereNotNull(column: C): this;
43
+ orNotNull(column: C): this;
44
+ groupBy(...columns: string[]): this;
45
+ order(column: C, direction?: OrderDirection): this;
46
+ orderBy(column: C, direction?: OrderDirection): this;
47
+ asc(column: C): this;
48
+ desc(column: C): this;
49
+ limit(val: number | string): this;
50
+ offset(val: number | string): this;
51
+ get args(): Values;
52
+ get arguments(): Values;
53
+ get bindings(): Values;
54
+ get query(): string;
55
+ get sql(): string;
56
+ get raw(): string;
57
+ }
58
+
59
+ type Primitive = null | number | string | boolean;
60
+ type Value = Primitive | undefined;
61
+ type Values = Value[];
62
+ type Operator = '=' | '!=' | '<' | '>' | '<=' | '>=' | 'LIKE';
63
+ type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc';
64
+ type DBSchema = z.ZodObject<any>;
65
+ type SchemaObject = Record<string, z.ZodTypeAny>;
66
+ type SchemaKeys<TSchema extends DBSchema> = TSchema extends z.ZodObject<infer TShape> ? keyof TShape : TSchema extends SchemaObject ? keyof TSchema : never;
67
+ type RunFn<S, T, C extends keyof T = keyof T> = (qb: QueryBuilder<S, T, C>) => Promise<Result<T, C>>;
68
+ type Pipe<S, T, C extends keyof T = keyof T> = {
69
+ run: RunFn<S, T, C>;
70
+ };
71
+ type Result<T, C extends keyof T> = {
72
+ changes: number;
73
+ duration: number;
74
+ lastId?: number | string;
75
+ rowsRead: number;
76
+ rowsWritten: number;
77
+ success: boolean;
78
+ results: Item<T, C>[];
79
+ };
80
+ type Item<B, S extends keyof B, T = Pick<B, S>> = {
81
+ [K in keyof T]: T[K];
82
+ } & {};
83
+ type WhereFn<T, C extends keyof T = keyof T> = (q: IClauseBuilder<T, C>) => void;
84
+ type WhereArgs<T, C extends keyof T = keyof T> = [WhereFn<T, C>] | [C, T[C]] | [C, Operator, T[C]];
85
+ interface IClauseBuilder<T, C extends keyof T = keyof T> {
86
+ where(fn: WhereFn<T, C>): this;
87
+ where(column: C, value: T[C]): this;
88
+ where(column: C, operator: Operator, value: T[C]): this;
89
+ where(...args: WhereArgs<T>): this;
90
+ on(fn: WhereFn<T, C>): this;
91
+ on(column: C, value: T[C]): this;
92
+ on(column: C, operator: Operator, value: T[C]): this;
93
+ on(...args: WhereArgs<T>): this;
94
+ orWhere(fn: WhereFn<T, C>): this;
95
+ orWhere(column: C, value: T[C]): this;
96
+ orWhere(column: C, operator: Operator, value: T[C]): this;
97
+ orWhere(...args: WhereArgs<T>): this;
98
+ orOn(fn: WhereFn<T, C>): this;
99
+ orOn(column: C, value: T[C]): this;
100
+ orOn(column: C, operator: Operator, value: T[C]): this;
101
+ orOn(...args: WhereArgs<T>): this;
102
+ whereIn(column: C, values: T[C][]): this;
103
+ in(column: C, values: T[C][]): this;
104
+ whereNotIn(column: C, values: T[C][]): this;
105
+ notIn(column: C, values: T[C][]): this;
106
+ orWhereIn(column: C, values: T[C][]): this;
107
+ orIn(column: C, values: T[C][]): this;
108
+ orWhereNotIn(column: C, values: T[C][]): this;
109
+ orNotIn(column: C, values: T[C][]): this;
110
+ whereBetween(column: C, one: T[C], two: T[C]): this;
111
+ between(column: C, one: T[C], two: T[C]): this;
112
+ orWhereBetween(column: C, one: T[C], two: T[C]): this;
113
+ orBetween(column: C, one: T[C], two: T[C]): this;
114
+ whereNotBetween(column: C, one: T[C], two: T[C]): this;
115
+ notBetween(column: C, one: T[C], two: T[C]): this;
116
+ orWhereNotBetween(column: C, one: T[C], two: T[C]): this;
117
+ orNotBetween(column: C, one: T[C], two: T[C]): this;
118
+ whereNull(column: C): this;
119
+ onNull(column: C): this;
120
+ orWhereNull(column: C): this;
121
+ orOnNull(column: C): this;
122
+ whereNotNull(column: C): this;
123
+ onNotNull(column: C): this;
124
+ orWhereNotNull(column: C): this;
125
+ orNotNull(column: C): this;
126
+ }
127
+ type JoinArgs<S, J extends keyof S> = [
128
+ WhereFn<S[J]>
129
+ ] | [keyof S[J], S[J][keyof S[J]]] | [keyof S[J], Operator, S[J][keyof S[J]]] | [keyof S[J], keyof S, keyof S[keyof S]] | [keyof S[J], Operator, S[J][keyof S[J]]] | [keyof S[J], Operator, keyof S, keyof S[keyof S]];
130
+ interface IJoinBuilder<S> {
131
+ join<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this;
132
+ join<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, value: T[C]): this;
133
+ join<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, operator: Operator, value: T[C]): this;
134
+ join<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, table2: J2, column2: C2): this;
135
+ join<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this;
136
+ join<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
137
+ innerJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this;
138
+ innerJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, value: T[C]): this;
139
+ innerJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, operator: Operator, value: T[C]): this;
140
+ innerJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, table2: J2, column2: C2): this;
141
+ innerJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this;
142
+ innerJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
143
+ leftJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this;
144
+ leftJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, value: T[C]): this;
145
+ leftJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, operator: Operator, value: T[C]): this;
146
+ leftJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, table2: J2, column2: C2): this;
147
+ leftJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this;
148
+ leftJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
149
+ rightJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this;
150
+ rightJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, value: T[C]): this;
151
+ rightJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, operator: Operator, value: T[C]): this;
152
+ rightJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, table2: J2, column2: C2): this;
153
+ rightJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this;
154
+ rightJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
155
+ crossJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this;
156
+ crossJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, value: T[C]): this;
157
+ crossJoin<J extends keyof S, T extends S[J], C extends keyof T>(table: J, column: C, operator: Operator, value: T[C]): this;
158
+ crossJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, table2: J2, column2: C2): this;
159
+ crossJoin<J extends keyof S, T extends S[J], C extends keyof T, J2 extends keyof S, C2 extends keyof S[J2]>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this;
160
+ crossJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this;
161
+ }
162
+
163
+ export { type DBSchema as D, type Operator as O, type Pipe as P, QueryBuilder as Q, type RunFn as R, type SchemaKeys as S, type WhereFn as W, type OrderDirection as a };
@@ -0,0 +1,2 @@
1
+ export { Q as QueryBuilder } from './index-CwrzXlna.js';
2
+ import 'zod';
@@ -0,0 +1 @@
1
+ export{QueryBuilder}from"./chunk-NVO75XBO.js";
package/package.json CHANGED
@@ -1,4 +1,72 @@
1
1
  {
2
- "name": "forj",
3
- "version": "0.0.0"
2
+ "name": "forj",
3
+ "description": "SQLite ORM and Query Builder whitout dependencies",
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "files": ["dist"],
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ },
12
+ "./d1": {
13
+ "import": "./dist/d1.js",
14
+ "types": "./dist/d1.d.ts"
15
+ }
16
+ },
17
+ "scripts": {
18
+ "bench": "tsx tests/benchmark/*.bench.ts",
19
+ "build": "tsup",
20
+ "prepublishOnly": "bun run build"
21
+ },
22
+ "dependencies": {
23
+ "@cloudflare/workers-types": "^4.20260113.0",
24
+ "pluralize": "^8.0",
25
+ "zod": "^3.19.1"
26
+ },
27
+ "devDependencies": {
28
+ "@types/pluralize": "^0.0.33",
29
+ "bun-types": "^1.2.13",
30
+ "terser": "^5.46.0",
31
+ "tiny-glob": "^0.2",
32
+ "tsup": "^8.5.1",
33
+ "tsx": "^4.19.4",
34
+ "typescript": "^5.8.3"
35
+ },
36
+ "engines": {
37
+ "node": ">=18.0.0"
38
+ },
39
+ "publishConfig": {
40
+ "registry": "https://registry.npmjs.org"
41
+ },
42
+ "author": "Zunq <open-source@zunq.com>",
43
+ "license": "MIT",
44
+ "homepage": "https://zunq.dev",
45
+ "repository": "git://github.com/attla/forj",
46
+ "bugs": "https://github.com/attla/forj/issues",
47
+ "keywords": [
48
+ "forj",
49
+ "attla",
50
+ "resources",
51
+ "support",
52
+ "sqlite",
53
+ "orm",
54
+ "query",
55
+ "builder",
56
+ "query builder",
57
+ "query-builder",
58
+ "db",
59
+ "database",
60
+ "sql",
61
+ "mysql",
62
+ "postgres",
63
+ "postgresql",
64
+ "typescript",
65
+ "aws",
66
+ "lambda",
67
+ "llrt",
68
+ "deno",
69
+ "bun",
70
+ "nodejs"
71
+ ]
4
72
  }