Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Repository {
- constructor(props) {
- this.props = props;
- this.data = new Map();
- this._id = 0;
- }
- get count() {
- return this.data.size;
- }
- get id() {
- return this._id++;
- }
- add(entity) {
- for (let property in this.props) {
- if (!entity.hasOwnProperty(property)) {
- throw new Error('Property ' + property + ' is missing from the entity!')
- }
- let propsPropertyType = this.props[property];
- let entityPropertyType = typeof entity[property];
- if (propsPropertyType !== entityPropertyType) {
- throw new TypeError('Property ' + property + ' is of incorrect type!')
- }
- }
- let currentId = this.id;
- this.data.set(currentId, entity);
- return currentId
- }
- get(idToReturn) {
- if (!this.data.has(idToReturn)) {
- throw new Error('Entity with id: ' + idToReturn + ' does not exist!');
- }
- return this.data.get(idToReturn);
- }
- update(id, newEntity) {
- if (!this.data.has(id)) {
- throw new Error('Entity with id: ' + id + ' does not exist!')
- }
- for (let property in this.props) {
- if (!newEntity.hasOwnProperty(property)) {
- throw new Error('Property ' + property + ' is missing from the entity!')
- }
- let propsPropertyType = this.props[property];
- let entityPropertyType = typeof newEntity[property];
- if (propsPropertyType !== entityPropertyType) {
- throw new TypeError('Property ' + property + ' is of incorrect type!')
- }
- }
- this.data.set(id, newEntity);
- }
- del(idToRemove) {
- if (!this.data.has(idToRemove)) {
- let result = 'Entity with id: ' + idToRemove + ' does not exist!';
- // console.log(result)
- throw new Error(result)
- }
- this.data.delete(idToRemove);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement