@nodescript/contrib-mongodb
Advanced tools
Comparing version 0.0.2 to 0.1.0
import { MongoProtocol } from '@nodescript/adapter-mongodb-protocol'; | ||
import { Disposable } from '@nodescript/core/types'; | ||
import { RpcClient } from '@nodescript/protocomm'; | ||
declare const SYM_MONGODB_CONNECTION: unique symbol; | ||
export declare function requireConnection(value: unknown): MongoDbConnection; | ||
export declare class MongoDbConnection implements Disposable { | ||
ws: WebSocket; | ||
rpcClient: RpcClient<MongoProtocol>; | ||
connected: boolean; | ||
constructor(ws: WebSocket); | ||
export declare class MongoDbConnection { | ||
readonly databaseUrl: string; | ||
readonly adapterUrl: string; | ||
rpc: MongoProtocol; | ||
constructor(databaseUrl: string, adapterUrl: string); | ||
get Mongo(): import("@nodescript/adapter-mongodb-protocol").MongoDomain; | ||
get [SYM_MONGODB_CONNECTION](): boolean; | ||
get Mongo(): import("@nodescript/adapter-mongodb-protocol").MongoDomain; | ||
dispose(): void; | ||
private sendRequest; | ||
private onMessage; | ||
private onClose; | ||
} | ||
export {}; |
import { mongoProtocol } from '@nodescript/adapter-mongodb-protocol'; | ||
import { InvalidTypeError } from '@nodescript/core/util'; | ||
import { RpcClient } from '@nodescript/protocomm'; | ||
import { MongoDbConnectionError } from './errors.js'; | ||
import { createHttpClient } from '@nodescript/protocomm'; | ||
const SYM_MONGODB_CONNECTION = Symbol.for('ns:MongoDbConnection'); | ||
export function requireConnection(value) { | ||
if (!value[SYM_MONGODB_CONNECTION]) { | ||
throw new InvalidTypeError('MongoDB Connection required. Use the output of MongoDB Connect node.'); | ||
if (value[SYM_MONGODB_CONNECTION]) { | ||
return value; | ||
} | ||
const connection = value; | ||
if (!connection.connected) { | ||
throw new MongoDbConnectionError('MongoDB disconnected'); | ||
} | ||
return connection; | ||
throw new InvalidTypeError('MongoDB Connection required. Use the output of MongoDB Connect node.'); | ||
} | ||
export class MongoDbConnection { | ||
constructor(ws) { | ||
constructor(databaseUrl, adapterUrl) { | ||
this.databaseUrl = databaseUrl; | ||
this.adapterUrl = adapterUrl; | ||
const parsedUrl = new URL(adapterUrl); | ||
const secret = parsedUrl.username; | ||
parsedUrl.username = ''; | ||
parsedUrl.password = ''; | ||
const rpc = createHttpClient(mongoProtocol, { | ||
baseUrl: parsedUrl.href, | ||
headers: secret ? { | ||
authorization: `Bearer ${secret}`, | ||
} : undefined, | ||
}); | ||
Object.defineProperties(this, { | ||
rpcClient: { | ||
databaseUrl: { | ||
enumerable: false, | ||
value: new RpcClient(mongoProtocol, req => this.sendRequest(req)), | ||
value: databaseUrl, | ||
}, | ||
ws: { | ||
adapterUrl: { | ||
enumerable: false, | ||
value: ws, | ||
value: adapterUrl, | ||
}, | ||
connected: { | ||
rpc: { | ||
enumerable: false, | ||
writable: true, | ||
value: true, | ||
} | ||
value: rpc, | ||
}, | ||
}); | ||
ws.addEventListener('message', ev => this.onMessage(ev)); | ||
ws.addEventListener('close', () => this.onClose()); | ||
} | ||
get Mongo() { | ||
return this.rpc.Mongo; | ||
} | ||
get [SYM_MONGODB_CONNECTION]() { | ||
return true; | ||
} | ||
get Mongo() { | ||
return this.rpcClient.client.Mongo; | ||
} | ||
dispose() { | ||
this.ws.close(); | ||
} | ||
sendRequest(req) { | ||
this.ws.send(JSON.stringify(req)); | ||
} | ||
onMessage(ev) { | ||
this.rpcClient.processMessage(ev.data); | ||
} | ||
onClose() { | ||
this.rpcClient.handleClose(); | ||
this.connected = false; | ||
} | ||
} | ||
//# sourceMappingURL=MongoDbConnection.js.map |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.Aggregate', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Aggregate', | ||
description: 'Runs an aggregation pipeline in specified MongoDB collection.', | ||
@@ -35,2 +35,3 @@ keywords: ['mongodb', 'database', 'aggregate'], | ||
const { documents } = await connection.Mongo.aggregate({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -37,0 +38,0 @@ pipeline, |
@@ -5,3 +5,2 @@ import { ModuleCompute, ModuleDefinition } from '@nodescript/core/types'; | ||
adapterUrl: string; | ||
secret: string; | ||
}; | ||
@@ -8,0 +7,0 @@ type R = Promise<unknown>; |
@@ -1,26 +0,18 @@ | ||
import { MongoDbConnectionError } from '../lib/errors.js'; | ||
import { MongoDbConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.3.0', | ||
moduleName: 'MongoDB.Connect', | ||
version: '2.2.3', | ||
moduleName: 'Mongo DB / Connect', | ||
description: 'Connects to a MongoDB database. Returns the connection required by other nodes.', | ||
keywords: ['mongodb', 'database', 'storage', 'connect'], | ||
params: { | ||
url: { | ||
schema: { type: 'string' }, | ||
}, | ||
adapterUrl: { | ||
schema: { | ||
type: 'string', | ||
default: 'wss://mongodb.adapters.nodescript.dev/ws' | ||
}, | ||
advanced: true, | ||
}, | ||
secret: { | ||
schema: { | ||
type: 'string', | ||
default: '' | ||
}, | ||
advanced: true, | ||
}, | ||
url: { | ||
schema: { type: 'string' }, | ||
label: 'databaseUrl', | ||
}, | ||
}, | ||
@@ -35,19 +27,15 @@ result: { | ||
export const compute = async (params, ctx) => { | ||
const ws = await new Promise((resolve, reject) => { | ||
const ws = new WebSocket(params.adapterUrl); | ||
ws.addEventListener('open', () => { | ||
resolve(ws); | ||
}); | ||
ws.addEventListener('error', () => { | ||
reject(new MongoDbConnectionError('Could not connect to MongoDB adapter')); | ||
}); | ||
}); | ||
const { url } = params; | ||
const connection = new MongoDbConnection(ws); | ||
const disposableId = 'MongoDB.Connect:' + url; | ||
await ctx.dispose(disposableId); | ||
ctx.trackDisposable(disposableId, connection); | ||
await connection.Mongo.connect({ url }); | ||
const adapterUrl = getAdapterUrl(params, ctx); | ||
const databaseUrl = params.url; | ||
const connection = new MongoDbConnection(databaseUrl, adapterUrl); | ||
await connection.Mongo.connect({ databaseUrl }); | ||
return connection; | ||
}; | ||
function getAdapterUrl(params, ctx) { | ||
const local = ctx.getLocal('ADAPTER_MONGODB_URL'); | ||
if (local) { | ||
return local; | ||
} | ||
return params.adapterUrl || 'https://mongodb.adapters.nodescript.dev'; | ||
} | ||
//# sourceMappingURL=MongoDb.Connect.js.map |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.FindMany', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Find Many', | ||
description: 'Finds documents in specified MongoDB collection.', | ||
@@ -65,2 +65,3 @@ keywords: ['mongodb', 'database', 'find', 'query'], | ||
const { documents } = await connection.Mongo.findMany({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -67,0 +68,0 @@ filter, |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.FindOne', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Find One', | ||
description: 'Finds one document in specified MongoDB collection.', | ||
@@ -43,2 +43,3 @@ keywords: ['mongodb', 'database', 'find', 'query'], | ||
const { document } = await connection.Mongo.findOne({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -45,0 +46,0 @@ filter, |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.InsertMany', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Insert Many', | ||
description: 'Inserts multiple documents into specified MongoDB collection.', | ||
@@ -38,2 +38,3 @@ keywords: ['mongodb', 'database', 'insert'], | ||
const res = await connection.Mongo.insertMany({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -40,0 +41,0 @@ documents, |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.InsertOne', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Insert One', | ||
description: 'Inserts a single document into specified MongoDB collection.', | ||
@@ -35,2 +35,3 @@ keywords: ['mongodb', 'database', 'insert'], | ||
const res = await connection.Mongo.insertOne({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -37,0 +38,0 @@ document, |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.ReplaceOne', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Replace One', | ||
description: 'Replaces a single document matching criteria in specified MongoDB collection.', | ||
@@ -47,2 +47,3 @@ keywords: ['mongodb', 'database', 'replace', 'save'], | ||
return await connection.Mongo.replaceOne({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -49,0 +50,0 @@ filter, |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.1.0', | ||
moduleName: 'MongoDB.UpdateOne', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Update Many', | ||
description: 'Updates documents matching criteria in specified MongoDB collection.', | ||
@@ -43,2 +43,3 @@ keywords: ['mongodb', 'database', 'update'], | ||
return await connection.Mongo.updateMany({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -45,0 +46,0 @@ filter, |
import { requireConnection } from '../lib/MongoDbConnection.js'; | ||
export const module = { | ||
version: '1.2.0', | ||
moduleName: 'MongoDB.UpdateOne', | ||
version: '2.2.1', | ||
moduleName: 'Mongo DB / Update One', | ||
description: 'Updates a single document matching criteria in specified MongoDB collection.', | ||
@@ -47,2 +47,3 @@ keywords: ['mongodb', 'database', 'update'], | ||
return await connection.Mongo.updateOne({ | ||
databaseUrl: connection.databaseUrl, | ||
collection, | ||
@@ -49,0 +50,0 @@ filter, |
{ | ||
"name": "@nodescript/contrib-mongodb", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"type": "module", | ||
@@ -30,7 +30,7 @@ "description": "MongoDB Connector Nodes", | ||
"dependencies": { | ||
"@nodescript/adapter-mongodb-protocol": "^1.3.4", | ||
"@nodescript/core": "^5.8.1" | ||
"@nodescript/adapter-mongodb-protocol": "^1.7.3", | ||
"@nodescript/core": "^7.8.0" | ||
}, | ||
"devDependencies": { | ||
"@nodescript/cli": "^0.7.4", | ||
"@nodescript/cli": "^1.2.0", | ||
"@nodescript/eslint-config": "^1.0.3", | ||
@@ -37,0 +37,0 @@ "@types/mocha": "^9.1.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38902
712
+ Added@nodescript/core@7.22.0(transitive)
- Removed@nodescript/core@5.23.1(transitive)
Updated@nodescript/core@^7.8.0