This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
155 lines (133 loc) · 4.44 KB
/
index.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var verymodel = require('verymodel');
var Padlock = require('padlock').Padlock;
var underscore = require('underscore');
var indexes = require('./lib/indexes');
var importexport = require ('./lib/importexport');
var base = require('./lib/base');
var children = require('./lib/children');
var foreign = require('./lib/foreign');
var extensions = require('./lib/extensions');
var uuid = require('uuid-v4');
var LevelDulcimer = require('level-dulcimer');
var RiakDulcimer = require('riak-dulcimer');
var util = require('util');
var extra_types = require('./lib/types');
verymodel.registerTypes(extra_types);
var model_cache = {};
function getModel(name) {
if (typeof name === 'string') {
return model_cache[name];
}
return name;
}
var default_db = null;
var default_bucket = 'default';
function makeModelLevely(mf) {
mf.options.savelock = new Padlock();
mf.runWithLock = function (callback) {
mf.options.savelock.runwithlock(callback, [mf.options.savelock.release.bind(mf.options.savelock)]);
};
mf.connect = function (opts) {
if (typeof opts === 'string') {
opts = {path: opts, type: 'level'};
}
if (typeof opts.type === 'undefined' || opts.type === 'level') {
mf.options.db = LevelDulcimer(opts.path);
} else if (opts.type === 'riak') {
mf.options.db = RiakDulcimer(util.format("riak://%s:%d/%s", opts.host, opts.port, opts.bucket || default_bucket || 'default'));
} else {
throw Error("Invalid DB Specification");
}
return mf.options.db;
};
if (typeof mf.options.name !== 'string') {
throw new Error("Model factories must include a name option.");
}
model_cache[mf.options.name] = mf;
if (!mf.options.hasOwnProperty('foreignDepth')) {
mf.options.foreignDepth = 5;
}
if ((mf.options.keyType === 'uuid' || typeof mf.options.keyType === 'undefined') && typeof mf.options.keyGenerator === 'undefined') {
mf.options.keyGenerator = function (cb) {
cb(false, uuid());
};
}
mf.addDefinition({
key: {
private: true,
},
vclock: {
private: true,
save: false,
},
bucket: {
private: true,
save: false,
},
});
if (mf.options.saveKey) {
mf.definition.key.private = false;
}
mf.getModel = getModel;
mf.handleOpts = function (name, opts, callback) {
if (typeof callback === 'undefined') {
opts = {cb: opts};
} else {
opts.cb = callback;
}
if (!opts.db && !mf.options.db && default_db !== null && mf.options.useGlobalDB !== false) {
mf.options.db = default_db;
opts.db = default_db;
}
if (!opts.db) {
opts.db = mf.options.db;
}
if (!opts.bucket) {
opts.bucket = mf.options.bucket || default_bucket;
}
if (typeof opts.cb !== 'function') {
throw Error('The last argument in ' + name + ' must be a function');
}
if (!opts.db) {
throw new Error("Model factories must include a db option of a levelup instance with valueEncoding of json.");
}
if (typeof opts.depth === 'undefined') {
opts.depth = mf.options.foreignDepth;
}
opts.prefix = opts.prefix || mf.options.name;
return opts;
};
indexes(mf);
base(mf);
children(mf);
foreign(mf);
importexport(mf);
extensions(mf);
return mf;
}
function VeryLevelModel() {
verymodel.VeryModel.apply(this, arguments);
makeModelLevely(this);
}
VeryLevelModel.prototype = Object.create(verymodel.VeryModel.prototype);
module.exports = {
Model: VeryLevelModel,
connect: function (opts) {
if (typeof opts === 'string') {
opts = {path: opts, type: 'level'};
}
if (typeof opts.type === 'undefined' || opts.type === 'level') {
default_db = LevelDulcimer(opts.path);
} else if (opts.type === 'riak') {
default_db = RiakDulcimer(util.format("riak://%s:%d/%s", opts.host, opts.port, opts.bucket || 'default'));
} else {
throw Error("Invalid DB Specification");
}
if (opts.bucket) {
default_bucket = opts.bucket;
}
return default_db;
},
getModel: getModel,
registerTypes: verymodel.RegisterTypes,
};