@gapi/sequelize 1.8.122

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.prettierrc ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "singleQuote": true,
3
+ "printWidth": 80
4
+ }
package/README.md ADDED
@@ -0,0 +1,189 @@
1
+ # @Gapi/Sequelize
2
+
3
+ <!-- ![Build Status](http://gitlab.youvolio.com/gapi/gapi/badges/branch/build.svg) -->
4
+
5
+ #### @Gapi Sequelize module @StrongTyped
6
+
7
+ ##### For questions/issues you can write ticket [here](http://gitlab.youvolio.com/gapi/gapi-sequelize/issues)
8
+ ##### This module is intended to be used with [GAPI](https://github.com/Stradivario/gapi)
9
+
10
+ ## Installation and basic examples:
11
+ ##### To install this Gapi module, run:
12
+
13
+ ```bash
14
+ $ npm install @gapi/sequelize --save
15
+ ```
16
+
17
+ ## Consuming gapi-sequelize
18
+
19
+ ##### Import inside AppModule or CoreModule
20
+ ```typescript
21
+
22
+ import { Module } from '@rxdi/core';
23
+ import { SequelizeModule } from '@gapi/sequelize';
24
+
25
+ @Module({
26
+ imports: [
27
+ SequelizeModule.forRoot({
28
+ dialect: 'postgres',
29
+ host: process.env.DB_HOST || '',
30
+ port: process.env.DB_PORT || 5432,
31
+ username: process.env.DB_USERNAME || '',
32
+ password: process.env.DB_PASSWORD || '',
33
+ database: process.env.DB_NAME || 'your-database',
34
+ storage: ':memory:',
35
+ logging: false,
36
+ modelPaths: [process.cwd() + '/src/models'],
37
+ force: false
38
+ })
39
+ ]
40
+ })
41
+ export class CoreModule { }
42
+ ```
43
+
44
+ ##### Create folder root/src/models and put this testing User Typescript-Sequelize Model with Uppercase name example: "User.ts"
45
+ ```typescript
46
+
47
+ import {
48
+ Table,
49
+ Column,
50
+ Model,
51
+ CreatedAt,
52
+ UpdatedAt,
53
+ DeletedAt,
54
+ PrimaryKey,
55
+ AutoIncrement,
56
+ HasMany,
57
+ DataType,
58
+ BelongsToMany
59
+ } from 'sequelize-typescript';
60
+
61
+ export interface UserSettings {
62
+ sidebar: boolean;
63
+ }
64
+
65
+ @Table
66
+ export class User extends Model<User> {
67
+
68
+ @PrimaryKey
69
+ @AutoIncrement
70
+ @Column
71
+ id: number;
72
+
73
+ @Column
74
+ name: string;
75
+
76
+ @Column({
77
+ type: DataType.ENUM({ values: ['ADMIN', 'USER'] })
78
+ })
79
+ type: 'ADMIN' | 'USER';
80
+
81
+ @Column({
82
+ type: DataType.JSONB,
83
+ allowNull: true
84
+ })
85
+ settings: UserSettings;
86
+
87
+
88
+ @CreatedAt
89
+ creationDate: Date;
90
+
91
+ @UpdatedAt
92
+ updatedOn: Date;
93
+
94
+ @DeletedAt
95
+ deletionDate: Date;
96
+
97
+ }
98
+
99
+ ```
100
+
101
+ ##### Final use this class inside your services the following way
102
+
103
+ ```typescript
104
+ import { Service } from "@rxdi/core";
105
+ import { UserType } from "../types/user.type";
106
+ import { User } from '../../../models/User';
107
+
108
+ @Service()
109
+ export class AnotherService {
110
+ trimFirstLetter(username: string): string {
111
+ return username.charAt(1);
112
+ }
113
+ }
114
+
115
+ @Service()
116
+ export class UserService {
117
+ constructor(
118
+ private anotherService: AnotherService
119
+ ) { }
120
+
121
+ async findUser(id: number): Promise<User> {
122
+ return await User.findOne({ where: { id: id } });
123
+ }
124
+
125
+ async addUser(user: User): Promise<User> {
126
+ return await User.create(user);
127
+ }
128
+
129
+ async deleteUser(id: number) {
130
+ return await User.destroy({ where: { id: id } });
131
+ }
132
+
133
+ async updateUser(user: User) {
134
+ return await User.update(user, {
135
+ where: {
136
+ id: user.id
137
+ }
138
+ })
139
+ }
140
+
141
+ }
142
+
143
+ ```
144
+
145
+ #### Advanced getting sequelize instance to manage your sequelize connection
146
+
147
+ ```typescript
148
+ import { Service } from '@rxdi/core';
149
+ import { SequelizeService } from '@gapi/sequelize';
150
+
151
+ @Service()
152
+ export class SequelizePrivateService extends SequelizeService implements SequelizeService {
153
+ sequelize: Sequelize;
154
+ constructor() {
155
+ super({
156
+ dialect: 'postgres',
157
+ host: process.env.DB_HOST || '',
158
+ port: process.env.DB_PORT || '5432',
159
+ username: process.env.DB_USERNAME || '',
160
+ password: process.env.DB_PASSWORD || '',
161
+ name: process.env.DB_NAME || 'your-database',
162
+ storage: ':memory:',
163
+ logging: false,
164
+ modelPaths: [process.cwd() + '/src/models'],
165
+ force: false
166
+ })
167
+ }
168
+ }
169
+
170
+ ```
171
+
172
+ #### Next import SequelizePrivateService inside Core or AppModule
173
+
174
+ ```typescript
175
+ import { Module } from '@rxdi/core';
176
+ import { SequelizeModule } from '@gapi/sequelize';
177
+ import { SequelizePrivateService } from './services/sequelize/sequelize.service.ts';
178
+
179
+ @Module({
180
+ services: [SequelizePrivateService]
181
+ })
182
+ export class CoreModule { }
183
+
184
+ ```
185
+
186
+
187
+ TODO: Better documentation...
188
+
189
+ Enjoy ! :)
@@ -0,0 +1,7 @@
1
+ import { ModuleWithServices } from '@rxdi/core';
2
+ import { SequelizeConfig } from './sequelize.interface';
3
+ export declare class SequelizeModule {
4
+ static forRoot(config: SequelizeConfig): ModuleWithServices;
5
+ }
6
+ export * from './sequelize.interface';
7
+ export * from './sequelize-config.service';
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ };
15
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
17
+ };
18
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
19
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
20
+ return new (P || (P = Promise))(function (resolve, reject) {
21
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
22
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
23
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
24
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
25
+ });
26
+ };
27
+ var SequelizeModule_1;
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.SequelizeModule = void 0;
30
+ const core_1 = require("@rxdi/core");
31
+ const sequelize_typescript_1 = require("sequelize-typescript");
32
+ const sequelize_config_service_1 = require("./sequelize-config.service");
33
+ let SequelizeModule = SequelizeModule_1 = class SequelizeModule {
34
+ static forRoot(config) {
35
+ return {
36
+ module: SequelizeModule_1,
37
+ services: [
38
+ {
39
+ provide: sequelize_config_service_1.SEQUELIZE,
40
+ lazy: true,
41
+ useFactory: () => __awaiter(this, void 0, void 0, function* () {
42
+ const sequelize = new sequelize_typescript_1.Sequelize(config);
43
+ sequelize.addModels(config.models ? config.models : []);
44
+ yield sequelize.sync(config);
45
+ return sequelize;
46
+ }),
47
+ },
48
+ ],
49
+ };
50
+ }
51
+ };
52
+ SequelizeModule = SequelizeModule_1 = __decorate([
53
+ core_1.Module()
54
+ ], SequelizeModule);
55
+ exports.SequelizeModule = SequelizeModule;
56
+ __exportStar(require("./sequelize.interface"), exports);
57
+ __exportStar(require("./sequelize-config.service"), exports);
@@ -0,0 +1,2 @@
1
+ import { InjectionToken } from '@rxdi/core';
2
+ export declare const SEQUELIZE: InjectionToken<unknown>;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SEQUELIZE = void 0;
4
+ const core_1 = require("@rxdi/core");
5
+ exports.SEQUELIZE = new core_1.InjectionToken('gapi-sequelize-config-token');
@@ -0,0 +1,18 @@
1
+ import { SequelizeOptions } from 'sequelize-typescript';
2
+ import { Dialect } from 'sequelize/types';
3
+ export declare class SequelizeConfig implements SequelizeOptions {
4
+ dialect?: Dialect;
5
+ host?: string;
6
+ port?: number;
7
+ username?: string;
8
+ password?: string;
9
+ storage?: string;
10
+ logging?: boolean;
11
+ modelPaths?: string[];
12
+ models?: any[];
13
+ database?: string;
14
+ force?: boolean;
15
+ modifyFunctions?: {
16
+ sync?: () => {};
17
+ };
18
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SequelizeConfig = void 0;
4
+ class SequelizeConfig {
5
+ constructor() {
6
+ this.modifyFunctions = {};
7
+ }
8
+ }
9
+ exports.SequelizeConfig = SequelizeConfig;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@gapi/sequelize",
3
+ "version": "1.8.122",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/Stradivario/gapi-sequelize.git"
7
+ },
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "module": "./dist/index.js",
11
+ "typings": "./dist/index.d.ts",
12
+ "scripts": {
13
+ "patch": "npm run build && npm version patch && npm publish --update-readme --access public && npm run delete-dist",
14
+ "delete-dist": "rm -rf dist",
15
+ "clean": "git clean -dxf",
16
+ "lint": "npx eslint . --ext .ts",
17
+ "lint-fix": "npx eslint . --fix --ext .ts",
18
+ "build": "rm -rf dist && tsc || true"
19
+ },
20
+ "author": {
21
+ "name": "Kristian Tachev(Stradivario)",
22
+ "email": "kristiqn.tachev@gmail.com"
23
+ },
24
+ "keywords": [
25
+ "graphql",
26
+ "gapi",
27
+ "node"
28
+ ],
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "http://gitlab.youvolio.com/gapi/gapi-sequelize/issues"
32
+ },
33
+ "dependencies": {
34
+ "sequelize": "^5.8.2",
35
+ "sequelize-typescript": "^1.1.0"
36
+ },
37
+ "devDependencies": {
38
+ "@rxdi/core": "^0.7.114"
39
+ }
40
+ }
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { Module, ModuleWithServices } from '@rxdi/core';
2
+ import { Sequelize } from 'sequelize-typescript';
3
+
4
+ import { SEQUELIZE } from './sequelize-config.service';
5
+ import { SequelizeConfig } from './sequelize.interface';
6
+
7
+ @Module()
8
+ export class SequelizeModule {
9
+ public static forRoot(config: SequelizeConfig): ModuleWithServices {
10
+ return {
11
+ module: SequelizeModule,
12
+ services: [
13
+ {
14
+ provide: SEQUELIZE,
15
+ lazy: true,
16
+ useFactory: async () => {
17
+ const sequelize = new Sequelize(config);
18
+ sequelize.addModels(config.models ? config.models : []);
19
+ await sequelize.sync(config);
20
+ return sequelize;
21
+ },
22
+ },
23
+ ],
24
+ };
25
+ }
26
+ }
27
+
28
+ export * from './sequelize.interface';
29
+ export * from './sequelize-config.service';
@@ -0,0 +1,3 @@
1
+ import { InjectionToken } from '@rxdi/core';
2
+
3
+ export const SEQUELIZE = new InjectionToken('gapi-sequelize-config-token');
@@ -0,0 +1,20 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { SequelizeOptions } from 'sequelize-typescript';
3
+ import { Dialect } from 'sequelize/types';
4
+
5
+ export class SequelizeConfig implements SequelizeOptions {
6
+ dialect?: Dialect;
7
+ host?: string;
8
+ port?: number;
9
+ username?: string;
10
+ password?: string;
11
+ storage?: string;
12
+ logging?: boolean;
13
+ modelPaths?: string[];
14
+ models?: any[];
15
+ database?: string;
16
+ force?: boolean;
17
+ modifyFunctions?: {
18
+ sync?: () => {};
19
+ } = {};
20
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "module": "commonjs",
5
+ "target": "es6",
6
+ "baseUrl": ".",
7
+ "stripInternal": true,
8
+ "emitDecoratorMetadata": true,
9
+ "experimentalDecorators": true,
10
+ "moduleResolution": "node",
11
+ "outDir": "dist",
12
+ "rootDir": "./src",
13
+ "lib": [
14
+ "es2017",
15
+ "es2016",
16
+ "es2015",
17
+ "es6",
18
+ "dom",
19
+ "esnext.asynciterable"
20
+ ],
21
+ "skipLibCheck": true,
22
+ "types": []
23
+ },
24
+ "files": [
25
+ "./src/index.ts"
26
+ ]
27
+ }