Skip to content

Commit

Permalink
seed 填充数据
Browse files Browse the repository at this point in the history
  • Loading branch information
slTrust committed Feb 15, 2021
1 parent fb19e8d commit 101f6fc
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 10 deletions.
33 changes: 32 additions & 1 deletion dist/entity/Post.js
Expand Up @@ -21,7 +21,8 @@ var _typeorm = require("typeorm");

var _dec, _dec2, _dec3, _dec4, _class, _class2, _descriptor, _descriptor2, _descriptor3, _temp;

var Post = (_dec = (0, _typeorm.Entity)('posts'), _dec2 = (0, _typeorm.PrimaryGeneratedColumn)('increment'), _dec3 = (0, _typeorm.Column)('varchar'), _dec4 = (0, _typeorm.Column)('text'), _dec(_class = (_class2 = (_temp = function Post(attributes) {
var Post = (_dec = (0, _typeorm.Entity)('posts'), _dec2 = (0, _typeorm.PrimaryGeneratedColumn)('increment'), _dec3 = (0, _typeorm.Column)('varchar'), _dec4 = (0, _typeorm.Column)('text'), _dec(_class = (_class2 = (_temp = // Partial 代表不用把Post的所有属性传进来
function Post(attributes) {
(0, _classCallCheck2["default"])(this, Post);
(0, _initializerDefineProperty2["default"])(this, "id", _descriptor, this);
(0, _initializerDefineProperty2["default"])(this, "title", _descriptor2, this);
Expand All @@ -43,4 +44,34 @@ var Post = (_dec = (0, _typeorm.Entity)('posts'), _dec2 = (0, _typeorm.PrimaryGe
writable: true,
initializer: null
})), _class2)) || _class);
/*
@Entity('posts')
export class Post {
@PrimaryGeneratedColumn('increment')
id: number;
@Column('varchar')
title: string;
@Column('text')
content: string;
// 啰嗦的方式
constructor(title:string,content:string) {
this.title = title;
this.content = content;
}
}
// 另一种方式:这个会有问题
@Entity('posts')
export class Post {
@PrimaryGeneratedColumn('increment')
id: number;
constructor(
@Column('varchar') public title:string,
@Column('text') public content:string
) {
}
}
*/

exports.Post = Post;
29 changes: 27 additions & 2 deletions dist/index.js → dist/seed.js
Expand Up @@ -10,16 +10,41 @@ require("reflect-metadata");

var _typeorm = require("typeorm");

var _Post = require("./entity/Post");

(0, _typeorm.createConnection)().then( /*#__PURE__*/function () {
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(connection) {
var posts;
return _regenerator["default"].wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
console.log(connection);
connection.close();
_context.next = 2;
return connection.manager.find(_Post.Post);

case 2:
posts = _context.sent;

if (!(posts.length === 0)) {
_context.next = 7;
break;
}

_context.next = 6;
return connection.manager.save([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(function (n) {
return new _Post.Post({
title: "Post ".concat(n),
content: "\u8FD9\u662F\u6211\u7684\u7B2C".concat(n, "\u7BC7\u6587\u7AE0")
});
}));

case 6:
console.log('posts 数据填充了');

case 7:
connection.close();

case 8:
case "end":
return _context.stop();
}
Expand Down
28 changes: 28 additions & 0 deletions scripts/03seed填充数据.md
@@ -0,0 +1,28 @@
### seed 填充数据

- 前置操作 参考 01docker.md 内容,新建好容器,配置好数据库连接

- 开第一个终端 `yarn dev`
- 可以编译我们的 typeorm代码

- 开第二个终端执行数据库迁移

```$xslt
# 创建数据库表
yarn migration:run
# revert操作
yarn migration:revert
```

- 填充数据

```
# 项目根目录
node dist/seed.js
```

- 进入docker容器内部查询

```
select * from posts;
```
31 changes: 31 additions & 0 deletions src/entity/Post.ts
Expand Up @@ -9,7 +9,38 @@ export class Post {
@Column('text')
content: string;

// Partial 代表不用把Post的所有属性传进来
constructor(attributes: Partial<Post>) {
Object.assign(this, attributes);
}
}

/*
@Entity('posts')
export class Post {
@PrimaryGeneratedColumn('increment')
id: number;
@Column('varchar')
title: string;
@Column('text')
content: string;
// 啰嗦的方式
constructor(title:string,content:string) {
this.title = title;
this.content = content;
}
}
// 另一种方式:这个会有问题
@Entity('posts')
export class Post {
@PrimaryGeneratedColumn('increment')
id: number;
constructor(
@Column('varchar') public title:string,
@Column('text') public content:string
) {
}
}
*/
7 changes: 0 additions & 7 deletions src/index.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/seed.ts
@@ -0,0 +1,15 @@
import "reflect-metadata";
import {createConnection} from "typeorm";
import {Post} from "./entity/Post";

createConnection().then(async connection => {
const posts = await connection.manager.find(Post);

if (posts.length === 0) {
await connection.manager.save([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(n => {
return new Post({title: `Post ${n}`, content: `这是我的第${n}篇文章`});
}));
console.log('posts 数据填充了');
}
connection.close();
}).catch(error => console.log(error));

0 comments on commit 101f6fc

Please sign in to comment.