/
README
140 lines (112 loc) · 3.73 KB
/
README
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
This is a _in progress_ version of a haxe wrapper for ChrisKv's javascript impl at
http://github.com/christkv/node-mongodb-native
To use, place ChrisKv's work in, e.g. .node_libraries, .node_libraries/mongodb
I've only implemented what I need, and I haven't been very faithful to the underlying driver as I wanted to implement some simple serialization for haxe objects and also have static typing on objects. I'm not happy with the internals but the API as shown below is ok.
My serialization stuff is meant to provide a means to get a simple typed object into the db or over the wire as json in a human recognisable and queryable form, and reconstruct it from that simple form. So haxe serialization is not used as it can not be queried. haxe.rtti is used to reconstruct the object to it's original class, thereby not requiring to insert meta data into mongo.
Here's an example of usage so far, you'll also need my bdog,http://github.com/blackdog66/bdog, library for Serialize.hx.
Given ...
class Persist implements haxe.rtti.Infos {
public function new() {}
}
enum AccountType {
REAL;
FREE;
}
enum TxnClass {
DB;
CR;
}
enum Txn {
CARD(v:Float);
WIN(v:Float);
JACKPOT(v:Float);
INITIAL(v:Float);
}
enum AccStatus {
OK;
BAD;
}
class Session extends Persist {
public var _id:String;
public var sessionID:String;
public var userID:String;
public var loggedOn:String;
public var loggedOff:String;
public var ipAddress:String;
public var role:Role;
}
class Account extends Persist {
public var _id:String;
public var type:AccountType;
public var site:String;
public var balance:Float;
public var currency:String;
public var status:AccStatus;
public var lastTransOn:String;
}
class User extends Persist {
public var _id:String;
public var firstname:String;
public var lastname:String;
public var email:String;
public var password:String;
public var signupOn:String;
public var site:String;
public var role:Role;
public var handle:String;
public var accounts:Array<Account>;
public var updatedBy:String;
}
class Ledger extends Persist {
public var _id:String;
public var userID:String;
public var accountType:AccountType;
public var tvalue:Float;
public var newBalance:Float;
public var summary:String;
public var addedOn:String;
public var addedBy:String;
public var transType:String;
}
then this works ...
public static function
signUp(email:String,password:String,
first:String,last:String,handle:String,
role:Role,site:String,
ip:String,fn:{uuid:String,user:User}->Void) {
var
db = new MongoPool('localhost',27017,'accounts',10),
users = db.col(User);
users.findOne({email:email,password:password},function(user) {
if (user == null) {
var
entryDate = Date.now().toString(),
u = new User();
u.email=email;
u.password=password;
u.firstname=first;
u.lastname=last;
u.handle="temp";
u.role=role;
u.site=site;
u.signupOn = entryDate;
u.accounts = accTemplates; // should be a copy?
u.updatedBy = null;
users.insert(u,function(newUser:User) {
newSession(db,newUser,ip,function(uuid) {
var trns = initialTrans(u,entryDate,MongoDB.ID(newUser));
var ledgers = db.col(Ledger);
ledgers.insertMany(trns,function(d) {
trace("closing for "+email);
db.close();
fn({uuid:uuid,user:newUser});
});
});
});
} else {
db.close();
fn({uuid:null,user:user});
}
});
}
3