Skip to content

Commit

Permalink
adonis create database models
Browse files Browse the repository at this point in the history
  • Loading branch information
melicheradam committed Apr 4, 2022
1 parent 1dfeac1 commit 702ae0b
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 33 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 20 additions & 0 deletions adonisApi/app/Models/Channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DateTime } from 'luxon'
import { BaseModel, column, belongsTo, BelongsTo } from '@ioc:Adonis/Lucid/Orm'
import User from './User'

export default class Channel extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public name: string

@belongsTo(() => User)
public owner: BelongsTo<typeof User>

@column.dateTime({ autoCreate: true })
public createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime
}
22 changes: 22 additions & 0 deletions adonisApi/app/Models/Message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DateTime } from 'luxon'
import { BaseModel, belongsTo, BelongsTo, column } from '@ioc:Adonis/Lucid/Orm'
import Channel from './Channel'
import User from './User'

export default class Message extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public content: string

@belongsTo(() => User)
public user: BelongsTo<typeof User>

@belongsTo(() => Channel)
public channel: BelongsTo<typeof Channel>

@column.dateTime({ autoCreate: true })
public createdAt: DateTime

}
64 changes: 64 additions & 0 deletions adonisApi/app/Models/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { DateTime } from 'luxon'
import Hash from '@ioc:Adonis/Core/Hash'
import { column, beforeSave, BaseModel, manyToMany, ManyToMany } from '@ioc:Adonis/Lucid/Orm'
import Channel from './Channel'

export default class User extends BaseModel {
@column({ isPrimary: true })
public id: number

@column()
public email: string

@column()
public firstName: string

@column()
public lastName: string

@column()
public nickName: string

@column({ serializeAs: null })
public password: string

@column()
public rememberMeToken?: string

@column.dateTime({ autoCreate: true })
public createdAt: DateTime

@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime

@manyToMany(() => Channel, {
pivotTable: 'channel_users',
pivotForeignKey: 'user_id',
pivotRelatedForeignKey: 'channel_id',
pivotTimestamps: true,
})
public channels: ManyToMany<typeof Channel>

@manyToMany(() => User, {
pivotTable: 'invites',
pivotForeignKey: 'to_user_id',
pivotRelatedForeignKey: 'channel_id',
pivotTimestamps: true,
})
public invites: ManyToMany<typeof User>

@manyToMany(() => User, {
pivotTable: 'kicks',
pivotForeignKey: 'to_user_id',
pivotRelatedForeignKey: 'channel_id',
pivotTimestamps: true,
})
public kicks: ManyToMany<typeof User>

@beforeSave()
public static async hashPassword (user: User) {
if (user.$dirty.password) {
user.password = await Hash.make(user.password)
}
}
}
30 changes: 0 additions & 30 deletions adonisApi/app/Models/auth.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class AuthsSchema extends BaseSchema {
protected tableName = 'auths'
export default class UsersSchema extends BaseSchema {
protected tableName = 'users'

public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').primary()
table.string('email', 255).notNullable()
table.string('password', 180).notNullable()
table.string('remember_me_token').nullable()
table.string('firstName', 255).notNullable()
table.string('lastName', 255).notNullable()
table.string('nickName', 255)

/**
* Uses timestampz for PostgreSQL and DATETIME2 for MSSQL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class ApiTokens extends BaseSchema {
public async up() {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').primary()
table.integer('user_id').unsigned().references('id').inTable('auths').onDelete('CASCADE')
table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE')
table.string('name').notNullable()
table.string('type').notNullable()
table.string('token', 64).notNullable().unique()
Expand Down
23 changes: 23 additions & 0 deletions adonisApi/database/migrations/1649075700397_channels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class Channels extends BaseSchema {
protected tableName = 'channels'

public async up () {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').primary()
table.string('name', 255).notNullable()

table.integer("owner").notNullable().references("id").inTable("users").onDelete("CASCADE")
/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}

public async down () {
this.schema.dropTable(this.tableName)
}
}
23 changes: 23 additions & 0 deletions adonisApi/database/migrations/1649075701896_messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class Messages extends BaseSchema {
protected tableName = 'messages'

public async up () {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').primary()
table.string("content", 8095).notNullable()
table.integer("user").notNullable().references("id").inTable("users")
table.integer("channel").notNullable().references("id").inTable("channels").onDelete("CASCADE")
/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}

public async down () {
this.schema.dropTable(this.tableName)
}
}
29 changes: 29 additions & 0 deletions adonisApi/database/migrations/1649083609099_channel_users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class ChannelUsers extends BaseSchema {
protected tableName = 'channel_users'

public async up () {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').primary()

table.integer('user_id').unsigned().notNullable()
.references('id').inTable('users').onDelete('CASCADE')
table.integer('channel_id').unsigned().notNullable()
.references('id').inTable('channels').onDelete('CASCADE')
table.string("status", 255).notNullable()

/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })

table.unique(['user_id', 'channel_id'])
})
}

public async down () {
this.schema.dropTable(this.tableName)
}
}
28 changes: 28 additions & 0 deletions adonisApi/database/migrations/1649084818248_invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class Invites extends BaseSchema {
protected tableName = 'invites'

public async up () {
this.schema.createTable(this.tableName, (table) => {
table.increments('id').primary()

table.integer('from_user_id').unsigned().notNullable()
.references('id').inTable('users').onDelete('CASCADE')
table.integer('to_user_id').unsigned().notNullable()
.references('id').inTable('users').onDelete('CASCADE')
table.integer('channel_id').unsigned().notNullable()
.references('id').inTable('channels').onDelete('CASCADE')

/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}

public async down () {
this.schema.dropTable(this.tableName)
}
}
28 changes: 28 additions & 0 deletions adonisApi/database/migrations/1649084822818_kicks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BaseSchema from '@ioc:Adonis/Lucid/Schema'

export default class Kicks extends BaseSchema {
protected tableName = 'kicks'

public async up () {
this.schema.createTable(this.tableName, (table) => {
table.increments('id')

table.integer('from_user_id').unsigned().notNullable()
.references('id').inTable('users').onDelete('CASCADE')
table.integer('to_user_id').unsigned().notNullable()
.references('id').inTable('users').onDelete('CASCADE')
table.integer('channel_id').unsigned().notNullable()
.references('id').inTable('channels').onDelete('CASCADE')

/**
* Uses timestamptz for PostgreSQL and DATETIME2 for MSSQL
*/
table.timestamp('created_at', { useTz: true })
table.timestamp('updated_at', { useTz: true })
})
}

public async down () {
this.schema.dropTable(this.tableName)
}
}

0 comments on commit 702ae0b

Please sign in to comment.