import { Sequelize } from "sequelize" ; import config from "./config" ; import { initModels , Order , OrderAttributes , OrderCreationAttributes } from "./models/init-models" ; class SampleApp { sequelize : Sequelize ; constructor ( ) { this . sequelize = new Sequelize ( config . dbname , config . user , config . pass , config . options ) ; } async run ( ) { try { const { Customer , Order } = initModels ( this . sequelize ) ; const cust = await Customer . findOne ( { where : { "firstName" : "Hanna" , "lastName" : "Moos" } , include : [ 'orders' ] } ) ; if ( cust == null ) { return ; } const ord = await Order . findOne ( { where : { orderNumber : "542639" } } ) ; const millis = new Date ( ) . getTime ( ) . toString ( ) ; const orderNumber = "55" + millis . substring ( 6 , 10 ) ; const attr = { customerId : cust . id , orderNumber : orderNumber , totalAmount : 223.45 , status : 'PROCESSING' } ; await Order . create ( attr ) . then ( ( order2 ) => { console . log ( order2 ) ; return Order . findAll ( { where : { "customerId" : cust . id } } ) . then ( ( rows ) => { rows . forEach ( r => console . log ( r . orderNumber + " " + r . totalAmount ) ) ; } ) ; } ) ; } finally { this . sequelize . close ( ) ; } } } const app = new SampleApp ( ) ; app . run ( ) . catch ( err => console . error ( err ) ) ;