forj 0.1.5 → 0.1.6
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 +1 -1
- package/src/clause-builder.ts +1 -2
- package/src/query-builder.ts +6 -6
- package/src/utils.ts +10 -4
package/package.json
CHANGED
package/src/clause-builder.ts
CHANGED
|
@@ -79,9 +79,8 @@ export default class ClauseBuilder<
|
|
|
79
79
|
// @ts-ignore
|
|
80
80
|
column = parseColumn(String(column), this.#table)
|
|
81
81
|
|
|
82
|
-
if (this.#schema && !zSame(column, value, this.#schema))
|
|
82
|
+
if (this.#schema && !zSame(column.replace(/"/g, ''), value, this.#schema))
|
|
83
83
|
throw new Error(`Table column '${String(column)}' of type '${zType(column, this.#schema)}' is not assignable as type of '${typeof value}'.`)
|
|
84
|
-
}
|
|
85
84
|
|
|
86
85
|
return isJoinCompare(value, this.#schema) // @ts-ignore
|
|
87
86
|
? this.#clause(`${column} ${operator} ${value}`, [], logical) // @ts-ignore
|
package/src/query-builder.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
formatValue,
|
|
6
6
|
isJoinCompare,
|
|
7
7
|
zSame, zType,
|
|
8
|
+
sqlName,
|
|
8
9
|
} from './utils'
|
|
9
10
|
import type {
|
|
10
11
|
IJoinBuilder, IClauseBuilder,
|
|
@@ -89,7 +90,7 @@ export default class QueryBuilder<
|
|
|
89
90
|
...args: JoinArgs<S, J>
|
|
90
91
|
) {
|
|
91
92
|
this.#hasJoin = true
|
|
92
|
-
const query = (type ? type + ' ' : '') + `JOIN ${table as string} ON `
|
|
93
|
+
const query = (type ? type + ' ' : '') + `JOIN ${sqlName(table as string)} ON `
|
|
93
94
|
|
|
94
95
|
if (typeof args[0] == 'function') {
|
|
95
96
|
const join = new ClauseBuilder<S[J]>(table as string, this.#schema)
|
|
@@ -107,7 +108,6 @@ export default class QueryBuilder<
|
|
|
107
108
|
value = operator
|
|
108
109
|
operator = '='
|
|
109
110
|
} else if (length == 3 && !isOperator(operator)) { // @ts-ignore
|
|
110
|
-
// console.log(column, operator, value, value2) // @ts-ignore
|
|
111
111
|
value = parseColumn(value as string, operator as string) // TODO: check if value is a valid column
|
|
112
112
|
|
|
113
113
|
if (this.#schema && !isJoinCompare(value, this.#schema))
|
|
@@ -115,16 +115,16 @@ export default class QueryBuilder<
|
|
|
115
115
|
|
|
116
116
|
operator = '='
|
|
117
117
|
} else if (length == 4) { // @ts-ignore
|
|
118
|
-
// console.log(column, operator, value, value2) // @ts-ignore
|
|
119
118
|
value = parseColumn(value2 as string, value as string)
|
|
120
119
|
operator = '='
|
|
121
120
|
}
|
|
122
121
|
|
|
123
122
|
const col = parseColumn(String(column), String(table))
|
|
124
|
-
if (this.#schema && !zSame(col, value, this.#schema))
|
|
125
|
-
throw new Error(`Table column '${col}' of type '${zType(col, this.#schema)}' is not assignable as type of '${typeof value}'.`)
|
|
126
123
|
|
|
127
|
-
if (!isJoinCompare(value, this.#schema)) {
|
|
124
|
+
if (!isJoinCompare(value, this.#schema)) {
|
|
125
|
+
if (this.#schema && !zSame(col.replace(/"/g, ''), value, this.#schema))
|
|
126
|
+
throw new Error(`Table column '${col}' of type '${zType(col, this.#schema)}' is not assignable as type of '${typeof value}'.`)
|
|
127
|
+
// @ts-ignore
|
|
128
128
|
this.#clauses.args = [value] // @ts-ignore // TODO: https://developers.cloudflare.com/d1/worker-api/#type-conversion
|
|
129
129
|
value = '?'
|
|
130
130
|
}
|
package/src/utils.ts
CHANGED
|
@@ -25,7 +25,9 @@ export function parseSelectColumn(
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export function parseColumn(name: string, table: string, hasJoin: boolean = true) {
|
|
28
|
-
return !hasJoin || name.includes('.')
|
|
28
|
+
return !hasJoin || name.includes('.')
|
|
29
|
+
? name.split('.').map(col => sqlName(col)).join('.')
|
|
30
|
+
: sqlName(table) + '.' + sqlName(name)
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
export function formatValue(value: any): string {
|
|
@@ -125,13 +127,17 @@ export const zSame = (key: string, val: any, schema?: any, deep: boolean = false
|
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
export function isJoinCompare(val: any, schema?: DBSchema) {
|
|
128
|
-
|
|
129
|
-
if (!schema || typeof val != 'string' || !val?.includes('.'))
|
|
130
|
+
if (typeof val != 'string' || !val?.includes('.'))
|
|
130
131
|
return false
|
|
131
132
|
|
|
132
|
-
|
|
133
|
+
if (!schema)
|
|
134
|
+
return true
|
|
135
|
+
|
|
136
|
+
const keys = zGet(val.replace(/"/g, ''), schema)
|
|
137
|
+
// const keys = zGet(val, schema)
|
|
133
138
|
return keys && keys?.length
|
|
134
139
|
}
|
|
140
|
+
|
|
135
141
|
// List taken from `aKeywordTable` in https://github.com/sqlite/sqlite/blob/378bf82e2bc09734b8c5869f9b148efe37d29527/tool/mkkeywordhash.c#L172
|
|
136
142
|
// prettier-ignore
|
|
137
143
|
export const SQLITE_KEYWORDS = new Set([
|