Skip to content

Commit

Permalink
get the project in a buildable state
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Aug 13, 2024
1 parent 7f36336 commit 02d7f90
Show file tree
Hide file tree
Showing 76 changed files with 246 additions and 231 deletions.
3 changes: 1 addition & 2 deletions accounting-service-api/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "@ftgo/accounting-service-api",
"version": "0.0.1",
"type": "module",
"dependencies": {
"tslib": "^2.3.0"
},
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true
}
4 changes: 2 additions & 2 deletions accounting-service-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './lib/entities';
export * from './lib/service';
export * from './lib/entities.js';
export * from './lib/service.js';
13 changes: 13 additions & 0 deletions accounting-service-api/src/lib/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { entity, JSONEntity, JSONPartial, uuid, UUID } from '@deepkit/type';

import { Consumer } from '@ftgo/consumer-service-api';

@entity.name('account')
export class Account {
readonly id: UUID = uuid();
readonly consumerId: Consumer['id'];

static create(data: JSONPartial<Account>): Account {
return Object.assign(new Account(), data);
}
}
13 changes: 0 additions & 13 deletions accounting-service-api/src/lib/entities/account.ts

This file was deleted.

1 change: 0 additions & 1 deletion accounting-service-api/src/lib/entities/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions accounting-service-api/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"compilerOptions": {
"outDir": "../dist/out-tsc",
"declaration": true,
"module": "esnext",
"moduleResolution": "node",
"types": ["node"]
},
"include": ["src/**/*.ts"],
Expand Down
4 changes: 0 additions & 4 deletions accounting-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "development",
"inputs": ["accounting-service"],
"options": {
"buildLibsFromSource": false,
"generatePackageJson": true,
"outputPath": "dist/accounting-service",
"ssr": "src/main.ts",
"emptyOutDir": true,
"outputFileName": "main.mjs"
},
"configurations": {
Expand Down
4 changes: 2 additions & 2 deletions accounting-service/src/account.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Repository } from '@ftgo/common';
import { RestateRepository } from '@ftgo/common';
import { Account } from '@ftgo/accounting-service-api';

export class AccountRepository extends Repository(Account) {}
export class AccountRepository extends RestateRepository(Account) {}
4 changes: 2 additions & 2 deletions accounting-service/src/accounting.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { restate, RestateServiceContext } from 'deepkit-restate';

import { Consumer, KafkaConsumerTopic } from '@ftgo/consumer-service-api';
import {
AccountingServiceApi,
AccountingServiceHandlers,
} from '@ftgo/accounting-service-api';
import { Consumer, KafkaConsumerTopic } from '@ftgo/consumer-service-api';

import { AccountRepository } from './account.repository';
import { AccountRepository } from './account.repository.js';

@restate.service<AccountingServiceApi>()
export class AccountingService implements AccountingServiceHandlers {
Expand Down
6 changes: 3 additions & 3 deletions accounting-service/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { RestateModule } from 'deepkit-restate';
import { provideDatabase } from '@ftgo/common';
import { Account } from '@ftgo/accounting-service-api';

import { AccountingServiceConfig } from './config';
import { AccountingService } from './accounting.service';
import { AccountRepository } from './account.repository';
import { AccountingServiceConfig } from './config.js';
import { AccountingService } from './accounting.service.js';
import { AccountRepository } from './account.repository.js';

void new App({
config: AccountingServiceConfig,
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "@ftgo/common",
"version": "0.0.1",
"type": "module",
"dependencies": {
"tslib": "^2.3.0"
},
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true
}
2 changes: 1 addition & 1 deletion common/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"reportsDirectory": "../coverage/common"
"reportsDirectory": "../../coverage/common"
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './lib/config';
export * from './lib/database';
export * from './lib/entities';
export * from './lib/repository';
export * from './lib/types';
export * from './lib/config.js';
export * from './lib/database.js';
export * from './lib/entities.js';
export * from './lib/repository.js';
export * from './lib/types.js';
2 changes: 1 addition & 1 deletion common/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export class DatabaseConfig {
export class ServiceConfig {
readonly database: DatabaseConfig;
readonly restate: RestateConfig;
readonly framework?: FrameworkConfig;
readonly framework: FrameworkConfig = new FrameworkConfig();
}
6 changes: 3 additions & 3 deletions common/src/lib/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { PostgresDatabaseAdapter } from '@deepkit/postgres';
import { ClassType } from '@deepkit/core';
import { FactoryProvider } from '@deepkit/injector';

import { DatabaseConfig } from './config';
import { DatabaseConfig } from './config.js';

export class Database extends OrmDatabase {
constructor(
{ user, host, password, port, name, schema }: DatabaseConfig,
entities: readonly ClassType[],
entities: ClassType[],
) {
const uri = `postgres://${user}:${password}@${host}:${port}/${name}?schema=${schema}`;
super(new PostgresDatabaseAdapter(uri), entities);
}
}

export function provideDatabase(
entities: readonly ClassType[],
entities: ClassType[],
): FactoryProvider<Database> {
return {
provide: Database,
Expand Down
4 changes: 2 additions & 2 deletions common/src/lib/entities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {float} from "@deepkit/type";
import { float } from '@deepkit/type';

export class Money {
static ZERO = new Money(0)
static ZERO = new Money(0);

constructor(readonly amount: float) {}

Expand Down
63 changes: 47 additions & 16 deletions common/src/lib/repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { ClassType } from '@deepkit/core';
import { ChangesInterface, DeepPartial, JSONEntity } from '@deepkit/type';
import {
ChangesInterface,
DeepPartial,
JSONEntity,
JSONPartial,
} from '@deepkit/type';
import {
RestateContextStorage,
RestateCustomContext,
RunAction,
} from 'deepkit-restate';
import {
Database,
DatabaseQueryModel,
Expand All @@ -8,37 +18,58 @@ import {
PatchResult,
} from '@deepkit/orm';

export function Repository<T extends OrmEntity>(
entity: ClassType<T> & { create(data: JSONEntity<T>): T },
export function RestateRepository<T extends OrmEntity>(
entity: ClassType<T> & { create(data: JSONPartial<T>): T },
) {
return class BaseRepository {
constructor(readonly database: Database) {}
return class Repository {
constructor(
readonly contextStorage: RestateContextStorage,
readonly database: Database,
) {}

async findOne(filter: DatabaseQueryModel<T>['filter']): Promise<T> {
return await this.database.query(entity).filter(filter).findOne();
get #ctx(): Pick<RestateCustomContext, 'run'> {
return (
this.contextStorage.getStore() || {
run: async (action: RunAction<any>) => action(),
}
);
}

async deleteOne(
// async findOne(filter: DatabaseQueryModel<T>['filter']): Promise<T> {
// return await this.database.query(entity).filter(filter).findOne();
// }

async delete(
filter: DatabaseQueryModel<T>['filter'],
): Promise<DeleteResult<T>> {
return await this.database.query(entity).filter(filter).deleteOne();
return await this.#ctx.run<DeleteResult<T>>(() =>
this.database.query(entity).filter(filter).deleteOne(),
);
}

async find(filter: DatabaseQueryModel<T>['filter']): Promise<readonly T[]> {
return await this.database.query(entity).filter(filter).find();
// async find(filter: DatabaseQueryModel<T>['filter']): Promise<readonly T[]> {
// return await this.database.query(entity).filter(filter).find();
// }

async find(filter: DatabaseQueryModel<T>['filter']): Promise<T> {
return await this.database.query(entity).filter(filter).findOne();
}

async update(
filter: DatabaseQueryModel<T>['filter'],
changes: ChangesInterface<T> | DeepPartial<T>,
): Promise<PatchResult<T>> {
return await this.database.query(entity).filter(filter).patchOne(changes);
return await this.#ctx.run<PatchResult<T>>(() =>
this.database.query(entity).filter(filter).patchOne(changes),
);
}

async create(data: JSONEntity<T>): Promise<T> {
const et = entity.create(data);
await this.database.persist(et);
return et;
async create(data: JSONPartial<T>): Promise<T> {
return await this.#ctx.run<T>(async () => {
const et = entity.create(data);
await this.database.persist(et);
return et;
});
}
};
}
2 changes: 2 additions & 0 deletions common/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"compilerOptions": {
"outDir": "../dist/out-tsc",
"declaration": true,
"module": "esnext",
"moduleResolution": "node",
"types": ["node"]
},
"include": ["src/**/*.ts"],
Expand Down
3 changes: 1 addition & 2 deletions consumer-service-api/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "@ftgo/consumer-service-api",
"version": "0.0.1",
"type": "module",
"dependencies": {
"tslib": "^2.3.0"
},
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true
}
6 changes: 3 additions & 3 deletions consumer-service-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './lib/entities';
export * from './lib/service';
export * from './lib/topics';
export * from './lib/entities.js';
export * from './lib/service.js';
export * from './lib/topics.js';
20 changes: 20 additions & 0 deletions consumer-service-api/src/lib/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
entity,
JSONEntity,
JSONPartial,
PrimaryKey,
uuid,
UUID,
} from '@deepkit/type';

import { PersonName } from '@ftgo/common';

@entity.name('consumer')
export class Consumer {
readonly id: UUID & PrimaryKey = uuid();
readonly name: PersonName;

static create(data: JSONPartial<Consumer>) {
return Object.assign(new Consumer(), data);
}
}
14 changes: 0 additions & 14 deletions consumer-service-api/src/lib/entities/consumer.ts

This file was deleted.

1 change: 0 additions & 1 deletion consumer-service-api/src/lib/entities/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion consumer-service-api/src/lib/topics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RestateKafkaTopic } from 'deepkit-restate';

import { Consumer } from './entities';
import { Consumer } from './entities.js';

export type KafkaConsumerTopic = RestateKafkaTopic<
'consumer',
Expand Down
2 changes: 2 additions & 0 deletions consumer-service-api/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"compilerOptions": {
"outDir": "../dist/out-tsc",
"declaration": true,
"module": "esnext",
"moduleResolution": "node",
"types": ["node"]
},
"include": ["src/**/*.ts"],
Expand Down
4 changes: 0 additions & 4 deletions consumer-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
"executor": "@nx/vite:build",
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "development",
"inputs": ["consumer-service"],
"options": {
"buildLibsFromSource": false,
"generatePackageJson": true,
"outputPath": "dist/consumer-service",
"ssr": "src/main.ts",
"emptyOutDir": true,
"outputFileName": "main.mjs"
},
"configurations": {
Expand Down
4 changes: 2 additions & 2 deletions consumer-service/src/consumer.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Repository } from '@ftgo/common';
import { RestateRepository } from '@ftgo/common';
import { Consumer } from '@ftgo/consumer-service-api';

export class ConsumerRepository extends Repository(Consumer) {}
export class ConsumerRepository extends RestateRepository(Consumer) {}
5 changes: 3 additions & 2 deletions consumer-service/src/consumer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { restate, RestateServiceContext } from 'deepkit-restate';
import { RestateKafkaProducer } from 'deepkit-restate/kafka';
import { UUID } from '@deepkit/type';

import { PersonName } from '@ftgo/common';
import {
Consumer,
ConsumerServiceApi,
ConsumerServiceHandlers,
KafkaConsumerTopic,
} from '@ftgo/consumer-service-api';
import { PersonName } from '@ftgo/common';

import { ConsumerRepository } from './consumer.repository';
import { ConsumerRepository } from './consumer.repository.js';

@restate.service<ConsumerServiceApi>()
export class ConsumerService implements ConsumerServiceHandlers {
Expand All @@ -26,5 +26,6 @@ export class ConsumerService implements ConsumerServiceHandlers {
this.consumer.create({ name }),
);
await this.kafka.produce<KafkaConsumerTopic>([consumer]);
return consumer.id;
}
}
Loading

0 comments on commit 02d7f90

Please sign in to comment.