Skip to content

Commit a06c399

Browse files
authored
version 5.0.0 (#6)
release v5.0.0 * add repository create entity method * repository map to entity model * refactor and clean repository * refactor repository * update sample * update README.md
1 parent 9e8a50b commit a06c399

31 files changed

+1912
-760
lines changed

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ package-lock.json
55
tslint.json
66
tsconfig.json
77
.prettierrc
8-
sample
8+
sample
9+
.github

README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Express Cassandra utilities module for [NestJS](https://github.com/nestjs/nest)
3131
## Installation
3232

3333
```bash
34-
$ npm i --save @iaminfinity/express-cassandra express-cassandra
34+
$ npm i --save @iaminfinity/express-cassandra
3535
```
3636
## Usage
3737

@@ -160,6 +160,42 @@ export class PersonService {
160160

161161
## Using Repository
162162

163+
```typescript
164+
import { Module } from '@nestjs/common';
165+
import { ExpressCassandraModule } from '@iaminfinity/express-cassandra';
166+
import { PhotoService } from './photo.service';
167+
import { PhotoController } from './photo.controller';
168+
import { PhotoEntity } from './photo.entity';
169+
170+
@Module({
171+
imports: [ExpressCassandraModule.forFeature([PhotoEntity])],
172+
providers: [PhotoService],
173+
controllers: [PhotoController],
174+
})
175+
export class PhotoModule {}
176+
```
177+
178+
```typescript
179+
import { Injectable } from '@nestjs/common';
180+
import { InjectRepository, Repository } from '@iaminfinity/express-cassandra';
181+
import { PhotoEntity } from './photo.entity';
182+
import { Observable } from 'rxjs';
183+
184+
@Injectable()
185+
export class PersonService {
186+
constructor(
187+
@InjectRepository(PhotoEntity)
188+
private readonly photoRepository: Repository<PhotoEntity>,
189+
) {}
190+
191+
getById(id: id): Observable<PhotoEntity> {
192+
return this.photoRepository.findOne({id});
193+
}
194+
}
195+
```
196+
197+
## Using Custom Repository
198+
163199
Let's create a repository:
164200

165201
```typescript
@@ -170,7 +206,7 @@ import { Observable } from 'rxjs';
170206
@EntityRepository(PhotoEntity)
171207
export class PhotoRepository extends Repository<PhotoEntity> {
172208
findById(id: any): Observable<PhotoEntity> {
173-
return this.findOne({ id: id }, { raw: true });
209+
return this.findOne({ id: id });
174210
}
175211
}
176212
```
@@ -209,7 +245,7 @@ export class PersonService {
209245
private readonly photoRepository: PhotoRepository,
210246
) {}
211247

212-
getById(id: id): Observable<PhotoEntity> {
248+
getById(id: any): Observable<PhotoEntity> {
213249
return this.photoRepository.findById(id);
214250
}
215251
}

lib/express-casandra-core.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export class ExpressCassandraCoreModule implements OnModuleDestroy {
6868
}
6969
Logger.log('Closing connection', 'ExpressCassandraModule');
7070
const connection = this.moduleRef.get(getConnectionToken(this.options));
71+
// tslint:disable-next-line:no-unused-expression
7172
connection && (await connection.closeAsync());
7273
}
7374

lib/express-cassandra.providers.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,48 @@ import {
33
getConnectionToken,
44
getRepositoryToken,
55
} from './utils/cassandra-orm.utils';
6-
import { EXPRESS_CASSANDRA_MODULE_OPTIONS } from './express-cassandra.constant';
76
import { defer } from 'rxjs';
87
import { loadModel, Repository } from './orm';
98
import { getEntity } from './orm/utils/decorator.utils';
9+
import { Provider } from '@nestjs/common';
10+
import { RepositoryFactory } from './orm/repositories/repository.factory';
1011

1112
export function createExpressCassandraProviders(
1213
entities?: Function[],
1314
connection?: string | any,
1415
) {
1516
const providerModel = entity => ({
1617
provide: getModelToken(entity),
17-
useFactory: async (client: any, options) => {
18+
useFactory: async (client: any) => {
1819
return await defer(() => loadModel(client, entity)).toPromise();
1920
},
20-
inject: [getConnectionToken(connection), EXPRESS_CASSANDRA_MODULE_OPTIONS],
21+
inject: [getConnectionToken(connection)],
2122
});
2223

23-
const provideRepository = EntityRepository => {
24-
const entitySchema = getEntity(EntityRepository);
24+
const provideRepository = entity => ({
25+
provide: getRepositoryToken(entity),
26+
useFactory: async (model: any) => RepositoryFactory.create(entity, model),
27+
inject: [getModelToken(entity)],
28+
});
29+
30+
const provideCustomRepository = EntityRepository => {
31+
const entity = getEntity(EntityRepository);
2532
return {
2633
provide: getRepositoryToken(EntityRepository),
27-
useFactory: async model => createRepository(EntityRepository, model),
28-
inject: [getModelToken(entitySchema)],
34+
useFactory: async model =>
35+
RepositoryFactory.create(entity, model, EntityRepository),
36+
inject: [getModelToken(entity)],
2937
};
3038
};
3139

32-
const models = (entities || []).map(entity => {
40+
const providers: Provider[] = [];
41+
(entities || []).forEach(entity => {
3342
if (entity.prototype instanceof Repository) {
34-
return provideRepository(entity);
43+
providers.push(provideCustomRepository(entity));
44+
return;
3545
}
36-
return providerModel(entity);
46+
providers.push(providerModel(entity), provideRepository(entity));
3747
});
38-
return [...models];
39-
}
4048

41-
const createRepository = (EntityRepository, model: any): Repository => {
42-
return Object.assign(new EntityRepository(), { entity: model });
43-
};
49+
return [...providers];
50+
}

lib/index.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
import { types } from 'cassandra-driver';
2-
3-
export { doBatchAsync, doBatch } from 'express-cassandra';
42
export * from 'cassandra-driver';
5-
63
export * from './interfaces';
74
export * from './express-cassandra.module';
85
export * from './utils/express-cassandra.decorator';
96
export * from './orm';
107

11-
export function uuid(id?): types.Uuid {
8+
export const uuid = (id?: any): types.Uuid => {
129
if (!id) {
1310
return types.Uuid.random();
1411
}
1512
if (typeof id === 'string') {
1613
return types.Uuid.fromString(id);
1714
}
18-
if (id instanceof types.Uuid) {
19-
return id;
20-
}
2115
return id;
22-
}
16+
};
2317

24-
export function isUuid(id): boolean {
25-
return id && id instanceof types.Uuid ? true : false;
26-
}
18+
export const isUuid = (id: any): boolean => id && id instanceof types.Uuid;
2719

28-
export function timeuuid(idOrDate?: string | Date): types.TimeUuid {
20+
export const timeuuid = (idOrDate?: string | Date): types.TimeUuid => {
2921
if (!idOrDate) {
3022
return new types.TimeUuid();
3123
}
@@ -36,15 +28,7 @@ export function timeuuid(idOrDate?: string | Date): types.TimeUuid {
3628
return types.TimeUuid.fromDate(idOrDate);
3729
}
3830
return idOrDate;
39-
}
40-
41-
export function isTimeUuid(id): boolean {
42-
return id && id instanceof types.TimeUuid ? true : false;
43-
}
44-
45-
export declare function doBatchAsync(queries: string[]): Promise<any>;
31+
};
4632

47-
export declare function doBatch(
48-
queries: string[],
49-
callback: (err: Error) => void,
50-
): void;
33+
export const isTimeUuid = (id: any): boolean =>
34+
id && id instanceof types.TimeUuid;

lib/orm/decorators/column.decorator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { addAttribute } from '../utils/decorator.utils';
33

44
export function Column(options: ColumnOptions): PropertyDecorator {
55
return (target: object, propertyName: string) => {
6-
// Reflect.defineMetadata(COLUMN_METADATA, options, target);
76
addAttribute(target, propertyName, options);
87
};
98
}

lib/orm/decorators/entity.decorator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ export function Entity(
2323

2424
setEntityName(target.prototype, name);
2525
addOptions(target.prototype, options);
26-
// Reflect.defineMetadata(ENTITY_METADATA, options, target);
2726
};
2827
}

0 commit comments

Comments
 (0)