Comparing version 0.1.0 to 0.2.0
91
index.js
const { | ||
ObjectView, StringView, TypeViewMixin, TypedArrayViewMixin, ArrayViewMixin, | ||
ObjectView, StringView, TypeViewMixin, BooleanView, | ||
} = require('structurae'); | ||
@@ -7,2 +7,6 @@ const ObjectIdView = require('./lib/objectid-view'); | ||
const RegexView = require('./lib/regex-view'); | ||
const BinaryView = require('./lib/binary-view'); | ||
const CodeView = require('./lib/code-view'); | ||
const LongView = require('./lib/long-view'); | ||
const TimestampView = require('./lib/timestamp-view'); | ||
@@ -113,3 +117,3 @@ const BSONTypes = { | ||
hasValue = fieldName && valueLength; | ||
const fieldOptions = View.schema[fieldName]; | ||
const fieldOptions = View.layout[fieldName]; | ||
start = fieldOptions.start; | ||
@@ -169,11 +173,9 @@ fieldLength = fieldOptions.length; | ||
static toBSON(view, start = 0) { | ||
const { fields, schema } = this; | ||
const { fields, layout } = this; | ||
const result = {}; | ||
for (let i = 0; i < fields.length; i++) { | ||
const name = fields[i]; | ||
const { | ||
BSON, View, start: fieldStart, length: fieldLength, | ||
} = schema[name]; | ||
const json = View.toJSON(view, start + fieldStart, fieldLength); | ||
result[name] = BSON ? new BSON(json) : json; | ||
const { View, start: fieldStart, length: fieldLength } = layout[name]; | ||
result[name] = View.toBSON ? View.toBSON(view, start + fieldStart, fieldLength) | ||
: View.toJSON(view, start + fieldStart, fieldLength); | ||
} | ||
@@ -186,61 +188,46 @@ return result; | ||
...ObjectView.types, | ||
double(field) { | ||
const { size } = field; | ||
field.View = size ? TypedArrayViewMixin('float64', true) | ||
: TypeViewMixin('float64', true); | ||
field.length = field.View.getLength(size); | ||
double() { | ||
return TypeViewMixin('float64', true); | ||
}, | ||
binData: 'binary', | ||
binData() { | ||
return BinaryView; | ||
}, | ||
binary(field) { | ||
field.View = TypedArrayViewMixin('uint8', true); | ||
field.BSON = BSONView.BSON && BSONView.BSON.Binary; | ||
binary() { | ||
return BinaryView; | ||
}, | ||
objectId(field) { | ||
const { size } = field; | ||
field.View = size ? ArrayViewMixin(ObjectIdView) : ObjectIdView; | ||
field.BSON = BSONView.BSON && BSONView.BSON.ObjectId; | ||
field.length = field.View.getLength(size); | ||
objectId() { | ||
return ObjectIdView; | ||
}, | ||
bool: 'boolean', | ||
bool() { | ||
return BooleanView; | ||
}, | ||
date(field) { | ||
const { size } = field; | ||
field.View = size ? ArrayViewMixin(DateView) : DateView; | ||
field.length = field.View.getLength(size); | ||
date() { | ||
return DateView; | ||
}, | ||
regex(field) { | ||
const { size } = field; | ||
field.View = size ? ArrayViewMixin(RegexView) : RegexView; | ||
regex() { | ||
return RegexView; | ||
}, | ||
javascript: 'string', | ||
javascript() { | ||
return CodeView; | ||
}, | ||
timestamp(field) { | ||
const { size } = field; | ||
field.View = size ? TypedArrayViewMixin('biguint64', true) | ||
: TypeViewMixin('biguint64', true); | ||
field.length = field.View.getLength(size); | ||
timestamp() { | ||
return TimestampView; | ||
}, | ||
int(field) { | ||
const { size } = field; | ||
field.View = size ? TypedArrayViewMixin('int32', true) | ||
: TypeViewMixin('int32', true); | ||
field.length = field.View.getLength(size); | ||
int() { | ||
return TypeViewMixin('int32', true); | ||
}, | ||
long(field) { | ||
const { size } = field; | ||
field.View = size ? TypedArrayViewMixin('bigint64', true) | ||
: TypeViewMixin('bigint64', true); | ||
field.length = field.View.getLength(size); | ||
long() { | ||
return LongView; | ||
}, | ||
// todo decimal | ||
// todo long & timestamp & javascript toBSON | ||
}; | ||
@@ -250,7 +237,9 @@ | ||
BSONView.BSON = undefined; | ||
module.exports = (BSON) => { | ||
BSONView.BSON = BSON; | ||
ObjectIdView.BSON = BSON.ObjectId; | ||
BinaryView.BSON = BSON.Binary; | ||
CodeView.BSON = BSON.Code; | ||
LongView.BSON = BSON.Long; | ||
TimestampView.BSON = BSON.Timestamp; | ||
return BSONView; | ||
}; |
@@ -29,6 +29,2 @@ | ||
toBSON() { | ||
return this.constructor.toBSON(this); | ||
} | ||
static getLength() { | ||
@@ -64,2 +60,7 @@ return 12; | ||
} | ||
static toBSON(view, start = 0) { | ||
const value = this.toJSON(view, start); | ||
return new this.BSON(value); | ||
} | ||
} | ||
@@ -69,4 +70,4 @@ | ||
ObjectIdView.isPrimitive = false; | ||
ObjectIdView.BSON = undefined; | ||
module.exports = ObjectIdView; |
{ | ||
"name": "bsonview", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Schema-based BSON using structurae's ObjectView binary protocol.", | ||
"main": "index.js", | ||
"keywords": [ | ||
"BSON", | ||
"binary protocol", | ||
"schema", | ||
"optimization", | ||
"mongodb", | ||
"structurae" | ||
], | ||
"directories": { | ||
@@ -28,3 +36,3 @@ "lib": "lib" | ||
"dependencies": { | ||
"structurae": "^2.3.0" | ||
"structurae": "^3.0.0" | ||
}, | ||
@@ -31,0 +39,0 @@ "devDependencies": { |
13659
11
347
+ Addedstructurae@3.3.0(transitive)
- Removedstructurae@2.3.0(transitive)
Updatedstructurae@^3.0.0