Skip to content

Commit

Permalink
fix: not seed if data already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Leoglme committed Jan 22, 2024
1 parent 96555ae commit 27588e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/Controllers/Http/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import UserService from 'App/Services/UserService'
import ChannelService from "App/Services/ChannelService";
import ChannelService from 'App/Services/ChannelService'

export default class UserController {
private async index({ response }) {
Expand Down
27 changes: 16 additions & 11 deletions database/seeders/AdminUser.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
import User from "App/Models/User";
import Env from "@ioc:Adonis/Core/Env";
import AuthService from "App/Services/AuthService";
import User from 'App/Models/User'
import Env from '@ioc:Adonis/Core/Env'
import AuthService from 'App/Services/AuthService'

export default class extends BaseSeeder {
public async run () {
public async run() {
const username = Env.get('SITE_NAME')
const token = await AuthService.computedToken(username)
await User.createMany([
{
username,
token
}
])
const token: string = await AuthService.computedToken(username)

const userAlreadyExists: User | null = await User.findBy('username', username)

if (!userAlreadyExists) {
await User.createMany([
{
username,
token,
},
])
}
}
}
18 changes: 11 additions & 7 deletions database/seeders/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
import Channel from "App/Models/Channel";
import Channel from 'App/Models/Channel'

export default class extends BaseSeeder {
public async run () {
await Channel.createMany([
{
name: 'general'
}
])
public async run() {
const channelAlreadyExists: Channel | null = await Channel.findBy('name', 'general')

if (!channelAlreadyExists) {
await Channel.createMany([
{
name: 'general',
},
])
}
}
}

0 comments on commit 27588e3

Please sign in to comment.