Skip to content

Commit

Permalink
feat(repository): initial AtomicCrudRepository implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
biniam committed Nov 5, 2018
1 parent 216bf85 commit 1b7548c
Show file tree
Hide file tree
Showing 3 changed files with 421 additions and 0 deletions.
69 changes: 69 additions & 0 deletions packages/repository/src/repositories/atomic.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {Entity} from '../model';
import {DataObject, Options, AnyObject} from '../common-types';
import {Filter} from '../query';
import {EntityCrudRepository} from './repository';
import {
DefaultCrudRepository,
juggler,
ensurePromise,
} from './legacy-juggler-bridge';
import * as assert from 'assert';
import * as legacy from 'loopback-datasource-juggler';

export interface AtomicCrudRepository<T extends Entity, ID>
extends EntityCrudRepository<T, ID> {
/**
* Finds one record matching the filter object. If not found, creates
* the object using the data provided as second argument. In this sense it is
* the same as `find`, but limited to one object. Returns an object, not
* collection. If you don't provide the filter object argument, it tries to
* locate an existing object that matches the `data` argument.
*
* @param filter Filter object used to match existing model instance
* @param entity Entity to be used for creating a new instance or match
* existing instance if filter is empty
* @param options Options for the operation
* @returns A promise that will be resolve with the created or found instance
* and a 'created' boolean value
*/
findOrCreate(
filter: Filter<T>,
entity: DataObject<T>,
options?: Options,
): Promise<[T, boolean]>;
}

export class DefaultAtomicCrudRepository<T extends Entity, ID>
extends DefaultCrudRepository<T, ID>
implements AtomicCrudRepository<T, ID> {
constructor(
entityClass: typeof Entity & {
prototype: T;
},
dataSource: juggler.DataSource,
) {
assert(
dataSource.connector !== undefined,
`Connector instance must exist and support atomic operations`,
);
super(entityClass, dataSource);
}

async findOrCreate(
filter: Filter<T>,
entity: DataObject<T>,
options?: AnyObject | undefined,
): Promise<[T, boolean]> {
if (
this.dataSource.connector &&
typeof this.dataSource.connector.findOrCreate === 'function'
) {
const result = await ensurePromise(
this.modelClass.findOrCreate(filter as legacy.Filter, entity, options),
);
return [this.toEntity(result[0]), result[1]];
} else {
throw new Error('Method not implemented.');
}
}
}
1 change: 1 addition & 0 deletions packages/repository/src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './legacy-juggler-bridge';
export * from './kv.repository.bridge';
export * from './repository';
export * from './constraint-utils';
export * from './atomic.repository';
Loading

0 comments on commit 1b7548c

Please sign in to comment.