iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 6
0
Software Development

線上娃娃機-js開發篇系列 第 9

typeorm 與 娃娃機資料庫規劃

  • 分享至 

  • xImage
  •  

使用 typeorm 要先定義entry 以下為主要的欄位規劃
有分一對一 一對多 多對一 三種常用的

會員資料表 這邊有一對一的像 fb goolge 另外就寫在另外的 table 要用到的時候再去join


import { BaseEntity, Column, Entity, Index, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryColumn, PrimaryGeneratedColumn, RelationId } from "typeorm";
import { MemberShipDb } from "./MemberShipDb";
import { MemberKind } from "./MemberKind";
import { OrderRecord } from "./OrderRecord";
import { MemberPlayRecord } from "./MemberPlayRecord";
import { MemberWallet } from "./MemberWallet";
import { MemberFacebook } from "./MemberFacebook";
import { MemberGoogle } from "./MemberGoogle";
import { MachineViewer } from "./MachineViewer"
import { MachinePlayWork } from "./MachinePlayWork";
import { MachineMemberQueue } from './MachineMemberQueue'
import { Machine } from "./Machine";
import { MemberData } from "./MemberData";
import { ContactUs } from "./ContactUs";
import { MemberTools } from './MemberTools'

@Entity()
export class Member {

    @PrimaryGeneratedColumn()
    id!: number;

    @Column({ default: '' })
    account!: string;

    @Column({ default: '' })
    orgName!: string

    @Column({ default: '' })
    name!: string;

    @Column({ default: '' })
    email!: string;


    @Column({ default: '' })
    uuid!: string;

    @Column({ default: '1111' })
    password!: string;

    @Column({ default: '' })
    shipAddress!: string;

    @Column({ default: false })
    isOrderEpager!: boolean;

    @Column({ default: 0 })
    nowPoint!: number;

    @Column({ default: 0 })
    nowTicket!: number;

    @OneToOne(type => MemberFacebook, memberFacebook => memberFacebook.member)
    @JoinColumn()
    memberFacebook!: MemberFacebook


    @OneToOne(type => MemberGoogle, memberGoogle => memberGoogle.member)
    @JoinColumn()
    memberGoogle!: MemberGoogle;


    @Column({ default: false })
    isPlaying!: boolean;


    @ManyToOne(type => Machine, machine => machine.playMember)
    playMachine!: Machine;

  

    @OneToMany(type => MemberShipDb, (memberShipDb: any) => memberShipDb.member)
    memberShipDb!: MemberShipDb[]
 

    @OneToMany(type => OrderRecord, orderRecord => orderRecord.member)
    orderRecord!: OrderRecord[]

    @OneToMany(type => MemberPlayRecord, memberPlayRecord => memberPlayRecord.member)
    memberPlayRecord!: MemberPlayRecord[]

    @OneToMany(type => MemberWallet, memberWallet => memberWallet.member)
    memberWallet!: MemberWallet[]

    @ManyToOne(type => MemberKind, memberKind => memberKind.member)
    memberKind!: MemberKind

    @OneToMany(type => ContactUs, contactUs => contactUs.member)
    contactUs!: ContactUs[]

    @OneToMany(type => MachineViewer, machineViewer => machineViewer.member)
    machineViewer!: MachineViewer[]


    @OneToOne(type => MachinePlayWork, machinePlayWork => machinePlayWork.nowPlayMember)
    @JoinColumn()
    machinePlayWork!: MachinePlayWork


    @OneToMany(type => MachineMemberQueue, machineMemberQueue => machineMemberQueue.member)
    machineMemberQueue!: MachineMemberQueue[]

    @OneToMany(type => MemberTools, memberTools => memberTools.member)
    memberTools!: MemberTools[]

    @OneToOne(type => MemberData, memberData => memberData.member)
    @JoinColumn()
    memberData!: MemberData

    @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
    createDateTime!: Date;


   
}

第二個介紹的是機器欄位 機器常用的有 影像wss來源 還有ip 跟目前遊玩的資訊

import { BaseEntity, Column, Entity, Index, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryColumn, PrimaryGeneratedColumn, RelationId } from "typeorm";
import { MachineStatus } from "./MachineStatus";
import { Item } from "./Item";
import { MemberPlayRecord } from "./MemberPlayRecord";
import { MachineViewer } from "./MachineViewer";
import { MachinePlayWork } from "./MachinePlayWork";
 import { MachineMemberQueue } from "./MachineMemberQueue";
import { Member } from "./Member";

@Entity()
export class Machine {

    @PrimaryGeneratedColumn()
    id!: number;

    @Column()
    machineName!: string

    @Column()
    macAddress!: string

    @Column({ default: '' })
    memo!: string

    @Column({ default: '' })
    recordSocketIp!: string

    @Column({ default: '' })
    frontPort!: string

    @Column({ default: '' })
    backPort!: string

    @Column({ default: 0 })
    cameraMode!: number;

    @Column({ default: 0 })
    freeMaxCombo!: number;

    @Column({ default: '' })
    itemDescription!: string;


    @Column({ default: '' })
    itemName!: string

    @Column({ default: '' })
    itemPhoto!: string

    @Column({ default: '' })
    itemPhoto1!: string

    @Column({ default: '' })
    itemPhoto2!: string

    @Column({ default: 0 })
    node!: number

    @Column({ default: 0 })
    order!: number

    @Column({ default: 0 })
    playPoint!: number

    @Column({ default: 0 })
    playTicket!: number

    @Column({ default: 0 })
    promisePoint!: number

    @Column({ default: '' })
    rtmpUrl1!: string

    @Column({ default: '' })
    rtmpUrl2!: string
 
    @Column({ default: 0 })
    tag!: number

    @ManyToOne(type => MachineStatus, machineStatus => machineStatus.machine)
    machineStatus!: MachineStatus

    @OneToMany(type => MemberPlayRecord, memberPlayRecord => memberPlayRecord.machine)
    memberPlayRecord!: MemberPlayRecord

    @ManyToOne(type => Item, item => item.machines)
    item!: Item

    @OneToMany(type => MachineViewer, machineViewer => machineViewer.machine)
    machineViewer!: MachineViewer[]


    @OneToOne(type => MachinePlayWork, machinePlayWork => machinePlayWork.machine)
    @JoinColumn()
    machinePlayWork!: MachinePlayWork



    @OneToMany(type => MachineMemberQueue, machineMemberQueue => machineMemberQueue.machine)
    machineMemberQueue!: MachineMemberQueue


    @OneToMany(type => Member, member => member.playMachine)
    playMember!: Member[]

   


}

遊玩紀錄 紀錄當下遊玩的狀況 並且有記錄影像檔案 方便日後調閱


import { BaseEntity, Column, Entity, Index, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryColumn, PrimaryGeneratedColumn, RelationId } from "typeorm";
import { Member } from "./Member";
import { MemberWallet } from "./MemberWallet";
import { Machine } from "./Machine";
import { ShipFlow } from "./ShipFlow";
import { ShipWork } from "./ShipWork";
import { CoinWay } from './CoinWay'
import { MemberTools } from './MemberTools'
@Entity()
export class MemberPlayRecord {

    @PrimaryGeneratedColumn()
    id!: number;

    @Column()
    playTime!: Date;

    @Column({ default: '' })
    itemName!: string;

    @Column({ default: '' })
    itemPhoto!: string;

    @Column({ default: 0 })
    costPlayPoint!: number;

    @Column({ default: 0 })
    costPlayTicket!: number;

    @Column({ default: 0 })
    promisePoint!: number;

    @Column({ default: '' })
    recordVideo!: string;

    @Column({ default: false })
    isWin!: boolean;

    @ManyToOne(type => ShipFlow, shipFlow => shipFlow.memberPlayRecord)
    shipFlow!: ShipFlow;

    @ManyToOne(type => ShipWork, shipWork => shipWork.memberPlayRecord)
    shipWork!: ShipWork;



    @ManyToOne(type => MemberWallet, memberWallet => memberWallet.memberPlayRecord)
    memberWallet!: MemberWallet;

    @ManyToOne(type => Machine, machine => machine.memberPlayRecord)
    machine!: Machine;

    @ManyToOne(type => Member, member => member.memberPlayRecord)
    member!: Member;


    @ManyToOne(type => CoinWay, coinWay => coinWay.memberPlayRecord)
    coinWay!: CoinWay;

    @ManyToOne(type => MemberTools, memberTools => memberTools.memberPlayRecord)
    memberTools!: MemberTools;

    @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
    createDateTime!: Date;
}


這邊使用 dotenv 可以放一些不同的環境變數
在設定typeorm時候 去判別 process.env.NODE_ENV 要載入哪一個環境變數
https://www.npmjs.com/package/dotenv


const env = require('dotenv').config(".env").parsed
const envProduct = require('dotenv').config(".envProduct").parsed

export const ormconfig: any = {
  "type": "mysql",
  "host": "127.0.0.1",
  "port": 3306,
  "username": "root",
  "password": "test",
  "database": "clawtest",
  "synchronize": true,
  "logging": false,
  "entities": ["/src/entity/**/*.ts"],
  "migrations": ["src/migration/**/*.ts"],
  "subscribers": ["src/subscriber/**/*.ts"],
  "cli": { "entitiesDir": "src/entity", "migrationsDir": "src/migration", "subscribersDir": "src/subscriber" }
}
let ormconfigEzClawSetup: any = { ...ormconfig }
switch (process.env.NODE_ENV) {
  case 'product':
    console.log(`TypeOrm Link to  product : ${envProduct.LOCAL_HOST}`)
    ormconfigEzClawSetup.name = "clawtest";
    ormconfigEzClawSetup.host = envProduct.LOCAL_HOST;
    ormconfigEzClawSetup.username = envProduct.LOCAL_USER;
    ormconfigEzClawSetup.password = envProduct.LOCAL_PASSWORD;
    ormconfigEzClawSetup.database = envProduct.LOCAL_DB;
    ormconfigEzClawSetup.entities = ["src/entity/**/*.ts"];
    ormconfigEzClawSetup.synchronize = true;
    break
  default:
    console.log(`TypeOrm Link to  local : ${env.LOCAL_HOST}`)
    ormconfigEzClawSetup.name = "clawtest";
    ormconfigEzClawSetup.host = env.LOCAL_HOST;
    ormconfigEzClawSetup.username = env.LOCAL_USER;
    ormconfigEzClawSetup.password = env.LOCAL_PASSWORD;
    ormconfigEzClawSetup.database = env.LOCAL_DB
    ormconfigEzClawSetup.entities = ["src/entity/**/*.ts"];
    ormconfigEzClawSetup.synchronize = true;
    break
}

export default ormconfigEzClawSetup



以上大概就是主要欄位的規劃 當然還有很多相關連的小table 但主要就是這三個大table 在掌管我的線上娃娃機的程式


上一篇
線上娃娃機- apollo client init
下一篇
線上娃娃機-App 設定篇
系列文
線上娃娃機-js開發篇11
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言