How to get the schema validator from a mongodb collection using node.js driver? - mongodb-nodejs-driver

Is there a ways to get the current schema validator from an existing collection in mongodb 5? I want to write schema migration script for an existing schema by retrieving the current schema validator, adding the changes and updating it with
db.command({
collMod: collectionName,
validator: schema,
});
Mongo shell (mongosh) provides db.getCollectionInfos({name: "<collection_name>"})[0].options.validator to get the schema validator. However, there seems to be nothing equivalent in node.js driver to get the validator. Not sure why but it looks like this is by design.

I found the following solution. Are there any other solutions?
const collection = await db.command({
listCollections:1.0,
filter: {name: '<collection_name>'}
});
let schema = collection.cursor.firstBatch[0].options.validator;

Related

Custom fields with a GraphQL query

Possibly exposing my ignorance of apollo-server but hoping someone can help: so ATM I have some schemas stitched together with #graphql-tools; all very simple, cool. I can make queries without problem.
There's a desire to add custom fields to given queries, so that we add extra data from other sources into the requested existing query template.
To explain by example: say the schema looks like this:
type User {
id
projectId
}
I'm trying to develop something so that the query getUserById($id...) can provide a template like so:
query userById($id: ID!) {
userById(id: $id) {
id
project {
id
name
# whatever other fields I want from Project type
}
}
}
And then apollo/graphql would then make a separate, asynchronous request to fetch the project for that given User.
As I understand graphql-tools, I can see resolvers allow the ability to make async requests for extra data ... but my problem is by defining project { within the query template, an error is thrown because - of course - project is not defined in the actual Schema itself.
Is there a way to filter and remove fields from a given query, somewhere in the chain of events? A custom apollo-server plugin perhaps? As I said I'm exposing my ignorance here but I've got a little lost in how apollo behaves in tandem with GraphQl.

Can this block of code of mine can be improved

[This is to populate data from two tables that one has two foreign keys from the same column as reference on the other table]
https://i.stack.imgur.com/D8fiv.png
[This is my schema for the table with the foreign key]
https://i.stack.imgur.com/eYDL0.png
This is written in laravel and it is working however i have an itchy feeling that this is wrong
As someone else has commented, you should put the code in your question. More context might also be necessary as it's not clear what you are trying to return from your view(). Are you returning $citizen, $family, or both? It would also be helpful to include what version of Laravel you are using. I'll assume you are using the latest Laravel 8.
Your code should work but you are making more work for yourself if you don't utilize Laravel's relationships. If you do, all the query stuff you have in your code can be reduced to just a few short lines of code such as this:
public function view(Citizen $citizen)
{
$citizen->load('family');
return $citizen;
}
Database Migration
You can shorten your migration by using foreignId(). See docs for details
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
Before:
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('citizens');
After:
$table->foreignId('client_id')->constrained();
Route Model Binding
I'm assuming your view() method is in your controller and is called from a route. Since the one parameter required is the client's id, you can use Route Model Binding and skip fetching the client from the database.
https://laravel.com/docs/8.x/routing#route-model-binding
public function view(Citizen $citizen)
{
// your code
}
Relationships
You seem to have relationships set up but you aren't using them to load data. You should be able to load the related objects without any manual queries.
If you have a belongsTo(One to Many) relationship on the Citizen model to the Family model, you can load it like this:
$citizen->load('family');
or just use it like this
$relationship = $citizen->family->relationship;
I don't know what the relationships between your models are but you should read up on the different relationship types in the docs.
https://laravel.com/docs/8.x/eloquent-relationships

Laravel validation uses eloquent or raw query

I have a question related to laravel validation, My question is when we apply rules like unique or exists, validator query through eloquent model or execute raw query? I am using Laravel 4.2.
I have found answer of my question. According to my investigation Validators run query through Query builder, and by default query builder uses default connection, If you want change the connection, you can through following code.
$verifier = \App::make('validation.presence');
$verifier->setConnection('other_connection_name');
$validation = $this->validator->make($data, static::$rules);
$validation->setPresenceVerifier($verifier);
if($validation->fails()) throw new ValidationException($validation->messages());

View change history entity for any date with hibernate

There is a problem - I have to watch the state of the table with entities for any date.
Please tell me how to implement this task via envers, or another solution is better and easier than envers
I use in my project spring-boot, spring-data and hibernate.
For this I tried to use Hibernate-envers.
My code looks like this, but it does not solved the problem:
List list = auditReader.createQuery()
.forRevisionsOfEntity(Entity.class, false, true)
.addOrder(AuditEntity.revisionNumber().desc())
.addProjection(AuditEntity.property("id").distinct())
.add(AuditEntity.revisionNumber().le(auditReader.getRevisionNumberForDate(new Date())))
.getResultList();
I found solution in question.
list = auditReader.createQuery()
.forRevisionsOfEntity(Entity.class, true, true)
.add(AuditEntity.revisionNumber().le(auditReader.getRevisionNumberForDate(date)))
.add(AuditEntity.revisionNumber().maximize().computeAggregationInInstanceContext())
.add(AuditEntity.revisionType().ne(RevisionType.DEL))
.getResultList();
This query returns a ready list of entities. Function 'computeAggregationInInstanceContext()' works according to the documentation and would like a GROUP BY statement:
Compute aggregated expression in the context of each entity instance separately.
Dont forget catch 'RevisionDoesNotExistException', in case you return empty list

EF Code Migrations vs DB Scheme MS SQL and Oracle

I am using Entity Framework Code Fist with migrations. I have a problem that migrations are bound with concrete DB schema. This is not such a problem with MS SQL. But in Oracle: schema = user. So my data model is bound with a DB User that can change.
When I change default schema with modelBuilder.HasDefaultSchema("SCHEME_NAME") I have to generate a new migration but I want to be able to deploy my app to any DB user in Oracle without having to change code and recopmile the project.
Well, you have multiple options for achieving this, such as:
Using automatic migrations and making the
modelBuilder.HasDefaultSchema(dbUserName) functions use an input
parameter. But this has multiple disadvantages such as not being
able to create migrations, instead every time it is automatically
created which has limits when deploying (not being able to create
scripts from it for deploy etc.)
You can implement a custom migration step which inherits from the CreateTableOperation class but as an input it does not take ("SCHEMA_NAME.TABLE_NAME" ...) but "TABLE_NAME" and dynamically gets the schema name when it is run (see one of my post about creating a custom migration operation to get the general idea)
Retrieving the user schema name and concatenating at migrations.
If you want the fastest solution I would choose the third option, which would simply look like this:
var schemaName = MigrationHelper.GetUserSpecificSchemaName();
CreateTable(String.Format("{0}.People", schemaName),
c => new
{
Id = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.Id);
Because don't forget that basically the code in these migrations run just like any other C# code, which is invoked through the Add-Migration PowerShell script method.
For implementing the GetUserSpecificSchemaName you can use ADO.NET that retrieves it from your Oracle database.

Resources