Skip to content

Commit

Permalink
refactoring persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
Umed Khudoiberdiev committed Oct 31, 2017
1 parent 731775c commit b0c37d6
Show file tree
Hide file tree
Showing 103 changed files with 625 additions and 461 deletions.
6 changes: 3 additions & 3 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ createConnection(/*...*/).then(async connection => {
let allPhotos = await photoRepository.find();
console.log("All photos from the db: ", allPhotos);

let firstPhoto = await photoRepository.findOneById(1);
let firstPhoto = await photoRepository.findOne(1);
console.log("First photo from the db: ", firstPhoto);

let meAndBearsPhoto = await photoRepository.findOne({ name: "Me and Bears" });
Expand Down Expand Up @@ -462,7 +462,7 @@ import {Photo} from "./entity/Photo";
createConnection(/*...*/).then(async connection => {

/*...*/
let photoToUpdate = await photoRepository.findOneById(1);
let photoToUpdate = await photoRepository.findOne(1);
photoToUpdate.name = "Me, my friends and polar bears";
await photoRepository.save(photoToUpdate);

Expand All @@ -483,7 +483,7 @@ import {Photo} from "./entity/Photo";
createConnection(/*...*/).then(async connection => {

/*...*/
let photoToRemove = await photoRepository.findOneById(1);
let photoToRemove = await photoRepository.findOne(1);
await photoRepository.remove(photoToRemove);

}).catch(error => console.log(error));
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ user.age = 25;
await repository.save(user);

const allUsers = await repository.find();
const firstUser = await repository.findOneById(1);
const firstUser = await repository.findOne(1);
const timber = await repository.findOne({ firstName: "Timber", lastName: "Saw" });

await repository.remove(timber);
Expand Down Expand Up @@ -149,7 +149,7 @@ user.age = 25;
await user.save();

const allUsers = await User.find();
const firstUser = await User.findOneById(1);
const firstUser = await User.findOne(1);
const timber = await User.findOne({ firstName: "Timber", lastName: "Saw" });

await timber.remove();
Expand Down Expand Up @@ -711,7 +711,7 @@ createConnection(/*...*/).then(async connection => {
let allPhotos = await photoRepository.find();
console.log("All photos from the db: ", allPhotos);

let firstPhoto = await photoRepository.findOneById(1);
let firstPhoto = await photoRepository.findOne(1);
console.log("First photo from the db: ", firstPhoto);

let meAndBearsPhoto = await photoRepository.findOne({ name: "Me and Bears" });
Expand Down Expand Up @@ -741,7 +741,7 @@ import {Photo} from "./entity/Photo";
createConnection(/*...*/).then(async connection => {

/*...*/
let photoToUpdate = await photoRepository.findOneById(1);
let photoToUpdate = await photoRepository.findOne(1);
photoToUpdate.name = "Me, my friends and polar bears";
await photoRepository.save(photoToUpdate);

Expand All @@ -761,7 +761,7 @@ import {Photo} from "./entity/Photo";
createConnection(/*...*/).then(async connection => {

/*...*/
let photoToRemove = await photoRepository.findOneById(1);
let photoToRemove = await photoRepository.findOne(1);
await photoRepository.remove(photoToRemove);

}).catch(error => console.log(error));
Expand Down Expand Up @@ -1186,7 +1186,7 @@ await connection.manager.save(photo);
// now lets load them:
const loadedPhoto = await connection
.getRepository(Photo)
.findOneById(1, { relations: ["albums"] });
.findOne(1, { relations: ["albums"] });
```

`loadedPhoto` will be equal to:
Expand Down
6 changes: 3 additions & 3 deletions docs/connection-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Learn more about [Entity Manager and Repository](working-with-entity-manager.md)
```typescript
const manager: EntityManager = connection.manager;
// you can call manager methods, for example find:
const user = await manager.findOneById(1);
const user = await manager.findOne(1);
```

* `mongoManager` - `MongoEntityManager` used to work with connection entities in mongodb connections.
Expand All @@ -165,7 +165,7 @@ For more information about MongoEntityManager see [MongoDB](./mongodb.md) docume
```typescript
const manager: MongoEntityManager = connection.mongoManager;
// you can call manager or mongodb-manager specific methods, for example find:
const user = await manager.findOneById(1);
const user = await manager.findOne(1);
```

* `connect` - Performs connection to the database.
Expand Down Expand Up @@ -233,7 +233,7 @@ Learn more about [Repositories](working-with-entity-manager.md).
```typescript
const repository = connection.getRepository(User);
// now you can call repository methods, for example find:
const users = await repository.findOneById(1);
const users = await repository.findOne(1);
```

* `getTreeRepository` - Gets `TreeRepository` of the given entity.
Expand Down
2 changes: 1 addition & 1 deletion docs/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class UserController {

@Get("/users/:id")
getAll(@Param("id") userId: number) {
return getRepository(User).findOneById(User);
return getRepository(User).findOne(userId);
}

}
Expand Down
2 changes: 1 addition & 1 deletion docs/eager-and-lazy-relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ await connection.manager.save(question);
Example how to load objects inside lazy relations:

```typescript
const question = await connection.getRepository(Question).findOneById(1);
const question = await connection.getRepository(Question).findOne(1);
const answers = await question.answers;
// you'll have all question's answers inside "answers" variable now
```
Expand Down
10 changes: 5 additions & 5 deletions docs/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ When you save entities using `save` it always tries to find a entity in the data
If id/ids are found then it will update this row in the database.
If there is no row with the id/ids, a new row will be inserted.

To find a entity by id you can use `manager.findOneById` or `repository.findOneById`. Example:
To find a entity by id you can use `manager.findOne` or `repository.findOne`. Example:

```typescript
// find one by id with single primary key
const person = await connection.manager.findOneById(Person, 1);
const person = await connection.getRepository(Person).findOneById(1);
const person = await connection.manager.findOne(Person, 1);
const person = await connection.getRepository(Person).findOne(1);

// find one by id with composite primary keys
const user = await connection.manager.findOneById(User, { firstName: "Umed", lastName: "Khudoiberdiev" });
const user = await connection.getRepository(User).findOneById({ firstName: "Umed", lastName: "Khudoiberdiev" });
const user = await connection.manager.findOne(User, { firstName: "Timber", lastName: "Saw" });
const user = await connection.getRepository(User).findOne({ firstName: "Timber", lastName: "Saw" });
```

### Special columns
Expand Down
9 changes: 2 additions & 7 deletions docs/entity-manager-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,11 @@ const [timbers, timbersCount] = await manager.findAndCount(User, { firstName: "T
const users = await manager.findByIds(User, [1, 2, 3]);
```

* `findOne` - Finds first entity that matches given find options.

```typescript
const timber = await manager.findOne(User, { firstName: "Timber" });
```

* `findOneById` - Finds entity with given id.
* `findOne` - Finds first entity that matches given id or find options.

```typescript
const user = await manager.findOne(User, 1);
const timber = await manager.findOne(User, { firstName: "Timber" });
```

* `clear` - Clears all the data from the given table (truncates/drops it).
Expand Down
4 changes: 2 additions & 2 deletions docs/example-with-express.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ createConnection().then(connection => {
});

app.get("/users/:id", async function(req: Request, res: Response) {
return userRepository.findOneById(req.params.id);
return userRepository.findOne(req.params.id);
});

app.post("/users", async function(req: Request, res: Response) {
Expand All @@ -222,7 +222,7 @@ createConnection().then(connection => {
});

app.delete("/users/:id", async function(req: Request, res: Response) {
return userRepository.removeById(req.params.id);
return userRepository.remove(req.params.id);
});

// start express server
Expand Down
4 changes: 2 additions & 2 deletions docs/relational-query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ This code is equivalent to doing this:
import {getManager} from "typeorm";

const postRepository = getRepository(Post);
const post = await postRepository.findOneById(1, { relations: ["categories"] });
const post = await postRepository.findOne(1, { relations: ["categories"] });
post.categories.push(category);
await postRepository.save(post);
```
Expand Down Expand Up @@ -110,7 +110,7 @@ To load those relations you can use following code:
```typescript
import {getConnection} from "typeorm";

const post = await getConnection().manager.findOneById(Post, 1);
const post = await getConnection().manager.findOne(Post, 1);

post.categories = await getConnection()
.createQueryBuilder()
Expand Down
9 changes: 2 additions & 7 deletions docs/repository-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,11 @@ const [timbers, timbersCount] = await repository.findAndCount({ firstName: "Timb
const users = await repository.findByIds([1, 2, 3]);
```

* `findOne` - Finds first entity that matches given find options.

```typescript
const timber = await repository.findOne({ firstName: "Timber" });
```

* `findOneById` - Finds entity with given id.
* `findOne` - Finds first entity that matches given id or find options.

```typescript
const user = await repository.findOne(1);
const timber = await repository.findOne({ firstName: "Timber" });
```

* `query` - Executes a raw SQL query.
Expand Down
3 changes: 2 additions & 1 deletion docs/working-with-entity-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ Example how to use it:

```typescript
import {getManager} from "typeorm";
import {User} from "./entity/User";

const entityManager = getManager(); // you can also get it via getConnection().manager
const user = await entityManager.findOneById(1);
const user = await entityManager.findOne(User, 1);
user.name = "Umed";
await entityManager.save(user);
```
2 changes: 1 addition & 1 deletion docs/working-with-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {getRepository} from "typeorm";
import {User} from "./entity/User";

const userRepository = getRepository(User); // you can also get it via getConnection().getRepository() or getManager().getRepository()
const user = await userRepository.findOneById(1);
const user = await userRepository.findOne(1);
user.name = "Umed";
await userRepository.save(user);
```
Expand Down
2 changes: 1 addition & 1 deletion sample/sample11-all-types-entity/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ createConnection(options).then(connection => {
return postRepository.save(entity);
})
.then(entity => {
return postRepository.findOneById(entity.id);
return postRepository.findOne(entity.id);
})
.then(entity => {
console.log("Entity is loaded: ", entity);
Expand Down
4 changes: 2 additions & 2 deletions sample/sample13-everywhere-abstraction/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ createConnection(options).then(connection => {
.save(post)
.then(post => {
console.log("Post has been saved");
return postRepository.findOneById(post.id);
return postRepository.findOne(post.id);
})
.then(loadedPost => {
console.log("post is loaded: ", loadedPost);
return blogRepository.save(blog);
})
.then(blog => {
console.log("Blog has been saved");
return blogRepository.findOneById(blog.id);
return blogRepository.findOne(blog.id);
})
.then(loadedBlog => {
console.log("blog is loaded: ", loadedBlog);
Expand Down
8 changes: 4 additions & 4 deletions sample/sample25-insert-from-inverse-side/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ createConnection(options).then(connection => {
let postRepository = connection.getRepository(Post);
let authorRepository = connection.getRepository(Author);

const authorPromise = authorRepository.findOneById(1).then(author => {
const authorPromise = authorRepository.findOne(1).then(author => {
if (!author) {
author = new Author();
author.name = "Umed";
return authorRepository.save(author).then(savedAuthor => {
return authorRepository.findOneById(1);
return authorRepository.findOne(1);
});
}
return author;
});

const postPromise = postRepository.findOneById(1).then(post => {
const postPromise = postRepository.findOne(1).then(post => {
if (!post) {
post = new Post();
post.title = "Hello post";
post.text = "This is post contents";
return postRepository.save(post).then(savedPost => {
return postRepository.findOneById(1);
return postRepository.findOne(1);
});
}
return post;
Expand Down
2 changes: 1 addition & 1 deletion sample/sample26-embedded-tables/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ createConnection(options).then(connection => {
console.log("question has been saved: ", savedQuestion);

// lets load it now:
return questionRepository.findOneById(savedQuestion.id);
return questionRepository.findOne(savedQuestion.id);
})
.then(loadedQuestion => {
console.log("question has been loaded: ", loadedQuestion);
Expand Down
2 changes: 1 addition & 1 deletion sample/sample27-composite-primary-keys/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ createConnection(options).then(async connection => {
console.log("Post has been saved: ", post);

console.log("now loading the post: ");
const loadedPost = await postRepository.findOneById({ id: 1, type: "person" });
const loadedPost = await postRepository.findOne({ id: 1, type: "person" });
console.log("loaded post: ", loadedPost);

}, error => console.log("Error: ", error));
18 changes: 9 additions & 9 deletions sample/sample28-single-table-inheritance/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ createConnection(options).then(async connection => {
console.log("employee has been saved: ", employee);

console.log("now loading the employee: ");
const loadedEmployee = await employeeRepository.findOneById(1);
const loadedEmployee = await employeeRepository.findOne(1);
console.log("loaded employee: ", loadedEmployee);

console.log("-----------------");
Expand All @@ -53,7 +53,7 @@ createConnection(options).then(async connection => {
console.log("homesitter has been saved: ", homesitter);

console.log("now loading the homesitter: ");
const loadedHomesitter = await homesitterRepository.findOneById(2);
const loadedHomesitter = await homesitterRepository.findOne(2);
console.log("loaded homesitter: ", loadedHomesitter);

console.log("-----------------");
Expand All @@ -70,23 +70,23 @@ createConnection(options).then(async connection => {
console.log("student has been saved: ", student);

console.log("now loading the student: ");
const loadedStudent = await studentRepository.findOneById(3);
const loadedStudent = await studentRepository.findOne(3);
console.log("loaded student: ", loadedStudent);

console.log("-----------------");
const secondEmployee = await employeeRepository.findOneById(2);
const secondEmployee = await employeeRepository.findOne(2);
console.log("Non exist employee: ", secondEmployee);
const thirdEmployee = await employeeRepository.findOneById(3);
const thirdEmployee = await employeeRepository.findOne(3);
console.log("Non exist employee: ", thirdEmployee);
console.log("-----------------");
const secondHomesitter = await homesitterRepository.findOneById(1);
const secondHomesitter = await homesitterRepository.findOne(1);
console.log("Non exist homesitter: ", secondHomesitter);
const thirdHomesitter = await homesitterRepository.findOneById(3);
const thirdHomesitter = await homesitterRepository.findOne(3);
console.log("Non exist homesitter: ", thirdHomesitter);
console.log("-----------------");
const secondStudent = await studentRepository.findOneById(1);
const secondStudent = await studentRepository.findOne(1);
console.log("Non exist student: ", secondStudent);
const thirdStudent = await studentRepository.findOneById(2);
const thirdStudent = await studentRepository.findOne(2);
console.log("Non exist student: ", thirdStudent);


Expand Down
Loading

0 comments on commit b0c37d6

Please sign in to comment.