MongoRuntimeError Unable to parse with URL - mongodb

I am trying to connect to a mongo db using the nodejs mongo driver and I'm doing this in a cypress project. I get the error in the title. Below is the simplified version of my code.
import {MongoClient} from 'mongodb';
export class SomeRepository {
static insertSomething(): void {
// Error in the line below: MongoRuntimeError Unable to parse localhost:27017 with URL
const client = new MongoClient('mongodb://localhost:27017');
}
}
Mongodb is running because I can connect from the terminal. Also tried replacing localhost with 127.0.0.1 and adding the authSource parameter to the connection string.
The reason I'm mentioning cypress is because in a simple node project that only connects to mongodb everything works as expected. Package.json below
{
"name": "e2e",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"cypress": "10.8.0",
"cypress-wait-until": "1.7.2",
"headers-utils": "3.0.2",
"mongodb": "4.10.0",
"otplib": "12.0.1",
"pg": "8.7.3",
"pg-native": "3.0.1",
"typescript": "4.9.3"
}
}

The error is in the way you are passing the url, it is necessary that you follow a pattern, in mongodb to connect you need to have this pattern that I will pass below:
Format:
mongodb://<user>:<password>#<host>
Format with filled values:
mongodb://root:mypassword#localhost:27017/

The reason it’s not working is because you’re calling a NodeJS library in a cypress test. Cypress tests run inside a browser and cannot run nodejs libraries.
If you wanted to execute nodejs code in cypress you must create a cypress task https://docs.cypress.io/api/commands/task#Syntax
// cypress.config.js
import { SomeRepository } from ‘./file/somewhere’
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
on(‘task’, {
insertSomething() {
return SomeRepository.insertSomething();
}
}
}
}
})
// to call in a cypress test
it(‘test’, function () {
cy.task(‘insertSomething’).then(value => /* do something */);
}
});

Related

No migrations are pending using Typeorm CLI and NestJS

The issue I'm facing is with running migrations using #nestjs/typeorm ^9.0.1 and typeorm ^0.3.12. Despite being able to build the project and create migrations, when I try to run them, typeorm can't find the migration files (No migrations are pending).
I have noticed that the version 0.3.x of typeorm requires Datasource object and that in the version 9.x of #nestjs/typeorm, TypeOrmModuleOptions does not have the cli : {migrationsDir : string} attribute.
Here's a link to the project that reproduces the problem, and an example directory structure :
src\configs\database\dataSource.config.ts
src\configs\database\postgres.config.ts
src\configs\database\migrations\1676252827402-Chat.ts
postgres.config.ts
import { DataSourceOptions } from "typeorm";
import { join } from "path";
import { config } from "dotenv";
import CustomNamingStrategy from "./customNamingStrategy";
import { registerAs } from "#nestjs/config";
import { TypeOrmModuleOptions } from "#nestjs/typeorm";
config();
export const databaseConfig: DataSourceOptions = {
name: "default",
type: "postgres",
url: process.env.DATABASE_URL,
ssl:
process.env.DATABASE_ENABLE_SSL === "true"
? {
rejectUnauthorized: false,
}
: false,
logging: process.env.DATABASE_ENABLE_LOGGING === "true",
entities: [join(__dirname, "../../models/*/", "*.entity.{ts,js}")],
migrations: [join(__dirname, "migrations/*.{ts,js}")],
synchronize: false,
migrationsRun: false,
namingStrategy: new CustomNamingStrategy(),
};
export default registerAs(
"database",
() =>
({
...databaseConfig,
keepConnectionAlive: true,
} as TypeOrmModuleOptions)
);
dataSource.config.ts
import { DataSource } from "typeorm";
import { databaseConfig } from "./postgres.config";
export const AppDataSource = new DataSource(databaseConfig);
package.json
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli"
"migration:create": "yarn run typeorm migration:create"
"migration:run": "yarn run typeorm migration:run -d src/configs/database/dataSource.config.ts"
Although I can use the create command successfully, when I use the run command, it only displays the message "No migrations are pending". I attempted to resolve the issue by changing migrations path to the DataSourceOptions.migrations list, but it did not work. I also tried replacing the cli parameter datasource with the dist datasource, as well as adjusting the migration paths, but these changes did not fix the issue.
I'm facing a similar issue using the same version of typeorm (^0.3.12), so there might be a bug in this version.
It seems that the migrations are not found anymore if the DataSourceOptions.migrations property is set with the path to the migrations folder.
The ^0.3.12 version seems to be working fine only if the migration classes are directly referenced in DataSourceOptions.migrations, similar to the following:
import { Chat1676252827402 } from './migrations/1676252827402-Chat.ts';
export const databaseConfig: DataSourceOptions = {
...
migrations: [Chat1676252827402],
...
};

Proper way to test Chalice app with env variables

I have a Chalice API project with the following config under .chalice/config.json:
{
"version": "2.0",
"app_name": "my-app",
"manage_iam_role": true,
"stages": {
"local": {
"api_gateway_stage": "local",
"environment_variables": {
"STAGE_NAME": "local",
"MY_ENV_VAR": "value-local"
}
},
"staging": {
"api_gateway_stage": "stg",
"environment_variables": {
"STAGE_NAME": "staging",
"MY_ENV_VAR": "value-staging"
}
},
"production": {
"api_gateway_stage": "prod",
"environment_variables": {
"STAGE_NAME": "production",
"MY_ENV_VAR": "value-prod"
}
}
}
}
The app code uses some env variables like MY_ENV_VAR, loaded with os.environ["MY_ENV_VAR"].
The tests look like the following (let's say in a tests/test_my_endpoint.py file):
from http import HTTPStatus
from app import app
from chalice.test import Client
def test_my_endpoint() -> None:
with Client(app, stage_name="local") as client:
response = client.http.get("/my-endpoint")
assert response.status_code == HTTPStatus.OK
But it seems stage_name="local" doesn't mean variables for the stage local are loaded for the tests, so during tests execution, env variables are not found and tests fail. I'm not sure how setting this argument for chalice.test.Client is useful then.
For testing locally, I can always load environment variables locally, but for CI/CD, I would like the tests to use the env variable of a stage defined in .chalice/config.json. Chalice documentation isn't very clear about the good practice here, and about how stage_name argument works exactly.
How can I fix that, and generally speaking what is the proper way to create tests for a Chalice app using the env variables?

Migrating to Meteor 1.7.0.4 : MongoDB : "updating the path ... would create a conflict at ..."

Im using meteor, and im trying to go from meteor 1.6 to 1.7
But im encountering errors im unable to get informations about in my tests :
tests.jpg
I get multiple things like this (some are maybe redondant, unused or obsolete) :
Error: Updating the path 'relationChangeRate' would create a conflict at 'relationChangeRate'
at Function.MongoError.create (C:\Users\fquesselaire\AppData\Local\.meteor\packages\npm-mongo\3.0.11\npm\node_modules\mongodb-core\lib\error.js:45:10)
at toError (C:\Users\fquesselaire\AppData\Local\.meteor\packages\npm-mongo\3.0.11\npm\node_modules\mongodb\lib\utils.js:149:22)
at C:\Users\fquesselaire\AppData\Local\.meteor\packages\npm-mongo\3.0.11\npm\node_modules\mongodb\lib\collection.js:1029:39
at C:\Users\fquesselaire\AppData\Local\.meteor\packages\npm-mongo\3.0.11\npm\node_modules\mongodb-core\lib\connection\pool.js:544:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
not occurring in 1.6.*
I tried upgrade my mongodb to 3.6.6, no effect.
Im also unable to get the entire stack in tests to see what is exactly enforcing.
my .meteor/versions :
255kb:meteor-status#1.5.0
accounts-base#1.4.2
accounts-password#1.5.1
akasha:fs-extra#0.26.3
alanning:roles#1.2.16
aldeed:autoform#6.3.0
aldeed:autoform-bs-button-group-input#2.0.0
aldeed:autoform-select2#3.0.1
aldeed:collection2-core#2.1.2
aldeed:template-extension#4.1.0
allow-deny#1.1.0
arillo:flow-router-helpers#0.5.2
autoupdate#1.4.1
babel-compiler#7.1.1
babel-runtime#1.2.4
base64#1.0.11
binary-heap#1.0.10
blaze#2.3.2
blaze-html-templates#1.1.2
blaze-tools#1.0.10
boilerplate-generator#1.5.0
caching-compiler#1.1.12
caching-html-compiler#1.1.3
callback-hook#1.1.0
check#1.3.1
coffeescript#1.0.17
cultofcoders:mocha#2.4.6
dburles:collection-helpers#1.1.0
dburles:factory#1.1.0
ddp#1.4.0
ddp-client#2.3.3
ddp-common#1.4.0
ddp-rate-limiter#1.0.7
ddp-server#2.2.0
deps#1.0.12
diff-sequence#1.1.0
dynamic-import#0.4.1
ecmascript#0.11.1
ecmascript-runtime#0.7.0
ecmascript-runtime-client#0.7.2
ecmascript-runtime-server#0.7.1
ejson#1.1.0
email#1.2.3
es5-shim#4.8.0
fourseven:scss#4.9.0
geojson-utils#1.0.10
hot-code-push#1.0.4
html-tools#1.0.11
htmljs#1.0.11
http#1.4.1
id-map#1.1.0
idmontie:migrations#1.0.3
jquery#1.11.11
kadira:blaze-layout#2.3.0
kadira:flow-router#2.12.1
lai:collection-extensions#0.2.1_1
launch-screen#1.1.1
livedata#1.0.18
lmieulet:meteor-coverage#1.1.4
localstorage#1.2.0
logging#1.1.20
matb33:collection-hooks#0.8.4
meteor#1.9.2
meteor-base#1.4.0
meteorhacks:picker#1.0.3
meteortesting:browser-tests#1.0.0
meteortesting:mocha#1.0.0
minifier-css#1.3.1
minifier-js#2.3.5
minimongo#1.4.4
mobile-experience#1.0.5
mobile-status-bar#1.0.14
modern-browsers#0.1.2
modules#0.12.2
modules-runtime#0.10.2
momentjs:moment#2.22.2
mongo#1.5.1
mongo-dev-server#1.1.0
mongo-id#1.0.7
mouse0270:bootstrap-notify#3.1.3
natestrauser:select2#4.0.3
npm-bcrypt#0.9.3
npm-mongo#3.0.11
observe-sequence#1.0.16
ordered-dict#1.1.0
ostrio:cookies#2.3.0
ostrio:files#1.10.1
practicalmeteor:chai#2.1.0_1
practicalmeteor:loglevel#1.2.0_2
practicalmeteor:mocha-core#1.0.1
practicalmeteor:sinon#1.14.1_2
promise#0.11.1
raix:eventemitter#0.1.3
random#1.1.0
rate-limit#1.0.9
reactive-dict#1.2.1
reactive-var#1.0.11
reload#1.2.0
retry#1.1.0
rocketchat:streamer#0.5.0
routepolicy#1.0.13
service-configuration#1.0.11
session#1.1.8
sewdn:collection-behaviours#0.3.0
sha#1.0.9
shell-server#0.3.1
socket-stream-client#0.2.2
softwarerero:accounts-t9n#1.3.11
spacebars#1.0.15
spacebars-compiler#1.1.3
srp#1.0.12
standard-minifier-css#1.4.1
standard-minifier-js#2.3.4
templating#1.3.2
templating-compiler#1.3.3
templating-runtime#1.3.2
templating-tools#1.1.2
tmeasday:check-npm-versions#0.3.2
tmeasday:test-reporter-helpers#0.2.1
tracker#1.2.0
ui#1.0.13
underscore#1.0.10
url#1.2.0
useraccounts:bootstrap#1.14.2
useraccounts:core#1.14.2
useraccounts:flow-routing#1.14.2
webapp#1.6.2
webapp-hashing#1.0.9
zimme:active-route#2.3.2
my node packages :
"dependencies": {
"#babel/runtime": "^7.0.0-beta.54",
"#mdi/font": "^2.5.94",
"babel-runtime": "^6.26.0",
"bcrypt": "^3.0.0",
"bootstrap": "^3.3.7",
"bootstrap-material-design": "^0.5.10",
"bootstrap-slider": "^10.0.2",
"bootstrap-toggle": "^2.2.2",
"clone": "^2.1.1",
"csv-parse": "^2.5.0",
"d3": "^5.5.0",
"file-saver": "^1.3.8",
"fs-extra": "^3.0.1",
"gm": "^1.23.0",
"jquery": "^2.2.4",
"jsondiffpatch": "^0.3.11",
"lodash": "^4.17.10",
"meteor-node-stubs": "^0.4.1",
"mkdirp": "^0.5.1",
"object-path": "^0.11.4",
"popper.js": "^1.14.3",
"select2": "^4.0.4",
"select2-bootstrap-theme": "^0.1.0-beta.10",
"simpl-schema": "^1.5.3",
"uuid": "^3.3.2",
"xmldoc": "^1.1.0",
"zip-local": "^0.3.4"
},
"devDependencies": {
"chromedriver": "^2.40.0",
"gulp": "^3.9.1",
"selenium-webdriver": "^3.6.0"
}
if you have any clue about what is going on ?
i can give more informations about what's happening in the app, but right now a simple explain about what this error mean would be awesome already. as i said, this error only append in 1.7.
EDIT: forget to say that my test throw this on WORKING code.
Features are working well, but not the tests.
EDIT²:
This method is the one that update the collection projectSettings.
_duplicateProjectSettings(newSession) {
const settings = this.session.projectSettings();
delete settings._id;
settings.sessionId = newSession._id;
const newSettings = newSession.projectSettings();
ProjectSettings.update(newSettings._id, { $set: settings });
}
relationChangeRate is a simpl-schema field of projectSettings collection as it (i use collection2-core) :
relationChangeRate: {
type: Number,
allowedValues: _.values(RelationRates),
defaultValue: 0.0
}
projectSettings extends mongo.collection, but in this case, update method is not override.
The code seams correct to me, as the _id is well removed and set to the new object before update request. but maybe im wrong ?
EDIT³:
ProjectSettings is a collection-helper:
Here is the code :
Sessions.helpers({
[...]
projectSettings() {
const conds = { sessionId: this._id };
return ProjectSettings.findOne(conds);
},
[...]
A collection helper basically add a method on each document from this collection. It means that any session object will have this method. In a collection helper, "this" refer to the current collection object.
so should be good ?
_duplicateProjectSettings could be write like this:
_duplicateProjectSettings(newSession) {
// const settings = this.session.projectSettings();
const settings = ProjectSettings.findOne({ sessionId: this.session._id });
delete settings._id;
settings.sessionId = newSession._id;
// const newSettings = newSession.projectSettings();
const newSettings = ProjectSettings.findOne({ sessionId: newSession._id });
ProjectSettings.update(newSettings._id, { $set: settings });
}
(I tried it too, same result)
Running this from shell worked for me... not the best solution. I had the problem when I tried to login with accounts-password package.
$ meteor mongo
meteor:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
Ok,
This is an half-answer but
After some extended search i figured that i had a test script that reset MONGO_URL as follow :
set MONGO_URL=
set TEST_BROWSER_DRIVER=chrome
set TEST_WATCH=1
set TEST_CLIENT=0
set TEST_SERVER=1
meteor test -p 3100 --driver-package=cultofcoders:mocha %*
after figuring that i launched the tests on my local dev db, and tests passed (smashing my db by the way lol). So, it means it could be version related, or test-conditions related.
I am not able to print the version of mongodb used for tests, so i don't know the tests conditions exactly.
All i know is so :
Meteor "normal run" Mongo version : 3.6.4
My local Mongo version : 3.6.6

MissingMethodException on connect with Npgsql + Dotnet CLI on CentOs 7.1

I'm running Dotnet CLI version 1.0.0-rc2-002439 on CentOS 7.1
I've built a prototype Console application that just performs a select against a PostgreSQL 9.4 database.
I'm only targeting DNXCORE50 and on windows, everything works just fine. When I'm running it on CentOS I get the following exception on conn.Open()
Unhandled Exception: System.MissingMethodException: Method not found: 'Void System.Net.Security.SslStream.AuthenticateAsClient(System.String, System.Security.Cryptography.X509Certificates.X509CertificateCollection, System.Security.Authentication.SslProtocols, Boolean)'.
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.Open()
at Npgsql.NpgsqlConnectorPool.GetPooledConnector(NpgsqlConnection Connection)
at Npgsql.NpgsqlConnectorPool.RequestConnector(NpgsqlConnection connection)
at Npgsql.NpgsqlConnection.OpenInternal(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnection.Open()
at SensorBot.Core.Services.ConfigService.GetConfig()
at ConsoleApplication.Program.Main(String[] args)
Here is the project.json I'm using :
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": false
},
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0-rc2-23811"
},
},
"runtimes": { "centos.7-x64": { } },
"frameworks": {
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Threading": "4.0.11-beta-23516",
"System.IO": "4.1.0-*",
"System.IO.FileSystem": "4.0.1-rc2-23811",
"System.Runtime.Serialization.Primitives": "4.1.1-*",
"System.Dynamic.Runtime": "4.0.11-*",
"System.Net.Security": "4.0.0-beta-23405",
"System.Net.NetworkInformation": "4.1.0-beta-23405",
"System.Text.RegularExpressions": "4.0.12-rc2-23811",
"Npgsql": "3.1.0-alpha6"
}
}
}
}
I have seen on the npgsql's Github that using https://www.myget.org/gallery/npgsql-unstable as a package source and using the unstable version of Npgsql fixes it but when I use "Npgsql": "3.1.0-unstable0458" the app doesn't compile and says that this version of npgsql is not compatible with DNXCore 5.0.
There must be something I'm doing wrong because other people don't seem to have issues with this any more.
Any ideas ?
I've had the same exception with npgsql version 3.1.0-alpha6
Unhandled Exception: System.MissingMethodException: Method not found: 'Void System.Net.Security.SslStream.AuthenticateAsClient(System.String, System.Security.Cryptography.X509Certificates.X509CertificateCollection, System.Security.Authentication.SslProtocols, Boolean)'.
However I was able to succeed with both versions: 3.1.0-unstable0458 and 3.1.0-unstable0478.
Currently I'm using Centos 7.2, Dotnet CLI version 1.0.0-rc2-002510. As a test application I've created a simple console app with one select to postgresql.
project.json looks like this:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-23811"
},
"Npgsql": "3.1.0-unstable0478"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
}
}
}
Because I use SSL connection to db I had to add few options to my connection string: SSL Mode=Require;Trust Server Certificate=True;Use SSL Stream=True.
Maybe you should try to use a newer version of dotnet cli. If you have any questions about my os/dotnet environment, please let me know.

VSCode - adding c# assembly reference

Getting the following error:
The name 'File' does not exist in the current context[dnxcore50]
Tried to build it in-case it was to do with VSCode alone. It threw me same error.
I have installed the dependency using the following:
dnu install System.IO
Gave me the following message
c:\Users\user1\Dropbox\dev\csharp\vscode\ads\WeightedGraphs>dnu
install System.IO
GET https://www.nuget.org/api/v2/FindPackagesById()?Id='System.IO'.
OK https://www.nuget.org/api/v2/FindPackagesById()?Id='System.IO' 679ms
System.IO.4.0.10-beta-22816 was added to project.json.
Restoring packages for c:\Users\user1\Dropbox\dev\csharp\vscode\ads\WeightedGraphs\project.json
Writing lock file c:\Users\user1\Dropbox\dev\csharp\vscode\ads\WeightedGraphs\project.lock.json
Restore complete, 302ms elapsed
project.json
{
"version": "1.0.0-*",
"dependencies": {
"System.IO": "4.0.10-beta-22816"
},
"commands": {
"run": "run"
},
"frameworks": {
"dnx451": {},
"dnxcore50": {
"dependencies": {
"System.Collections": "4.0.10-beta-22816",
"System.Linq": "4.0.0-beta-22816",
"System.Console": "4.0.0-beta-22816"
}
}
}
}
In.cs
using System.IO;
public class In{
public In(string s){
var filename = s;
var path = "";
if(File.Exists(path)){
//Code TO Be Added
}
}
}
You also need to add "System.IO.FileSystem" to your dependencies.