diff --git a/README.md b/README.md index f602220c..076f2243 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ await client.connect( ```ts // Defining schema interface interface UserSchema { - _id: Bson.ObjectId; + _id: ObjectId; username: string; password: string; } @@ -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"), }); ``` @@ -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 diff --git a/deps.ts b/deps.ts index 96003ff4..9a59421b 100644 --- a/deps.ts +++ b/deps.ts @@ -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"; diff --git a/mod.ts b/mod.ts index 8593f71b..91a00a78 100644 --- a/mod.ts +++ b/mod.ts @@ -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"; diff --git a/src/auth/base.ts b/src/auth/base.ts index 35a07a05..a34c9012 100644 --- a/src/auth/base.ts +++ b/src/auth/base.ts @@ -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( diff --git a/src/auth/scram.ts b/src/auth/scram.ts index b65b983b..39b505ab 100644 --- a/src/auth/scram.ts +++ b/src/auth/scram.ts @@ -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"; @@ -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)], ), @@ -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); diff --git a/src/auth/x509.ts b/src/auth/x509.ts index 71fe3f98..029b0ae6 100644 --- a/src/auth/x509.ts +++ b/src/auth/x509.ts @@ -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; diff --git a/src/client.ts b/src/client.ts index 9ca86684..a4de8edd 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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; diff --git a/src/collection/collection.ts b/src/collection/collection.ts index d2de59ba..97249e93 100644 --- a/src/collection/collection.ts +++ b/src/collection/collection.ts @@ -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 { @@ -8,7 +8,6 @@ import { CreateIndexOptions, DeleteOptions, DistinctOptions, - Document, DropIndexOptions, DropOptions, Filter, @@ -163,13 +162,13 @@ export class Collection { options?: InsertOptions, ): Promise< { - insertedIds: (Bson.ObjectId | Required>["_id"])[]; + insertedIds: (ObjectId | Required>["_id"])[]; insertedCount: number; } > { const insertedIds = docs.map((doc) => { if (!doc._id) { - doc._id = new Bson.ObjectId(); + doc._id = new ObjectId(); } return doc._id; @@ -364,7 +363,7 @@ export class Collection { } } -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)) { diff --git a/src/collection/commands/aggregate.ts b/src/collection/commands/aggregate.ts index 9e5325cf..d02de00c 100644 --- a/src/collection/commands/aggregate.ts +++ b/src/collection/commands/aggregate.ts @@ -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; diff --git a/src/collection/commands/find.ts b/src/collection/commands/find.ts index c9df5bb2..92978966 100644 --- a/src/collection/commands/find.ts +++ b/src/collection/commands/find.ts @@ -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; diff --git a/src/collection/commands/update.ts b/src/collection/commands/update.ts index 07708122..1e851d03 100644 --- a/src/collection/commands/update.ts +++ b/src/collection/commands/update.ts @@ -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 { @@ -8,7 +8,7 @@ interface UpdateResponse { n: number; upserted?: { index: number; - _id: Bson.ObjectId; + _id: ObjectId; }[]; } diff --git a/src/database.ts b/src/database.ts index 37a6fab9..9b2f5c49 100644 --- a/src/database.ts +++ b/src/database.ts @@ -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: { diff --git a/src/gridfs/bucket.ts b/src/gridfs/bucket.ts index 6b79e85c..f635dcd7 100644 --- a/src/gridfs/bucket.ts +++ b/src/gridfs/bucket.ts @@ -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"; @@ -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; @@ -49,7 +49,7 @@ export class GridFSBucket { */ openUploadStream(filename: string, options?: GridFSUploadOptions) { return this.openUploadStreamWithId( - new Bson.ObjectId(), + new ObjectId(), filename, options, ); @@ -87,8 +87,8 @@ export class GridFSBucket { filename: string, source: ReadableStream, options?: GridFSUploadOptions, - ): Promise { - const objectid = new Bson.ObjectId(); + ): Promise { + const objectid = new ObjectId(); await source.pipeTo( await this.openUploadStreamWithId(objectid, filename, options), ); diff --git a/src/gridfs/indexes.ts b/src/gridfs/indexes.ts index 82ed0d22..7250699a 100644 --- a/src/gridfs/indexes.ts +++ b/src/gridfs/indexes.ts @@ -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"; diff --git a/src/gridfs/upload.ts b/src/gridfs/upload.ts index 5fe270e3..708b0489 100644 --- a/src/gridfs/upload.ts +++ b/src/gridfs/upload.ts @@ -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 { @@ -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; @@ -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; @@ -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)), }); } diff --git a/src/protocol/cursor.ts b/src/protocol/cursor.ts index f1749e7f..ccbbe5bd 100644 --- a/src/protocol/cursor.ts +++ b/src/protocol/cursor.ts @@ -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 { id: bigint | number | string; @@ -54,7 +53,7 @@ export class CommandCursor { } 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 || []; diff --git a/src/protocol/handshake.ts b/src/protocol/handshake.ts index 032576d5..8f577863 100644 --- a/src/protocol/handshake.ts +++ b/src/protocol/handshake.ts @@ -1,5 +1,5 @@ import { WireProtocol } from "./protocol.ts"; -import { Document } from "../types.ts"; +import { Document } from "../../deps.ts"; export const driverMetadata = { driver: { diff --git a/src/protocol/message.ts b/src/protocol/message.ts index 92cd3aae..f85066ab 100644 --- a/src/protocol/message.ts +++ b/src/protocol/message.ts @@ -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; @@ -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); @@ -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; }); @@ -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; @@ -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; } diff --git a/src/protocol/protocol.ts b/src/protocol/protocol.ts index 3a516dad..9462f365 100644 --- a/src/protocol/protocol.ts +++ b/src/protocol/protocol.ts @@ -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"; diff --git a/src/types.ts b/src/types.ts index 2984ef8b..1affe2b8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,8 +1,16 @@ -import { Bson } from "../deps.ts"; +import { + Binary, + BSONRegExp, + Decimal128, + Document, + Double, + Int32, + Long, + ObjectId, + Timestamp, +} from "../deps.ts"; import { WriteConcern } from "./types/read_write_concern.ts"; -export type Document = Bson.Document; - export interface Server { host: string; port: number; @@ -593,11 +601,11 @@ export interface DropIndexOptions { comment?: Document; } -type BitwiseType = Bson.Binary | Array | number; +type BitwiseType = Binary | Array | number; -type IntegerType = number | Bson.Int32 | Bson.Long; +type IntegerType = number | Int32 | Long; -type NumericType = IntegerType | Bson.Decimal128 | Bson.Double; +type NumericType = IntegerType | Decimal128 | Double; interface RootFilterOperators extends Document { $and?: Filter[]; @@ -632,7 +640,7 @@ interface FilterOperators extends Document { $expr?: Document; $jsonSchema?: Document; $mod?: TValue extends number ? [number, number] : never; - $regex?: string | RegExp | Bson.BSONRegExp; + $regex?: string | RegExp | BSONRegExp; $geoIntersects?: { $geometry: Document }; $geoWithin?: Document; $near?: Document; @@ -656,7 +664,7 @@ interface FilterOperators extends Document { interface UpdateOperators extends Document { $currentDate?: DocumentOperator< T, - Bson.Timestamp | Date, + Timestamp | Date, true | { $type: "date" | "timestamp" } >; $inc?: DocumentOperator; @@ -772,11 +780,10 @@ type Flatten = T extends Array ? Item : T; type IsAny = 0 extends (1 & T) ? Y : N; -// deno-lint-ignore no-explicit-any -export type InsertDocument = +export type InsertDocument = & Omit & { - _id?: TDocument["_id"] | Bson.ObjectId; + _id?: TDocument["_id"] | ObjectId; }; type KeysOfType = { diff --git a/src/types/gridfs.ts b/src/types/gridfs.ts index 79ae9729..66fc9550 100644 --- a/src/types/gridfs.ts +++ b/src/types/gridfs.ts @@ -1,19 +1,19 @@ -import { Bson } from "../../deps.ts"; -import { Document, ReadPreference } from "../types.ts"; +import { Binary, Document, ObjectId } from "../../deps.ts"; +import { ReadPreference } from "../types.ts"; import { ReadConcern, WriteConcern } from "../types/read_write_concern.ts"; -export type FileId = Bson.ObjectId; +export type FileId = ObjectId; export interface Chunk { - _id: Bson.ObjectId; + _id: ObjectId; // deno-lint-ignore camelcase - files_id: Bson.ObjectId; + files_id: ObjectId; n: number; - data: Bson.Binary; + data: Binary; } export interface File { - _id: Bson.ObjectId; + _id: ObjectId; length: number; chunkSize: number; uploadDate: Date; diff --git a/tests/cases/import_worker.ts b/tests/cases/import_worker.ts index c98f42b4..190e002c 100644 --- a/tests/cases/import_worker.ts +++ b/tests/cases/import_worker.ts @@ -1,3 +1,6 @@ +/// +/// + import {} from "../../mod.ts"; onmessage = (_e) => {