Skip to content

Commit

Permalink
docs: spelling and general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lang committed Oct 3, 2017
1 parent 7c5adbf commit 76d177a
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 95 deletions.
39 changes: 19 additions & 20 deletions docs/active-record-data-mapper.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# Active Record vs Data Mapper

* [What is Active Record?](#what-is-active-record)
* [What is Data Mapper?](#what-is-data-mapper)
* [What is the Active Record pattern?](#what-is-the-active-record-pattern)
* [What is the Data Mapper pattern?](#what-is-the-data-mapper-pattern)
* [Which one should I choose?](#which-one-should-i-choose)

## What is Active Record?
## What is the Active Record pattern?

In TypeORM you can use both Active Record and Data Mapper patterns.
In TypeORM you can use both, the Active Record and the Data Mapper patterns.

Using Active Record approach you define all your query methods inside model itself,
and you save, remove, load objects using model methods.
Using the Active Record approach, you define all your query methods inside the model itself, and you save, remove and load objects using model methods.

Simply said active record is an approach to access your database within your models.
You can read more about active record on [wikipedia](https://en.wikipedia.org/wiki/Active_record_pattern).
Simply said the Active Record pattern is an approach to access your database within your models.
You can read more about the Active Record pattern on [Wikipedia](https://en.wikipedia.org/wiki/Active_record_pattern).

Example:

```typescript
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm";

Expand All @@ -37,7 +36,7 @@ export class User extends BaseEntity {
}
```

All active-record entities must extend `BaseEntity` class which provides methods to work with entity.
All active-record entities must extend the `BaseEntity` class which provides methods to work with the entity.
Example how to work with such entity:

```typescript
Expand All @@ -59,9 +58,9 @@ const timber = await User.findOne({ firstName: "Timber", lastName: "Saw" });
```

`BaseEntity` has most of methods standard `Repository` has.
Most of times you don't need to use `Repository` or `EntityManager` with active record entities.
Most of the times you don't need to use `Repository` or `EntityManager` with active record entities.

Now lets say we want to create a function that returns users by first and last names.
Now let's say we want to create a function that returns users by first and last names.
We can create such function as a static method in a `User` class:

```typescript
Expand Down Expand Up @@ -98,19 +97,19 @@ And use it just like other methods:
const timber = await User.findByName("Timber", "Saw");
```

## What is Data Mapper?
## What is the Data Mapper pattern?

In TypeORM you can use both Active Record and Data Mapper patterns.

Using Data Mapper approach you define all your query methods separate classes called "repositories",
Using the Data Mapper, approach you define all your query methods separate classes called "repositories",
and you save, remove, load objects using repositories.
In data mapper your entities are very dumb - they just define their properties and may have some "dummy" methods.

Simply said data mapper is an approach to access your database within repositories instead of models.
You can read more about data mapper on [wikipedia](https://en.wikipedia.org/wiki/Data_mapper_pattern).
You can read more about data mapper on [Wikipedia](https://en.wikipedia.org/wiki/Data_mapper_pattern).

Example:

```typescript
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm";

Expand Down Expand Up @@ -152,7 +151,7 @@ const newUsers = await userRepository.find({ isActive: true });
const timber = await userRepository.findOne({ firstName: "Timber", lastName: "Saw" });
```

Now lets say we want to create a function that returns users by first and last names.
Now let's say we want to create a function that returns users by first and last names.
We can create such function in a "custom repository".

```typescript
Expand Down Expand Up @@ -183,10 +182,10 @@ For more information about custom repositories refer [this documentation](workin

## Which one should I choose?

Decision is up to you.
The decision is up to you.
Both strategies have their own cons and pros.

One thing we should always keep in mind in software development is how we are going to maintain it.
`Data Mapper` approach helps you to keep maintainability of your software in a bigger apps more effective.
`Active record` approach helps you to keep things simple which work good in a small apps.
The `Data Mapper` approach helps you to keep maintainability of your software which is more effective in bigger apps.
The `Active record` approach helps you to keep things simple which work good in a small apps.
And simplicity is always a key to better maintainability.
26 changes: 13 additions & 13 deletions docs/caching.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Caching queries

You can cache results selected by a `QueryBuilder` methods: `getMany`, `getOne`, `getRawMany`, `getRawOne` and `getCount`.
To enable caching you need to explicitly enable it in connection options:
You can cache results selected by these `QueryBuilder` methods: `getMany`, `getOne`, `getRawMany`, `getRawOne` and `getCount`.
To enable caching you need to explicitly enable it in your connection options:

```typescript
{
Expand All @@ -14,7 +14,7 @@ To enable caching you need to explicitly enable it in connection options:
```

When you enable cache for the first time,
you must synchronize your database schema (using cli, migrations or simply option in connection).
you must synchronize your database schema (using CLI, migrations or the `synchronize` connection option).

Then in `QueryBuilder` you can enable query cache for any query:

Expand All @@ -26,12 +26,12 @@ const users = await connection
.getMany();
```

This will execute query to fetch all admin users and cache its result.
Next time when you execute same code it will get admin users from cache.
This will execute a query to fetch all admin users and cache its result.
Next time when you execute same code it will get admin users from the cache.
Default cache time is equal to `1000 ms`, e.g. 1 second.
It means cache will be invalid in 1 second after you first time call query builder code.
In practice it means if users open user page 150 times within 3 seconds only three queries will be executed during this period.
All other inserted users during 1 second of caching won't be returned to user.
This means cache will be invalid 1 second after you called the query builder code.
In practice, it means that if users open user page 150 times within 3 seconds only three queries will be executed during this period.
All users instered during the 1 second of caching won't be returned to the user.

You can change cache time manually:

Expand All @@ -57,7 +57,7 @@ Or globally in connection options:
}
```

Also you can set a "cache id":
Also, you can set a "cache id":

```typescript
const users = await connection
Expand All @@ -68,16 +68,16 @@ const users = await connection
```

It will allow you to granular control your cache,
for example clear cached results when you insert a new user:
for example, clear cached results when you insert a new user:

```typescript
await connection.queryResultCache.remove(["users_admins"]);
```


By default, TypeORM uses separate table called `query-result-cache` and stores all queries and results there.
By default, TypeORM uses a separate table called `query-result-cache` and stores all queries and results there.
If storing cache in a single database table is not effective for you,
you can change cache type to "redis" and TypeORM will store all cache records in redis instead.
you can change the cache type to "redis" and TypeORM will store all cached records in redis instead.
Example:

```typescript
Expand All @@ -98,4 +98,4 @@ Example:

"options" are [redis specific options](https://github.com/NodeRedis/node_redis#options-object-properties).

You can use `typeorm cache:clear` command to clear everything stored in cache.
You can use `typeorm cache:clear` to clear everything stored in the cache.
58 changes: 30 additions & 28 deletions docs/connection-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const connection = await createConnections([{
}]);
```

* `getConnectionManager()` - Gets connection manager which stores all created (using `createConnection` method) connections.
* `getConnectionManager()` - Gets connection manager which stores all created (using `createConnection()` or `createConnections()`) connections.

```typescript
import {getConnectionManager} from "typeorm";
Expand Down Expand Up @@ -79,7 +79,7 @@ const secondaryManager = getEntityManager("secondary-connection");
// you can use secondary connection manager methods
```

* `getRepository()` - Gets some entity's `Repository` from connection.
* `getRepository()` - Gets `Repository` for given entity from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.

```typescript
Expand All @@ -92,7 +92,7 @@ const blogRepository = getRepository(Blog, "secondary-connection");
// you can use secondary connection repository methods
```

* `getTreeRepository()` - Gets some entity's `TreeRepository` from connection.
* `getTreeRepository()` - Gets `TreeRepository` for given entity from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.

```typescript
Expand All @@ -105,7 +105,7 @@ const blogRepository = getTreeRepository(Blog, "secondary-connection");
// you can use secondary connection repository methods
```

* `getMongoRepository()` - Gets some entity's `MongoRepository` from connection.
* `getMongoRepository()` - Gets `MongoRepository` for given entity from connection.
Connection name can be specified to indicate what connection's entity manager should be taken.

```typescript
Expand All @@ -120,22 +120,23 @@ const blogRepository = getMongoRepository(Blog, "secondary-connection");

## `Connection` API

* `name` - Connection name. If you created nameless connection then its equal to "default".
* `name` - Connection name. If you created nameless connection then it's equal to "default".
You use this name when you work with multiple connections and call `getConnection(connectionName: string)`

```typescript
const connectionName: string = connection.name;
```

* `options` - Connection options used to create this connection.
For more information about connection options see [Connection Options](./connection-options.md) documentation.
Learn more about [Connection Options](./connection-options.md).

```typescript
const connectionOptions: ConnectionOptions = connection.options;
// you can cast connectionOptions to MysqlConnectionOptions or any other xxxConnectionOptions depend on database driver you use
// you can cast connectionOptions to MysqlConnectionOptions
// or any other xxxConnectionOptions depending on the database driver you use
```

* `isConnected` - Indicates if real connection to the database is established.
* `isConnected` - Indicates if a real connection to the database is established.

```typescript
const isConnected: boolean = connection.isConnected;
Expand All @@ -145,11 +146,12 @@ const isConnected: boolean = connection.isConnected;

```typescript
const driver: Driver = connection.driver;
// you can cast connectionOptions to MysqlDriver or any other xxxDriver depend on database driver you use
// you can cast connectionOptions to MysqlDriver
// or any other xxxDriver depending on the database driver you use
```

* `manager` - `EntityManager` used to work with connection entities.
For more information about EntityManager see [Entity Manager and Repository](working-with-entity-manager.md) documentation.
Learn more about [Entity Manager and Repository](working-with-entity-manager.md).

```typescript
const manager: EntityManager = connection.manager;
Expand All @@ -167,21 +169,21 @@ const user = await manager.findOneById(1);
```

* `connect` - Performs connection to the database.
When you use `createConnection` method it automatically calls this method and you usually don't need to call it by yourself.
When you use `createConnection` it automatically calls `connect` and you don't need to call it yourself.

```typescript
await connection.connect();
```

* `close` - Closes connection with the database.
Usually you call this method when your application is shutdown.
Usually, you call this method when your application is shutting down.

```typescript
await connection.close();
```

* `synchronize` - Synchronizes database schema. When `synchronize: true` is set in connection options it calls exactly this method.
Usually you call this method when your application is shutdown.
* `synchronize` - Synchronizes database schema. When `synchronize: true` is set in connection options it calls this method.
Usually, you call this method when your application is shuting down.

```typescript
await connection.synchronize();
Expand All @@ -208,7 +210,7 @@ await connection.undoLastMigration();
```

* `hasMetadata` - Checks if metadata for a given entity is registered.
Learn more about metadata in [Entity Metadata](./entity-metadata.md) documentation.
Learn more about [Entity Metadata](./entity-metadata.md).

```typescript
if (connection.hasMetadata(User))
Expand All @@ -217,7 +219,7 @@ if (connection.hasMetadata(User))

* `getMetadata` - Gets `EntityMetadata` of the given entity.
You can also specify a table name and if entity metadata with such table name is found it will be returned.
Learn more about metadata in [Entity Metadata](./entity-metadata.md) documentation.
Learn more about [Entity Metadata](./entity-metadata.md).

```typescript
const userMetadata = connection.getMetadata(User);
Expand All @@ -226,7 +228,7 @@ const userMetadata = connection.getMetadata(User);

* `getRepository` - Gets `Repository` of the given entity.
You can also specify a table name and if repository for given table is found it will be returned.
Learn more about repositories in [Repository](working-with-entity-manager.md) documentation.
Learn more about [Repositories](working-with-entity-manager.md).

```typescript
const repository = connection.getRepository(User);
Expand All @@ -236,17 +238,17 @@ const users = await repository.findOneById(1);

* `getTreeRepository` - Gets `TreeRepository` of the given entity.
You can also specify a table name and if repository for given table is found it will be returned.
Learn more about repositories in [Repository](working-with-entity-manager.md) documentation.
Learn more about [Repositories](working-with-entity-manager.md).

```typescript
const repository = connection.getTreeRepository(Category);
// now you can call tree repository methods, for example findTrees:
const categories = await repository.findTrees();
```

* `getMongoRepository` - Gets `getMongoRepository` of the given entity.
* `getMongoRepository` - Gets `MongoRepository` of the given entity.
This repository is used for entities in MongoDB connection.
Learn more about mongodb support refer to [MongoDB documentation](./mongodb.md).
Learn more about [MongoDB support](./mongodb.md).

```typescript
const repository = connection.getMongoRepository(User);
Expand All @@ -257,7 +259,7 @@ const category2 = await categoryCursor.next();
```

* `getCustomRepository` - Gets customly defined repository.
Learn more about custom repositories in [Repository](working-with-entity-manager.md) documentation.
Learn more about [custom repositories](working-with-entity-manager.md).

```typescript
const userRepository = connection.getCustomRepository(UserRepository);
Expand All @@ -266,7 +268,7 @@ const crazyUsers = await userRepository.findCrazyUsers();
```

* `transaction` - Provides a single transaction where multiple database requests will be executed in a single database transaction.
Learn more about transactions in [Transactions](./transactions.md) documentation.
Learn more about [Transactions](./transactions.md).

```typescript
await connection.transaction(async manager => {
Expand All @@ -282,8 +284,8 @@ await connection.transaction(async manager => {
const rawData = await connection.query(`SELECT * FROM USERS`);
```

* `createQueryBuilder` - Creates a query builder use to build SQL queries.
Learn more about query builder in [QueryBuilder](select-query-builder.md) documentation.
* `createQueryBuilder` - Creates a query builder, which can be used to build queries.
Learn more about [QueryBuilder](select-query-builder.md).

```typescript
const users = await connection.createQueryBuilder()
Expand All @@ -293,13 +295,13 @@ const users = await connection.createQueryBuilder()
.getMany();
```

* `createQueryRunner` - Creates a query runner used to work with a single real database connection, manage it and work with it.
Learn more about query runners in [QueryRunner](./query-runner.md) documentation.
* `createQueryRunner` - Creates a query runner used manage and work with a single real database connection.
Learn more about [QueryRunner](./query-runner.md).

```typescript
const queryRunner = connection.createQueryRunner();

// you can use it methods only after you call connect method
// you can use its methods only after you call connect
// which performs real database connection
await queryRunner.connect();

Expand Down Expand Up @@ -331,7 +333,7 @@ const defaultConnection = connectionManager.get("default");
const secondaryConnection = connectionManager.get("secondary");
```

* `has` - Checks if connection is registered in the given connection manager.
* `has` - Checks if a connection is registered in the given connection manager.

```typescript
if (connectionManager.has("default")) {
Expand Down
Loading

0 comments on commit 76d177a

Please sign in to comment.