imodel-pg 0.3.2 → 0.3.4
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/index.d.mts +1 -1
- package/index.mjs +21 -9
- package/package.json +1 -1
package/index.d.mts
CHANGED
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* imodel v0.3.
|
|
2
|
+
* imodel v0.3.4
|
|
3
3
|
* (c) 2019-2025 undefined
|
|
4
4
|
* @license undefined
|
|
5
5
|
*/
|
|
@@ -128,18 +128,28 @@ function getType(s, {
|
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
/** @import { Environment, ColumnOptions } from 'imodel' */
|
|
131
|
+
/**
|
|
132
|
+
*
|
|
133
|
+
* @param {string} t
|
|
134
|
+
*/
|
|
135
|
+
function toSqlString2(t) {
|
|
136
|
+
return `'${t.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}'`;
|
|
137
|
+
}
|
|
131
138
|
/**
|
|
132
139
|
*
|
|
133
140
|
* @param {any} v
|
|
134
141
|
* @param {string} type
|
|
135
142
|
* @returns
|
|
136
143
|
*/
|
|
137
|
-
function
|
|
144
|
+
function toSql(v, type) {
|
|
145
|
+
if (type === 'json' || type === 'object') {
|
|
146
|
+
return `${toSqlString2(JSON.stringify(type))}::JSON`;
|
|
147
|
+
}
|
|
138
148
|
if (typeof v === 'bigint' || typeof v === 'number') {
|
|
139
149
|
return String(v);
|
|
140
150
|
}
|
|
141
151
|
if (typeof v === 'string') {
|
|
142
|
-
return
|
|
152
|
+
return toSqlString2(v);
|
|
143
153
|
}
|
|
144
154
|
if (v === true) {
|
|
145
155
|
return 'TRUE';
|
|
@@ -148,8 +158,9 @@ function toSqlString(v, type) {
|
|
|
148
158
|
return 'FALSE';
|
|
149
159
|
}
|
|
150
160
|
if (v instanceof Date) {
|
|
151
|
-
return
|
|
161
|
+
return v.toISOString();
|
|
152
162
|
}
|
|
163
|
+
return null;
|
|
153
164
|
}
|
|
154
165
|
/**
|
|
155
166
|
*
|
|
@@ -178,10 +189,10 @@ function getDefault(env, value, type, array) {
|
|
|
178
189
|
}
|
|
179
190
|
if (array) {
|
|
180
191
|
const values = Array.isArray(v) ? v : [v];
|
|
181
|
-
const t = values.map(v =>
|
|
182
|
-
return Sql(`
|
|
192
|
+
const t = values.map(v => toSql(v, type)).filter(Boolean);
|
|
193
|
+
return Sql(`{${t.join(',')}}`);
|
|
183
194
|
}
|
|
184
|
-
const t =
|
|
195
|
+
const t = toSql(v, type);
|
|
185
196
|
if (t) {
|
|
186
197
|
return Sql(t);
|
|
187
198
|
}
|
|
@@ -1066,7 +1077,7 @@ async function loadDefault(env, query, fields) {
|
|
|
1066
1077
|
return fields;
|
|
1067
1078
|
}
|
|
1068
1079
|
const sql = Sql`
|
|
1069
|
-
SELECT ${Sql`,`.glue(data.map((v, k) => Sql`${v.default} as
|
|
1080
|
+
SELECT ${Sql`,`.glue(data.map((v, k) => Sql(`${v.default} as a${k}`)))}
|
|
1070
1081
|
`;
|
|
1071
1082
|
const values = await query(env, sql);
|
|
1072
1083
|
for (const [index, field] of data.entries()) {
|
|
@@ -1469,7 +1480,8 @@ WHERE table_name = ${table} AND constraint_type = 'PRIMARY KEY'
|
|
|
1469
1480
|
COLUMNs.push(nullable ? Sql`${COLUMN} DROP NOT NULL` : Sql`${COLUMN} SET NOT NULL`);
|
|
1470
1481
|
}
|
|
1471
1482
|
if (defaultValue !== (old.default ?? null)) {
|
|
1472
|
-
|
|
1483
|
+
const def = getDefault(env, defaultValue, type, array);
|
|
1484
|
+
COLUMNs.push(def ? Sql`${COLUMN} SET DEFAULT ${def}` : Sql`${COLUMN} DROP DEFAULT`);
|
|
1473
1485
|
}
|
|
1474
1486
|
}
|
|
1475
1487
|
{
|