Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic support for storing blobs in tables using $binary #251

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/client/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,36 @@ import { EJSON, ObjectId } from 'bson';
import mongoose from 'mongoose';

export function deserialize(data: Record<string, any>): Record<string, any> {
return data != null ? deserializeObjectIds(EJSON.deserialize(data)) : data;
return data != null ? deserializeObjectIds(EJSON.deserialize(deserializeBuffers(data))) : data;
}

function deserializeBuffers(data: Record<string, any>): Record<string, any> {
if (data == null) {
return data;
}
if (Array.isArray(data)) {
for (let i = 0; i < data.length; ++i) {
if (data[i] == null) {
continue;
}
if (typeof data[i].$binary === 'string') {
data[i] = Buffer.from(data[i].$binary, 'base64');
} else if (typeof data[i] === 'object') {
deserializeBuffers(data[i]);
}
}
}
for (const key of Object.keys(data)) {
if (data[key] == null) {
continue;
}
if (typeof data[key].$binary === 'string') {
data[key] = Buffer.from(data[key].$binary, 'base64');
} else if (typeof data[key] === 'object') {
deserializeBuffers(data[key]);
}
}
return data;
}

function deserializeObjectIds(data: Record<string, any>): Record<string, any> {
Expand Down
3 changes: 2 additions & 1 deletion src/client/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ class HTTP2Session {
':method': 'POST',
token
});
req.write(serialize(body), 'utf8');
const dataStr = serialize(body);
req.write(dataStr, 'utf8');
req.end();

let status = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/client/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function serializeValue(value: any): any {
//UUID handling. Subtype 03 or 04 is UUID. Refer spec : https://bsonspec.org/spec.html
return Buffer.from(value.$binary.base64, 'base64').toString('hex')
.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5');
} else if (value.$binary) {
return { $binary: value.$binary.base64 };
}
//Date handling
else if (value.$date) {
Expand Down
17 changes: 17 additions & 0 deletions tests/driver/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@ describe('Mongoose Model API level tests', async () => {
featureFlags: ['Feature-Flag-tables']
};
await mongoose.connect(testClient!.uri, options as mongoose.ConnectOptions);
await mongoose.connection.db.runCommand({
dropTable: {
name: 'bots'
}
});
const res = await mongoose.connection.db.runCommand({
createTable: {
name: 'bots',
Expand All @@ -739,12 +744,24 @@ describe('Mongoose Model API level tests', async () => {
},
name: {
type: 'text'
},
buf: {
type: 'blob'
}
}
}
}
});
assert.ok(res.status.ok);

const schema = new mongoose.Schema({ buf: Buffer }, { versionKey: false });
const TestModel = mongoose.model('Test', schema, 'bots');
const doc = new TestModel({ buf: Buffer.from('hello world') });
await doc.save();

const fromDb = await TestModel.findOne();
assert.equal(fromDb.buf.toString('utf8'), 'hello world');

await mongoose.disconnect();
});
});
Expand Down
Loading