Setting PRAGMA CIPHER on db with sequelize.js - javascript

i see that sequelize is compatible with sqlite's sqlcipher extension (docs | merged pr). the code suggests a way to authenticate on the db instance, but i can't find a way to set the cipher.
using sqlite3, can run something like this:
var sqlite = require('sqlite3);
var db = new sqlite.Database('test.db');
db.serialize(
// some sql commands
);
with the sequelize orm for sqlite i can get a db created, but can't get an encrypted instance:
var Sequelize = require('sequelize');
var db = new Sequelize('test.db', 'username', 'secretAsPragmaKey', {option1: true, option2: false});
db.query(
// some sql
);
i've tried running queries on this instance to explicitly set PRAGMA KEY to the password param, and PRAGMA CIPHER to sqlcipher's default [aes-256-cfb], but not while using sequelize.
does the encrypted db need to exist, and sequelize is just opening it? if so, how to ensure the keyed db exists without inserting tables?

Related

PostgreSQL query to Supabase Query

How can i make once of this PostgreSQL querys in supabase?
I tried reading supabase doc but it doesn't work
select rooms.id as room_id, members.id as member_id, user_id from rooms
inner join members
on rooms.id = members.room_id
where rooms.id = members.room_id
and user_id = 1
Or
select room_id, user_id, members.id from members
inner join rooms
on rooms.id = members.room_id
where members.user_id = 1
No, there's not really any performance difference from calling an .rpc() vs. just calling the api directly. You should be fine. Just make sure to set up your database correctly (indexes, joins, etc.) for best performance.
Hello this might not be supabase specific solution, but in my case what I do use when I need to run custom queries like in your own case here is to use the pg package from npm https://www.npmjs.com/package/pg.
I would just create a client and parse my supabase db credentials to it.
export const pgClient = () => {
console.error(`using Postgres ${process.env.host}`)
const client = new Client({
user: process.env.user,
host: process.env.host,
database: process.env.database,
password: process.env.password,
port: parseInt(process.env.port!)
})
return client
}
export it to my router and simply connect
const client = pgClient()
await client.connect()
Run my custom query directly on the DB
await client.query("SELECT * from auth.users")
You can query a foreign table using the Supabase client, but only if there's a foreign key set up:
Supabase select(): Query foreign tables
As stated in the comments, you can create a view for this purpose:
Create View
Or you can create a PosgreSQL function and call that with Supabase .rpc():

How to change PrismaClient database connection at runtime?

I have .env file like
DATABASE_URL="sqlserver://srv:50119;initial catalog=mydb;user=aaa;password=bbb;"
and then schema.prisma like
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["microsoftSqlServer"]
}
I generate a client using:
npx prisma generate
and then Prisma works great in my express app using:
const prisma = new PrismaClient();
Say I wanted to use a different db for user for multi-tenancy, how can I achieve this? Ideally I'd want to switch the db connection at runtime but it seems that DATABASE_URL is only read during prisma generate and not at runtime so the generated client ends up with a hardcoded db url.
You can use the datasource property to create a new PrismaClient instance and pass a dynamic URL.
datasources
Programmatically overrides properties of the datasource block in the schema.prisma file - for example, as part of
an integration test. See also: Data sources

NodeJS and Mysql connection configuration ignored

I have a weird behavior while I'm trying to query my MySQL database from a nodeJS API.
I define a connection pool to mysql on node using the following code
const mysql = require('mysql2')
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: 'mydb.user',
database: process.env.DB_DB,
password: process.env.DB_PWD,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
multipleStatements: true
}).promise()
Before that, I was using another user named mydb.owner defined in a .env file.
When I execute a query, I have the following error
Access denied for user 'mydb.owner'#'localhost' to database 'mydb'
That's not the user I've configured, that's the old one.
If I have a look on the Mysql connections, I can see that the user of the pool is correct:
show processlist;
Returns
Id User Host db
6 root localhost:37752 mydb
9 mydb.user localhost:38102 mydb
It seems I haven't any environment variable defined from elsewhere:
echo $DB_USER
Returns nothing.
The user seems to have the necessary rights :
SHOW GRANTS FOR 'mydb.user'#'localhost';
GRANT USAGE ON *.* TO 'mydb.user'#'localhost'
GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON `mydb`.* TO 'mydb.user'#'localhost' WITH GRANT OPTION
I don't understand why mysql2 returns me an error about my old user mydb.owner.
The query was a stored procedure and was created by default with the tag
CREATE definer = 'mydb.owner'#'localhost' PROCEDURE ...
No matter which user execute the stored procedure, it was impersonated with the mydb.owner account.
To specify that the procedure must be executed under the current account, I added the following instructions:
CREATE definer = 'mydb.owner'#'localhost' PROCEDURE ...()
SQL SECURITY INVOKER
BEGIN ...

How to start a new connection with CosmoDB graph database using gremlin on version ^3

I am trying to create a new gremlin client in node js, but I cannot find any documentation how to set up the connection with both a URL and a primary key (as generated in Azure CosmosDB).
Examples are available how to do this in versions < v3, such as here.
Documentation on the new version of gremlin is available on the new documentation, but it does not explain how to put the primary key into the objects (the package is not very clear either, I've tried to populate "cert" and "pfx" to no avail).
Does anyone know how I can connect to my azure CosmosDB gremlin API with node's gremlin package v^3.0.0?
Try adding an account key to the request body. I'm guessing by the properties of the connection string.
"AccountKey"= "YourReallyLongKeyHereYourReallyLongKeyHereYourReallyLongKeyHere"
Edit
After further research you might need to add an authorization header based on this documentation.
type={typeoftoken}&ver={tokenversion}&sig={hashsignature}
Example:
type=master&ver=1.0&sig=5mDuQBYA0kb70WDJoTUzSBMTG3owkC0/cEN4fqa18/s=
I have used the latest gremlin lib to connect to cosmos db. Here is my code:
const authenticator = new Gremlin.driver.auth.PlainTextSaslAuthenticator(
config.user,
config.password
);
const endpoint = `wss://${config.host}:${config.port}/gremlin`;
const client = new Gremlin.driver.Client(endpoint, {
authenticator,
mimeType: 'application/vnd.gremlin-v2.0+json',
rejectUnauthorized: true,
traversalsource: 'g',
});
Then you can use the following for submitting a command to the server which returns a promise:
query = 'g.V().count()';
client.submit(query).then(successfn,errorfn);
The config used is of the following format:
{
"host": "<cosmosdbname>.gremlin.cosmosdb.azure.com",
"password": "<secret-key>",
"port": 443,
"user": "/dbs/<dbname>/colls/<collectionName>",
}

Mongojs: findOne() doesn't work

I was trying to use findOne method. But it didn't show anything.It looks like it didn't execute. Would you like to help me solve this problem?
var mongojs = require('mongojs');
var databaseUrl = "mongodb:local:27017/mydb";
var db = mongojs(databaseUrl, ["profiles"]);
var password;
db.profiles.findOne({"userId": "liu1234"}, function(err, doc) {
if (err) throw err;
else console.log(doc);
});
The format of databaseUrl is incorrect. The mongodb driver is unable to find your database.
Try:
var databaseUrl = "mongodb://localhost:27017/mydb";
The first part, mongodb://, refers to the protocol that mongodb uses to interact with the database. The next part, localhost , is a hostname that points to your machine. :27017 refers to the default port that mongodb communicates over. And, obviously, /mydb refers to your database.
If you're using a default configuration, you don't even need to specify the protocol, the host, or the port. Mongojs assumes the defaults if you don't enter them, so you can use this instead:
var databaseUrl = "mydb";
For more information check out: https://github.com/mafintosh/mongojs

Categories

Resources