Comparing version 0.5.1 to 0.6.0
70
index.js
const { | ||
ObjectView, StringView, TypeViewMixin, BooleanView, ArrayView, | ||
ObjectView, ArrayView, BooleanView, StringView, TypeView, | ||
} = require('structurae'); | ||
const TypeViewMixin = require('./lib/type-view-mixin'); | ||
const ObjectIdView = require('./lib/objectid-view'); | ||
@@ -12,3 +13,23 @@ const DateView = require('./lib/date-view'); | ||
const BSONTypes = { | ||
TypeView.toBSON = function(view, start) { | ||
const value = this.toJSON(view, start); | ||
return new this.BSON(value); | ||
}; | ||
BooleanView.toBSON = StringView.toBSON = function(view, start, length) { | ||
return this.toJSON(view, start, length); | ||
}; | ||
ArrayView.toBSON = function(view, start, length) { | ||
const { View, itemLength } = this; | ||
const size = this.getSize(length); | ||
const array = new Array(size); | ||
for (let i = 0; i < size; i++) { | ||
const offset = this.getLength(i); | ||
array[i] = View.toBSON(view, start + offset, itemLength); | ||
} | ||
return array; | ||
}; | ||
const supportedBSONTypes = { | ||
0x01: 1, // double | ||
@@ -96,3 +117,3 @@ 0x02: 1, // string | ||
const elementType = bson[caret]; | ||
if (!BSONTypes[elementType]) { | ||
if (!Reflect.has(supportedBSONTypes, elementType)) { | ||
throw new TypeError(`Type ${elementType} is not supported.`); | ||
@@ -115,2 +136,3 @@ } | ||
start = offset + index * itemLength; | ||
SubView = View.View; | ||
hasValue = start < view.byteLength && valueLength; | ||
@@ -160,5 +182,15 @@ } else { | ||
static initialize() { | ||
super.initialize(); | ||
this.encodedFields = this.fields.map((name) => StringView.encoder.encode(name)); | ||
static initialize(ParentViewClass = BSONView) { | ||
const { schema } = this; | ||
const schemas = this.getSchemaOrdering(schema); | ||
for (let i = schemas.length - 1; i >= 0; i--) { | ||
const objectSchema = schemas[i]; | ||
const id = objectSchema.$id; | ||
if (Reflect.has(ObjectView.Views, id)) continue; | ||
const View = i === 0 ? this : class extends ParentViewClass {}; | ||
[View.layout, View.objectLength, View.fields] = this.getLayoutFromSchema(objectSchema); | ||
ObjectView.Views[id] = View; | ||
View.encodedFields = View.fields.map((name) => StringView.encoder.encode(name)); | ||
View.setDefaultBuffer(); | ||
} | ||
} | ||
@@ -185,4 +217,3 @@ | ||
const { View, start: fieldStart, length: fieldLength } = layout[name]; | ||
result[name] = View.toBSON ? View.toBSON(view, start + fieldStart, fieldLength) | ||
: View.toJSON(view, start + fieldStart, fieldLength); | ||
result[name] = View.toBSON(view, start + fieldStart, fieldLength); | ||
} | ||
@@ -194,5 +225,22 @@ return result; | ||
BSONView.types = { | ||
...ObjectView.types, | ||
boolean() { | ||
return BooleanView; | ||
}, | ||
string() { | ||
return StringView; | ||
}, | ||
number(field) { | ||
const { btype = 'float64' } = field; | ||
return TypeViewMixin(btype); | ||
}, | ||
integer(field) { | ||
const { btype = 'int32' } = field; | ||
return TypeViewMixin(btype); | ||
}, | ||
double() { | ||
return TypeViewMixin('float64', true); | ||
return TypeViewMixin('float64'); | ||
}, | ||
@@ -233,3 +281,3 @@ | ||
int() { | ||
return TypeViewMixin('int32', true); | ||
return TypeViewMixin('int32'); | ||
}, | ||
@@ -236,0 +284,0 @@ |
@@ -6,3 +6,5 @@ const { ArrayViewMixin } = require('structurae'); | ||
const value = this.toJSON(view, start, length); | ||
return new this.BSON(value); | ||
// todo refactor when https://github.com/mongodb/js-bson/pull/348 is fixed | ||
const string = value.map((i) => String.fromCharCode(i)).join(''); | ||
return new this.BSON(string); | ||
} | ||
@@ -9,0 +11,0 @@ } |
@@ -14,4 +14,8 @@ const { TypeViewMixin } = require('structurae'); | ||
} | ||
static toBSON(view, start) { | ||
return this.toJSON(view, start); | ||
} | ||
} | ||
module.exports = DateView; |
@@ -1,4 +0,4 @@ | ||
const { TypeViewMixin } = require('structurae'); | ||
const TypeViewMixin = require('./type-view-mixin'); | ||
class LongView extends TypeViewMixin('bigint64', true) { | ||
class LongView extends TypeViewMixin('bigint64') { | ||
static toBSON(view, start = 0) { | ||
@@ -10,4 +10,2 @@ const value = this.toJSON(view, start); | ||
LongView.BSON = globalThis.BSON && globalThis.BSON.Long; | ||
module.exports = LongView; |
@@ -1,4 +0,4 @@ | ||
const { TypeViewMixin } = require('structurae'); | ||
const TypeViewMixin = require('./type-view-mixin'); | ||
class TimestampView extends TypeViewMixin('biguint64', true) { | ||
class TimestampView extends TypeViewMixin('biguint64') { | ||
static toBSON(view, start = 0) { | ||
@@ -10,4 +10,2 @@ const value = this.toJSON(view, start); | ||
TimestampView.BSON = globalThis.BSON && globalThis.BSON.Timestamp; | ||
module.exports = TimestampView; |
{ | ||
"name": "bsonview", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Schema-based BSON using structurae's ObjectView binary protocol.", | ||
@@ -36,3 +36,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"structurae": "^3.0.3" | ||
"structurae": "^3.0.4" | ||
}, | ||
@@ -39,0 +39,0 @@ "devDependencies": { |
15914
12
410
Updatedstructurae@^3.0.4