Using with JavaScript

TypeORM can be used not only with TypeScript, but also with JavaScript.Everything is the same, except you need to omit types and if your platform does not support ES6 classes then you need to define objects with all required metadata.

app.js
  1. var typeorm = require("typeorm");
  2. typeorm.createConnection({
  3. type: "postgres",
  4. host: "localhost",
  5. port: 5432,
  6. username: "test",
  7. password: "admin",
  8. database: "test",
  9. synchronize: true,
  10. entitySchemas: [
  11. require("./entity/Post"),
  12. require("./entity/Category")
  13. ]
  14. }).then(function (connection) {
  15. var category1 = {
  16. name: "TypeScript"
  17. };
  18. var category2 = {
  19. name: "Programming"
  20. };
  21. var post = {
  22. title: "Control flow based type analysis",
  23. text: "TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.",
  24. categories: [
  25. category1, category2
  26. ]
  27. };
  28. var postRepository = connection.getRepository("Post");
  29. postRepository.save(post)
  30. .then(function(savedPost) {
  31. console.log("Post has been saved: ", savedPost);
  32. console.log("Now lets load all posts: ");
  33. return postRepository.find();
  34. })
  35. .then(function(allPosts) {
  36. console.log("All posts: ", allPosts);
  37. });
  38. }).catch(function(error) {
  39. console.log("Error: ", error);
  40. });
entity/Category.js
  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. };
entity/Post.js
  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. cascade: true
  22. }
  23. }
  24. };

You can checkout this example typeorm/javascript-example to learn more.