READ
server/model/register.ts
// ONE TIME ONLY
import { sql } from '~~/server/db'
export type registrationModel = {
id: string;
email: string;
name: string;
password: string;
age: string;
}
// READ
export const read = async() => {
const result = await sql({
query: 'SELECT * FROM registration'
}) as any
return result as registrationModel[]
}
CREATE
server/model/register.ts
// ONE TIME ONLY
import { sql } from '~~/server/db'
export type registrationModel = {
id: string;
email: string;
name: string;
password: string;
age: string;
}
// CREATE
export const create = async (body:any) => {
const result = await sql({
query: 'INSERT INTO registration (email, name, password, age) VALUES (?, ?, ?, ?)',
values: [ body.email, body.name, body.password, body.age ],
}) as any
return result.length ===1 ? (result[0] as registrationModel) : null
}
DELETE
server/controller/register.ts
// ONE TIME ONLY
import { H3Event } from 'h3';
import * as registerModel from '~~/server/model/register';
// DELETE
export const remove =async(evt: H3Event) => {
try {
const result = await registerModel.remove(evt.context.params?.id as string)
return {data: result}
} catch {
throw createError({
statusCode: 500,
statusMessage: 'Something went wrong'
})
}
}
server/model/register.ts
// ONE TIME ONLY
import { sql } from '~~/server/db'
export type registrationModel = {
id: string;
email: string;
name: string;
password: string;
age: string;
}
// DELETE
export const remove = async(id:any) => {
const result = await sql({
query: 'DELETE FROM registration WHERE id=?',
values: [id],
}) as any
return true
}
UPDATE
server/model/register.ts
// ONE TIME ONLY
import { sql } from '~~/server/db'
export type registrationModel = {
id: string;
email: string;
name: string;
password: string;
age: string;
}
// UPDATE
export const update = async (id:string, body:any) => {
const result = await sql({
query: 'UPDATE registration SET email=?, name=?, password=?, age=? WHERE id =?',
values: [ body.email, body.name, body.password, body.age, id ]
}) as any
return true
}