I want to use two dto's for one query param.
#Query() query: CurrencyTypeDto|PaginationLimitDto
I know I can use inheritance. Maybe there is another way?
NestJs offers something called IntersectionType() to combine two types into one new type (dto), to Implement that you need to:
export class queryDto extends IntersectionType(
CurrencyTypeDto,
PaginationLimitDto,
) {}
then you can use it: #Query() query: queryDto
Ref : intersection
Related
I have a nested typeorm OneToOne field in my model, call it Entity.related_entity. It's exposed to the API through the use of Nest/Crud endpoint.
I want to use the CRUD filter to filter on the related_entity fields, but currently it's only possible via ?filter=related_entity.some_field||$eq||10. How to make it so that it's possible to filter like this ?filter=some_field||$eq||10?
I do not think there is a way to register an alias for a filter. Assuming you are using the #Crud decorator on your controller, I think your best option would be the filter attribute of the query option (documented here). It allows you to register a function to transform your query search condition. In your case, you could have something like the following. Note that I have not tested that code and it might not work "as is", it is simply meant to give an idea of what the solution could look like.
function findAndReplaceAliases(search: SCondition) {
search.$and.map((filter) =>
filter.keys().contains('some_field')
? { 'related_entity.some_field': filter['some_field'] }
: filter
);
}
#Crud({
query: {
filter: findAndReplaceAliases
}
})
#Controller()
SomeController {}
Working on nest.js with TypeORM/Postgres.
I've got a calculated_properties column on an entity, like so:
parent.entity.ts
#Entity()
export class Parent {
...
#Column('jsonb')
calculated_properties: CalculatedProperties;
}
calculated_properties.entity.ts
export class CalculatedProperties {
SomeCalc: number,
Other Calc: number,
NestedCalcs: NestedCalc,
With nestedCalc being some other similar types to calculated_properties.entity.ts.
The problem is, when I try to run the app, I get the following error message:
Entity "CalculatedProperties" does not have a primary column. Primary column is required to have in all your entities. Use #PrimaryColumn decorator to add a primary column to your entity.
But I've nowhere said this child type is an entity, and I don't want it to have a PrimaryColumn. In fact, setting an #PrimaryColumn() on it still shows the error - am I missing something obvious here?
For example, I have entities like Photo, Company, Car, etc. I know that they have columns with same names and I can access it via QueryBuilder. I don't know which repository I will process, but it's passed like string parameter to my function. Like this:
function haveAccess(user, entity, entityId) {
// model is string like 'photo', 'company', 'car'
}
In fact, I want to check if user have access to the entity with given ID via separate function not binded to just one Entity.
Is it possible to initialize Repository or QueryBuilder for Entity in TypeORM just by string somehow?
You can get repositories like this:
import {getRepository} from "typeorm";
import {User} from "./entity/User";
const userRepository = getRepository(User); // you can also get it via getConnection().getRepository() or getManager().getRepository()
connection.getRepository() can take three things, an Object (annotated with #Entity), an EntitySchema, or a string.
getRepository<Entity>(target: ObjectType<Entity> | EntitySchema<Entity> | string): Repository<Entity>;
So in typescript that could be:
connection.getRepository<MyEntity>(MyEntity)
I am using sequelize for postgres and need to tokenize across tables for chosen fields to tokenize and detokenize before storing and after retrieving respectively.
Any suggested best practices where I can do this at a single place rather than doing this # every model's create/update/ get hooks?
I am looking for something like an interceptor pattern that can be used for all models at one at the sequelize instance level. is that possible?
The feature you are looking for, sounds like getter & setter for properties of sequelize Model. You can declare model with custom getter & setter like following.
const YourModel = sequelize.define("ModelName", {
secretData: {
type : //Data type
set(value) {
// do what ever processing you want to applied, which will be stored in db
this.setDataValue("secretData", PROCESSED_VALUE)
}
get() {
const storedValue = this.getDataValue("secretData")
// do whatever processing you want to applied, on storedData
return processedData
}
}
}
I understand that Sequelize has a Model function for setting multiple assocations in a hasMany relationship, like user1.setFollowing(), as well as a function to add a single assocation, like user1.addFollowing(). But the latter does not let you pass an array (I receive an error) - is there any function to let you add multiple associations, like user1.addFollowings?
You should use add{ModelName} for add one instance and set{ModelName} for array of instances
Project.hasMany(Task)
Task.hasMany(Project)
project.addTask(task1).success(function() {})
but
project.setTasks([task1, task2]).success(function() {})
example