feat: add Container and Page Module
This commit is contained in:
		
							
								
								
									
										1
									
								
								src/routes/container/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/routes/container/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| import './list.ts'; | ||||
							
								
								
									
										56
									
								
								src/routes/container/list.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/routes/container/list.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,56 @@ | ||||
| import { app } from '../../app.ts'; | ||||
| import { ContainerModel, ContainerData } from './models/index.ts'; | ||||
|  | ||||
| const list = app.route({ | ||||
|   path: 'container', | ||||
|   key: 'list', | ||||
| }); | ||||
| list.run = async (ctx) => { | ||||
|   const list = await ContainerModel.findAll(); | ||||
|   ctx.body = list; | ||||
|   return ctx; | ||||
| }; | ||||
|  | ||||
| list.addTo(app); | ||||
|  | ||||
| const add = app.route({ | ||||
|   path: 'container', | ||||
|   key: 'add', | ||||
| }); | ||||
| add.run = async (ctx) => { | ||||
|   // const data = ctx.query; | ||||
|   const data: ContainerData = { | ||||
|     className: 'name', | ||||
|     style: { | ||||
|       color: 'red', | ||||
|     }, | ||||
|     showChild: true, | ||||
|     shadowRoot: false, | ||||
|   }; | ||||
|   const container = await ContainerModel.create({ | ||||
|     title: 'title', | ||||
|     data: data as ContainerData, | ||||
|     description: 'description', | ||||
|     code: `console.log('hello world')`, | ||||
|     source: 'source', | ||||
|     type: 'typescript', | ||||
|   }); | ||||
|   ctx.body = container; | ||||
|   return ctx; | ||||
| }; | ||||
| add.addTo(app); | ||||
|  | ||||
| const deleteRoute = app.route({ | ||||
|   path: 'container', | ||||
|   key: 'delete', | ||||
| }); | ||||
| deleteRoute.run = async (ctx) => { | ||||
|   const id = ctx.query.id; | ||||
|   const container = await ContainerModel.findByPk(id); | ||||
|   if (container) { | ||||
|     await container.destroy(); | ||||
|   } | ||||
|   ctx.body = container; | ||||
|   return ctx; | ||||
| }; | ||||
| deleteRoute.addTo(app); | ||||
							
								
								
									
										64
									
								
								src/routes/container/models/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								src/routes/container/models/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| import { sequelize } from '../../../modules/sequelize.ts'; | ||||
| import { DataTypes, Model } from 'sequelize'; | ||||
|  | ||||
| export interface ContainerData { | ||||
|   style?: { [key: string]: string }; | ||||
|   className?: string; | ||||
|   showChild?: boolean; | ||||
|   shadowRoot?: boolean; | ||||
| } | ||||
| export class ContainerModel extends Model { | ||||
|   declare id: string; | ||||
|   declare title: string; | ||||
|   declare description: string; | ||||
|   declare type: string; | ||||
|   declare code: string; | ||||
|   declare source: string; | ||||
|   declare data: ContainerData; | ||||
| } | ||||
| ContainerModel.init( | ||||
|   { | ||||
|     id: { | ||||
|       type: DataTypes.UUID, | ||||
|       primaryKey: true, | ||||
|       defaultValue: DataTypes.UUIDV4, | ||||
|       comment: 'id', | ||||
|     }, | ||||
|     title: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     description: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     type: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     code: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     source: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     data: { | ||||
|       type: DataTypes.JSON, | ||||
|       defaultValue: {}, | ||||
|     }, | ||||
|     uid: { | ||||
|       type: DataTypes.UUID, | ||||
|       allowNull: true, | ||||
|     }, | ||||
|   }, | ||||
|   { | ||||
|     sequelize, | ||||
|     tableName: 'kv_container', | ||||
|   }, | ||||
| ); | ||||
|  | ||||
| ContainerModel.sync({ alter: true, logging: false }).catch((e) => { | ||||
|   console.error('ContainerModel sync', e); | ||||
| }); | ||||
							
								
								
									
										3
									
								
								src/routes/container/type.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/routes/container/type.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| import { ContainerData } from './models/index.ts'; | ||||
|  | ||||
| export { ContainerData }; | ||||
							
								
								
									
										1
									
								
								src/routes/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/routes/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| import './container/index.ts'; | ||||
							
								
								
									
										1
									
								
								src/routes/page/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/routes/page/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1 @@ | ||||
| import './list.ts' | ||||
							
								
								
									
										42
									
								
								src/routes/page/list.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								src/routes/page/list.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| import { app } from '../../app.ts'; | ||||
| import { PageModel } from './models/index.ts'; | ||||
|  | ||||
| app | ||||
|   .route({ | ||||
|     path: 'page', | ||||
|     key: 'list', | ||||
|   }) | ||||
|   .define(async (ctx) => { | ||||
|     ctx.body = await PageModel.findAll(); | ||||
|     return ctx; | ||||
|   }) | ||||
|   .addTo(app); | ||||
|  | ||||
| app | ||||
|   .route({ | ||||
|     path: 'page', | ||||
|     key: 'add', | ||||
|   }) | ||||
|   .define(async (ctx) => { | ||||
|     const data = ctx.query; | ||||
|     const page = await PageModel.create(data); | ||||
|     ctx.body = page; | ||||
|     return ctx; | ||||
|   }) | ||||
|   .addTo(app); | ||||
|  | ||||
| app | ||||
|   .route({ | ||||
|     path: 'page', | ||||
|     key: 'delete', | ||||
|   }) | ||||
|   .define(async (ctx) => { | ||||
|     const id = ctx.query.id; | ||||
|     const page = await PageModel.findByPk(id); | ||||
|     if (page) { | ||||
|       await page.destroy(); | ||||
|     } | ||||
|     ctx.body = page; | ||||
|     return ctx; | ||||
|   }) | ||||
|   .addTo(app); | ||||
							
								
								
									
										54
									
								
								src/routes/page/models/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/routes/page/models/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| import { sequelize } from '../../../modules/sequelize.ts'; | ||||
| import { DataTypes, Model } from 'sequelize'; | ||||
|  | ||||
| export interface PageData { | ||||
|   edges: any[]; | ||||
|   nodes: any[]; | ||||
|   viewport: any; | ||||
|   [key: string]: any; | ||||
| } | ||||
| export class PageModel extends Model { | ||||
|   declare id: string; | ||||
|   declare title: string; | ||||
|   declare description: string; | ||||
|   declare type: string; | ||||
|   declare data: PageData; | ||||
| } | ||||
| PageModel.init( | ||||
|   { | ||||
|     id: { | ||||
|       type: DataTypes.UUID, | ||||
|       primaryKey: true, | ||||
|       defaultValue: DataTypes.UUIDV4, | ||||
|       comment: 'id', | ||||
|     }, | ||||
|     title: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     description: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     type: { | ||||
|       type: DataTypes.STRING, | ||||
|       defaultValue: '', | ||||
|     }, | ||||
|     data: { | ||||
|       type: DataTypes.JSON, | ||||
|       defaultValue: {}, | ||||
|     }, | ||||
|     uid: { | ||||
|       type: DataTypes.UUID, | ||||
|       allowNull: true, | ||||
|     }, | ||||
|   }, | ||||
|   { | ||||
|     sequelize, | ||||
|     tableName: 'kv_page', | ||||
|   }, | ||||
| ); | ||||
|  | ||||
| PageModel.sync({ alter: true, logging: false }).catch((e) => { | ||||
|   console.error('PageModel sync', e); | ||||
| }); | ||||
		Reference in New Issue
	
	Block a user