Last active
January 26, 2023 13:32
-
-
Save adevinwild/e24d674406f0ec593ced4f567f23f940 to your computer and use it in GitHub Desktop.
Handle medusa.js insertion inside a table without the medusa-cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { User, UserRoles } from '@medusajs/medusa' | |
import { CreateUserInput } from '@medusajs/medusa/dist/types/user' | |
import { validateEmail } from '@medusajs/medusa/dist/utils/is-email' | |
import arg from 'arg' | |
import Scrypt from 'scrypt-kdf' | |
import { getConnection, Repository } from 'typeorm' | |
type ArgOption = { | |
createUser?: boolean | |
email?: string | |
password?: string | |
} | |
interface IArgumentParser { | |
parse(): ArgOption | |
} | |
class ArgumentParser implements IArgumentParser { | |
parse() { | |
const args = arg({ | |
'--user': Boolean, | |
'--email': String, | |
'--password': String, | |
'--u': '--user', | |
'-e': '--email', | |
'-p': '--password', | |
}) | |
return { | |
createUser: !!args['--user'] || false, | |
email: args['--email'] || '', | |
password: args['--password'] || '', | |
} | |
} | |
} | |
type UserCredentials = { | |
email: string | |
password: string | |
} | |
interface IUserCreator { | |
insert(credentials: UserCredentials): void | |
} | |
class UserCreator implements IUserCreator { | |
async getUserRepository(): Promise<Repository<User>> { | |
const connection = getConnection() | |
const manager = connection.manager | |
return manager.getRepository('user') | |
} | |
async insert({ email, password }): Promise<User> { | |
const userRepo = await this.getUserRepository() | |
const newUser: CreateUserInput & { | |
password_hash: string | |
} = { | |
email, | |
password_hash: '', | |
role: UserRoles.ADMIN, // ? You can change this to `UserRoles.MEMBER` if you want, btw UserRoles exists in `@medusajs/medusa/dist/types/user` | |
} | |
// ? Using the `validateEmail` function from `@medusajs/medusa/dist/utils/is-email` | |
const validatedEmail = validateEmail(email) | |
newUser.email = validatedEmail | |
newUser.password_hash = await this.hashPassword(password) | |
const user = userRepo.create(newUser) | |
const userSaved = await userRepo.save(user) | |
return userSaved | |
} | |
async hashPassword(password: string): Promise<string> { | |
// ? Using the `scrypt-kdf` package, this how Medusa does it | |
const buf = await Scrypt.kdf(password, { logN: 1, r: 1, p: 1 }) | |
return buf.toString('base64') | |
} | |
} | |
class ServerCLI { | |
constructor(private readonly argumentParser: ArgumentParser, private readonly userCreator: UserCreator) { | |
this.argumentParser = argumentParser | |
this.userCreator = userCreator | |
} | |
async run(): Promise<void> { | |
const { createUser, email, password } = this.argumentParser.parse() | |
// ? If the `--user` flag is passed, then we'll create a new user | |
if (createUser) { | |
if (!email || !password) { | |
throw new Error('Email and password are required ❌') | |
} | |
await this.userCreator.insert({ email, password }) | |
console.info('A new user has been created✅') | |
} | |
} | |
} | |
const serverCLI = new ServerCLI(new ArgumentParser(), new UserCreator()) | |
export default serverCLI |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment