Skip to content

Commit

Permalink
Merge pull request #42 from w3tecch/update
Browse files Browse the repository at this point in the history
refactor: add eslint, change to travis and add some sample files
  • Loading branch information
Gery Hirschfeld authored Apr 10, 2020
2 parents 14c6c71 + f244c65 commit cd7375b
Show file tree
Hide file tree
Showing 29 changed files with 3,073 additions and 2,931 deletions.
35 changes: 0 additions & 35 deletions .circleci/config.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-var-requires': 'off',
},
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ lib
es
coverage
.rpt2_cache
test.db

### node ###
logs
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"singleQuote": true,
"trailingComma": "all",
"semi": false,
"trailingComma": "all",
"tabWidth": 2,
"jsxBracketSameLine": true,
"printWidth": 120
Expand Down
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js
node_js:
- 10
before_install: # if "install" is overridden
# Repo for Yarn
- sudo apt-key adv --fetch-keys http://dl.yarnpkg.com/debian/pubkey.gpg
- echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
- sudo apt-get update -qq
- sudo apt-get install -y -qq yarn
cache:
yarn: true
install:
- yarn install
- yarn add -P typeorm
script:
- yarn run lint
- yarn run test
- yarn run build
- yarn run semantic-release || true
42 changes: 27 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ yarn add typeorm-seeding
```

Optional, for `Faker` types

```bash
npm install -D @types/faker
```
Expand All @@ -60,11 +61,18 @@ To configure the path to your seeds and factories change the TypeORM config file
```JavaScript
module.exports = {
...
seeds: ['src/seeds/**/*.seed.ts'],
factories: ['src/factories/**/*.factory.ts'],
seeds: ['src/seeds/**/*{.ts,.js}'],
factories: ['src/factories/**/*{.ts,.js}'],
}
```

or add the following variables to your `.env` file.

```
TYPEORM_SEEDING_FACTORIES=src/seeds/**/*{.ts,.js}
TYPEORM_SEEDING_SEEDS=src/factories/**/*{.ts,.js}
```

![divider](./w3tec-divider.png)

## ❯ Writing Seeders
Expand All @@ -82,7 +90,10 @@ export default class CreateUsers implements Seeder {
.createQueryBuilder()
.insert()
.into(User)
.values([{ firstName: 'Timber', lastName: 'Saw' }, { firstName: 'Phantom', lastName: 'Lancer' }])
.values([
{ firstName: 'Timber', lastName: 'Saw' },
{ firstName: 'Phantom', lastName: 'Lancer' },
])
.execute()
}
}
Expand All @@ -99,7 +110,7 @@ Once you have written your seeder, you can add this script to your `package.json
}
```

And then
And then

```bash
npm run seed
Expand All @@ -112,8 +123,8 @@ For all entities we want to seed, we need to define a factory. To do so we give
Settings can be used to pass some static value into the factory.

```typescript
import Faker from 'faker';
import { define } from "typeorm-seeding";
import Faker from 'faker'
import { define } from 'typeorm-seeding'
import { User } from '../entities'

define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
Expand All @@ -134,8 +145,8 @@ define(User, (faker: typeof Faker, settings: { roles: string[] }) => {
Handle relation in the entity factory like this.

```typescript
import Faker from 'faker';
import { define } from 'typeorm-seeding';
import Faker from 'faker'
import { define } from 'typeorm-seeding'
import { Pet } from '../entities'

define(Pet, (faker: typeof Faker, settings: undefined) => {
Expand All @@ -145,7 +156,7 @@ define(Pet, (faker: typeof Faker, settings: undefined) => {
const pet = new Pet()
pet.name = name
pet.age = faker.random.number()
pet.user = factory(User)({ roles: ['admin'] })
pet.user = factory(User)({ roles: ['admin'] }) as any
return pet
})
```
Expand Down Expand Up @@ -198,7 +209,7 @@ export default class CreatePets implements Seeder {
public async run(factory: Factory, connection: Connection): Promise<any> {
const em = connection.createEntityManager()

await times(10, async n => {
await times(10, async (n) => {
// This creates a pet in the database
const pet = await factory(Pet)().seed()
// This only returns a entity with fake data
Expand All @@ -216,11 +227,12 @@ Now you are able to execute your seeds with this command `npm run seed`.

### CLI Options

| Option | Default | Description |
| ------------------ | -------------- | ------------------------------------------------------------- |
| `--class` or `--c` | null | Option to specify a specific seeder class to run individually |
| `--config` | `ormconfig.js` | Path to the typeorm config file (json or js). |

| Option | Default | Description |
| ---------------------- | --------------- | --------------------------------------------------------------------------- |
| `--seed` or `-s` | null | Option to specify a specific seeder class to run individually. |
| `--connection` or `-c` | null | Name of the typeorm connection. Required if there are multiple connections. |
| `--configName` or `-n` | `ormconfig.js` | Name to the typeorm config file. |
| `--root` or `-r` | `process.cwd()` | Path to the typeorm config file. |

## ❯ License

Expand Down
20 changes: 20 additions & 0 deletions ormconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path')

module.exports = [
{
name: 'sample',
type: 'sqlite',
database: 'test.db',
entities: ['sample/entities/**/*{.ts,.js}'],
factories: ['sample/factories/**/*{.ts,.js}'],
seeds: ['sample/seeds/**/*{.ts,.js}'],
},
{
name: 'other',
type: 'sqlite',
database: 'test.db',
entities: ['sample/entities/**/*{.ts,.js}'],
factories: ['sample/factories/**/*{.ts,.js}'],
seeds: ['sample/seeds/**/*{.ts,.js}'],
}
]
72 changes: 37 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
{
"name": "typeorm-seeding",
"version": "0.0.0-development",
"version": "1.3.0",
"description": "TypeORM seeding",
"main": "dist/typeorm-seeding.js",
"module": "dist/typeorm-seeding.es.js",
"types": "dist/typeorm-seeding.d.ts",
"bin": {
"typeorm-seeding": "dist/cli.js"
},
"files": [
"dist"
],
"types": "dist/typeorm-seeding.d.ts",
"scripts": {
"prebuild": "rimraf dist",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json -c tslint.json",
"build": "npm run clean && rollup -c",
"lint": "eslint \"src/**/*.ts\" --fix",
"build": "tsc --project ./tsconfig.build.json",
"watch": "rollup -cw",
"clean": "rimraf dist",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"schema:drop": "ts-node ./node_modules/typeorm/cli.js schema:drop -c sample",
"schema:sync": "ts-node ./node_modules/typeorm/cli.js schema:sync -c sample",
"schema:log": "ts-node ./node_modules/typeorm/cli.js schema:log -c sample",
"seed:run": "ts-node ./src/cli.ts seed -c sample",
"seed:config": "ts-node ./src/cli.ts config -c sample"
},
"license": "MIT",
"author": "w3tec.ch <[email protected]>",
Expand All @@ -32,40 +36,38 @@
}
],
"devDependencies": {
"@types/faker": "^4.1.4",
"@types/jest": "^24.0.13",
"@types/yargs": "^13.0.0",
"jest": "^24.8.0",
"prettier": "^1.17.1",
"rimraf": "^2.6.2",
"rollup": "^0.66.2",
"rollup-plugin-cli": "^0.1.5",
"rollup-plugin-commonjs": "^9.1.8",
"rollup-plugin-hashbang": "^2.2.2",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-typescript2": "^0.21.1",
"semantic-release": "^15.13.27",
"ts-jest": "^24.0.2",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.18.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^3.0.3"
"@types/chalk": "^2.2.0",
"@types/faker": "^4.1.11",
"@types/glob": "7.1.1",
"@types/jest": "^25.1.4",
"@types/node": "13.9.8",
"@types/yargs": "^15.0.4",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-import": "^2.20.2",
"jest": "^25.2.6",
"prettier": "^2.0.2",
"rimraf": "^3.0.2",
"semantic-release": "^17.0.4",
"sqlite": "^3.0.6",
"ts-jest": "^25.3.0",
"typescript": "^3.8.3"
},
"dependencies": {
"@types/glob": "7.1.1",
"@types/node": "12.0.4",
"chalk": "2.4.2",
"chalk": "^4.0.0",
"faker": "4.1.0",
"glob": "7.1.3",
"ora": "3.4.0",
"glob": "7.1.6",
"ora": "4.0.3",
"reflect-metadata": "0.1.13",
"yargs": "13.2.4"
"yargs": "15.3.1"
},
"peerDependencies": {
"typeorm": "^0.2.20"
"typeorm": "^0.2.24"
},
"resolutions": {
"mem": ">=4.0.0"
},
"jest": {
"moduleFileExtensions": [
Expand Down
Loading

0 comments on commit cd7375b

Please sign in to comment.