tags: #database , #architectural-patterns
links: [[DataBase MOC]]
There are two popular patterns:
1. [Active Record](https://en.wikipedia.org/wiki/Active_record_pattern)
2. [Data Mapper](https://en.wikipedia.org/wiki/Data_mapper_pattern)
The Active Record pattern basically couples database with models(eg: UserModel). so you can access database within your models. The interface for basic functions like Insert, update,delete corresponds directly to the columns in database
```js
// User is a model
const user = new User()
user.firstName = "Subramanya"
user.lastName = "Chakravarthy"
await user.save()
await user.remove()
const users = await User.find({take: 5, skip: 2})
```
The Data Mapper pattern creates a connector(basically called repository) between database and models. Basically you create a model Object(eg: UserModel) and then use repository to implement save, delete methods.
Here model is only used to validate data that goes to database and repository handles connection to database and saving data into it
```js
const userRepository = connection.getRepository(user)
const user = new User()
user.firstName = "Subramanya"
user.lastName = "Chakravarthy"
await userRepository.save(user)
await userRepository.delete(user)
```