Skip to content

Commit

Permalink
Merge feat-dynamic-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jul 20, 2019
2 parents 86b0834 + e5b9f90 commit c8f1f09
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 23 deletions.
35 changes: 23 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Install npm dependencies with yarn (not with NPM!):
```bash
yarn
```
> After installing dependencies, yarn run the `postinstall` hook and mounted all packages with `npm link` (e.g. `yarn run repo:bootstrap`).
> After installing dependencies, yarn/npm run the `postinstall` hook and mounted all packages with `npm link` (e.g. `yarn run repo:bootstrap`).
Compile TypeScript:
```bash
Expand All @@ -54,10 +54,6 @@ yarn tsc
npm run tsc
```

Build project:
```
```

### Test

```bash
Expand All @@ -66,43 +62,58 @@ yarn test
npm run test
```

### Gflow
### Gflow (optional)

[Gflow](https://www.npmjs.com/package/gflow) is a command line tool to help developer with the Git Flow process used in Ts.ED.
[Gflow](https://www.npmjs.com/package/gflow) is a command line tool to help developer with the Git process used in Ts.ED.

Gflow help you to create a branch from production, rebase and run the test before pushing your branch on your remote repository.

```bash
npm install -g gflow
```

#### Start a feature branch
### Start a feature branch

```bash
git fetch
git branch --no-track -b feat-branch-name origin/production # !IMPORTANT
yarn

## OR
gflow new feat name_of_feat
```

#### Commit & Push a feature
### Commit & Push a feature

This command rebase your branch feature from the production branch, run the test and push your branch.

```bash
git commit -m "feat(domain): Your message"
```
> To write your commit message see [convention page](https://www.conventionalcommits.org/en/v1.0.0-beta.4/)
Then:
```bash
npm run test
git fetch
git rebase origin/production
git push -f

# OR using gflow (run fetch, rebase and push for you)
gflow push
```
> To write your commit message see [convention page](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit)

When your feature is ready to review, you can open a PR on Ts.ED github.

#### Finish a feature
### Finish a feature (repo owner and maintainers)

After the PR has been accepted, the feature will be automatically merge on the master branch, but
your feature isn't merge with the production branch.

To publish your feature on the production branch you need to run a this command:

```bash
gflow finish
gflow finish
```

> Note: This action works only on the Ts.ED repository (not on your fork).
Expand Down
33 changes: 22 additions & 11 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Install npm dependencies with yarn (not with NPM!):
```bash
yarn
```
> After installing dependencies, yarn run the `postinstall` hook and mounted all packages with `npm link` (e.g. `yarn run repo:bootstrap`).
> After installing dependencies, yarn/npm run the `postinstall` hook and mounted all packages with `npm link` (e.g. `yarn run repo:bootstrap`).
Compile TypeScript:
```bash
Expand All @@ -79,10 +79,6 @@ yarn tsc
npm run tsc
```

Build project:
```
```

### Test

```bash
Expand All @@ -91,35 +87,50 @@ yarn test
npm run test
```

### Gflow
### Gflow (optional)

[Gflow](https://www.npmjs.com/package/gflow) is a command line tool to help developer with the Git Flow process used in Ts.ED.
[Gflow](https://www.npmjs.com/package/gflow) is a command line tool to help developer with the Git process used in Ts.ED.

Gflow help you to create a branch from production, rebase and run the test before pushing your branch on your remote repository.

```bash
npm install -g gflow
```

#### Start a feature branch
### Start a feature branch

```bash
git fetch
git branch --no-track -b feat-branch-name origin/production # !IMPORTANT
yarn

## OR
gflow new feat name_of_feat
```

#### Commit & Push a feature
### Commit & Push a feature

This command rebase your branch feature from the production branch, run the test and push your branch.

```bash
git commit -m "feat(domain): Your message"
```
> To write your commit message see [convention page](https://www.conventionalcommits.org/en/v1.0.0-beta.4/)
Then:
```bash
npm run test
git fetch
git rebase origin/production
git push -f

# OR using gflow (run fetch, rebase and push for you)
gflow push
```
> To write your commit message see [convention page](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit)

When your feature is ready to review, you can open a PR on Ts.ED github.

#### Finish a feature
### Finish a feature (repo owner and maintainers)

After the PR has been accepted, the feature will be automatically merge on the master branch, but
your feature isn't merge with the production branch.
Expand Down
8 changes: 8 additions & 0 deletions docs/tutorials/mongoose.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ Be wary of circular dependencies. Direct references must be declared after the r

<<< @/docs/tutorials/snippets/mongoose/virtual-references.ts

### Dynamic References

`@tsed/mongoose` supports `mongoose` dynamic references between defined models.

This works by having a field with the referenced object model's name and a field with the referenced field.

<<< @/docs/tutorials/snippets/mongoose/dynamic-references.ts

## Register hook

Mongoose allows the developer to add pre and post [hooks / middlewares](http://mongoosejs.com/docs/middleware.html) to the schema.
Expand Down
11 changes: 11 additions & 0 deletions docs/tutorials/snippets/mongoose/dynamic-references.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {Model, Ref, DynamicRef} from "@tsed/mongoose";
import {Enum, Required} from "@tsed/common"

@Model()
export class DynamicRef {
@DynamicRef('type')
dynamicRef: Ref<ModelA | ModelB>

@Enum(['Mode lA', 'ModelB'])
type: string // This field has to match the referenced model's name
}
54 changes: 54 additions & 0 deletions packages/mongoose/src/decorators/dynamicRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {Property, Schema} from "@tsed/common";
import {applyDecorators, Store, StoreFn, StoreMerge} from "@tsed/core";
import {Schema as MongooseSchema} from "mongoose";
import {MONGOOSE_SCHEMA} from "../constants";

export type DynamicRef<T> = T | string;
/**
* Define a property as mongoose reference to other Model (decorated with @Model).
*
* ### Example
*
* ```typescript
* @Model()
* class FooModel {
*
* @DynamicRef('type')
* field: DynamicRef<OtherFooModel | OtherModel>
*
* @Enum(['OtherFooModel', 'OtherModel'])
* type: string
* }
*
* @Model()
* class OtherFooModel {
* }
*
* @Model()
* class OtherModel {
* }
* ```
*
* @param refPath
* @returns {Function}
* @decorator
* @mongoose
* @property
*/
export function DynamicRef(refPath: string) {
return applyDecorators(
Property({use: String}),
Schema({
type: String,
example: "5ce7ad3028890bd71749d477",
description: "Mongoose Ref ObjectId"
}),
StoreFn((store: Store) => {
delete store.get("schema").$ref;
}),
StoreMerge(MONGOOSE_SCHEMA, {
type: MongooseSchema.Types.ObjectId,
refPath
})
);
}
1 change: 1 addition & 0 deletions packages/mongoose/src/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./select";
export * from "./unique";
export * from "./virtualRef";
export * from "./objectID";
export * from "./dynamicRef";
30 changes: 30 additions & 0 deletions packages/mongoose/test/decorators/dynamicRef.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {descriptorOf, Store} from "@tsed/core";
import {expect} from "chai";
import {Schema} from "mongoose";
import {MONGOOSE_MODEL_NAME, MONGOOSE_SCHEMA} from "../../src/constants";
import {DynamicRef} from "../../src/decorators";

describe("@Ref()", () => {
it("should set metadata", () => {
// GIVEN

class RefTest {
}

Store.from(RefTest).set(MONGOOSE_MODEL_NAME, "RefTest");

// WHEN
class Test {
@DynamicRef("RefTest")
test: DynamicRef<RefTest>;
}

// THEN
const store = Store.from(Test, "test", descriptorOf(Test, "test"));

expect(store.get(MONGOOSE_SCHEMA)).to.deep.eq({
type: Schema.Types.ObjectId,
refPath: "RefTest"
});
});
});

0 comments on commit c8f1f09

Please sign in to comment.