├── .gitignore ├── README.md ├── examples.txt ├── index.js ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # parcel-bundler cache (https://parceljs.org/) 61 | .cache 62 | 63 | # next.js build output 64 | .next 65 | 66 | # nuxt.js build output 67 | .nuxt 68 | 69 | # vuepress build output 70 | .vuepress/dist 71 | 72 | # Serverless directories 73 | .serverless 74 | 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sequelize-typescript-associations 2 | 3 | Generate ModelInstance interfaces for your sequelize models. The ModelInstance interface should include type definitions (using Sequelize mixins) for all functions for working with associations. 4 | 5 | ## Usage 6 | 7 | ```shell 8 | $ yarn gen -b User -a Collection Collection Image Image User -s AssignerCollection AssigneeCollection ProfilePhoto ImageCreated InvitedBy -p AssignerCollections AssigneeCollections _ ImagesCreated _ -t HasMany BelongsToMany BelongsTo HasMany BelongsTo -j _ CollectionAssignee _ _ _ 9 | ``` 10 | 11 | This will generate the following interface: 12 | 13 | ```typescript 14 | export interface UserInstance extends Sequelize.Instance, UserAttributes { 15 | getAssignerCollections: Sequelize.HasManyGetAssociationsMixin; 16 | setAssignerCollections: Sequelize.HasManySetAssociationsMixin; 17 | addAssignerCollections: Sequelize.HasManyAddAssociationsMixin; 18 | addAssignerCollection: Sequelize.HasManyAddAssociationMixin; 19 | createAssignerCollection: Sequelize.HasManyCreateAssociationMixin; 20 | removeAssignerCollection: Sequelize.HasManyRemoveAssociationMixin; 21 | removeAssignerCollections: Sequelize.HasManyRemoveAssociationsMixin; 22 | hasAssignerCollection: Sequelize.HasManyHasAssociationMixin; 23 | hasAssignerCollections: Sequelize.HasManyHasAssociationsMixin; 24 | countAssignerCollections: Sequelize.HasManyCountAssociationsMixin; 25 | 26 | getAssigneeCollections: Sequelize.BelongsToManyGetAssociationsMixin; 27 | setAssigneeCollections: Sequelize.BelongsToManySetAssociationsMixin; 28 | addAssigneeCollections: Sequelize.BelongsToManyAddAssociationsMixin; 29 | addAssigneeCollection: Sequelize.BelongsToManyAddAssociationMixin; 30 | createAssigneeCollection: Sequelize.BelongsToManyCreateAssociationMixin; 31 | removeAssigneeCollection: Sequelize.BelongsToManyRemoveAssociationMixin; 32 | removeAssigneeCollections: Sequelize.BelongsToManyRemoveAssociationsMixin; 33 | hasAssigneeCollection: Sequelize.BelongsToManyHasAssociationMixin; 34 | hasAssigneeCollections: Sequelize.BelongsToManyHasAssociationsMixin; 35 | countAssigneeCollections: Sequelize.BelongsToManyCountAssociationsMixin; 36 | 37 | getProfilePhoto: Sequelize.BelongsToGetAssociationMixin; 38 | setProfilePhoto: Sequelize.BelongsToSetAssociationMixin; 39 | createProfilePhoto: Sequelize.BelongsToCreateAssociationMixin; 40 | 41 | getImagesCreated: Sequelize.HasManyGetAssociationsMixin; 42 | setImagesCreated: Sequelize.HasManySetAssociationsMixin; 43 | addImagesCreated: Sequelize.HasManyAddAssociationsMixin; 44 | addImageCreated: Sequelize.HasManyAddAssociationMixin; 45 | createImageCreated: Sequelize.HasManyCreateAssociationMixin; 46 | removeImageCreated: Sequelize.HasManyRemoveAssociationMixin; 47 | removeImagesCreated: Sequelize.HasManyRemoveAssociationsMixin; 48 | hasImageCreated: Sequelize.HasManyHasAssociationMixin; 49 | hasImagesCreated: Sequelize.HasManyHasAssociationsMixin; 50 | countImagesCreated: Sequelize.HasManyCountAssociationsMixin; 51 | 52 | getInvitedBy: Sequelize.BelongsToGetAssociationMixin; 53 | setInvitedBy: Sequelize.BelongsToSetAssociationMixin; 54 | createInvitedBy: Sequelize.BelongsToCreateAssociationMixin; 55 | }; 56 | ``` 57 | 58 | The usage is as follows. 59 | 60 | - The argument following the -b flag gives the base model for which we are creating the interface for. 61 | 62 | - The array of arguments following the -a flag give the model name's for the models for which we are creating associations for. 63 | 64 | - The array of arguments following the -s flag give the aliases you want to give for each association, in singular form. 65 | 66 | - The array of arguments following the -p flag give the aliases you want to give for each association, in plural form. 67 | 68 | - The array of arguments following the -t flag give the type of association, i.e. on of BelongsTo, BelongsToMany, HasOne, or HasMany. These should match the function used in to create the association in the Model.associate function. 69 | 70 | - The array of arguments following the -j flag give the name of the join table to use for the BelongsToMany (n:m) association. 71 | 72 | Note that, 73 | 74 | - the i'th argument in each array correspond to each other. 75 | 76 | - if giving an argument for the i'th association doesn't apply, an underscore should be given. E.g., in above example, a User only has one ProfilePhoto, so it doesn't have a plural. Similarly, for InvitedBy. Also observe, only AssigneeCollections is a many-to-many (as BelongsToMany is used to define it) so we don't need to specify join tables for the other associations. 77 | 78 | ## Using Custom Join Tables 79 | 80 | In Sequelize, one may simply specify a join table name on both BelongsToMany calls to have sequelize automatically create a join table in the schema. E.g, 81 | 82 | ```typescript 83 | Collection.BelongsToMany(Image, { through: "CollectionImage" }); 84 | Image.BelongsToMany(Collection, { through: "CollectionImage" }); 85 | ``` 86 | 87 | If you want to add more attributes to the join table, or customise it in some way, you can define the join table just like any other model and than set that to the `through` field. E.g., 88 | 89 | ```typescript 90 | User.BelongsToMany(Collection, { through: CollectionAssigneeAttributes }); 91 | Collection.BelongsToMany(User, { through: CollectionAssigneeAttributes }); 92 | ``` 93 | 94 | Which is chosen has implications on the mixin definitions. Consider the mixins for the above to n:m associations. The first is defined as follows. Note the third type parameter to the mixin. 95 | 96 | ```typescript 97 | setImages: Sequelize.BelongsToManySetAssociationsMixin 98 | ``` 99 | 100 | In this case, we only want to pass in the string of the join table name. In the other case, the join table is defined manually, so the join tables Attribute's interface needs to be passed in as the last parameter. 101 | 102 | ```typescript 103 | setAssigneeCollections: Sequelize.BelongsToManySetAssociationsMixin; 104 | ``` 105 | 106 | This library supports both. To force the latter case, simply pass in a join table model name, e.g. `-j CollectionAssignee`, and the script will automatically put `CollectionAssigneeAttributes` in the third type parameter. For the former case, wrap the join table name in double quotes (probably have to be escaped at command line). E.g., `-j \"CollectionImage\"` will put `"CollectionImage"` in the third type parameter. 107 | 108 | ## Known Issues 109 | 110 | This library assumes that the primary key of the association will always be its `id` field. This is not always the case (e.g. the primary key of an image could be its hash). 111 | -------------------------------------------------------------------------------- /examples.txt: -------------------------------------------------------------------------------- 1 | yarn run v1.7.0 2 | $ node index.js -b CollectionImage -a User -s ActionedBy -p _ -t BelongsTo -j _ 3 | export interface CollectionImageInstance extends Sequelize.Instance, CollectionImageAttributes { 4 | 5 | getActionedBy: Sequelize.BelongsToGetAssociationMixin; 6 | setActionedBy: Sequelize.BelongsToSetAssociationMixin; 7 | createActionedBy: Sequelize.BelongsToCreateAssociationMixin; 8 | 9 | }; 10 | 11 | Done in 0.16s. 12 | yarn run v1.7.0 13 | $ node index.js -b Image -a Tag Collection User User Annotation AnnotationHistory ModelDataSet -s Tag Collection DeletedBy CreatedBy Annotation AnnotationHistory ModelDataSet -p Tags Collections _ _ Annotations AnnotationHistories ModelDataSets -t BelongsToMany BelongsToMany BelongsTo BelongsTo HasMany HasMany BelongsToMany -j '"ImageTag"' CollectionImage _ _ _ _ ModelDataSetImage 14 | export interface ImageInstance extends Sequelize.Instance, ImageAttributes { 15 | 16 | getTags: Sequelize.BelongsToManyGetAssociationsMixin; 17 | setTags: Sequelize.BelongsToManySetAssociationsMixin; 18 | addTags: Sequelize.BelongsToManyAddAssociationsMixin; 19 | addTag: Sequelize.BelongsToManyAddAssociationMixin; 20 | createTag: Sequelize.BelongsToManyCreateAssociationMixin; 21 | removeTag: Sequelize.BelongsToManyRemoveAssociationMixin; 22 | removeTags: Sequelize.BelongsToManyRemoveAssociationsMixin; 23 | hasTag: Sequelize.BelongsToManyHasAssociationMixin; 24 | hasTags: Sequelize.BelongsToManyHasAssociationsMixin; 25 | countTags: Sequelize.BelongsToManyCountAssociationsMixin; 26 | 27 | getCollections: Sequelize.BelongsToManyGetAssociationsMixin; 28 | setCollections: Sequelize.BelongsToManySetAssociationsMixin; 29 | addCollections: Sequelize.BelongsToManyAddAssociationsMixin; 30 | addCollection: Sequelize.BelongsToManyAddAssociationMixin; 31 | createCollection: Sequelize.BelongsToManyCreateAssociationMixin; 32 | removeCollection: Sequelize.BelongsToManyRemoveAssociationMixin; 33 | removeCollections: Sequelize.BelongsToManyRemoveAssociationsMixin; 34 | hasCollection: Sequelize.BelongsToManyHasAssociationMixin; 35 | hasCollections: Sequelize.BelongsToManyHasAssociationsMixin; 36 | countCollections: Sequelize.BelongsToManyCountAssociationsMixin; 37 | 38 | getDeletedBy: Sequelize.BelongsToGetAssociationMixin; 39 | setDeletedBy: Sequelize.BelongsToSetAssociationMixin; 40 | createDeletedBy: Sequelize.BelongsToCreateAssociationMixin; 41 | 42 | getCreatedBy: Sequelize.BelongsToGetAssociationMixin; 43 | setCreatedBy: Sequelize.BelongsToSetAssociationMixin; 44 | createCreatedBy: Sequelize.BelongsToCreateAssociationMixin; 45 | 46 | getAnnotations: Sequelize.HasManyGetAssociationsMixin; 47 | setAnnotations: Sequelize.HasManySetAssociationsMixin; 48 | addAnnotations: Sequelize.HasManyAddAssociationsMixin; 49 | addAnnotation: Sequelize.HasManyAddAssociationMixin; 50 | createAnnotation: Sequelize.HasManyCreateAssociationMixin; 51 | removeAnnotation: Sequelize.HasManyRemoveAssociationMixin; 52 | removeAnnotations: Sequelize.HasManyRemoveAssociationsMixin; 53 | hasAnnotation: Sequelize.HasManyHasAssociationMixin; 54 | hasAnnotations: Sequelize.HasManyHasAssociationsMixin; 55 | countAnnotations: Sequelize.HasManyCountAssociationsMixin; 56 | 57 | getAnnotationHistories: Sequelize.HasManyGetAssociationsMixin; 58 | setAnnotationHistories: Sequelize.HasManySetAssociationsMixin; 59 | addAnnotationHistories: Sequelize.HasManyAddAssociationsMixin; 60 | addAnnotationHistory: Sequelize.HasManyAddAssociationMixin; 61 | createAnnotationHistory: Sequelize.HasManyCreateAssociationMixin; 62 | removeAnnotationHistory: Sequelize.HasManyRemoveAssociationMixin; 63 | removeAnnotationHistories: Sequelize.HasManyRemoveAssociationsMixin; 64 | hasAnnotationHistory: Sequelize.HasManyHasAssociationMixin; 65 | hasAnnotationHistories: Sequelize.HasManyHasAssociationsMixin; 66 | countAnnotationHistories: Sequelize.HasManyCountAssociationsMixin; 67 | 68 | getModelDataSets: Sequelize.BelongsToManyGetAssociationsMixin; 69 | setModelDataSets: Sequelize.BelongsToManySetAssociationsMixin; 70 | addModelDataSets: Sequelize.BelongsToManyAddAssociationsMixin; 71 | addModelDataSet: Sequelize.BelongsToManyAddAssociationMixin; 72 | createModelDataSet: Sequelize.BelongsToManyCreateAssociationMixin; 73 | removeModelDataSet: Sequelize.BelongsToManyRemoveAssociationMixin; 74 | removeModelDataSets: Sequelize.BelongsToManyRemoveAssociationsMixin; 75 | hasModelDataSet: Sequelize.BelongsToManyHasAssociationMixin; 76 | hasModelDataSets: Sequelize.BelongsToManyHasAssociationsMixin; 77 | countModelDataSets: Sequelize.BelongsToManyCountAssociationsMixin; 78 | 79 | }; 80 | 81 | Done in 0.15s. 82 | yarn run v1.7.0 83 | $ node index.js -b Model -a ClassGroup ModelDataSet -s ClassGroup DataSet -p ClassGroups DataSets -t BelongsTo HasMany -j _ _ 84 | export interface ModelInstance extends Sequelize.Instance, ModelAttributes { 85 | 86 | getClassGroup: Sequelize.BelongsToGetAssociationMixin; 87 | setClassGroup: Sequelize.BelongsToSetAssociationMixin; 88 | createClassGroup: Sequelize.BelongsToCreateAssociationMixin; 89 | 90 | getDataSets: Sequelize.HasManyGetAssociationsMixin; 91 | setDataSets: Sequelize.HasManySetAssociationsMixin; 92 | addDataSets: Sequelize.HasManyAddAssociationsMixin; 93 | addDataSet: Sequelize.HasManyAddAssociationMixin; 94 | createDataSet: Sequelize.HasManyCreateAssociationMixin; 95 | removeDataSet: Sequelize.HasManyRemoveAssociationMixin; 96 | removeDataSets: Sequelize.HasManyRemoveAssociationsMixin; 97 | hasDataSet: Sequelize.HasManyHasAssociationMixin; 98 | hasDataSets: Sequelize.HasManyHasAssociationsMixin; 99 | countDataSets: Sequelize.HasManyCountAssociationsMixin; 100 | 101 | }; 102 | 103 | Done in 0.17s. 104 | yarn run v1.7.0 105 | $ node index.js -b ModelDataSet -a Model Image -s Model Image -p _ Images -t BelongsTo BelongsToMany -j _ ModelDataSetImage 106 | export interface ModelDataSetInstance extends Sequelize.Instance, ModelDataSetAttributes { 107 | 108 | getModel: Sequelize.BelongsToGetAssociationMixin; 109 | setModel: Sequelize.BelongsToSetAssociationMixin; 110 | createModel: Sequelize.BelongsToCreateAssociationMixin; 111 | 112 | getImages: Sequelize.BelongsToManyGetAssociationsMixin; 113 | setImages: Sequelize.BelongsToManySetAssociationsMixin; 114 | addImages: Sequelize.BelongsToManyAddAssociationsMixin; 115 | addImage: Sequelize.BelongsToManyAddAssociationMixin; 116 | createImage: Sequelize.BelongsToManyCreateAssociationMixin; 117 | removeImage: Sequelize.BelongsToManyRemoveAssociationMixin; 118 | removeImages: Sequelize.BelongsToManyRemoveAssociationsMixin; 119 | hasImage: Sequelize.BelongsToManyHasAssociationMixin; 120 | hasImages: Sequelize.BelongsToManyHasAssociationsMixin; 121 | countImages: Sequelize.BelongsToManyCountAssociationsMixin; 122 | 123 | }; 124 | 125 | Done in 0.17s. 126 | yarn run v1.7.0 127 | $ node index.js -b User -a Collection Collection CollectionImage Image Image -s AssignerCollection AssigneeCollection CollectionImageActionedOn ImageCreated ImageDeleted -p AssignerCollections AssigneeCollections CollectionImagesActionedOn ImagesCreated ImagesDeleted -t HasMany BelongsToMany HasMany HasMany HasMany -j _ '"CollectionAssignee"' _ _ _ 128 | export interface UserInstance extends Sequelize.Instance, UserAttributes { 129 | 130 | getAssignerCollections: Sequelize.HasManyGetAssociationsMixin; 131 | setAssignerCollections: Sequelize.HasManySetAssociationsMixin; 132 | addAssignerCollections: Sequelize.HasManyAddAssociationsMixin; 133 | addAssignerCollection: Sequelize.HasManyAddAssociationMixin; 134 | createAssignerCollection: Sequelize.HasManyCreateAssociationMixin; 135 | removeAssignerCollection: Sequelize.HasManyRemoveAssociationMixin; 136 | removeAssignerCollections: Sequelize.HasManyRemoveAssociationsMixin; 137 | hasAssignerCollection: Sequelize.HasManyHasAssociationMixin; 138 | hasAssignerCollections: Sequelize.HasManyHasAssociationsMixin; 139 | countAssignerCollections: Sequelize.HasManyCountAssociationsMixin; 140 | 141 | getAssigneeCollections: Sequelize.BelongsToManyGetAssociationsMixin; 142 | setAssigneeCollections: Sequelize.BelongsToManySetAssociationsMixin; 143 | addAssigneeCollections: Sequelize.BelongsToManyAddAssociationsMixin; 144 | addAssigneeCollection: Sequelize.BelongsToManyAddAssociationMixin; 145 | createAssigneeCollection: Sequelize.BelongsToManyCreateAssociationMixin; 146 | removeAssigneeCollection: Sequelize.BelongsToManyRemoveAssociationMixin; 147 | removeAssigneeCollections: Sequelize.BelongsToManyRemoveAssociationsMixin; 148 | hasAssigneeCollection: Sequelize.BelongsToManyHasAssociationMixin; 149 | hasAssigneeCollections: Sequelize.BelongsToManyHasAssociationsMixin; 150 | countAssigneeCollections: Sequelize.BelongsToManyCountAssociationsMixin; 151 | 152 | getCollectionImagesActionedOn: Sequelize.HasManyGetAssociationsMixin; 153 | setCollectionImagesActionedOn: Sequelize.HasManySetAssociationsMixin; 154 | addCollectionImagesActionedOn: Sequelize.HasManyAddAssociationsMixin; 155 | addCollectionImageActionedOn: Sequelize.HasManyAddAssociationMixin; 156 | createCollectionImageActionedOn: Sequelize.HasManyCreateAssociationMixin; 157 | removeCollectionImageActionedOn: Sequelize.HasManyRemoveAssociationMixin; 158 | removeCollectionImagesActionedOn: Sequelize.HasManyRemoveAssociationsMixin; 159 | hasCollectionImageActionedOn: Sequelize.HasManyHasAssociationMixin; 160 | hasCollectionImagesActionedOn: Sequelize.HasManyHasAssociationsMixin; 161 | countCollectionImagesActionedOn: Sequelize.HasManyCountAssociationsMixin; 162 | 163 | getImagesCreated: Sequelize.HasManyGetAssociationsMixin; 164 | setImagesCreated: Sequelize.HasManySetAssociationsMixin; 165 | addImagesCreated: Sequelize.HasManyAddAssociationsMixin; 166 | addImageCreated: Sequelize.HasManyAddAssociationMixin; 167 | createImageCreated: Sequelize.HasManyCreateAssociationMixin; 168 | removeImageCreated: Sequelize.HasManyRemoveAssociationMixin; 169 | removeImagesCreated: Sequelize.HasManyRemoveAssociationsMixin; 170 | hasImageCreated: Sequelize.HasManyHasAssociationMixin; 171 | hasImagesCreated: Sequelize.HasManyHasAssociationsMixin; 172 | countImagesCreated: Sequelize.HasManyCountAssociationsMixin; 173 | 174 | getImagesDeleted: Sequelize.HasManyGetAssociationsMixin; 175 | setImagesDeleted: Sequelize.HasManySetAssociationsMixin; 176 | addImagesDeleted: Sequelize.HasManyAddAssociationsMixin; 177 | addImageDeleted: Sequelize.HasManyAddAssociationMixin; 178 | createImageDeleted: Sequelize.HasManyCreateAssociationMixin; 179 | removeImageDeleted: Sequelize.HasManyRemoveAssociationMixin; 180 | removeImagesDeleted: Sequelize.HasManyRemoveAssociationsMixin; 181 | hasImageDeleted: Sequelize.HasManyHasAssociationMixin; 182 | hasImagesDeleted: Sequelize.HasManyHasAssociationsMixin; 183 | countImagesDeleted: Sequelize.HasManyCountAssociationsMixin; 184 | 185 | }; 186 | 187 | Done in 0.15s. 188 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ArgumentParser = require('argparse').ArgumentParser; 4 | 5 | const initArgParser = (ArgParserClass) => { 6 | const parser = new ArgParserClass({ 7 | version: '0.0.1', 8 | addHelp: true, 9 | description: 'generate mixins given association name' 10 | }); 11 | 12 | parser.addArgument( 13 | [ '-b', '--baseModelName' ], 14 | { help: 'name of base model' }, 15 | ) 16 | 17 | parser.addArgument( 18 | [ '-a', '--associationModelName' ], 19 | { help: 'array of name of model of associations', nargs: '+' }, 20 | ); 21 | 22 | parser.addArgument( 23 | [ '-s', '--singular' ], 24 | { help: 'array of singular of alias names (the alias for association, may be same as associationModelName)', nargs: '+' } 25 | ); 26 | 27 | parser.addArgument( 28 | [ '-p', '--plural' ], 29 | { help: 'array of plural of alias names (if n/a then use _)', nargs: '+' }, 30 | ); 31 | 32 | parser.addArgument( 33 | [ '-t', '--type' ], 34 | { help: 'array of either BelongsTo or HasMany or HasOne or BelongsToMany', nargs: '+' } 35 | ); 36 | 37 | parser.addArgument( 38 | [ '-j', '--joinTable' ], 39 | { help: 'array of join table names, either JoinTableName or an join table attributes e.g. JoinTableAttributes', nargs: '+' } 40 | ) 41 | 42 | return parser; 43 | } 44 | 45 | // FIXME: this doesn't seem to work 46 | // const assertArgLengthsAreTheSame = (args) => { 47 | // ["associationModelName", "plural", "type", "joinTable"].map(k => console.log(args[k].length)); 48 | // console.log(args.associationModelName.length === args.plural.length === args.type.length === args.joinTable.length); 49 | // return args.associationModelName.length === args.plural.length === args.type.length === args.joinTable.length; 50 | // }; 51 | 52 | const addMixinsBelongsTo = (associationModelName, singular) => { 53 | const AssocInstance = `${associationModelName}Instance`; 54 | const AssocAttributes = `${associationModelName}Attributes`; 55 | return ( 56 | ` 57 | get${singular}: Sequelize.BelongsToGetAssociationMixin<${AssocInstance}>; 58 | set${singular}: Sequelize.BelongsToSetAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 59 | create${singular}: Sequelize.BelongsToCreateAssociationMixin<${AssocAttributes}>; 60 | ` 61 | ); 62 | }; 63 | 64 | const addMixinsHasOne = (associationModelName, singular) => { 65 | const AssocInstance = `${associationModelName}Instance`; 66 | const AssocAttributes = `${associationModelName}Attributes`; 67 | return ( 68 | ` 69 | get${singular}: Sequelize.HasOneGetAssociationMixin<${AssocInstance}>; 70 | set${singular}: Sequelize.HasOneSetAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 71 | create${singular}: Sequelize.HasOneCreateAssociationMixin<${AssocAttributes}>; 72 | ` 73 | ); 74 | }; 75 | 76 | const addMixinsHasMany = (associationModelName, singular, plural) => { 77 | const AssocInstance = `${associationModelName}Instance`; 78 | const AssocAttributes = `${associationModelName}Attributes`; 79 | return ( 80 | ` 81 | get${plural}: Sequelize.HasManyGetAssociationsMixin<${AssocInstance}>; 82 | set${plural}: Sequelize.HasManySetAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"]>; 83 | add${plural}: Sequelize.HasManyAddAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"]>; 84 | add${singular}: Sequelize.HasManyAddAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 85 | create${singular}: Sequelize.HasManyCreateAssociationMixin<${AssocAttributes}, ${AssocInstance}>; 86 | remove${singular}: Sequelize.HasManyRemoveAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 87 | remove${plural}: Sequelize.HasManyRemoveAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"]>; 88 | has${singular}: Sequelize.HasManyHasAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 89 | has${plural}: Sequelize.HasManyHasAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"]>; 90 | count${plural}: Sequelize.HasManyCountAssociationsMixin; 91 | ` 92 | ); 93 | }; 94 | 95 | const addMixinsBelongsToMany = (associationModelName, singular, plural, joinTableName) => { 96 | const AssocInstance = `${associationModelName}Instance`; 97 | const AssocAttributes = `${associationModelName}Attributes`; 98 | const JoinTableAttributes = joinTableName[0] === '\"' || joinTableName[0] === "\'" 99 | ? joinTableName 100 | : `${joinTableName}Attributes`; 101 | 102 | return ( 103 | ` 104 | get${plural}: Sequelize.BelongsToManyGetAssociationsMixin<${AssocInstance}>; 105 | set${plural}: Sequelize.BelongsToManySetAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"], ${JoinTableAttributes}>; 106 | add${plural}: Sequelize.BelongsToManyAddAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"], ${JoinTableAttributes}>; 107 | add${singular}: Sequelize.BelongsToManyAddAssociationMixin<${AssocInstance}, ${AssocInstance}["id"], ${JoinTableAttributes}>; 108 | create${singular}: Sequelize.BelongsToManyCreateAssociationMixin<${AssocAttributes}, ${AssocInstance}["id"], ${JoinTableAttributes}>; 109 | remove${singular}: Sequelize.BelongsToManyRemoveAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 110 | remove${plural}: Sequelize.BelongsToManyRemoveAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"]>; 111 | has${singular}: Sequelize.BelongsToManyHasAssociationMixin<${AssocInstance}, ${AssocInstance}["id"]>; 112 | has${plural}: Sequelize.BelongsToManyHasAssociationsMixin<${AssocInstance}, ${AssocInstance}["id"]>; 113 | count${plural}: Sequelize.BelongsToManyCountAssociationsMixin; 114 | ` 115 | ); 116 | }; 117 | 118 | const generateInterface = ({ baseModelName, associationModelName, singular, plural, type, joinTable }) => { 119 | let mixinsString = ''; 120 | for (let i = 0; i < associationModelName.length; i++) { 121 | if (type[i] === "BelongsTo") { 122 | mixinsString += addMixinsBelongsTo(associationModelName[i], singular[i]); 123 | } else if (type[i] === "HasOne") { 124 | mixinsString += addMixinsHasOne(associationModelName[i], singular[i]); 125 | } else if (type[i] === "HasMany") { 126 | mixinsString += addMixinsHasMany(associationModelName[i], singular[i], plural[i]); 127 | } else if (type[i] === "BelongsToMany") { 128 | mixinsString += addMixinsBelongsToMany(associationModelName[i], singular[i], plural[i], joinTable[i]); 129 | } else { 130 | console.error("incorrect type: ", type[i]); 131 | return; 132 | } 133 | } 134 | 135 | let interfaceString = 136 | `export interface ${baseModelName}Instance extends Sequelize.Instance<${baseModelName}Attributes>, ${baseModelName}Attributes { 137 | ${mixinsString} 138 | }; 139 | `; 140 | 141 | return interfaceString; 142 | }; 143 | 144 | const main = () => { 145 | const parser = initArgParser(ArgumentParser); 146 | const args = parser.parseArgs(); 147 | const interfaceString = generateInterface(args); 148 | console.log(interfaceString); 149 | } 150 | 151 | main(); 152 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sequelize-typescript-associations", 3 | "version": "0.0.1", 4 | "description": "generate mixins for ts sqlize ModelInstance interface", 5 | "main": "index.js", 6 | "author": "Ahmer Butt ", 7 | "license": "MIT", 8 | "private": true, 9 | "scripts": { 10 | "gen": "node index.js" 11 | }, 12 | "dependencies": { 13 | "argparse": "^1.0.10" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | argparse@^1.0.10: 6 | version "1.0.10" 7 | resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" 8 | dependencies: 9 | sprintf-js "~1.0.2" 10 | 11 | sprintf-js@~1.0.2: 12 | version "1.0.3" 13 | resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" 14 | --------------------------------------------------------------------------------