项目作者: synle

项目描述 :
Sequelize (SQL ORM for Node) Typescript Decorator that simplifies declarations of Sequelize models...
高级语言: TypeScript
项目地址: git://github.com/synle/sequelize-typescript-decorators.git
创建时间: 2020-03-17T20:35:49Z
项目社区:https://github.com/synle/sequelize-typescript-decorators

开源协议:

下载


CI Job Status
npm version

sequelize-typescript-decorators

This documents how I set up decorators and use them with sequelize (node JS ORM library) to reduce boilderplate

TODO’s

  • extract plumbing into a method and reuse it instead of having user of this library do it…
  • add support for other adapters: SQLITE, PG, etc…
  • deploy to npm modules instead of using github
  • integrate with CI pipeline to build stuffs automatically
  • add docs
  • add unit tests

Credentials

  1. DB_URL=mysql://root:StrongP@assword@127.0.0.1:3306/my_database

How to use

Install it

To install from npm

  1. npm install --save sequelize-typescript-decorators@^2
  2. # based on your database engine, you will need to include different things

Then declare it in your model…

In our example, Email can have one or many attachments. Keep that in mind for relationship

./models/schema.ts
  1. import {
  2. Relationship,
  3. table,
  4. attribute,
  5. relationship,
  6. index,
  7. } from "sequelize-typescript-decorators";
  8. import { DataTypes, Model } from "sequelize";
  9. @table("attachments", {
  10. timestamps: false,
  11. })
  12. @index([
  13. {
  14. unique: false,
  15. fields: ["messageId"],
  16. },
  17. {
  18. unique: false,
  19. fields: ["fileName"],
  20. },
  21. ])
  22. export class Attachment extends Model {
  23. @attribute(Attachment, {
  24. allowNull: false,
  25. primaryKey: true,
  26. })
  27. public id!: string;
  28. @attribute(Attachment, { allowNull: false })
  29. public messageId!: string;
  30. @attribute(Attachment, { allowNull: false })
  31. public mimeType!: string;
  32. @attribute(Attachment, { allowNull: false })
  33. public fileName!: string;
  34. @attribute(Attachment, { allowNull: false })
  35. public path!: string;
  36. }
  37. @table("emails", {
  38. timestamps: false,
  39. })
  40. @index([
  41. {
  42. unique: false,
  43. fields: ["threadId"],
  44. },
  45. {
  46. unique: false,
  47. fields: ["from"],
  48. },
  49. ])
  50. export class Email extends Model {
  51. @attribute(Email, {
  52. allowNull: false,
  53. primaryKey: true,
  54. })
  55. public id!: string;
  56. @attribute(Email, { allowNull: false })
  57. public threadId!: string;
  58. @attribute(Email, { allowNull: false })
  59. public from!: string;
  60. @attribute(Email)
  61. public to!: string;
  62. @attribute(Email)
  63. public bcc!: string;
  64. @attribute(Email, { type: DataTypes.TEXT })
  65. public body!: string;
  66. @attribute(Email, { type: DataTypes.TEXT })
  67. public rawBody!: string;
  68. @attribute(Email)
  69. public subject!: string;
  70. @attribute(Email)
  71. public date!: number;
  72. @attribute(Email)
  73. public headers!: string;
  74. @relationship(Email, {
  75. relationship: Relationship.hasMany,
  76. sourceKey: "id",
  77. foreignModel: Attachment,
  78. foreignKey: "messageId",
  79. as: "attachments",
  80. })
  81. public Attachments!: any[];
  82. }
  83. export default {
  84. Attachment,
  85. Email,
  86. };
./models/factory.ts
  1. import { Sequelize } from 'sequelize';
  2. import {
  3. initDatabase,
  4. } from 'sequelize-typescript-decorators';
  5. import Models from "./schema";
  6. /**
  7. * this routine will initialize the database, please only run this once per all...
  8. */
  9. export default async () => {
  10. // this is an example to connect to sqlite3
  11. // set up your connection accordingly
  12. const dbConnectionString = process.env.DB_URL || "";
  13. const sequelize = new Sequelize("note_synchronize", "username", "password", {
  14. dialect: "sqlite",
  15. storage: dbConnectionString,
  16. logging: false,
  17. });
  18. const models = Object.keys(Models).map((modelName) => Models[modelName]);
  19. await initDatabase(sequelize, models);
  20. };

How to call and use it?

Somewhere in your entry code, runs and wait for the init

  1. import initDatabase from "./src/models/factory";
  2. import Models from "./src/models/schema";
  3. async function _doWork(){
  4. await initDatabase();
  5. // start your work here
  6. // get list of emails and associated attachments...
  7. const matchedEmailsResponse = await Models.Email.findAll({
  8. include: [
  9. {
  10. model: Models.Attachment,
  11. required: false,
  12. },
  13. ],
  14. });
  15. }
  16. _doWork();

How to contribute?

Create PR against master.

Note on release pipeline

  1. version="$(cat package.json | jq .version)"
  2. git tag $version
  3. git push origin $version