-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd81835
commit 15250f8
Showing
11 changed files
with
176 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules | ||
node_modules/ | ||
package-lock.json | ||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,33 @@ | ||
const typeorm = require("typeorm"); // import * as typeorm from "typeorm"; | ||
const Post = require("./model/Post").Post; // import {Post} from "./model/Post"; | ||
const Category = require("./model/Category").Category; // import {Category} from "./model/Category"; | ||
|
||
typeorm.createConnection({ | ||
type: "mysql", | ||
host: "localhost", | ||
port: 3306, | ||
username: "test", | ||
password: "test", | ||
database: "test", | ||
synchronize: true, | ||
logging: false, | ||
entities: [ | ||
require("./entity/PostSchema"), | ||
require("./entity/CategorySchema") | ||
] | ||
}).then(function (connection) { | ||
|
||
const category1 = new Category(0, "TypeScript"); | ||
const category2 = new Category(0, "Programming"); | ||
|
||
return connection | ||
.manager | ||
.save([category1, category2]) | ||
.then(() => { | ||
|
||
let post = new Post(); | ||
post.title = "Control flow based type analysis"; | ||
post.text = "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters."; | ||
post.categories = [category1, category2]; | ||
|
||
let postRepository = connection.getRepository(Post); | ||
postRepository.save(post) | ||
.then(function(savedPost) { | ||
console.log("Post has been saved: ", savedPost); | ||
console.log("Now lets load all posts: "); | ||
|
||
return postRepository.find(); | ||
}) | ||
.then(function(allPosts) { | ||
console.log("All posts: ", allPosts); | ||
}); | ||
}); | ||
|
||
}).catch(function(error) { | ||
console.log("Error: ", error); | ||
}); | ||
import { DataSource } from "typeorm"; | ||
|
||
import Post from "./model/Post.js"; | ||
import Category from "./model/Category.js"; | ||
|
||
const dataSource = new DataSource({ | ||
type: "better-sqlite3", | ||
database: "app3-es6.db", | ||
synchronize: true, | ||
logging: false, | ||
entities: [Post.schema, Category.schema], | ||
}); | ||
|
||
await dataSource.initialize(); | ||
|
||
const category1 = new Category(1, "TypeScript"); | ||
const category2 = new Category(2, "Programming"); | ||
|
||
await Category.save([category1, category2]); | ||
|
||
const post = new Post(); | ||
post.title = "Control flow based type analysis"; | ||
post.text = | ||
"TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters."; | ||
post.categories = [category1, category2]; | ||
|
||
const savedPost = await post.save(); | ||
console.log("Post has been saved: ", savedPost); | ||
console.log("Now lets load all posts: "); | ||
|
||
const allPosts = await Post.find({ relations: { categories: true } }); | ||
|
||
console.log("All posts: ", allPosts); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"checkJs": true, | ||
"module": "Node16", | ||
"strict": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
/*export */ class Category { | ||
constructor(id, name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
import { BaseEntity, EntitySchema } from "typeorm"; | ||
|
||
export default class Category extends BaseEntity { | ||
/** | ||
* | ||
* @param {number} [id] | ||
* @param {string} [name] | ||
*/ | ||
constructor(id, name) { | ||
super(); | ||
this.id = id; | ||
this.name = name; | ||
} | ||
} | ||
|
||
module.exports = { | ||
Category: Category | ||
}; | ||
Category.definition = /** @type {const} */ ({ | ||
name: "Category", | ||
target: Category, | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: "int", | ||
generated: true, | ||
}, | ||
name: { | ||
type: "varchar", | ||
}, | ||
}, | ||
}); | ||
|
||
Category.schema = new EntitySchema(Category.definition); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,47 @@ | ||
/*export */ class Post { | ||
constructor(id, title, text, categories) { | ||
this.id = id; | ||
this.title = title; | ||
this.text = text; | ||
this.categories = categories; | ||
} | ||
import { BaseEntity, EntitySchema } from "typeorm"; | ||
|
||
export default class Post extends BaseEntity { | ||
/** | ||
* | ||
* @param {number} [id] | ||
* @param {string} [title] | ||
* @param {string} [text] | ||
* @param {import('./Category.js').default[]} [categories] | ||
*/ | ||
constructor(id, title, text, categories) { | ||
super(); | ||
this.id = id; | ||
this.title = title; | ||
this.text = text; | ||
this.categories = categories; | ||
} | ||
} | ||
|
||
module.exports = { | ||
Post: Post | ||
}; | ||
Post.definition = /** @type {const} */ ({ | ||
name: "Post", | ||
target: Post, | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: "int", | ||
generated: true, | ||
}, | ||
title: { | ||
type: "varchar", | ||
}, | ||
text: { | ||
type: "text", | ||
}, | ||
}, | ||
relations: { | ||
categories: { | ||
target: "Category", | ||
type: "many-to-many", | ||
joinTable: true, | ||
cascade: true, | ||
}, | ||
}, | ||
}); | ||
|
||
// @ts-expect-error | ||
Post.schema = new EntitySchema(Post.definition); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type": "module"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
type Nullables<T> = { | ||
[P in keyof T]: T[P] extends { nullable: true } ? P : never; | ||
}[keyof T]; | ||
|
||
type Type<T> = T extends "varchar" | "text" | ||
? string | ||
: T extends "boolean" | ||
? boolean | ||
: T extends "int" | ||
? number | ||
: T extends "datetime" | ||
? Date | ||
: T extends "blob" | ||
? Buffer | ||
: unknown; | ||
|
||
type ColumnProps<T> = { | ||
-readonly [P in Exclude<keyof T, Nullables<T>>]: Type<T[P]["type"]>; | ||
} & { | ||
-readonly [P in Nullables<T>]?: Type<T[P]["type"]>; | ||
}; | ||
|
||
type Model<T> = import("./models.js").Models[T]; | ||
|
||
type RelationProps<T> = { | ||
-readonly [P in keyof T]: T[P]["type"] extends "many-to-many" | "one-to-many" | ||
? Model<T[P]["target"]>[] | ||
: T[P]["type"] extends "many-to-one" | "one-to-one" | ||
? Model<T[P]["target"]> | ||
: unknown; | ||
}; | ||
|
||
/** Helper type to automatically extract props from schema definition */ | ||
export type EntityProps<T> = ColumnProps<T["columns"]> & | ||
RelationProps<T["relations"]>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { EntityProps } from "./helpers.js"; | ||
|
||
/** Add your models here to get automatic types for them */ | ||
|
||
import Category from "../model/Category.js"; | ||
import Post from "../model/Category.js"; | ||
|
||
module "../model/Category.js" { | ||
export type CategoryProps = EntityProps<typeof Category.definition>; | ||
export default interface Category extends CategoryProps {} | ||
} | ||
|
||
module "../model/Post.js" { | ||
export type PostProps = EntityProps<typeof Post.definition>; | ||
export default interface Post extends PostProps {} | ||
} | ||
|
||
export type Models = { | ||
Category: Category; | ||
Post: Post; | ||
}; |