@better-auth/cli 1.2.6-beta.6 → 1.2.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/dist/index.mjs +103 -21
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -404,20 +404,33 @@ const generateDrizzleSchema = async ({
|
|
|
404
404
|
const tables = getAuthTables(options);
|
|
405
405
|
const filePath = file || "./auth-schema.ts";
|
|
406
406
|
const databaseType = adapter.options?.provider;
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
);
|
|
413
|
-
const bigint = databaseType !== "sqlite" ? "bigint" : "";
|
|
414
|
-
const text = databaseType === "mysql" ? "varchar, text" : "text";
|
|
415
|
-
let code = `import { ${databaseType}Table, ${text}, ${int}${hasBigint ? `, ${bigint}` : ""}, ${timestampAndBoolean} } from "drizzle-orm/${databaseType}-core";
|
|
416
|
-
`;
|
|
407
|
+
if (!databaseType) {
|
|
408
|
+
throw new Error(
|
|
409
|
+
`Database provider type is undefined during Drizzle schema generation. Please define a \`provider\` in the Drizzle adapter config. Read more at https://better-auth.com/docs/adapters/drizzle`
|
|
410
|
+
);
|
|
411
|
+
}
|
|
417
412
|
const fileExist = existsSync(filePath);
|
|
418
|
-
|
|
413
|
+
let code = generateImport({ databaseType, tables });
|
|
414
|
+
for (const tableKey in tables) {
|
|
419
415
|
let getType = function(name, field) {
|
|
416
|
+
if (!databaseType) {
|
|
417
|
+
throw new Error(
|
|
418
|
+
`Database provider type is undefined during Drizzle schema generation. Please define a \`provider\` in the Drizzle adapter config. Read more at https://better-auth.com/docs/adapters/drizzle`
|
|
419
|
+
);
|
|
420
|
+
}
|
|
420
421
|
name = convertToSnakeCase(name);
|
|
422
|
+
if (field.references?.field === "id") {
|
|
423
|
+
if (options.advanced?.database?.useNumberId) {
|
|
424
|
+
if (databaseType === "pg") {
|
|
425
|
+
return `serial('${name}').primaryKey()`;
|
|
426
|
+
} else if (databaseType === "mysql") {
|
|
427
|
+
return `int('${name}').autoIncrement().primaryKey()`;
|
|
428
|
+
} else {
|
|
429
|
+
return `integer({ mode: 'number' }).primaryKey({ autoIncrement: true })`;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return databaseType === "pg" ? `uuid('${name}')` : `text('${name}')`;
|
|
433
|
+
}
|
|
421
434
|
const type = field.type;
|
|
422
435
|
const typeMap = {
|
|
423
436
|
string: {
|
|
@@ -439,20 +452,45 @@ const generateDrizzleSchema = async ({
|
|
|
439
452
|
sqlite: `integer('${name}', { mode: 'timestamp' })`,
|
|
440
453
|
pg: `timestamp('${name}')`,
|
|
441
454
|
mysql: `timestamp('${name}')`
|
|
455
|
+
},
|
|
456
|
+
"number[]": {
|
|
457
|
+
sqlite: `integer('${name}').array()`,
|
|
458
|
+
pg: field.bigint ? `bigint('${name}', { mode: 'number' }).array()` : `integer('${name}').array()`,
|
|
459
|
+
mysql: field.bigint ? `bigint('${name}', { mode: 'number' }).array()` : `int('${name}').array()`
|
|
460
|
+
},
|
|
461
|
+
"string[]": {
|
|
462
|
+
sqlite: `text('${name}').array()`,
|
|
463
|
+
pg: `text('${name}').array()`,
|
|
464
|
+
mysql: `text('${name}').array()`
|
|
442
465
|
}
|
|
443
466
|
};
|
|
444
|
-
return typeMap[type][databaseType
|
|
467
|
+
return typeMap[type][databaseType];
|
|
445
468
|
};
|
|
446
|
-
const
|
|
447
|
-
const
|
|
448
|
-
const
|
|
469
|
+
const table = tables[tableKey];
|
|
470
|
+
const modelName = getModelName(table.modelName, adapter.options);
|
|
471
|
+
const fields = table.fields;
|
|
472
|
+
let id = "";
|
|
473
|
+
if (options.advanced?.database?.useNumberId) {
|
|
474
|
+
id = `int("id").autoincrement.primaryKey()`;
|
|
475
|
+
} else {
|
|
476
|
+
if (databaseType === "mysql") {
|
|
477
|
+
id = `varchar('id', { length: 36 }).primaryKey()`;
|
|
478
|
+
} else if (databaseType === "pg") {
|
|
479
|
+
id = `uuid('id').primaryKey()`;
|
|
480
|
+
} else {
|
|
481
|
+
id = `text('id').primaryKey()`;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
449
484
|
const schema = `export const ${modelName} = ${databaseType}Table("${convertToSnakeCase(
|
|
450
485
|
modelName
|
|
451
486
|
)}", {
|
|
452
487
|
id: ${id},
|
|
453
488
|
${Object.keys(fields).map((field) => {
|
|
454
489
|
const attr = fields[field];
|
|
455
|
-
return `${field}: ${getType(field, attr)}${attr.required ? ".notNull()" : ""}${attr.unique ? ".unique()" : ""}${attr.references ? `.references(()=> ${
|
|
490
|
+
return `${field}: ${getType(field, attr)}${attr.required ? ".notNull()" : ""}${attr.unique ? ".unique()" : ""}${attr.references ? `.references(()=> ${getModelName(
|
|
491
|
+
attr.references.model,
|
|
492
|
+
adapter.options
|
|
493
|
+
)}.${attr.references.field}, { onDelete: '${attr.references.onDelete || "cascade"}' })` : ""}`;
|
|
456
494
|
}).join(",\n ")}
|
|
457
495
|
});`;
|
|
458
496
|
code += `
|
|
@@ -465,6 +503,27 @@ ${schema}
|
|
|
465
503
|
overwrite: fileExist
|
|
466
504
|
};
|
|
467
505
|
};
|
|
506
|
+
function generateImport({
|
|
507
|
+
databaseType,
|
|
508
|
+
tables
|
|
509
|
+
}) {
|
|
510
|
+
let imports = [];
|
|
511
|
+
const hasBigint = Object.values(tables).some(
|
|
512
|
+
(table) => Object.values(table.fields).some((field) => field.bigint)
|
|
513
|
+
);
|
|
514
|
+
imports.push(`${databaseType}Table`);
|
|
515
|
+
imports.push(
|
|
516
|
+
databaseType === "mysql" ? "varchar, text" : databaseType === "pg" ? "uuid, text" : "text"
|
|
517
|
+
);
|
|
518
|
+
imports.push(hasBigint ? databaseType !== "sqlite" ? "bigint" : "" : "");
|
|
519
|
+
imports.push(databaseType !== "sqlite" ? "timestamp, boolean" : "");
|
|
520
|
+
imports.push(databaseType === "mysql" ? "int" : "integer");
|
|
521
|
+
return `import { ${imports.map((x) => x.trim()).filter((x) => x !== "").join(", ")} } from "drizzle-orm/${databaseType}-core";
|
|
522
|
+
`;
|
|
523
|
+
}
|
|
524
|
+
function getModelName(modelName, options) {
|
|
525
|
+
return options?.usePlural ? `${modelName}s` : modelName;
|
|
526
|
+
}
|
|
468
527
|
|
|
469
528
|
const generatePrismaSchema = async ({
|
|
470
529
|
adapter,
|
|
@@ -500,7 +559,11 @@ const generatePrismaSchema = async ({
|
|
|
500
559
|
}
|
|
501
560
|
const schema = produceSchema(schemaPrisma, (builder) => {
|
|
502
561
|
for (const table in tables) {
|
|
503
|
-
let getType = function(
|
|
562
|
+
let getType = function({
|
|
563
|
+
isBigint,
|
|
564
|
+
isOptional,
|
|
565
|
+
type
|
|
566
|
+
}) {
|
|
504
567
|
if (type === "string") {
|
|
505
568
|
return isOptional ? "String?" : "String";
|
|
506
569
|
}
|
|
@@ -533,7 +596,11 @@ const generatePrismaSchema = async ({
|
|
|
533
596
|
if (provider === "mongodb") {
|
|
534
597
|
builder.model(modelName).field("id", "String").attribute("id").attribute(`map("_id")`);
|
|
535
598
|
} else {
|
|
536
|
-
|
|
599
|
+
if (options.advanced?.database?.useNumberId) {
|
|
600
|
+
builder.model(modelName).field("id", "Int").attribute("id").attribute("default(autoincrement())");
|
|
601
|
+
} else {
|
|
602
|
+
builder.model(modelName).field("id", "String").attribute("id");
|
|
603
|
+
}
|
|
537
604
|
}
|
|
538
605
|
}
|
|
539
606
|
for (const field in fields) {
|
|
@@ -549,17 +616,31 @@ const generatePrismaSchema = async ({
|
|
|
549
616
|
}
|
|
550
617
|
builder.model(modelName).field(
|
|
551
618
|
field,
|
|
552
|
-
|
|
619
|
+
field === "id" && options.advanced?.database?.useNumberId ? getType({
|
|
620
|
+
isBigint: false,
|
|
621
|
+
isOptional: false,
|
|
622
|
+
type: "number"
|
|
623
|
+
}) : getType({
|
|
624
|
+
isBigint: attr?.bigint || false,
|
|
625
|
+
isOptional: !attr?.required,
|
|
626
|
+
type: attr.references?.field === "id" ? options.advanced?.database?.useNumberId ? "number" : "string" : attr.type
|
|
627
|
+
})
|
|
553
628
|
);
|
|
554
629
|
if (attr.unique) {
|
|
555
630
|
builder.model(modelName).blockAttribute(`unique([${field}])`);
|
|
556
631
|
}
|
|
557
632
|
if (attr.references) {
|
|
633
|
+
let action = "Cascade";
|
|
634
|
+
if (attr.references.onDelete === "no action") action = "NoAction";
|
|
635
|
+
else if (attr.references.onDelete === "set null") action = "SetNull";
|
|
636
|
+
else if (attr.references.onDelete === "set default")
|
|
637
|
+
action = "SetDefault";
|
|
638
|
+
else if (attr.references.onDelete === "restrict") action = "Restrict";
|
|
558
639
|
builder.model(modelName).field(
|
|
559
640
|
`${attr.references.model.toLowerCase()}`,
|
|
560
641
|
capitalizeFirstLetter(attr.references.model)
|
|
561
642
|
).attribute(
|
|
562
|
-
`relation(fields: [${field}], references: [${attr.references.field}], onDelete:
|
|
643
|
+
`relation(fields: [${field}], references: [${attr.references.field}], onDelete: ${action})`
|
|
563
644
|
);
|
|
564
645
|
}
|
|
565
646
|
if (!attr.unique && !attr.references && provider === "mysql" && attr.type === "string") {
|
|
@@ -589,7 +670,8 @@ const generatePrismaSchema = async ({
|
|
|
589
670
|
});
|
|
590
671
|
return {
|
|
591
672
|
code: schema.trim() === schemaPrisma.trim() ? "" : schema,
|
|
592
|
-
fileName: filePath
|
|
673
|
+
fileName: filePath,
|
|
674
|
+
overwrite: true
|
|
593
675
|
};
|
|
594
676
|
};
|
|
595
677
|
const getNewPrisma = (provider) => `generator client {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/cli",
|
|
3
|
-
"version": "1.2.6
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "The CLI for Better Auth",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"repository": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"tinyexec": "^0.3.1",
|
|
47
47
|
"yocto-spinner": "^0.1.1",
|
|
48
48
|
"zod": "^3.23.8",
|
|
49
|
-
"better-auth": "1.2.6
|
|
49
|
+
"better-auth": "1.2.6"
|
|
50
50
|
},
|
|
51
51
|
"files": [
|
|
52
52
|
"dist"
|