accel-record
Advanced tools
Comparing version 0.3.0 to 0.3.1
{ | ||
"name": "accel-record", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "", | ||
@@ -34,6 +34,6 @@ "type": "module", | ||
"dependencies": { | ||
"accel-record-core": "^0.3.0", | ||
"accel-record-factory": "^0.3.0", | ||
"prisma-generator-accel-record": "^0.3.0" | ||
"accel-record-core": "^0.3.1", | ||
"accel-record-factory": "^0.3.1", | ||
"prisma-generator-accel-record": "^0.3.1" | ||
} | ||
} |
@@ -633,1 +633,83 @@ # Accel Record | ||
``` | ||
## Query Interface | ||
### Model Query Interface | ||
Here are some examples of using the interface to perform queries on models. | ||
Each method allows you to query the model in a type-safe manner using information generated from the model definition. | ||
You can also take advantage of IDE autocompletion. | ||
For more details, refer to the list of methods in the Relation class. | ||
```ts | ||
import { User } from "./models/index.js"; | ||
User.where({ | ||
name: "John", | ||
age: { ">=": 18 }, | ||
email: { endsWith: "@example.com" }, | ||
}) | ||
.order("createdAt", "desc") | ||
.includes("posts", "setting") | ||
.offset(10) | ||
.limit(10); | ||
User.where({ name: ["John", "Alice"] }).exists(); | ||
User.joins("profile").where("Profile.name = ?", "John").count(); | ||
User.first()?.posts.destroyAll(); | ||
``` | ||
The model query interface does not currently support features like GROUP BY. | ||
This is because these queries have limited benefits from using schema type information. | ||
If you need to execute queries that cannot be achieved with the model query interface, please use raw SQL or the Knex QueryBuilder explained below. | ||
### Executing Raw SQL Queries | ||
You can use the `Model.connection.execute()` method to execute raw SQL queries and synchronously retrieve the results. | ||
```ts | ||
import { Model } from "accel-record"; | ||
const rows = Model.connection.execute( | ||
`select firstName, count(id) as cnt | ||
from User | ||
group by firstName`, | ||
[] | ||
); | ||
console.log(rows); | ||
// => [{ firstName: "John", cnt: 1 }, { firstName: "Alice", cnt: 2 }] | ||
``` | ||
### Executing Queries with Knex QueryBuilder | ||
You can use Knex to build and execute queries. | ||
We have added an `execute()` method to Knex's QueryBuilder, which allows you to execute queries synchronously. | ||
For more details on the functionality, refer to the following link: | ||
[Knex Query Builder | Knex.js](https://knexjs.org/guide/query-builder.html) | ||
```ts | ||
import { Model } from "accel-record"; | ||
import { User } from "./models/index.js"; | ||
// You can obtain an instance of Knex with Model.connection.knex. | ||
const knex = Model.connection.knex; | ||
const rows = knex | ||
.select("name", knex.raw("SUM(score) as total")) | ||
.from("Score") | ||
.groupBy("name") | ||
.execute(); | ||
console.log(rows); | ||
// => [{ name: "John", total: "1" }, { name: "Alice", total: "2" }] | ||
// You can perform queries on the corresponding tables for each model using the queryBuilder property. | ||
const rows = User.queryBuilder.select("name").groupBy("name").execute(); | ||
console.log(rows); // => [{ name: "John" }, { name: "Alice" }] | ||
``` |
18556
715