정식으로 저장소 엔진에 작성하기 #

다음 예시에서는 저장소 엔진에 권한을 부여하는 방법을 보여줍니다.

StorageWrite 함수를 사용하여 한 번에 여러 저장소 객체를 작성할 때 객체는 단일 트랜잭션으로 작성되어 모든 객체가 성공적으로 작성되거나 모두 실패하도록 만듭니다.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
userID, _ := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)

unlockedHats := map[string]interface{}{
    "hats": []string{"alien", "cowboy", "space-soldier"},
}

hatBytes, err := json.Marshal(unlockedHats)
if err != nil {
    return err
}

unlockedSkins := map[string]interface{}{
    "skins": []string{"ninja", "robot", "bear"},
}

skinBytes, err := json.Marshal(unlockedSkins)
if err != nil {
    return err
}

hatWrite := &runtime.StorageWrite{
    Collection:      "Unlocks",
    Key:             "Hats",
    UserID:          userID,
    Value:           string(hatBytes),
    PermissionRead:  1, // Only the server and owner can read
    PermissionWrite: 0, // Only the server can write
}

skinWrite := &runtime.StorageWrite{
    Collection:      "Unlocks",
    Key:             "Skins",
    UserID:          userID,
    Value:           string(skinBytes),
    PermissionRead:  1, // Only the server and owner can read
    PermissionWrite: 0, // Only the server can write
}

_, err = nk.StorageWrite(ctx, []*runtime.StorageWrite{hatWrite, skinWrite})
if err != nil {
    return err
}