2017-01-11 3 views
1

shopify 명령의 데이터베이스를 만들어 shopify 관리 영역에서 수행 할 수없는 고급 쿼리 및 판매 보고서를 실행할 수 있도록해야합니다. 나는 Sails .12와 mysql에 빌드하고있다. Shopify를 사용하면 webhook을 등록 할 수 있으므로 주문이있을 때마다 JSON으로 주문 데이터가있는 지정된 URL에 대한 POST가 생성됩니다. 주문한 제품은 JSON의 배열이 POST의 값 중 하나로 객체입니다 : 많은에 하나 인 line_items 테이블에 주문외부 API에서 여러 모델로 Sailsjs MVC 맵 매개 변수

{ 
    "id": 123456, 
    "email": "[email protected]", 
    "created_at": "2017-01-10T14:26:25-05:00", 
...//many more entires 
    "line_items": [ 
    { 
     "id": 24361829895, 
     "variant_id": 12345, 
     "title": "T-Shirt", 
     "quantity": 1, 
     "price": "140.00", 
    }, 
    { 
     "id": 44361829895, 
     "variant_id": 42345, 
     "title": "Hat", 
     "quantity": 1, 
     "price": "40.00", 
    }, 
    ] 
} 
내가 주문 테이블에 순서를 저장해야

및 제품 관계; 하나의 주문에는 많은 line_item (주문한 제품)이있을 수 있습니다. Webhook에서 보낸 100 개가 넘는 키 - 값 쌍이 있으며 모든 것을 저장합니다. 나는 내가 매우 긴 Order.js 및 Line_item.js 파일이 이제 데이터 유형을 정의 내 두 모델을 만들었습니다, 그리고 나는

line_items: { 
    collection: 'line_item', 
    via: 'order_id' 
}, 
내 Order.js에서

을 사용하고 있습니다
order_id: { 
    model: 'order' 
}, 

내 Line_item.js 모델에서 관련이 있습니다. 이것이 내 두 테이블을 부정하는 올바른 방법입니까? 또한 JSON을 모델 매개 변수에 매핑하는 코드는 어디에 두어야합니까? 이 코드를 컨트롤러에 넣으면 각 json 값을 올바른 매개 변수에 매핑하는 데 100 줄 이상의 코드를 입력해야합니다. 두 모델/테이블을 어떻게 저장할 수 있습니까? 예를 들면 :

var newOrder = {}; 
    newOrder.id =req.param('id'); 
    newOrder.email = req.param('email'); 
    newOrder.name = req.param('name'); 
    ...//over 100 lines more, then Order.create(newOrder, ...) 

    var newLine_items = req.params('line_items'); //an array 
    _.forEach(newLine_items, function(line_item){ 
     var newLine_item = {}; 
     newLine_item.id = line_item.id; 
     newLine_item.order_id = newOrder.id; 
     newLine_item.title = line_item.title; 
     //etc for over 20 more lines, then Line_item.create(newLine_item, ...) 
    }); 
옥스포드 쉼표 :)의 사용 외에, 음, 완전히 합리적인 소리

답변

1

I need to save the order into an Orders table, and the products ordered into a line_items table that is a one to many relation; one order can have many line_items (products ordered).

There are over 100 key-value pairs sent by the webhook

나는 정확히 이것을 이해 모르겠어요 또는이 프로세스 내에서 사용되는 것입니다.

그렇다고해서 모델에 JSON 값을 가진 단일 속성을 가진 다음 각 속성을 수동으로 계산하는 대신 JSON으로 검색하고 작업하는 것이 도움이 될 수 있습니다. 저기서하고 있니?

실제로 사용 사례와 데이터 사용 방법에 따라 다르지만 포맷이 변경되면 문제가 발생할 수 있습니다. JSON 객체로 저장되고 구문 분석되는 경우 문제가 될 수 있습니다.

Also, where would I put the code that maps the JSON to the model parameters

는 v0.12.x에서 Services를보십시오.

v1에서 서비스는 계속 작동하지만이 논리를 Helpers으로 옮기는 것이 좋은 옵션 일 수는 있지만 custom model method이 더 나은 것으로 보입니다.

var newOrder = req.allParams(); 

// Store the order 
Order 
.create(newOrders) 
.exec(function (err, result) { 
    if (err) // handle the error 

    var newLine_items = {}; 

    _.forEach(newOrder.line_items, function(line_item) { 
     // Add the order id for association 
     line_item.order_id = result.id; 
     // Add the new line item with the orders id 
     newLine_items.push(line_item); 
    }); 

    // Store the orders line items 
    LineItems 
    .create(newLine_items) 
    .exec(function (err, result) { 
     if (err) // handle the error 

     // Handle success 
    }); 
}); 

그리고 주문 모델의 라이프 사이클 콜백 :

beforeCreate: function (values, cb) { 
    delete(values.line_items); 
    cb(); 
} 

다음
var newOrder = req.allParams(); 
newLine_items = {}; 
_.forEach(newOrder.line_items, function(line_item) { 
    newLine_items.push(line_item); 
}); 

당신의 논리가 어떻게 보이는지입니다 : 여기

는 코드의 짧은 버전입니다

하지만 돛의 버전 1에서 모델 메서드가 실제로 bluebird 약속을 조사해야합니다. pt를 지원하고 내 예제에서 시작하는 피라미드의 피라미드를 무효화하는 데 도움이되며 피해야 할 대상이기도합니다. P

+0

"webhook에서 전송 된 키 - 값 쌍"은 shopify가 100 개가 넘는 json 객체입니다. 모든 것을 json 객체로 저장하고 싶다면 mongodb 만 사용할 수 있지만 주어진 기간 동안 각 제품이 몇 개나 팔렸는 지와 같은 관계형 쿼리를 수행해야합니다. – hamncheez

+0

@hamncheez 그러면 데이터 처리를위한 코드가 너무 많아서 서비스를 살펴본 다음 그 부분에 필요한 모든 로직에 대한 메소드가있는 서비스를 빌드하면 문제가 해결되지 않은 것입니다. 이 작업을 한 번만 수행하면됩니다. 그러면 작업이 끝났고이 기능을 사용할 수 있습니까? –

+0

@hamncheez 답안이 끝날 때 편집을 확인하십시오. –