New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for querying JSON data info #2256
Comments
can you please explain what do you call JSON data here? |
If you store JSON data in specific JSON typed columns then you can filter and select on the nested JSON values. This article gives an overview of how to do it with Postgres. |
Exactly as @thldev said. I'm currently using |
I store various JSONB data in Postgres. It would be nice to use const user = await getRepository(User).findOne({
profile: {
id: profile.id,
provider: profile.provider,
},
}); Instead of: const user = await getRepository(User).createQueryBuilder()
.where('profile @> :profile', {
profile: {
id: profile.id,
provider: profile.provider,
},
}).getOne(); |
The same issue I encountered, Wish Supporting for querying JSON data info |
Further to supporting queries, other functionalities such as |
The main problem for me is that you should know the type of values when querying json data to properly pass them. type MyValueType = string | number | object | any[];
function queryByMyValue(myValue: MyValueType) {
...
.andWhere('myEntityAlias.myValue = :myValue', { myValue })
...
}
queryByMyValue(['foo','bar','baz']); Error:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I have the following entity structure: export interface MessengerSetup {
client: MessengerClient;
creatorId: string;
}
@Entity()
export class SignalEntity extends BaseEntity {
@PrimaryGeneratedColumn()
id!: number;
@Column('simple-json')
messengerSetup: MessengerSetup;
constructor(messengerSetup: MessengerSetup) {
super();
this.messengerSetup = messengerSetup;
}
} I wish I could do: async findEntities(client: MessengerClient, creatorId: string) {
const records = await SignalEntity.find({messengerSetup: {client, creatorId}});
// ...
} Since it does not work I am filtering on the client-side as a workaround which is bad since I need to query everything from the database in the first place: async findEntities(client: MessengerClient, creatorId: string) {
const records = await SignalEntity.find();
const filtered = records.filter(record => record.messengerSetup.client === client && record.messengerSetup.creatorId === creatorId);
// ...
} |
You can still do it in the DB query, you just have to use the raw SQL. I do something like: const res = await this.createQueryBuilder("app_instance")
.leftJoinAndSelect(
AppRegistry,
"app_registry",
"app_registry.appDefinitionAddress = app_instance.appDefinition",
)
.leftJoinAndSelect("app_instance.channel", "channel")
.where("app_registry.name = :name", { name: SimpleLinkedTransferAppName })
.andWhere(`app_instance."latestState"::JSONB @> '{ "paymentId": "${paymentId}" }'`)
.andWhere(
`app_instance."latestState"::JSONB #> '{"coinTransfers",0,"to"}' = '"${senderSignerAddress}"'`,
)
.getOne(); This works great, but I would much prefer a real syntax. |
it would be great to have the support for export class Product extends AbstractEntity {
@Column('jsonb')
body: string | any;
@OneToMany(() => ProductImage, productImage => productImage.product, {eager: true})
productImages: ProductImage[];
} so the ideal query would look like this(not possible now) public async findByUser(user: User, filter: IBCProductFilter): Promise<Product[]> {
return getRepository(Product)
.find({
where: {
user,
"body @> :body": {body: filter},
},
});
} instead it requires workaround with public async findByUser(user: User, filter: IBCProductFilter): Promise<Product[]> {
const productIds = await getRepository(Product)
.createQueryBuilder('p')
.select('p.id')
.leftJoin("p.user", "u")
.where("u.id = :userId", {userId: user.id})
.andWhere("body @> :body", {body: filter})
.getMany();
const ids = productIds.map(p => p.id);
return getRepository(Product).findByIds(ids);
} |
This comment has been minimized.
This comment has been minimized.
As said above - this can be done with the Still, a more explicitly defined way to access it would be cool. |
How to use this in SELECT, when I wan to access some property of json column/object?? |
We already support We can add additional json operators the same way (contributions are welcomed btw). Obviously we cannot support super complex stuff via Can one create a good proposal for the feature(-s) requested in this thread? |
Issue type:
[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[ ]
mongodb
[x]
mssql
[x]
mysql
/mariadb
[ ]
oracle
[x]
postgres
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
TypeORM version:
[x]
latest
[ ]
@next
[ ]
0.x.x
(or put your version here)It would be nice to be able to have a single API to be able to query information from a property in the JSON data stored in a
mysql
/mariadb
/postgres
database, as they all seem to have their own APIs.The text was updated successfully, but these errors were encountered: