Skip to content

Commit

Permalink
Proposal: Provide direct exports of Bson lib (#338)
Browse files Browse the repository at this point in the history
* BREAKING: Removed default wrapping of Bson

This follows the Node.js Driver better

* refactor: made non breaking
  • Loading branch information
lucsoft authored Mar 2, 2022
1 parent b4b48d8 commit 7c419d5
Show file tree
Hide file tree
Showing 22 changed files with 102 additions and 67 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ await client.connect(
```ts
// Defining schema interface
interface UserSchema {
_id: Bson.ObjectId;
_id: ObjectId;
username: string;
password: string;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ const all_users = await users.find({ username: { $ne: null } }).toArray();

// find by ObjectId
const user1_id = await users.findOne({
_id: new Bson.ObjectId("SOME OBJECTID STRING"),
_id: new ObjectId("SOME OBJECTID STRING"),
});
```

Expand Down Expand Up @@ -204,3 +204,8 @@ https://docs.mongodb.com/manual/reference/command/
### API style refer to

http://mongodb.github.io/node-mongodb-native/3.6/api/

### Local testing with Docker

1. `docker run -d -p 27017:27017 mongo`
2. deno test -A
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as Bson from "https://deno.land/x/web_bson@v0.1.9/mod.ts";
export * from "https://deno.land/x/web_bson@v0.1.9/mod.ts";
export { crypto } from "https://deno.land/std@0.126.0/crypto/mod.ts";
export { BufReader } from "https://deno.land/std@0.126.0/io/mod.ts";
export { writeAll } from "https://deno.land/std@0.126.0/streams/conversion.ts";
Expand Down
19 changes: 18 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,22 @@ export { MongoClient } from "./src/client.ts";
export { Database } from "./src/database.ts";
export { Collection } from "./src/collection/mod.ts";
export * from "./src/types.ts";
export { Bson } from "./deps.ts";
export * as Bson from "./deps.ts";
export {
Binary,
BSONRegExp,
BSONSymbol,
Code,
DBRef,
Decimal128,
Double,
Int32,
Long,
MaxKey,
MinKey,
ObjectId,
Timestamp,
UUID,
} from "./deps.ts";
export type { Document } from "./deps.ts";
export { GridFSBucket } from "./src/gridfs/bucket.ts";
3 changes: 2 additions & 1 deletion src/auth/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConnectOptions, Credential, Document } from "../types.ts";
import { ConnectOptions, Credential } from "../types.ts";
import { WireProtocol } from "../protocol/mod.ts";
import { Document } from "../../deps.ts";

export abstract class AuthPlugin {
abstract prepare(
Expand Down
8 changes: 4 additions & 4 deletions src/auth/scram.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Credential, Document } from "../types.ts";
import { Credential } from "../types.ts";
import { saslprep } from "../utils/saslprep/mod.ts";
import { AuthContext, AuthPlugin } from "./base.ts";
import { HandshakeDocument } from "../protocol/handshake.ts";
import { MongoDriverError } from "../error.ts";
import { b64, Bson, crypto as stdCrypto, hex } from "../../deps.ts";
import { b64, Binary, crypto as stdCrypto, Document, hex } from "../../deps.ts";
import { driverMetadata } from "../protocol/mod.ts";
import { pbkdf2 } from "./pbkdf2.ts";

Expand Down Expand Up @@ -83,7 +83,7 @@ export function makeFirstMessage(
return {
saslStart: 1,
mechanism,
payload: new Bson.Binary(
payload: new Binary(
Uint8Array.from(
[...enc.encode("n,,"), ...clientFirstMessageBare(username, nonce)],
),
Expand Down Expand Up @@ -183,7 +183,7 @@ export async function continueScramConversation(
const saslContinueCmd = {
saslContinue: 1,
conversationId: response.conversationId,
payload: new Bson.Binary(enc.encode(clientFinal)),
payload: new Binary(enc.encode(clientFinal)),
};

const result = await protocol.commandSingle(db, saslContinueCmd);
Expand Down
3 changes: 2 additions & 1 deletion src/auth/x509.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Credential, Document } from "../types.ts";
import { Credential } from "../types.ts";
import { AuthContext, AuthPlugin } from "./base.ts";
import { HandshakeDocument } from "../protocol/handshake.ts";
import { driverMetadata } from "../protocol/mod.ts";
import { Document } from "../../deps.ts";

export interface X509Command extends Document {
authenticate: number;
Expand Down
8 changes: 2 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Database } from "./database.ts";
import {
BuildInfo,
ConnectOptions,
Document,
ListDatabaseInfo,
} from "./types.ts";
import { BuildInfo, ConnectOptions, ListDatabaseInfo } from "./types.ts";
import { parse } from "./utils/uri.ts";
import { MongoDriverError } from "./error.ts";
import { Cluster } from "./cluster.ts";
import { Document } from "../deps.ts";

export class MongoClient {
#cluster?: Cluster;
Expand Down
9 changes: 4 additions & 5 deletions src/collection/collection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bson } from "../../deps.ts";
import { Document, ObjectId } from "../../deps.ts";
import { MongoDriverError, MongoInvalidArgumentError } from "../error.ts";
import { WireProtocol } from "../protocol/mod.ts";
import {
Expand All @@ -8,7 +8,6 @@ import {
CreateIndexOptions,
DeleteOptions,
DistinctOptions,
Document,
DropIndexOptions,
DropOptions,
Filter,
Expand Down Expand Up @@ -163,13 +162,13 @@ export class Collection<T> {
options?: InsertOptions,
): Promise<
{
insertedIds: (Bson.ObjectId | Required<InsertDocument<T>>["_id"])[];
insertedIds: (ObjectId | Required<InsertDocument<T>>["_id"])[];
insertedCount: number;
}
> {
const insertedIds = docs.map((doc) => {
if (!doc._id) {
doc._id = new Bson.ObjectId();
doc._id = new ObjectId();
}

return doc._id;
Expand Down Expand Up @@ -364,7 +363,7 @@ export class Collection<T> {
}
}

export function hasAtomicOperators(doc: Bson.Document | Bson.Document[]) {
export function hasAtomicOperators(doc: Document | Document[]) {
if (Array.isArray(doc)) {
for (const document of doc) {
if (hasAtomicOperators(document)) {
Expand Down
3 changes: 2 additions & 1 deletion src/collection/commands/aggregate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Document } from "../../../deps.ts";
import { CommandCursor } from "../../protocol/cursor.ts";
import { WireProtocol } from "../../protocol/protocol.ts";
import { AggregateOptions, Document } from "../../types.ts";
import { AggregateOptions } from "../../types.ts";

interface AggregateCursorContext {
dbName: string;
Expand Down
3 changes: 2 additions & 1 deletion src/collection/commands/find.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Document } from "../../../deps.ts";
import { CommandCursor, WireProtocol } from "../../protocol/mod.ts";
import { Document, FindOptions } from "../../types.ts";
import { FindOptions } from "../../types.ts";

interface FindCursorContext {
dbName: string;
Expand Down
6 changes: 3 additions & 3 deletions src/collection/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bson } from "../../../deps.ts";
import { Document, UpdateOptions } from "../../types.ts";
import { Document, ObjectId } from "../../../deps.ts";
import { UpdateOptions } from "../../types.ts";
import { WireProtocol } from "../../protocol/mod.ts";

interface UpdateResponse {
Expand All @@ -8,7 +8,7 @@ interface UpdateResponse {
n: number;
upserted?: {
index: number;
_id: Bson.ObjectId;
_id: ObjectId;
}[];
}

Expand Down
3 changes: 2 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Collection } from "./collection/mod.ts";
import { CommandCursor } from "./protocol/mod.ts";
import { CreateUserOptions, Document } from "./types.ts";
import { CreateUserOptions } from "./types.ts";
import { Cluster } from "./cluster.ts";
import { Document } from "../deps.ts";

interface ListCollectionsReponse {
cursor: {
Expand Down
8 changes: 4 additions & 4 deletions src/gridfs/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Bson } from "../../deps.ts";
import { Collection } from "../collection/collection.ts";
import { FindCursor } from "../collection/commands/find.ts";
import { Database } from "../database.ts";
Expand All @@ -14,6 +13,7 @@ import {
import { checkIndexes } from "./indexes.ts";
import { createUploadStream } from "./upload.ts";
import { MongoRuntimeError } from "../error.ts";
import { ObjectId } from "../../deps.ts";

export class GridFSBucket {
#chunksCollection: Collection<Chunk>;
Expand Down Expand Up @@ -49,7 +49,7 @@ export class GridFSBucket {
*/
openUploadStream(filename: string, options?: GridFSUploadOptions) {
return this.openUploadStreamWithId(
new Bson.ObjectId(),
new ObjectId(),
filename,
options,
);
Expand Down Expand Up @@ -87,8 +87,8 @@ export class GridFSBucket {
filename: string,
source: ReadableStream,
options?: GridFSUploadOptions,
): Promise<Bson.ObjectId> {
const objectid = new Bson.ObjectId();
): Promise<ObjectId> {
const objectid = new ObjectId();
await source.pipeTo(
await this.openUploadStreamWithId(objectid, filename, options),
);
Expand Down
2 changes: 1 addition & 1 deletion src/gridfs/indexes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document } from "../types.ts";
import { Document } from "../../deps.ts";
import { Collection } from "../collection/collection.ts";
import { Chunk, File } from "../types/gridfs.ts";

Expand Down
10 changes: 5 additions & 5 deletions src/gridfs/upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bson } from "../../deps.ts";
import { Collection } from "../../mod.ts";
import { Binary, ObjectId } from "../../deps.ts";
import { Collection } from "../collection/mod.ts";
import { Chunk, File, GridFSUploadOptions } from "../types/gridfs.ts";

export interface BucketInfo {
Expand All @@ -11,7 +11,7 @@ export interface BucketInfo {
export function createUploadStream(
{ chunkSizeBytes, chunksCollection, filesCollection }: BucketInfo,
filename: string,
id: Bson.ObjectId,
id: ObjectId,
options?: GridFSUploadOptions,
) {
const chunkSizeBytesCombined = options?.chunkSizeBytes ?? chunkSizeBytes;
Expand All @@ -37,7 +37,7 @@ export function createUploadStream(
await chunksCollection.insertOne({
files_id: id,
n: chunksInserted,
data: new Bson.Binary(uploadBuffer),
data: new Binary(uploadBuffer),
});

bufferPosition = 0;
Expand All @@ -51,7 +51,7 @@ export function createUploadStream(
await chunksCollection.insertOne({
files_id: id,
n: chunksInserted,
data: new Bson.Binary(uploadBuffer.slice(0, bufferPosition)),
data: new Binary(uploadBuffer.slice(0, bufferPosition)),
});
}

Expand Down
5 changes: 2 additions & 3 deletions src/protocol/cursor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Bson } from "../../deps.ts";
import { WireProtocol } from "./protocol.ts";
import { Document } from "../types.ts";
import { parseNamespace } from "../utils/ns.ts";
import { Document, Long } from "../../deps.ts";

export interface CommandCursorOptions<T> {
id: bigint | number | string;
Expand Down Expand Up @@ -54,7 +53,7 @@ export class CommandCursor<T> {
}

const { cursor } = await this.#protocol.commandSingle(this.#db!, {
getMore: Bson.Long.fromBigInt(this.#id!),
getMore: Long.fromBigInt(this.#id!),
collection: this.#collection,
});
this.#batches = cursor.nextBatch || [];
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/handshake.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WireProtocol } from "./protocol.ts";
import { Document } from "../types.ts";
import { Document } from "../../deps.ts";

export const driverMetadata = {
driver: {
Expand Down
11 changes: 5 additions & 6 deletions src/protocol/message.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MessageHeader, OpCode, setHeader } from "./header.ts";
import { Document } from "../types.ts";
import { Bson } from "../../deps.ts";
import { deserialize, Document, serialize } from "../../deps.ts";

type MessageFlags = number;

Expand Down Expand Up @@ -32,7 +31,7 @@ function serializeSections(
let totalLen = 0;
const buffers = sections.map((section) => {
if ("document" in section) {
const document = Bson.serialize(section.document);
const document = serialize(section.document);
const section0 = new Uint8Array(1 + document.byteLength);
new DataView(section0.buffer).setUint8(0, 0);
section0.set(document, 1);
Expand All @@ -42,7 +41,7 @@ function serializeSections(
const identifier = encoder.encode(section.identifier + "\0");
let documentsLength = 0;
const docs = section.documents.map((doc) => {
const document = Bson.serialize(doc);
const document = serialize(doc);
documentsLength += document.byteLength;
return document;
});
Expand Down Expand Up @@ -114,7 +113,7 @@ export function deserializeMessage(
pos++;
if (kind === 0) {
const docLen = view.getInt32(pos, true);
const document = Bson.deserialize(
const document = deserialize(
new Uint8Array(view.buffer.slice(pos, pos + docLen)),
);
pos += docLen;
Expand Down Expand Up @@ -149,7 +148,7 @@ function parseDocuments(buffer: Uint8Array): Document[] {
const view = new DataView(buffer);
while (pos < buffer.byteLength) {
const docLen = view.getInt32(pos, true);
const doc = Bson.deserialize(buffer.slice(pos, pos + docLen));
const doc = deserialize(buffer.slice(pos, pos + docLen));
docs.push(doc);
pos += docLen;
}
Expand Down
9 changes: 7 additions & 2 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { BufReader, Deferred, deferred, writeAll } from "../../deps.ts";
import {
BufReader,
Deferred,
deferred,
Document,
writeAll,
} from "../../deps.ts";
import {
MongoDriverError,
MongoErrorInfo,
MongoServerError,
} from "../error.ts";
import { Document } from "../types.ts";
import { handshake } from "./handshake.ts";
import { parseHeader } from "./header.ts";
import { deserializeMessage, Message, serializeMessage } from "./message.ts";
Expand Down
Loading

0 comments on commit 7c419d5

Please sign in to comment.