Skip to content

Commit e7b4bb1

Browse files
committed
db: migrate ok, seed continue finish
1 parent 4197fe1 commit e7b4bb1

File tree

9 files changed

+91
-13
lines changed

9 files changed

+91
-13
lines changed

apps/library/src/app/db-config/migrations/2211230342_createBook.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export async function up(knex: Knex) {
2525
});
2626

2727
await knex.schema.table('book_instance', (t) => {
28-
t.foreign('book_id').references('id').inTable('book');
28+
t.foreign('book_id').references('id').inTable('book')
29+
.onDelete('CASCADE').onUpdate('CASCADE');
2930
});
3031
}
3132

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Knex } from 'knex';
2+
import { BookModel } from '../../models/book.db.model';
3+
4+
export async function seed(knex: Knex): Promise<any> {
5+
await BookModel.query(knex).del();
6+
await BookModel.query(knex).insert([
7+
{
8+
name: 'Workout',
9+
isbn: '1',
10+
category: 'history',
11+
available_quantity: 10,
12+
total: 10,
13+
lost: 0,
14+
},
15+
{
16+
name: '10 funny',
17+
isbn: '2',
18+
category: 'physical',
19+
available_quantity: 10,
20+
total: 10,
21+
lost: 0,
22+
},
23+
{
24+
name: 'Web programing',
25+
isbn: '3',
26+
category: 'history',
27+
available_quantity: 10,
28+
total: 10,
29+
lost: 0,
30+
},
31+
{
32+
name: 'How to talk funny with people',
33+
isbn: '4',
34+
category: 'biology',
35+
available_quantity: 10,
36+
total: 10,
37+
lost: 0,
38+
},
39+
{
40+
name: 'Talk easily',
41+
isbn: '5',
42+
category: 'geography',
43+
available_quantity: 10,
44+
total: 10,
45+
lost: 0,
46+
}
47+
]);
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Knex } from 'knex';
2+
import { BookInstanceModel } from '../../models/book-instance.db.model';
3+
4+
export async function seed(knex: Knex): Promise<any> {
5+
await BookInstanceModel.query(knex).delete();
6+
await BookInstanceModel.query(knex).insert([
7+
{
8+
status: 'activated',
9+
book_id: 2,
10+
},
11+
{
12+
status: 'activated',
13+
book_id: 1,
14+
},
15+
{
16+
status: 'activated',
17+
book_id: 1,
18+
},
19+
{
20+
status: 'activated',
21+
book_id: 2,
22+
},
23+
]);
24+
}

apps/library/src/app/db-config/stub/migration.stub

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Knex from 'knex';
1+
import { Knex } from 'knex';
22

33
const tableName = '';
44

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as Knex from 'knex';
1+
import { Knex } from 'knex';
22

33
export async function seed(knex: Knex): Promise<any> {
44
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
import { BookModel } from 'apps/library/src/app/models/book.db.model';
21
import { Model } from 'objection';
32
import { BaseModel } from './base.model';
4-
import { BookStatus } from '../../../../../libs/cattalog/src/lib/modeling/book-instance.modeling';
3+
import { BookStatus } from '../../../../../libs/cattalog/src/lib/modeling/BookStatus.type';
4+
import { BookModel } from './book.db.model';
5+
import { number } from 'fp-ts-std';
56

6-
export class BookInstanceModel extends BaseModel{
7+
export class BookInstanceModel extends BaseModel {
78
static tableName = 'book_instance';
89

910
status: BookStatus;
11+
book: BookModel;
12+
book_id: number;
1013

1114
static RelationMappings = {
1215
owner: {
1316
relation: Model.BelongsToOneRelation,
14-
modelClass: BookModel,
17+
modelClass: `${__dirname}/book.db.model`,
1518
join: {
1619
from: 'book_instance.book_id',
1720
to: 'book.id',
18-
}
19-
}
20-
}
21+
},
22+
},
23+
};
2124
}

apps/library/src/app/models/book.db.model.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BookInstance } from 'libs/catalogue/src/lib/book-instance';
21
import { Model } from 'objection';
32
import { BaseModel } from './base.model';
3+
import { BookInstanceModel } from './book-instance.db.model';
44

55
export class BookModel extends BaseModel{
66
static tableName = 'book';
@@ -17,7 +17,7 @@ export class BookModel extends BaseModel{
1717
static RelationMappings = {
1818
owner: {
1919
relation: Model.HasManyRelation,
20-
modelClass: BookInstance,
20+
modelClass: BookInstanceModel,
2121
join: {
2222
from: 'book.id',
2323
to: 'book_instance.book_id'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export type BookStatus = "achived" | "activated" | "pending"

libs/cattalog/src/lib/modeling/book-instance.modeling.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Uuid } from "@library/shared/doomain"
22
import { Book } from './book.modelling';
3+
import { BookStatus } from './BookStatus.type';
34

4-
export type BookStatus = "achived" | "activated" | "pending"
55

66
export class BookInstance{
77
id: Uuid;

0 commit comments

Comments
 (0)