-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add permit notes to db; migration, models, and types
- Loading branch information
Showing
6 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
app/src/db/migrations/20241115000000_012-add-permit-notes.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable max-len */ | ||
import stamps from '../stamps'; | ||
|
||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Create public schema tables | ||
.then(() => | ||
knex.schema.createTable('permit_note', (table) => { | ||
table.uuid('permit_note_id').primary(); // Primary key | ||
table | ||
.uuid('permit_id') | ||
.notNullable() | ||
.references('permit_id') | ||
.inTable('public.permit') | ||
.onUpdate('CASCADE') | ||
.onDelete('CASCADE'); // Foreign key to 'permit' | ||
table.text('note').notNullable().defaultTo(''); | ||
table.boolean('is_deleted').notNullable().defaultTo(false); | ||
stamps(knex, table); | ||
}) | ||
) | ||
|
||
// Create public schema table triggers | ||
.then(() => | ||
knex.schema.raw(`CREATE TRIGGER before_update_permit_note_trigger | ||
BEFORE UPDATE ON public.permit_note | ||
FOR EACH ROW | ||
EXECUTE FUNCTION public.set_updated_at();`) | ||
) | ||
|
||
// Create audit triggers | ||
.then(() => | ||
knex.schema.raw(`CREATE TRIGGER audit_permit_note_trigger | ||
AFTER UPDATE OR DELETE ON public.permit_note | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE audit.if_modified_func();`) | ||
) | ||
); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return ( | ||
Promise.resolve() | ||
// Drop the audit triggers | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS audit_permit_note_trigger ON public.permit_note')) | ||
// Drop public schema table triggers | ||
.then(() => knex.schema.raw('DROP TRIGGER IF EXISTS before_update_permit_note_trigger ON public.permit_note')) | ||
// Drop public schema tables | ||
.then(() => knex.schema.dropTableIfExists('permit_note')) | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Prisma } from '@prisma/client'; | ||
|
||
import type { Stamps } from '../stamps'; | ||
import type { PermitNote } from '../../types'; | ||
|
||
// Define types | ||
const _permitNote = Prisma.validator<Prisma.permit_noteDefaultArgs>()({}); | ||
|
||
type PrismaRelationPermitNote = Omit<Prisma.permit_noteGetPayload<typeof _permitNote>, keyof Stamps>; | ||
|
||
export default { | ||
toPrismaModel(input: PermitNote): PrismaRelationPermitNote { | ||
return { | ||
permit_note_id: input.permitNoteId, | ||
permit_id: input.permitId, | ||
note: input.note, | ||
is_deleted: input.isDeleted | ||
}; | ||
}, | ||
|
||
fromPrismaModel(input: PrismaRelationPermitNote): PermitNote { | ||
return { | ||
permitNoteId: input.permit_note_id, | ||
permitId: input.permit_id, | ||
note: input.note, | ||
isDeleted: input.is_deleted | ||
}; | ||
} | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { IStamps } from '../interfaces/IStamps'; | ||
|
||
export type PermitNote = { | ||
permitNoteId: string; // Primary Key | ||
permitId: string; | ||
note: string; | ||
isDeleted: boolean; | ||
} & Partial<IStamps>; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Joi from 'joi'; | ||
|
||
export const permitNoteSchema = Joi.object({ | ||
permitNoteId: Joi.string().max(255).required(), | ||
permitId: Joi.string().max(255).required(), | ||
note: Joi.string().max(255).required(), | ||
isDeleted: Joi.boolean() | ||
}); |