This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Feature/school related models #29
Open
florian-lefebvre
wants to merge
2
commits into
op-ent:main
Choose a base branch
from
florian-lefebvre:feature/school-related-models
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { DecoratorFn, LucidModel } from '@ioc:Adonis/Lucid/Orm' | ||
| // import { column as originalColumn } from '@ioc:Adonis/Lucid/Orm' | ||
|
|
||
| export type EnumDecorator = () => DecoratorFn | ||
|
|
||
| export const enumColumn: EnumDecorator = () => { | ||
| return function decorateAsColumn(target, property) { | ||
| const Model = target.constuctor as LucidModel | ||
| Model.boot() | ||
| Model.$addColumn(property, { | ||
| prepare: (value: string[]): string => `{${value.join(',')}}`, | ||
| consume: (value: string): string[] => value.slice(1, -1).split(','), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // declare module '@ioc:Adonis/Lucid/Orm' { | ||
| // export const column: typeof originalColumn & { enum: EnumDecorator } | ||
| // } | ||
|
|
||
| // column.enum = enumColumn |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { DecoratorFn, LucidModel } from '@ioc:Adonis/Lucid/Orm' | ||
| // import { column as originalColumn } from '@ioc:Adonis/Lucid/Orm' | ||
|
|
||
| export type JsonDecorator = () => DecoratorFn | ||
|
|
||
| export const jsonColumn: JsonDecorator = () => { | ||
| return function decorateAsColumn(target, property) { | ||
| const Model = target.constuctor as LucidModel | ||
| Model.boot() | ||
| Model.$addColumn(property, { | ||
| prepare: (value: {}): string => JSON.stringify(value), | ||
| consume: (value: string): {} => JSON.parse(value), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // declare module '@ioc:Adonis/Lucid/Orm' { | ||
| // export const column: typeof originalColumn & { json: JsonDecorator } | ||
| // } | ||
|
|
||
| // column.json = jsonColumn |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { DateTime } from 'luxon' | ||
| import { BaseModel, BelongsTo, belongsTo, column } from '@ioc:Adonis/Lucid/Orm' | ||
| import User from './User' | ||
| import { enumColumn } from 'App/Helpers/ModelEnum' | ||
| import School from './School' | ||
|
|
||
| export type MemberRole = 'student' | 'teacher' | 'parent' | 'staff' | ||
|
|
||
| export default class Member extends BaseModel { | ||
| @column({ isPrimary: true }) | ||
| public id: number | ||
|
|
||
| @belongsTo(() => User) | ||
| public user: BelongsTo<typeof User> | ||
|
|
||
| @belongsTo(() => School) | ||
| public school: BelongsTo<typeof School> | ||
|
|
||
| @enumColumn() | ||
| public roles: MemberRole[] | ||
|
|
||
| @column.dateTime({ autoCreate: true }) | ||
| public createdAt: DateTime | ||
|
|
||
| @column.dateTime({ autoCreate: true, autoUpdate: true }) | ||
| public updatedAt: DateTime | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { DateTime } from 'luxon' | ||
| import { BaseModel, column, HasMany, hasMany } from '@ioc:Adonis/Lucid/Orm' | ||
| import { jsonColumn } from 'App/Helpers/ModelJson' | ||
| import Member from './Member' | ||
|
|
||
| export type SchoolContact = { | ||
| email: string | ||
| phone: string | ||
| } | ||
|
|
||
| export type SchoolAddress = { | ||
| street: string | ||
| city: string | ||
| state: string | ||
| zip: string | ||
| } | ||
|
|
||
| export default class School extends BaseModel { | ||
| @column({ isPrimary: true }) | ||
| public id: number | ||
|
|
||
| @column() | ||
| public name: string | ||
|
|
||
| @jsonColumn() | ||
| public contact: SchoolContact | ||
|
|
||
| @jsonColumn() | ||
| public address: SchoolAddress | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same - it would be better to have another model for the adress with a relation to the school ( |
||
|
|
||
| @column() | ||
| public identifier: string | ||
|
|
||
| @column.dateTime({ autoCreate: true }) | ||
| public createdAt: DateTime | ||
|
|
||
| @hasMany(() => Member, { foreignKey: 'school_id' }) | ||
| public members: HasMany<typeof Member> | ||
|
|
||
| @column.dateTime({ autoCreate: true, autoUpdate: true }) | ||
| public updatedAt: DateTime | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { DateTime } from 'luxon' | ||
| import { BaseModel, belongsTo, BelongsTo, column, computed } from '@ioc:Adonis/Lucid/Orm' | ||
| import User from './User' | ||
|
|
||
| export default class UserDetail extends BaseModel { | ||
| @column({ isPrimary: true }) | ||
| public id: number | ||
|
|
||
| @column() | ||
| public firstName: string | ||
|
|
||
| @column() | ||
| public lastName: string | ||
|
|
||
| @computed() | ||
| public get fullName() { | ||
| return `${this.firstName} ${this.lastName}` | ||
| } | ||
|
|
||
| @belongsTo(() => User) | ||
| public member: BelongsTo<typeof User> | ||
|
|
||
| @column.dateTime({ autoCreate: true }) | ||
| public createdAt: DateTime | ||
|
|
||
| @column.dateTime({ autoCreate: true, autoUpdate: true }) | ||
| public updatedAt: DateTime | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import BaseSchema from '@ioc:Adonis/Lucid/Schema' | ||
|
|
||
| export default class extends BaseSchema { | ||
| protected tableName = 'members' | ||
|
|
||
| public async up() { | ||
| this.schema.raw("CREATE TYPE member_role AS ENUM('student', 'teacher', 'parent', 'staff')") | ||
|
|
||
| this.schema.createTable(this.tableName, (table) => { | ||
| table.increments('id') | ||
| table.integer('user_id').unsigned().references('users.id').onDelete('CASCADE') | ||
| table.integer('school_id').unsigned().references('schools.id').onDelete('CASCADE') | ||
| table.specificType('roles', 'member_role[]').notNullable() | ||
|
|
||
| /** | ||
| * 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) | ||
| this.schema.raw('DROP TYPE member_role') | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import BaseSchema from '@ioc:Adonis/Lucid/Schema' | ||
|
|
||
| export default class extends BaseSchema { | ||
| protected tableName = 'user_details' | ||
|
|
||
| public async up() { | ||
| this.schema.createTable(this.tableName, (table) => { | ||
| table.increments('id') | ||
| table.integer('user_id').unsigned().references('users.id').onDelete('CASCADE') | ||
| table.string('first_name', 32).notNullable() | ||
| table.string('last_name', 64).notNullable() | ||
|
|
||
| /** | ||
| * 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) | ||
| } | ||
| } |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import BaseSchema from '@ioc:Adonis/Lucid/Schema' | ||
|
|
||
| export default class extends BaseSchema { | ||
| protected tableName = 'schools' | ||
|
|
||
| public async up() { | ||
| this.schema.createTable(this.tableName, (table) => { | ||
| table.increments('id') | ||
| table.string('name', 255).notNullable() | ||
| table.json('contact').notNullable() | ||
| table.json('address').notNullable() | ||
| table.string('identifier').notNullable() | ||
|
|
||
| /** | ||
| * 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) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be better to have another model for the contact information with a relation to the school (
hasOne+belongsTo)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was too lazy I have to admit 😅 I will do this thx