drizzle-graphql-plus 0.8.18 → 0.8.20
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.cjs +128 -75
- package/index.cjs.map +1 -1
- package/index.js +128 -75
- package/index.js.map +1 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2264,89 +2264,95 @@ var allowedNameChars2 = /^[a-zA-Z0-9_]+$/;
|
|
|
2264
2264
|
var customScalars = /* @__PURE__ */ new Set();
|
|
2265
2265
|
var enumDefinitions = /* @__PURE__ */ new Map();
|
|
2266
2266
|
var requiredFieldFilters = /* @__PURE__ */ new Set();
|
|
2267
|
+
var foreignKeyTypes = /* @__PURE__ */ new Map();
|
|
2267
2268
|
var columnToSDL = (column, columnName, tableName, forceNullable = false) => {
|
|
2268
2269
|
let baseType;
|
|
2269
2270
|
if (column.customGraphqlType) {
|
|
2270
2271
|
baseType = column.customGraphqlType;
|
|
2271
2272
|
} else {
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
baseType = "
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
customScalars.add("Date");
|
|
2288
|
-
break;
|
|
2289
|
-
case "string":
|
|
2290
|
-
if (column.enumValues?.length) {
|
|
2291
|
-
const enumName = `${capitalize(tableName)}${capitalize(
|
|
2292
|
-
columnName
|
|
2293
|
-
)}Enum`;
|
|
2294
|
-
baseType = enumName;
|
|
2295
|
-
if (!enumDefinitions.has(enumName)) {
|
|
2296
|
-
enumDefinitions.set(enumName, {
|
|
2297
|
-
name: enumName,
|
|
2298
|
-
values: column.enumValues.map(
|
|
2299
|
-
(e, index) => allowedNameChars2.test(e) ? e : `Option${index}`
|
|
2300
|
-
)
|
|
2301
|
-
});
|
|
2273
|
+
const foreignKeyType = foreignKeyTypes.get(`${tableName}.${columnName}`);
|
|
2274
|
+
if (foreignKeyType) {
|
|
2275
|
+
baseType = foreignKeyType;
|
|
2276
|
+
} else {
|
|
2277
|
+
switch (column.dataType) {
|
|
2278
|
+
case "boolean":
|
|
2279
|
+
baseType = "Boolean";
|
|
2280
|
+
break;
|
|
2281
|
+
case "json":
|
|
2282
|
+
if (column.columnType === "PgGeometryObject") {
|
|
2283
|
+
baseType = "PgGeometryObject";
|
|
2284
|
+
customScalars.add("PgGeometryObject");
|
|
2285
|
+
} else {
|
|
2286
|
+
baseType = "JSON";
|
|
2287
|
+
customScalars.add("JSON");
|
|
2302
2288
|
}
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2289
|
+
break;
|
|
2290
|
+
case "date":
|
|
2291
|
+
baseType = "Date";
|
|
2292
|
+
customScalars.add("Date");
|
|
2293
|
+
break;
|
|
2294
|
+
case "string":
|
|
2295
|
+
if (column.enumValues?.length) {
|
|
2296
|
+
const enumName = `${capitalize(tableName)}${capitalize(
|
|
2297
|
+
columnName
|
|
2298
|
+
)}Enum`;
|
|
2299
|
+
baseType = enumName;
|
|
2300
|
+
if (!enumDefinitions.has(enumName)) {
|
|
2301
|
+
enumDefinitions.set(enumName, {
|
|
2302
|
+
name: enumName,
|
|
2303
|
+
values: column.enumValues.map(
|
|
2304
|
+
(e, index) => allowedNameChars2.test(e) ? e : `Option${index}`
|
|
2305
|
+
)
|
|
2306
|
+
});
|
|
2307
|
+
}
|
|
2308
|
+
} else {
|
|
2309
|
+
baseType = "String";
|
|
2310
|
+
}
|
|
2311
|
+
break;
|
|
2312
|
+
case "bigint":
|
|
2313
|
+
baseType = "BigInt";
|
|
2314
|
+
customScalars.add("BigInt");
|
|
2315
|
+
break;
|
|
2316
|
+
case "number":
|
|
2317
|
+
if (is7(column, PgInteger2) || is7(column, PgSerial2) || is7(column, MySqlInt2) || is7(column, MySqlSerial2) || is7(column, SQLiteInteger2)) {
|
|
2318
|
+
baseType = "Int";
|
|
2319
|
+
} else {
|
|
2320
|
+
baseType = "Float";
|
|
2321
|
+
}
|
|
2322
|
+
break;
|
|
2323
|
+
case "buffer":
|
|
2324
|
+
baseType = "[Int!]";
|
|
2325
|
+
break;
|
|
2326
|
+
case "array":
|
|
2327
|
+
if (column.columnType === "PgVector") {
|
|
2328
|
+
baseType = "[Float!]";
|
|
2329
|
+
} else if (column.columnType === "PgGeometry") {
|
|
2330
|
+
baseType = "[Float!]";
|
|
2331
|
+
} else {
|
|
2332
|
+
const scalarName = `${capitalize(tableName)}${capitalize(
|
|
2333
|
+
columnName
|
|
2334
|
+
)}Array`;
|
|
2335
|
+
baseType = scalarName;
|
|
2336
|
+
customScalars.add(scalarName);
|
|
2337
|
+
}
|
|
2338
|
+
break;
|
|
2339
|
+
case "custom":
|
|
2340
|
+
default:
|
|
2341
|
+
if (column.columnType) {
|
|
2342
|
+
baseType = column.columnType;
|
|
2343
|
+
customScalars.add(column.columnType);
|
|
2344
|
+
} else {
|
|
2345
|
+
const customScalarName = `${capitalize(tableName)}${capitalize(
|
|
2346
|
+
columnName
|
|
2347
|
+
)}`;
|
|
2348
|
+
baseType = customScalarName;
|
|
2349
|
+
customScalars.add(customScalarName);
|
|
2350
|
+
}
|
|
2351
|
+
break;
|
|
2352
|
+
}
|
|
2347
2353
|
}
|
|
2348
2354
|
}
|
|
2349
|
-
if (!forceNullable && column.notNull
|
|
2355
|
+
if (!forceNullable && column.notNull) {
|
|
2350
2356
|
return `${baseType}!`;
|
|
2351
2357
|
}
|
|
2352
2358
|
return baseType;
|
|
@@ -2400,6 +2406,53 @@ var generateTypeDefs = (tables, relations) => {
|
|
|
2400
2406
|
customScalars.clear();
|
|
2401
2407
|
enumDefinitions.clear();
|
|
2402
2408
|
requiredFieldFilters.clear();
|
|
2409
|
+
foreignKeyTypes.clear();
|
|
2410
|
+
for (const [tableName, tableRelations] of Object.entries(relations)) {
|
|
2411
|
+
const tableInfo = tables[tableName];
|
|
2412
|
+
if (!tableInfo)
|
|
2413
|
+
continue;
|
|
2414
|
+
for (const [relationName, relationInfo] of Object.entries(tableRelations)) {
|
|
2415
|
+
const relation = relationInfo.relation;
|
|
2416
|
+
if (!is7(relation, One2))
|
|
2417
|
+
continue;
|
|
2418
|
+
const config = relation.config;
|
|
2419
|
+
if (!config?.fields || !config?.references)
|
|
2420
|
+
continue;
|
|
2421
|
+
const referencedTableName = relationInfo.targetTableName;
|
|
2422
|
+
const referencedTable = tables[referencedTableName];
|
|
2423
|
+
if (!referencedTable)
|
|
2424
|
+
continue;
|
|
2425
|
+
for (let i = 0; i < config.fields.length; i++) {
|
|
2426
|
+
const field = config.fields[i];
|
|
2427
|
+
const reference = config.references[i];
|
|
2428
|
+
if (!field || !reference)
|
|
2429
|
+
continue;
|
|
2430
|
+
const fieldColumnName = field.name;
|
|
2431
|
+
const referenceColumnName = reference.name;
|
|
2432
|
+
const foreignKeyColumn = Object.values(tableInfo.columns).find(
|
|
2433
|
+
(col) => col.name === fieldColumnName
|
|
2434
|
+
);
|
|
2435
|
+
const referencedColumn = Object.values(referencedTable.columns).find(
|
|
2436
|
+
(col) => col.name === referenceColumnName
|
|
2437
|
+
);
|
|
2438
|
+
if (!foreignKeyColumn || !referencedColumn)
|
|
2439
|
+
continue;
|
|
2440
|
+
const foreignKeyPropertyName = Object.keys(tableInfo.columns).find(
|
|
2441
|
+
(key) => tableInfo.columns[key] === foreignKeyColumn
|
|
2442
|
+
);
|
|
2443
|
+
if (!foreignKeyPropertyName)
|
|
2444
|
+
continue;
|
|
2445
|
+
const referencedCustomType = referencedColumn.customGraphqlType;
|
|
2446
|
+
const foreignKeyHasCustomType = !!foreignKeyColumn.customGraphqlType;
|
|
2447
|
+
if (referencedCustomType && !foreignKeyHasCustomType) {
|
|
2448
|
+
foreignKeyTypes.set(
|
|
2449
|
+
`${tableName}.${foreignKeyPropertyName}`,
|
|
2450
|
+
referencedCustomType
|
|
2451
|
+
);
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2403
2456
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
2404
2457
|
const typeName = capitalize(tableName);
|
|
2405
2458
|
const fields = [];
|