Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async initialize () {
api.models = {}
const sequelizeInstance = new Sequelize(
api.config.sequelize.database,
api.config.sequelize.username,
api.config.sequelize.password,
api.config.sequelize
)
api.sequelize = {
sequelize: sequelizeInstance,
connect: async () => {
const dir = path.normalize(api.projectRoot + '/models')
fs.readdirSync(dir).forEach((file) => {
var nameParts = file.split('/')
var name = nameParts[(nameParts.length - 1)].split('.')[0]
async run (data) {
data.response.success = false
const sessionData = await api.session.load(data.connection)
if (!sessionData) { throw new Error('Please log in to continue') }
const user = await api.models.user.findOne({ where: { id: sessionData.userId } })
if (!user) { throw new Error('user not found') }
data.response.user = user.apiData(api)
data.response.csrfToken = sessionData.csrfToken
data.response.success = true
}
}
async run (data) {
const user = await api.models.user.findOne({ where: { id: data.session.userId } })
if (!user) { throw new Error('user not found') }
data.response.user = user.apiData(api)
}
}