Skip to content

Commit 7d7af2f

Browse files
author
Umed Khudoiberdiev
committed
initial commit
0 parents  commit 7d7af2f

File tree

14 files changed

+323
-0
lines changed

14 files changed

+323
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Example how to use TypeORM with JavaScript
2+
3+
1. clone repository
4+
2. run `npm i`
5+
3. run `node src/app1-es5/index.js` to run simple example of usage with ES5.
6+
4. run `node src/app2-es5-json-schemas/index.js` to run example of usage with ES5 + schemas defined in a JSON.
7+
5. run `node src/app2-es6/index.js` to run simple example of usage with ES6.

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "typeorm-javascript-example",
3+
"version": "0.0.1",
4+
"description": "Example how to use TypeORM with JavaScript - ES5, ES6, ES7, ESNext.",
5+
"license": "MIT",
6+
"readmeFilename": "README.md",
7+
"author": {
8+
"name": "Umed Khudoiberdiev",
9+
"email": "pleerock.me@gmail.com"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/typeorm/javascript-example.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/typeorm/javascript-example/issues"
17+
},
18+
"tags": [
19+
"orm",
20+
"javascript-orm",
21+
"typeorm-sample",
22+
"typeorm-example"
23+
],
24+
"devDependencies": {
25+
"typescript": "^2.0.2"
26+
},
27+
"dependencies": {
28+
"pg": "^6.1.0",
29+
"reflect-metadata": "^0.1.8",
30+
"typeorm": "0.0.2-alpha.62"
31+
}
32+
}

src/app1-es5/entity/Category.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
name: "Category",
3+
columns: {
4+
id: {
5+
primary: true,
6+
type: "int",
7+
generated: true
8+
},
9+
name: {
10+
type: "string"
11+
}
12+
}
13+
};

src/app1-es5/entity/Post.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
name: "Post",
3+
columns: {
4+
id: {
5+
primary: true,
6+
type: "int",
7+
generated: true
8+
},
9+
title: {
10+
type: "string"
11+
},
12+
text: {
13+
type: "text"
14+
}
15+
},
16+
relations: {
17+
categories: {
18+
target: "Category",
19+
type: "many-to-many",
20+
joinTable: true,
21+
cascadeInsert: true
22+
}
23+
}
24+
};

src/app1-es5/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var typeorm = require("typeorm");
2+
3+
typeorm.createConnection({
4+
driver: {
5+
type: "postgres",
6+
host: "localhost",
7+
port: 5432,
8+
username: "test",
9+
password: "admin",
10+
database: "test"
11+
},
12+
entitySchemas: [
13+
require("./entity/Post"),
14+
require("./entity/Category")
15+
],
16+
autoSchemaSync: true
17+
}).then(function (connection) {
18+
19+
var category1 = {
20+
name: "TypeScript"
21+
};
22+
var category2 = {
23+
name: "Programming"
24+
};
25+
26+
var post = {
27+
title: "Control flow based type analysis",
28+
text: "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.",
29+
categories: [
30+
category1, category2
31+
]
32+
};
33+
34+
var postRepository = connection.getRepository("Post");
35+
postRepository.persist(post)
36+
.then(function(savedPost) {
37+
console.log("Post has been saved: ", savedPost);
38+
console.log("Now lets load all posts: ");
39+
40+
return postRepository.find();
41+
})
42+
.then(function(allPosts) {
43+
console.log("All posts: ", allPosts);
44+
});
45+
46+
47+
}).catch(function(error) {
48+
console.log("Error: ", error);
49+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Category",
3+
"columns": {
4+
"id": {
5+
"primary": true,
6+
"type": "int",
7+
"generated": true
8+
},
9+
"name": {
10+
"type": "string"
11+
}
12+
}
13+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "Post",
3+
"columns": {
4+
"id": {
5+
"primary": true,
6+
"type": "int",
7+
"generated": true
8+
},
9+
"title": {
10+
"type": "string"
11+
},
12+
"text": {
13+
"type": "text"
14+
}
15+
},
16+
"relations": {
17+
"categories": {
18+
"target": "Category",
19+
"type": "many-to-many",
20+
"joinTable": true,
21+
"cascadeInsert": true
22+
}
23+
}
24+
}

src/app2-es5-json-schemas/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var typeorm = require("typeorm");
2+
3+
typeorm.createConnection({
4+
driver: {
5+
type: "postgres",
6+
host: "localhost",
7+
port: 5432,
8+
username: "test",
9+
password: "admin",
10+
database: "test"
11+
},
12+
entitySchemas: [
13+
__dirname + "/entity/*.json"
14+
],
15+
autoSchemaSync: true
16+
}).then(function (connection) {
17+
18+
var category1 = {
19+
name: "TypeScript"
20+
};
21+
var category2 = {
22+
name: "Programming"
23+
};
24+
25+
var post = {
26+
title: "Control flow based type analysis",
27+
text: "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.",
28+
categories: [
29+
category1, category2
30+
]
31+
};
32+
33+
var postRepository = connection.getRepository("Post");
34+
postRepository.persist(post)
35+
.then(function(savedPost) {
36+
console.log("Post has been saved: ", savedPost);
37+
console.log("Now lets load all posts: ");
38+
39+
return postRepository.find();
40+
})
41+
.then(function(allPosts) {
42+
console.log("All posts: ", allPosts);
43+
});
44+
45+
46+
}).catch(function(error) {
47+
console.log("Error: ", error);
48+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Category = require("../model/Category"); // import {Category} from "../model/Category";
2+
const CategorySchema = {
3+
target: Category,
4+
columns: {
5+
id: {
6+
primary: true,
7+
type: "int",
8+
generated: true
9+
},
10+
name: {
11+
type: "string"
12+
}
13+
}
14+
};
15+
16+
module.exports = {
17+
CategorySchema: CategorySchema
18+
};

0 commit comments

Comments
 (0)