feat(types,module-sdk): setup authentication module skeleton (#5943)
**What** - add authentication module skeleton
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
export default (async () => {
|
||||
const { revertMigration } = await import("../migration-down")
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
await revertMigration()
|
||||
})()
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
export default (async () => {
|
||||
const { runMigrations } = await import("../migration-up")
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
await runMigrations()
|
||||
})()
|
||||
19
packages/authentication/src/scripts/bin/run-seed.ts
Normal file
19
packages/authentication/src/scripts/bin/run-seed.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { EOL } from "os"
|
||||
import { run } from "../seed"
|
||||
|
||||
const args = process.argv
|
||||
const path = args.pop() as string
|
||||
|
||||
export default (async () => {
|
||||
const { config } = await import("dotenv")
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-authentication-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
await run({ path })
|
||||
})()
|
||||
2
packages/authentication/src/scripts/index.ts
Normal file
2
packages/authentication/src/scripts/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./migration-up"
|
||||
export * from "./migration-down"
|
||||
50
packages/authentication/src/scripts/migration-down.ts
Normal file
50
packages/authentication/src/scripts/migration-down.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Modules } from "@medusajs/modules-sdk";
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types";
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils";
|
||||
import { EntitySchema } from "@mikro-orm/core";
|
||||
import * as AuthenticationModels from "@models";
|
||||
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of reverting the migrations.
|
||||
* @param options
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
export async function revertMigration({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||
Modules.AUTHENTICATION,
|
||||
options
|
||||
)!
|
||||
const entities = Object.values(
|
||||
AuthenticationModels
|
||||
) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
await migrator.down()
|
||||
|
||||
logger?.info("Authentication module migration executed")
|
||||
} catch (error) {
|
||||
logger?.error(
|
||||
`Authentication module migration failed to run - Error: ${error}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
62
packages/authentication/src/scripts/migration-up.ts
Normal file
62
packages/authentication/src/scripts/migration-up.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { Modules } from "@medusajs/modules-sdk";
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types";
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils";
|
||||
import { EntitySchema } from "@mikro-orm/core";
|
||||
import * as AuthenticationModels from "@models";
|
||||
|
||||
/**
|
||||
* This script is only valid for mikro orm managers. If a user provide a custom manager
|
||||
* he is in charge of running the migrations.
|
||||
* @param options
|
||||
* @param logger
|
||||
* @param moduleDeclaration
|
||||
*/
|
||||
export async function runMigrations({
|
||||
options,
|
||||
logger,
|
||||
}: Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
> = {}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||
Modules.AUTHENTICATION,
|
||||
options
|
||||
)!
|
||||
const entities = Object.values(
|
||||
AuthenticationModels
|
||||
) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
try {
|
||||
const migrator = orm.getMigrator()
|
||||
|
||||
const pendingMigrations = await migrator.getPendingMigrations()
|
||||
logger.info(
|
||||
`Running pending migrations: ${JSON.stringify(
|
||||
pendingMigrations,
|
||||
null,
|
||||
2
|
||||
)}`
|
||||
)
|
||||
|
||||
await migrator.up({
|
||||
migrations: pendingMigrations.map((m) => m.name),
|
||||
})
|
||||
|
||||
logger.info("Authentication module migration executed")
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Authentication module migration failed to run - Error: ${error}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close()
|
||||
}
|
||||
63
packages/authentication/src/scripts/seed.ts
Normal file
63
packages/authentication/src/scripts/seed.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as AuthenticationModels from "@models"
|
||||
import { EOL } from "os"
|
||||
import { resolve } from "path"
|
||||
|
||||
export async function run({
|
||||
options,
|
||||
logger,
|
||||
path,
|
||||
}: Partial<
|
||||
Pick<
|
||||
LoaderOptions<ModulesSdkTypes.ModuleServiceInitializeOptions>,
|
||||
"options" | "logger"
|
||||
>
|
||||
> & {
|
||||
path: string
|
||||
}) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
logger.info(`Loading seed data from ${path}...`)
|
||||
|
||||
const { authenticationData } = await import(
|
||||
resolve(process.cwd(), path)
|
||||
).catch((e) => {
|
||||
logger?.error(
|
||||
`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following: authenticationData.${EOL}${e}`
|
||||
)
|
||||
throw e
|
||||
})
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||
Modules.AUTHENTICATION,
|
||||
options
|
||||
)!
|
||||
const entities = Object.values(
|
||||
AuthenticationModels
|
||||
) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
const orm = await DALUtils.mikroOrmCreateConnection(
|
||||
dbData,
|
||||
entities,
|
||||
pathToMigrations
|
||||
)
|
||||
|
||||
const manager = orm.em.fork()
|
||||
|
||||
try {
|
||||
logger.info("Seeding authentication data..")
|
||||
|
||||
// TODO: implement authentication seed data
|
||||
// await createAuthUsers(manager, authUsersData)
|
||||
} catch (e) {
|
||||
logger.error(
|
||||
`Failed to insert the seed data in the PostgreSQL database ${dbData.clientUrl}.${EOL}${e}`
|
||||
)
|
||||
}
|
||||
|
||||
await orm.close(true)
|
||||
}
|
||||
Reference in New Issue
Block a user