@better-auth/cli 1.3.0-beta.1 → 1.3.0-beta.10

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.
Files changed (3) hide show
  1. package/README.md +41 -0
  2. package/dist/index.mjs +31 -12
  3. package/package.json +10 -2
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Better Auth CLI
2
+
3
+ Better Auth comes with a built-in CLI to help you manage the database schema needed for both core functionality and plugins.
4
+
5
+
6
+ ### **Init**
7
+
8
+ The CLI includes an `init` command to add Better Auth to your project.
9
+
10
+ ```bash title="terminal"
11
+ npx @better-auth/cli@latest init
12
+ ```
13
+
14
+ ### **Generate**
15
+
16
+ The `generate` command creates the schema required by Better Auth. If you're using a database adapter like Prisma or Drizzle, this command will generate the right schema for your ORM. If you're using the built-in Kysely adapter, it will generate an SQL file you can run directly on your database.
17
+
18
+ ```bash title="terminal"
19
+ npx @better-auth/cli@latest generate
20
+ ```
21
+
22
+ ### **Migrate**
23
+
24
+ The `migrate` command applies the Better Auth schema directly to your database. This is available if you’re using the built-in Kysely adapter. For other adapters, you'll need to apply the schema using your ORM's migration tool.
25
+
26
+ ```bash title="terminal"
27
+ npx @better-auth/cli@latest migrate
28
+ ```
29
+
30
+ ### **Secret**
31
+
32
+ The CLI also provides a way to generate a secret key for your Better Auth instance.
33
+
34
+ ```bash title="terminal"
35
+ npx @better-auth/cli@latest secret
36
+ ```
37
+
38
+
39
+ ## License
40
+
41
+ MIT
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { Command } from 'commander';
3
- import { z } from 'zod';
3
+ import * as z from 'zod/v4';
4
4
  import fs$1, { existsSync } from 'fs';
5
5
  import path from 'path';
6
6
  import yoctoSpinner from 'yocto-spinner';
@@ -13,12 +13,12 @@ import babelPresetTypeScript from '@babel/preset-typescript';
13
13
  import babelPresetReact from '@babel/preset-react';
14
14
  import fs from 'fs-extra';
15
15
  import fs$2 from 'fs/promises';
16
+ import prettier, { format } from 'prettier';
16
17
  import { produceSchema } from '@mrleebo/prisma-ast';
17
18
  import 'dotenv/config';
18
19
  import Crypto from 'crypto';
19
20
  import { parse } from 'dotenv';
20
21
  import semver from 'semver';
21
- import { format } from 'prettier';
22
22
  import { intro, log, outro, confirm, isCancel, cancel, spinner, text, select, multiselect } from '@clack/prompts';
23
23
  import { exec } from 'child_process';
24
24
 
@@ -398,7 +398,10 @@ const migrate = new Command("migrate").option(
398
398
  false
399
399
  ).action(migrateAction);
400
400
 
401
- function convertToSnakeCase(str) {
401
+ function convertToSnakeCase(str, camelCase) {
402
+ if (camelCase) {
403
+ return str;
404
+ }
402
405
  return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
403
406
  }
404
407
  const generateDrizzleSchema = async ({
@@ -415,7 +418,7 @@ const generateDrizzleSchema = async ({
415
418
  );
416
419
  }
417
420
  const fileExist = existsSync(filePath);
418
- let code = generateImport({ databaseType, tables });
421
+ let code = generateImport({ databaseType, tables, options });
419
422
  for (const tableKey in tables) {
420
423
  let getType = function(name, field) {
421
424
  if (!databaseType) {
@@ -423,15 +426,20 @@ const generateDrizzleSchema = async ({
423
426
  `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`
424
427
  );
425
428
  }
426
- name = convertToSnakeCase(name);
429
+ name = convertToSnakeCase(name, adapter.options?.camelCase);
427
430
  if (field.references?.field === "id") {
428
431
  if (options.advanced?.database?.useNumberId) {
429
432
  if (databaseType === "pg") {
430
- return `serial('${name}').primaryKey()`;
433
+ return `integer('${name}')`;
431
434
  } else if (databaseType === "mysql") {
432
- return `int('${name}').autoIncrement().primaryKey()`;
435
+ return `int('${name}')`;
433
436
  } else {
434
- return `integer({ mode: 'number' }).primaryKey({ autoIncrement: true })`;
437
+ return `integer('${name}')`;
438
+ }
439
+ }
440
+ if (field.references.field) {
441
+ if (databaseType === "mysql") {
442
+ return `varchar('${name}', { length: 36 })`;
435
443
  }
436
444
  }
437
445
  return `text('${name}')`;
@@ -476,7 +484,11 @@ const generateDrizzleSchema = async ({
476
484
  const fields = table.fields;
477
485
  let id = "";
478
486
  if (options.advanced?.database?.useNumberId) {
479
- id = `int("id").autoincrement.primaryKey()`;
487
+ if (databaseType === "pg") {
488
+ id = `serial("id").primaryKey()`;
489
+ } else {
490
+ id = `int("id").autoincrement.primaryKey()`;
491
+ }
480
492
  } else {
481
493
  if (databaseType === "mysql") {
482
494
  id = `varchar('id', { length: 36 }).primaryKey()`;
@@ -487,7 +499,8 @@ const generateDrizzleSchema = async ({
487
499
  }
488
500
  }
489
501
  const schema = `export const ${modelName} = ${databaseType}Table("${convertToSnakeCase(
490
- modelName
502
+ modelName,
503
+ adapter.options?.camelCase
491
504
  )}", {
492
505
  id: ${id},
493
506
  ${Object.keys(fields).map((field) => {
@@ -512,20 +525,25 @@ const generateDrizzleSchema = async ({
512
525
  ${schema}
513
526
  `;
514
527
  }
528
+ const formattedCode = await prettier.format(code, {
529
+ parser: "typescript"
530
+ });
515
531
  return {
516
- code,
532
+ code: formattedCode,
517
533
  fileName: filePath,
518
534
  overwrite: fileExist
519
535
  };
520
536
  };
521
537
  function generateImport({
522
538
  databaseType,
523
- tables
539
+ tables,
540
+ options
524
541
  }) {
525
542
  let imports = [];
526
543
  const hasBigint = Object.values(tables).some(
527
544
  (table) => Object.values(table.fields).some((field) => field.bigint)
528
545
  );
546
+ const useNumberId = options.advanced?.database?.useNumberId;
529
547
  imports.push(`${databaseType}Table`);
530
548
  imports.push(
531
549
  databaseType === "mysql" ? "varchar, text" : databaseType === "pg" ? "text" : "text"
@@ -533,6 +551,7 @@ function generateImport({
533
551
  imports.push(hasBigint ? databaseType !== "sqlite" ? "bigint" : "" : "");
534
552
  imports.push(databaseType !== "sqlite" ? "timestamp, boolean" : "");
535
553
  imports.push(databaseType === "mysql" ? "int" : "integer");
554
+ imports.push(useNumberId ? databaseType === "pg" ? "serial" : "" : "");
536
555
  return `import { ${imports.map((x) => x.trim()).filter((x) => x !== "").join(", ")} } from "drizzle-orm/${databaseType}-core";
537
556
  `;
538
557
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/cli",
3
- "version": "1.3.0-beta.1",
3
+ "version": "1.3.0-beta.10",
4
4
  "description": "The CLI for Better Auth",
5
5
  "module": "dist/index.mjs",
6
6
  "repository": {
@@ -8,12 +8,20 @@
8
8
  "url": "https://github.com/better-auth/better-auth",
9
9
  "directory": "packages/cli"
10
10
  },
11
+ "homepage": "https://www.better-auth.com/docs/concepts/cli",
11
12
  "main": "./dist/index.mjs",
12
13
  "publishConfig": {
13
14
  "executableFiles": [
14
15
  "./dist/index.mjs"
15
16
  ]
16
17
  },
18
+ "license": "MIT",
19
+ "keywords": [
20
+ "auth",
21
+ "cli",
22
+ "typescript",
23
+ "better-auth"
24
+ ],
17
25
  "exports": "./dist/index.mjs",
18
26
  "bin": "./dist/index.mjs",
19
27
  "devDependencies": {
@@ -47,7 +55,7 @@
47
55
  "tinyexec": "^0.3.1",
48
56
  "yocto-spinner": "^0.1.1",
49
57
  "zod": "^3.23.8",
50
- "better-auth": "1.3.0-beta.1"
58
+ "better-auth": "1.3.0-beta.10"
51
59
  },
52
60
  "files": [
53
61
  "dist"