@a77ay/better-auth-mikro-orm 0.1.1 → 0.2.0
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/README.md +16 -17
- package/dist/{cli-cy61BGrg.mjs → cli-C8Q_bfEy.mjs} +74 -40
- package/dist/cli.mjs +2 -3
- package/dist/index.d.mts +13 -25
- package/dist/index.mjs +1 -2
- package/package.json +15 -15
package/README.md
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
# @a77ay/better-auth-mikro-orm
|
|
2
2
|
|
|
3
|
-
MikroORM database adapter and entity generator for [Better Auth](https://www.better-auth.com).
|
|
3
|
+
[MikroORM](https://mikro-orm.io) database adapter and entity generator for [Better Auth](https://www.better-auth.com).
|
|
4
4
|
|
|
5
5
|
[**🔌 Database Adapter**](#1-adapter-setup) • [**⚡ CLI Entity Generator**](#2-schema--entity-generation)
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/@a77ay/better-auth-mikro-orm)
|
|
8
|
-
[](https://bundlephobia.com/package/@a77ay/better-auth-mikro-orm)
|
|
9
8
|
[](https://github.com/A77AY/better-auth-mikro-orm/actions/workflows/ci.yml)
|
|
10
9
|
[](https://codecov.io/gh/A77AY/better-auth-mikro-orm)
|
|
11
10
|
|
|
@@ -53,7 +52,7 @@ export const auth = betterAuth({
|
|
|
53
52
|
});
|
|
54
53
|
```
|
|
55
54
|
|
|
56
|
-
Pass your `MikroORM` instance, `EntityManager`, or a getter function. When passing `orm`, the adapter automatically uses `RequestContext.getEntityManager()` per HTTP request and falls back to `orm.em` for CLI commands and background scripts.
|
|
55
|
+
Pass your `MikroORM` instance, `EntityManager`, or a getter function. When passing `orm`, the adapter automatically uses `RequestContext.getEntityManager()` per HTTP request and falls back to a forked `orm.em` for CLI commands and background scripts.
|
|
57
56
|
|
|
58
57
|
### 2. Schema / Entity Generation
|
|
59
58
|
|
|
@@ -63,13 +62,13 @@ Use the package CLI to generate individual entity files and an `index.ts` barrel
|
|
|
63
62
|
|
|
64
63
|
```bash
|
|
65
64
|
# Using npx
|
|
66
|
-
npx
|
|
65
|
+
npx better-auth-mikro-orm generate \
|
|
67
66
|
--config ./src/auth.ts \
|
|
68
67
|
--output ./src/entities/auth \
|
|
69
68
|
--yes
|
|
70
69
|
|
|
71
70
|
# Using pnpm
|
|
72
|
-
pnpm exec
|
|
71
|
+
pnpm exec better-auth-mikro-orm generate \
|
|
73
72
|
--config ./src/auth.ts \
|
|
74
73
|
--output ./src/entities/auth \
|
|
75
74
|
--yes
|
|
@@ -146,18 +145,18 @@ The `mikroOrmAdapter` accepts the database instance or getter as its first argum
|
|
|
146
145
|
mikroOrmAdapter(em, options?)
|
|
147
146
|
```
|
|
148
147
|
|
|
149
|
-
| Parameter / Option | Type
|
|
150
|
-
| ------------------ |
|
|
151
|
-
| `em` (1st arg) | `MikroORM \| EntityManager \| (() => EntityManager)` | **Required**
|
|
152
|
-
| `provider` | `DatabaseProvider`
|
|
153
|
-
| `casing` | `"snake_case" \| "camelCase"`
|
|
154
|
-
| `entityStyle` | `"define-entity" \| "decorators"`
|
|
155
|
-
| `fileSuffix` | `string`
|
|
156
|
-
| `directory` | `string`
|
|
157
|
-
| `schemaFile` | `string`
|
|
158
|
-
| `debugLogs` | `boolean \| DBAdapterDebugLogOption`
|
|
159
|
-
| `usePlural` | `boolean`
|
|
160
|
-
| `transactions` | `boolean`
|
|
148
|
+
| Parameter / Option | Type | Default | Description |
|
|
149
|
+
| ------------------ | ---------------------------------------------------------------- | --------------------- | --------------------------------------------------------------------------------------- |
|
|
150
|
+
| `em` (1st arg) | `MikroORM \| EntityManager \| (() => EntityManager \| MikroORM)` | **Required** | MikroORM instance, `EntityManager`, or getter returning the request-scoped instance. |
|
|
151
|
+
| `provider` | `DatabaseProvider` | Auto-detected | Database provider (`"postgresql"`, `"sqlite"`, `"mysql"`, `"mongodb"`, or custom). |
|
|
152
|
+
| `casing` | `"snake_case" \| "camelCase"` | `"camelCase"` | Naming convention for generated database columns. |
|
|
153
|
+
| `entityStyle` | `"define-entity" \| "decorators"` | `"define-entity"` | Entity definition style (`defineEntity` fluent schema or `@Entity()` class decorators). |
|
|
154
|
+
| `fileSuffix` | `string` | `""` | Custom file suffix for entity files (e.g. `".entity"` generates `user.entity.ts`). |
|
|
155
|
+
| `directory` | `string` | `"src/entities/auth"` | Default output directory used by the package CLI. |
|
|
156
|
+
| `schemaFile` | `string` | `"auth-entities.ts"` | Default output file path for CLI schema generation. |
|
|
157
|
+
| `debugLogs` | `boolean \| DBAdapterDebugLogOption` | `false` | Enable Better Auth adapter debug logging. |
|
|
158
|
+
| `usePlural` | `boolean` | `false` | Use plural table names for generated entities. |
|
|
159
|
+
| `transactions` | `boolean` | `true` | Whether to execute multi-operation callbacks in a transaction. |
|
|
161
160
|
|
|
162
161
|
## Development
|
|
163
162
|
|
|
@@ -6,7 +6,7 @@ import { dirname, resolve } from "node:path";
|
|
|
6
6
|
import { parseArgs } from "node:util";
|
|
7
7
|
import { getAuthTables } from "better-auth/db";
|
|
8
8
|
import { createJiti } from "jiti";
|
|
9
|
-
|
|
9
|
+
// src/utils/strings.ts
|
|
10
10
|
function quote(value) {
|
|
11
11
|
return JSON.stringify(value);
|
|
12
12
|
}
|
|
@@ -27,8 +27,7 @@ function toSnakeCase(value) {
|
|
|
27
27
|
function toKebabCase(value) {
|
|
28
28
|
return toSnakeCase(value).replaceAll("_", "-");
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
//#region src/generator/utils.ts
|
|
30
|
+
// src/generator/utils.ts
|
|
32
31
|
function createModelNames(entries) {
|
|
33
32
|
const usedNames = /* @__PURE__ */ new Set();
|
|
34
33
|
return new Map(entries.map(([model]) => {
|
|
@@ -160,8 +159,7 @@ function scalarType(field, options, indexed = false) {
|
|
|
160
159
|
typescript: "string"
|
|
161
160
|
};
|
|
162
161
|
}
|
|
163
|
-
|
|
164
|
-
//#region src/generator/decorators.ts
|
|
162
|
+
// src/generator/decorators.ts
|
|
165
163
|
function renderDecoratorProperty(fieldName, field, entries, modelNames, options) {
|
|
166
164
|
const propertyName = entityPropertyName(fieldName, field);
|
|
167
165
|
const databaseName = databasePropertyName(fieldName, field, options);
|
|
@@ -180,8 +178,9 @@ function renderDecoratorProperty(fieldName, field, entries, modelNames, options)
|
|
|
180
178
|
opts.push(`deleteRule: ${quote(reference.onDelete ?? "cascade")}`);
|
|
181
179
|
if (reference.field !== "id") opts.push(`targetKey: ${quote(referencedProperty)}`);
|
|
182
180
|
if (field.required === false) opts.push("nullable: true");
|
|
181
|
+
const tsType = reference.field !== "id" && referencedField ? scalarType(referencedField, options).typescript : options.numericIds ? "number" : "string";
|
|
183
182
|
return ` @ManyToOne(() => ${targetModel}, { ${opts.join(", ")} })
|
|
184
|
-
${propertyKey(propertyName)}${optional}: ${
|
|
183
|
+
${propertyKey(propertyName)}${optional}: ${tsType};`;
|
|
185
184
|
}
|
|
186
185
|
if (Array.isArray(field.type)) {
|
|
187
186
|
const opts = [`items: () => ${stringArray(field.type)}`];
|
|
@@ -243,14 +242,26 @@ function referencedModels$1(table, entries) {
|
|
|
243
242
|
}
|
|
244
243
|
return models;
|
|
245
244
|
}
|
|
245
|
+
const decoratorSymbols = [
|
|
246
|
+
"Entity",
|
|
247
|
+
"Enum",
|
|
248
|
+
"Index",
|
|
249
|
+
"ManyToOne",
|
|
250
|
+
"PrimaryKey",
|
|
251
|
+
"Property",
|
|
252
|
+
"Unique"
|
|
253
|
+
];
|
|
254
|
+
function renderDecoratorImports(code) {
|
|
255
|
+
return `import { ${decoratorSymbols.filter((symbol) => code.includes(`@${symbol}(`)).join(", ")} } from "@mikro-orm/core";`;
|
|
256
|
+
}
|
|
246
257
|
function generateDecoratorsSchema(options) {
|
|
247
258
|
const entries = getSortedTableEntries(options.tables);
|
|
248
259
|
const modelNames = createModelNames(entries);
|
|
249
|
-
const
|
|
260
|
+
const body = entries.map(([defaultModel, table]) => renderDecoratorEntity(defaultModel, table, entries, modelNames, options)).join("\n\n");
|
|
250
261
|
const exports = entries.map(([model]) => ` ${modelNames.get(model)},`).join("\n");
|
|
251
|
-
return
|
|
262
|
+
return `${renderDecoratorImports(body)}
|
|
252
263
|
|
|
253
|
-
${
|
|
264
|
+
${body}
|
|
254
265
|
|
|
255
266
|
export const betterAuthEntities = [
|
|
256
267
|
${exports}
|
|
@@ -266,11 +277,12 @@ function generateDecoratorFiles(options) {
|
|
|
266
277
|
return `import { ${modelNames.get(model)} } from "./${fileNames.get(model).replace(/\.ts$/, "")}";`;
|
|
267
278
|
});
|
|
268
279
|
const importBlock = imports.length ? `${imports.join("\n")}\n` : "";
|
|
280
|
+
const entityCode = renderDecoratorEntity(defaultModel, table, entries, modelNames, options);
|
|
269
281
|
return {
|
|
270
282
|
path: fileNames.get(defaultModel),
|
|
271
|
-
code:
|
|
283
|
+
code: `${renderDecoratorImports(entityCode)}
|
|
272
284
|
${importBlock}
|
|
273
|
-
${
|
|
285
|
+
${entityCode}
|
|
274
286
|
`
|
|
275
287
|
};
|
|
276
288
|
});
|
|
@@ -294,8 +306,7 @@ ${exports}
|
|
|
294
306
|
});
|
|
295
307
|
return files;
|
|
296
308
|
}
|
|
297
|
-
|
|
298
|
-
//#region src/generator/define-entity.ts
|
|
309
|
+
// src/generator/define-entity.ts
|
|
299
310
|
function renderDefineProperty(fieldName, field, entries, modelNames, options) {
|
|
300
311
|
const propertyName = entityPropertyName(fieldName, field);
|
|
301
312
|
const databaseName = databasePropertyName(fieldName, field, options);
|
|
@@ -326,7 +337,8 @@ function renderDefineProperty(fieldName, field, entries, modelNames, options) {
|
|
|
326
337
|
if (typeof field.defaultValue === "boolean" || typeof field.defaultValue === "number" || typeof field.defaultValue === "string") chains.push(`default(${typeof field.defaultValue === "string" ? quote(field.defaultValue) : field.defaultValue})`);
|
|
327
338
|
if (field.unique) chains.push("unique()");
|
|
328
339
|
if (field.index) chains.push("index()");
|
|
329
|
-
|
|
340
|
+
const propertyExpression = reference && referencedEntry ? `() => ${chains.join(".")}` : chains.join(".");
|
|
341
|
+
return ` ${propertyKey(propertyName)}: ${propertyExpression},`;
|
|
330
342
|
}
|
|
331
343
|
function renderDefineIndexes(table) {
|
|
332
344
|
const indexes = table.indexes?.filter((index) => !index.unique) ?? [];
|
|
@@ -425,8 +437,7 @@ ${exports}
|
|
|
425
437
|
});
|
|
426
438
|
return files;
|
|
427
439
|
}
|
|
428
|
-
|
|
429
|
-
//#region src/generator/directory.ts
|
|
440
|
+
// src/generator/directory.ts
|
|
430
441
|
function generateMikroOrmSchemaDirectory(options) {
|
|
431
442
|
const files = (options.entityStyle ?? "define-entity") === "decorators" ? generateDecoratorFiles(options) : generateDefineEntityFiles(options);
|
|
432
443
|
return {
|
|
@@ -436,7 +447,7 @@ function generateMikroOrmSchemaDirectory(options) {
|
|
|
436
447
|
}
|
|
437
448
|
async function writeMikroOrmSchemaDirectory(options) {
|
|
438
449
|
const generated = generateMikroOrmSchemaDirectory(options);
|
|
439
|
-
const directory = resolve(process.cwd(), generated.directory);
|
|
450
|
+
const directory = resolve(options.cwd ?? process.cwd(), generated.directory);
|
|
440
451
|
await mkdir(directory, { recursive: true });
|
|
441
452
|
await Promise.all(generated.files.map(async (file) => {
|
|
442
453
|
const output = resolve(directory, file.path);
|
|
@@ -445,8 +456,7 @@ async function writeMikroOrmSchemaDirectory(options) {
|
|
|
445
456
|
}));
|
|
446
457
|
return generated;
|
|
447
458
|
}
|
|
448
|
-
|
|
449
|
-
//#region src/generator/generator.ts
|
|
459
|
+
// src/generator/generator.ts
|
|
450
460
|
/**
|
|
451
461
|
* Generate a deterministic MikroORM entity module for Better Auth and its plugins.
|
|
452
462
|
*
|
|
@@ -461,11 +471,21 @@ function generateMikroOrmSchema(options) {
|
|
|
461
471
|
overwrite: true
|
|
462
472
|
};
|
|
463
473
|
}
|
|
464
|
-
|
|
465
|
-
//#region src/adapter/where.ts
|
|
474
|
+
// src/adapter/where.ts
|
|
466
475
|
function escapeRegExp(value) {
|
|
467
476
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
468
477
|
}
|
|
478
|
+
function hasLikeWildcard(value) {
|
|
479
|
+
return /[%_\\]/.test(value);
|
|
480
|
+
}
|
|
481
|
+
function escapeLike(value) {
|
|
482
|
+
return value.replace(/[\\%_]/g, "\\$&");
|
|
483
|
+
}
|
|
484
|
+
function buildLikePattern(operator, value) {
|
|
485
|
+
if (operator === "contains") return `%${value}%`;
|
|
486
|
+
if (operator === "starts_with") return `${value}%`;
|
|
487
|
+
return `%${value}`;
|
|
488
|
+
}
|
|
469
489
|
function insensitivePattern(operator, value) {
|
|
470
490
|
const escaped = escapeRegExp(value);
|
|
471
491
|
switch (operator) {
|
|
@@ -514,7 +534,19 @@ function transformCondition({ field, mode, operator = "eq", value }, context) {
|
|
|
514
534
|
].includes(operator)) {
|
|
515
535
|
const column = property.fieldNames[0] ?? propName;
|
|
516
536
|
const normalized = value.toLowerCase();
|
|
517
|
-
|
|
537
|
+
if ([
|
|
538
|
+
"contains",
|
|
539
|
+
"starts_with",
|
|
540
|
+
"ends_with"
|
|
541
|
+
].includes(operator) && hasLikeWildcard(value)) {
|
|
542
|
+
const pattern = buildLikePattern(operator, escapeLike(normalized));
|
|
543
|
+
return { [raw("lower(??) like ? escape ?", [
|
|
544
|
+
column,
|
|
545
|
+
pattern,
|
|
546
|
+
"\\"
|
|
547
|
+
])]: [] };
|
|
548
|
+
}
|
|
549
|
+
const transformedValue = operator === "eq" || operator === "ne" ? normalized : buildLikePattern(operator, normalized);
|
|
518
550
|
return { [raw("lower(??)", [column])]: { [operator === "ne" ? "$ne" : operator === "eq" ? "$eq" : "$like"]: transformedValue } };
|
|
519
551
|
}
|
|
520
552
|
if ([
|
|
@@ -528,9 +560,16 @@ function transformCondition({ field, mode, operator = "eq", value }, context) {
|
|
|
528
560
|
const pattern = operator === "contains" ? new RegExp(escaped) : operator === "starts_with" ? new RegExp(`^${escaped}`) : new RegExp(`${escaped}$`);
|
|
529
561
|
return { [propName]: { $re: pattern } };
|
|
530
562
|
}
|
|
531
|
-
if (
|
|
532
|
-
|
|
533
|
-
|
|
563
|
+
if (hasLikeWildcard(value)) {
|
|
564
|
+
const column = property.fieldNames[0] ?? propName;
|
|
565
|
+
const pattern = buildLikePattern(operator, escapeLike(value));
|
|
566
|
+
return { [raw("?? like ? escape ?", [
|
|
567
|
+
column,
|
|
568
|
+
pattern,
|
|
569
|
+
"\\"
|
|
570
|
+
])]: [] };
|
|
571
|
+
}
|
|
572
|
+
return { [propName]: { $like: buildLikePattern(operator, value) } };
|
|
534
573
|
}
|
|
535
574
|
const mikroOperator = operator === "eq" ? "$eq" : `$${operator}`;
|
|
536
575
|
return { [propName]: { [mikroOperator]: value } };
|
|
@@ -548,8 +587,7 @@ function transformWhere(where, context) {
|
|
|
548
587
|
if (orConditions.length) return { $or: orConditions };
|
|
549
588
|
return { $and: andConditions };
|
|
550
589
|
}
|
|
551
|
-
|
|
552
|
-
//#region src/adapter/utils.ts
|
|
590
|
+
// src/adapter/utils.ts
|
|
553
591
|
function getEntityManager(provider) {
|
|
554
592
|
const target = typeof provider === "function" ? provider() : provider;
|
|
555
593
|
if (target && typeof target === "object" && "em" in target && target.em) {
|
|
@@ -597,8 +635,7 @@ async function findOneForMutation(em, entity, where, provider) {
|
|
|
597
635
|
const options = supportsPessimisticLock(em, provider) ? { lockMode: LockMode.PESSIMISTIC_WRITE } : void 0;
|
|
598
636
|
return em.findOne(entity, where, options);
|
|
599
637
|
}
|
|
600
|
-
|
|
601
|
-
//#region src/adapter/joins.ts
|
|
638
|
+
// src/adapter/joins.ts
|
|
602
639
|
/**
|
|
603
640
|
* Fetch and attach joined relations (1:1 and 1:M) to a single entity record.
|
|
604
641
|
*/
|
|
@@ -671,8 +708,7 @@ function selectFields(row, select, join, getSelectedFieldName) {
|
|
|
671
708
|
for (const joinedModel of Object.keys(join ?? {})) selected[joinedModel] = row[joinedModel];
|
|
672
709
|
return selected;
|
|
673
710
|
}
|
|
674
|
-
|
|
675
|
-
//#region src/adapter/options.ts
|
|
711
|
+
// src/adapter/options.ts
|
|
676
712
|
const generatorOptions = Symbol.for("@a77ay/better-auth-mikro-orm/generator-options");
|
|
677
713
|
function attachGeneratorOptions(factory, options) {
|
|
678
714
|
Object.defineProperty(factory, generatorOptions, { value: options ?? {} });
|
|
@@ -682,8 +718,7 @@ function readGeneratorOptions(value) {
|
|
|
682
718
|
if (typeof value !== "function") return void 0;
|
|
683
719
|
return value[generatorOptions];
|
|
684
720
|
}
|
|
685
|
-
|
|
686
|
-
//#region src/adapter/adapter.ts
|
|
721
|
+
// src/adapter/adapter.ts
|
|
687
722
|
function createOrmAdapter(provider, adapterOptions) {
|
|
688
723
|
return ({ getFieldName, options: authOptions }) => {
|
|
689
724
|
return {
|
|
@@ -798,11 +833,11 @@ function createOrmAdapter(provider, adapterOptions) {
|
|
|
798
833
|
const property = findProperty(meta, field);
|
|
799
834
|
if (!property) throw new BetterAuthError(`No field "${field}" exists on MikroORM entity "${meta.className}".`);
|
|
800
835
|
if (isMongo(transaction, adapterOptions?.provider)) {
|
|
801
|
-
|
|
802
|
-
if (
|
|
803
|
-
update[property.name] = current + delta;
|
|
836
|
+
if (typeof instance[property.name] !== "number") throw new TypeError(`Cannot increment non-numeric field "${field}" on "${model}".`);
|
|
837
|
+
if (delta !== 0) update[property.name] = { $inc: delta };
|
|
804
838
|
} else update[property.name] = raw("?? + ?", [property.fieldNames[0] ?? property.name, delta]);
|
|
805
839
|
}
|
|
840
|
+
if (Object.keys(update).length === 0) return toPlainObject(instance);
|
|
806
841
|
const primaryWhere = primaryKeyWhere(meta, instance);
|
|
807
842
|
if (await transaction.nativeUpdate(meta.class, { $and: [transformedWhere, primaryWhere] }, update) === 0) return null;
|
|
808
843
|
const updated = await transaction.findOne(meta.class, primaryWhere, { refresh: true });
|
|
@@ -849,8 +884,7 @@ function mikroOrmAdapter(em, options) {
|
|
|
849
884
|
return factory(authOptions);
|
|
850
885
|
}, options);
|
|
851
886
|
}
|
|
852
|
-
|
|
853
|
-
//#region src/cli/index.ts
|
|
887
|
+
// src/cli/index.ts
|
|
854
888
|
const help = `better-auth-mikro-orm
|
|
855
889
|
|
|
856
890
|
Generate one MikroORM entity per file from a Better Auth configuration.
|
|
@@ -930,7 +964,8 @@ async function runCli(arguments_, io = {}) {
|
|
|
930
964
|
}
|
|
931
965
|
const generated = await writeMikroOrmSchemaDirectory({
|
|
932
966
|
...generatorOptions,
|
|
933
|
-
directory
|
|
967
|
+
directory,
|
|
968
|
+
cwd
|
|
934
969
|
});
|
|
935
970
|
write(`Generated ${generated.files.length - 1} entities and index.ts in ${generated.directory}\n`);
|
|
936
971
|
return 0;
|
|
@@ -973,5 +1008,4 @@ async function findExistingFile(cwd, directory, files) {
|
|
|
973
1008
|
} catch {}
|
|
974
1009
|
}
|
|
975
1010
|
}
|
|
976
|
-
//#endregion
|
|
977
1011
|
export { getEntityManager as a, generateMikroOrmSchemaDirectory as c, readGeneratorOptions as i, writeMikroOrmSchemaDirectory as l, mikroOrmAdapter as n, resolveEntity as o, attachGeneratorOptions as r, generateMikroOrmSchema as s, runCli as t };
|
package/dist/cli.mjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { t as runCli } from "./cli-
|
|
3
|
-
|
|
2
|
+
import { t as runCli } from "./cli-C8Q_bfEy.mjs";
|
|
3
|
+
// src/cli.ts
|
|
4
4
|
runCli(process.argv.slice(2)).catch((error) => {
|
|
5
5
|
const message = error instanceof Error ? error.message : String(error);
|
|
6
6
|
process.stderr.write(`Error: ${message}\n`);
|
|
7
7
|
process.exitCode = 1;
|
|
8
8
|
});
|
|
9
|
-
//#endregion
|
|
10
9
|
export {};
|
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,6 @@ import { EntityManager, EntityMetadata, MikroORM } from "@mikro-orm/core";
|
|
|
2
2
|
import { BetterAuthOptions } from "better-auth";
|
|
3
3
|
import { DBAdapterDebugLogOption, DBAdapterSchemaCreation } from "better-auth/adapters";
|
|
4
4
|
import { BetterAuthDBSchema } from "better-auth/db";
|
|
5
|
-
//#region src/generator/types.d.ts
|
|
6
5
|
type EntityStyle = "define-entity" | "decorators";
|
|
7
6
|
type SchemaCasing = "snake_case" | "camelCase";
|
|
8
7
|
interface MikroOrmSchemaGeneratorOptions {
|
|
@@ -46,12 +45,10 @@ interface GeneratedSchemaDirectory {
|
|
|
46
45
|
directory: string;
|
|
47
46
|
files: GeneratedSchemaFile[];
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
//#endregion
|
|
54
|
-
//#region src/generator/generator.d.ts
|
|
48
|
+
export declare function generateMikroOrmSchemaDirectory(options: MikroOrmSchemaGeneratorOptions): GeneratedSchemaDirectory;
|
|
49
|
+
export declare function writeMikroOrmSchemaDirectory(options: MikroOrmSchemaGeneratorOptions & {
|
|
50
|
+
cwd?: string;
|
|
51
|
+
}): Promise<GeneratedSchemaDirectory>;
|
|
55
52
|
/**
|
|
56
53
|
* Generate a deterministic MikroORM entity module for Better Auth and its plugins.
|
|
57
54
|
*
|
|
@@ -59,16 +56,12 @@ declare function writeMikroOrmSchemaDirectory(options: MikroOrmSchemaGeneratorOp
|
|
|
59
56
|
* - `"define-entity"` (default): Modern MikroORM v7 fluent `defineEntity()` schema API
|
|
60
57
|
* - `"decorators"`: Class-based entities with `@Entity()`, `@Property()`, `@PrimaryKey()`, `@ManyToOne()`
|
|
61
58
|
*/
|
|
62
|
-
declare function generateMikroOrmSchema(options: MikroOrmSchemaGeneratorOptions): DBAdapterSchemaCreation;
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/adapter/utils.d.ts
|
|
59
|
+
export declare function generateMikroOrmSchema(options: MikroOrmSchemaGeneratorOptions): DBAdapterSchemaCreation;
|
|
65
60
|
type EntityRecord = Record<string, unknown>;
|
|
66
61
|
type EntityManagerProvider = EntityManager | MikroORM | (() => EntityManager | MikroORM);
|
|
67
62
|
type DatabaseProvider = "postgresql" | "postgres" | "sqlite" | "mysql" | "mariadb" | "mongodb" | "sqlserver" | "libsql" | (string & {});
|
|
68
|
-
declare function getEntityManager(provider: EntityManagerProvider): EntityManager;
|
|
69
|
-
declare function resolveEntity(em: EntityManager, model: string): EntityMetadata<EntityRecord>;
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/adapter/adapter.d.ts
|
|
63
|
+
export declare function getEntityManager(provider: EntityManagerProvider): EntityManager;
|
|
64
|
+
export declare function resolveEntity(em: EntityManager, model: string): EntityMetadata<EntityRecord>;
|
|
72
65
|
interface MikroOrmAdapterOptions {
|
|
73
66
|
/**
|
|
74
67
|
* Database provider (e.g. "postgresql", "sqlite", "mysql", "mongodb").
|
|
@@ -127,7 +120,7 @@ interface MikroOrmAdapterOptions {
|
|
|
127
120
|
/**
|
|
128
121
|
* Default output file path used by the Better Auth CLI schema generator.
|
|
129
122
|
*
|
|
130
|
-
* @default "
|
|
123
|
+
* @default "auth-entities.ts"
|
|
131
124
|
*/
|
|
132
125
|
schemaFile?: string;
|
|
133
126
|
/**
|
|
@@ -160,18 +153,13 @@ interface MikroOrmAdapterOptions {
|
|
|
160
153
|
*/
|
|
161
154
|
fileSuffix?: string;
|
|
162
155
|
}
|
|
163
|
-
declare function mikroOrmAdapter(em: EntityManagerProvider, options?: MikroOrmAdapterOptions): (authOptions: BetterAuthOptions) => import("better-auth").DBAdapter<BetterAuthOptions>;
|
|
164
|
-
//#endregion
|
|
165
|
-
//#region src/adapter/options.d.ts
|
|
156
|
+
export declare function mikroOrmAdapter(em: EntityManagerProvider, options?: MikroOrmAdapterOptions): (authOptions: BetterAuthOptions) => import("better-auth").DBAdapter<BetterAuthOptions>;
|
|
166
157
|
type AdapterFactory = (...arguments_: any[]) => unknown;
|
|
167
|
-
declare function attachGeneratorOptions<T extends AdapterFactory>(factory: T, options: MikroOrmAdapterOptions | undefined): T;
|
|
168
|
-
declare function readGeneratorOptions(value: unknown): MikroOrmAdapterOptions | undefined;
|
|
169
|
-
//#endregion
|
|
170
|
-
//#region src/cli/index.d.ts
|
|
158
|
+
export declare function attachGeneratorOptions<T extends AdapterFactory>(factory: T, options: MikroOrmAdapterOptions | undefined): T;
|
|
159
|
+
export declare function readGeneratorOptions(value: unknown): MikroOrmAdapterOptions | undefined;
|
|
171
160
|
interface CliIO {
|
|
172
161
|
cwd?: string;
|
|
173
162
|
stdout?: (message: string) => void;
|
|
174
163
|
}
|
|
175
|
-
declare function runCli(arguments_: string[], io?: CliIO): Promise<number>;
|
|
176
|
-
|
|
177
|
-
export { type CliIO, type DatabaseProvider, type EntityManagerProvider, type EntityRecord, type EntityStyle, type GeneratedSchemaDirectory, type GeneratedSchemaFile, type MikroOrmAdapterOptions, type MikroOrmSchemaGeneratorOptions, type SchemaCasing, attachGeneratorOptions, generateMikroOrmSchema, generateMikroOrmSchemaDirectory, getEntityManager, mikroOrmAdapter, readGeneratorOptions, resolveEntity, runCli, writeMikroOrmSchemaDirectory };
|
|
164
|
+
export declare function runCli(arguments_: string[], io?: CliIO): Promise<number>;
|
|
165
|
+
export type { CliIO, DatabaseProvider, EntityManagerProvider, EntityRecord, EntityStyle, GeneratedSchemaDirectory, GeneratedSchemaFile, MikroOrmAdapterOptions, MikroOrmSchemaGeneratorOptions, SchemaCasing };
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { attachGeneratorOptions, generateMikroOrmSchema, generateMikroOrmSchemaDirectory, getEntityManager, mikroOrmAdapter, readGeneratorOptions, resolveEntity, runCli, writeMikroOrmSchemaDirectory };
|
|
1
|
+
export { r as attachGeneratorOptions, s as generateMikroOrmSchema, c as generateMikroOrmSchemaDirectory, a as getEntityManager, n as mikroOrmAdapter, i as readGeneratorOptions, o as resolveEntity, t as runCli, l as writeMikroOrmSchemaDirectory } from "./cli-C8Q_bfEy.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@a77ay/better-auth-mikro-orm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "MikroORM database adapter and entity generator for Better Auth.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adapter",
|
|
@@ -46,25 +46,25 @@
|
|
|
46
46
|
"jiti": "^2.7.0"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@better-auth/test-utils": "^1.7.
|
|
50
|
-
"@changesets/changelog-github": "^1.0.
|
|
51
|
-
"@changesets/cli": "^3.0.
|
|
52
|
-
"@mikro-orm/core": "^7.
|
|
53
|
-
"@mikro-orm/mongodb": "^7.
|
|
54
|
-
"@mikro-orm/postgresql": "^7.
|
|
55
|
-
"@mikro-orm/sqlite": "^7.
|
|
56
|
-
"@types/node": "^26.
|
|
49
|
+
"@better-auth/test-utils": "^1.7.27",
|
|
50
|
+
"@changesets/changelog-github": "^1.0.1",
|
|
51
|
+
"@changesets/cli": "^3.0.3",
|
|
52
|
+
"@mikro-orm/core": "^7.2.11",
|
|
53
|
+
"@mikro-orm/mongodb": "^7.2.11",
|
|
54
|
+
"@mikro-orm/postgresql": "^7.2.11",
|
|
55
|
+
"@mikro-orm/sqlite": "^7.2.11",
|
|
56
|
+
"@types/node": "^26.6.6",
|
|
57
57
|
"@types/pg": "^8.23.1",
|
|
58
|
-
"@vitest/coverage-v8": "
|
|
59
|
-
"better-auth": "^1.7.
|
|
60
|
-
"pg": "^8.23.
|
|
58
|
+
"@vitest/coverage-v8": "5.1.0",
|
|
59
|
+
"better-auth": "^1.7.27",
|
|
60
|
+
"pg": "^8.23.1",
|
|
61
61
|
"typescript": "^7.0.2",
|
|
62
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@
|
|
63
|
-
"vite-plus": "
|
|
62
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@8.3.8000",
|
|
63
|
+
"vite-plus": "1.7.0"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
66
|
"@mikro-orm/core": "^7.0.0",
|
|
67
|
-
"better-auth": "^1.
|
|
67
|
+
"better-auth": "^1.7.0"
|
|
68
68
|
},
|
|
69
69
|
"devEngines": {
|
|
70
70
|
"packageManager": {
|