2017-03-04 106 views
0

我想在node.js腳本中創建Dynamo數據庫表。有沒有類似於mongo.model()的東西;在DynamoDB中...?

總之,我想發電機-DB代碼等同於以下內容:

var mongo = require('mongoose'); 
 
var MongoDB = mongo.connect('mongodb://localhost:27017/test').connection; 
 
MongoDB.on('error', function(err) { console.log(err.message); }); 
 
MongoDB.once('open', function() { 
 
    console.log("DynamoDB connection open"); 
 
}); 
 
var userschema = mongo.Schema({ 
 
\t name: String, 
 
\t nickname: {type: String,default: ''}, 
 
\t email: String, 
 
\t phone: String, 
 
\t type: String, 
 
\t port : String, 
 
\t deviceRegId: {type: String,default: ''}, 
 
\t assignFlag: Number, 
 
\t created: {type: Date,default: Date.now} \t , 
 
\t lastmsg : {type: String,default: ''} , 
 
\t lasttime : {type: Date,default: Date.now} , 
 
\t loginStatus : {type: Boolean,default: false} , 
 
\t isOnline : {type: Boolean,default: false} , 
 
\t chats: [{ 
 
\t \t \t from: String, 
 
\t \t \t msgfrom: Number, 
 
\t \t \t name: String, 
 
\t \t \t msg: String, 
 
\t \t \t date: {type: Date, default: Date.now}, 
 
\t \t \t flag: Number 
 
\t }] 
 
}); 
 
var agent = mongo.model('naveen', userschema); 
 
exports.mongo = mongo; 
 
exports.agent = agent;

我試圖尋找類似迪納摩功能,但找不到任何。任何幫助都會很有用。

+0

如何dynamoose https://www.npmjs.com/package/dynamoose – Molda

回答

0

以下是的示例代碼,如果不存在,則創建該表並使用默認值在其上創建一個項目。

請注意,DynamoDB上的屬性不能爲空值。例如,默認情況下,暱稱不能設置爲空字符串。

對於任何屬性,如果設置了空值並嘗試插入數據,則DynamoDB將引發驗證異常。

所以,默認不能是空字符串。

代碼: - 創建

var dynamoose = require('dynamoose'); 
dynamoose.AWS.config.update({ 
    accessKeyId: 'AKID', 
    secretAccessKey: 'SECRET', 
    region: 'us-east-1' 
}); 
dynamoose.local(); 
var Schema = dynamoose.Schema; 

var userSchema = new Schema({ 
    name: { 
     type: String, 
     hashKey: true 
    }, 
    nickname: String, 
    email: String, 
    phone: String, 
    type: String, 
    port: String, 
    deviceRegId: String, 
    assignFlag: Number, 
    created: { type: Date, default: Date.now }, 
    lastmsg: { type: String }, 
    lasttime: { type: Date, default: Date.now }, 
    loginStatus: { type: Boolean, default: false }, 
    isOnline: { type: Boolean, default: false }, 
    chats: [{ 
     from: String, 
     msgfrom: Number, 
     name: String, 
     msg: String, 
     date: { type: Date, default: Date.now }, 
     flag: Number 
    }] 
}, 
    { 
     throughput: { read: 15, write: 5 } 
    }); 
var Table = dynamoose.Table; 

var UserDetails = dynamoose.model('UserDetails', userSchema); 

var user1 = new UserDetails({ name: 'John' }); 

user1.save(function (err) { 
    if (err) { return console.log(err); } 
    console.log('Added a new item'); 
}); 

例題: -

日期值存儲爲數字。

Sample item inserted

+0

感謝您的幫助......現在,我發展我的code.If我得到任何錯誤,我會讓你知道.. –

+0

上面的答案有用嗎? – notionquest

相關問題