namirasoft-node-sequelize 1.4.11 → 1.4.13

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.
@@ -1,166 +1,166 @@
1
- import { BaseMetaColumn, BaseMetaTable, FilterItem, SortItem } from "namirasoft-core";
2
- import { Logger } from "namirasoft-log";
3
- import { BaseApplication, BaseDatabase, BaseFilterItemBuilder_JoinTable } from "namirasoft-node";
4
- import { VariableType } from "namirasoft-schema";
5
- import { Dialect, Op, Order, Sequelize, Transaction, WhereOptions } from "sequelize";
6
- import { BaseFilterItemBuilderSequelize } from "./BaseFilterItemBuilderSequelize";
7
- import { BaseSequelizeTable } from "./BaseSequelizeTable";
8
-
9
- export interface BaseSequelizeDatabaseSeedable<Model>
10
- {
11
- getOrNull(id: string | number | undefined, trx: Transaction | null): Promise<Model | null>;
12
- create(product: Model, trx: Transaction | null): Promise<Model>;
13
- update(product: Model, trx: Transaction | null): Promise<number[]>;
14
- }
15
-
16
- export abstract class BaseSequelizeDatabase extends BaseDatabase
17
- {
18
- public sequelize: Sequelize;
19
- private dialect: string;
20
- private name: string;
21
- constructor(dialect: Dialect, host: string, port: number, name: string, user: string, pass: string, logging: boolean = false, getConnectionString?: () => string)
22
- {
23
- super();
24
- let connectionString = getConnectionString?.();
25
- if (connectionString)
26
- this.sequelize = new Sequelize({
27
- dialect,
28
- dialectOptions: {
29
- connectionString
30
- },
31
- logging,
32
- pool: { min: 2, max: 10 }
33
- });
34
- else
35
- this.sequelize = new Sequelize(
36
- name,
37
- user,
38
- pass,
39
- {
40
- dialect,
41
- host,
42
- port,
43
- logging,
44
- pool: {
45
- min: 2,
46
- max: 10,
47
- }
48
- });
49
-
50
- this.dialect = dialect;
51
- this.name = name;
52
- }
53
- override getName()
54
- {
55
- return this.name;
56
- }
57
- override async connect()
58
- {
59
- if (!process.env.NAMIRASOFT_MUTE)
60
- {
61
- await this.sequelize.authenticate();
62
- Logger.main?.success(`Database ${this.dialect} was connected to ${this.name}`);
63
- }
64
- }
65
- override async sync(force: boolean)
66
- {
67
- if (!process.env.NAMIRASOFT_MUTE)
68
- await this.sequelize.sync({ force });
69
- }
70
- override async isHealthy()
71
- {
72
- try
73
- {
74
- this.sequelize.authenticate();
75
- return true;
76
- } catch (error)
77
- {
78
- }
79
- return false;
80
- }
81
- async close()
82
- {
83
- this.sequelize?.close();
84
- }
85
- async startTransaction<T>(handler: (trx: Transaction) => Promise<T>, trx: Transaction | null): Promise<T>
86
- {
87
- if (trx)
88
- return await handler(trx);
89
- trx = await this.sequelize.transaction();
90
- try
91
- {
92
- let result = await handler(trx);
93
- await trx.commit();
94
- return result;
95
- }
96
- catch (error)
97
- {
98
- await trx.rollback();
99
- throw error;
100
- }
101
- }
102
- async getFiltersConditions(table_main: BaseSequelizeTable<BaseSequelizeDatabase, any>, table_joins: { [table: string]: BaseFilterItemBuilder_JoinTable<WhereOptions> }, filters?: FilterItem[] | undefined): Promise<WhereOptions[]>
103
- {
104
- let builder = new BaseFilterItemBuilderSequelize(this);
105
- return await builder.build(table_main, table_joins, filters);
106
- }
107
- getAdvancedSearchConditions(columns: string[], search: string): WhereOptions[]
108
- {
109
- let conditions: WhereOptions[] = [];
110
- if (search)
111
- if (search.split)
112
- {
113
- let toks = search.split(' ');
114
- if (toks.length > 0)
115
- {
116
- for (let i = 0; i < toks.length; i++)
117
- {
118
- let rOpr = { [Op.like]: '%' + toks[i].trim() + '%' };
119
- let lOpr;
120
- let cs = columns.map(column =>
121
- Sequelize.fn("IFNULL", Sequelize.col(column), '')
122
- );
123
- lOpr = Sequelize.fn(
124
- "concat",
125
- ...cs
126
- );
127
- let condition = Sequelize.where(lOpr, rOpr);
128
- conditions.push(condition);
129
- }
130
- }
131
- }
132
- return conditions;
133
- }
134
- override getSortOptions(sorts?: SortItem[] | undefined): Order
135
- {
136
- let table = new BaseMetaTable(null, "", "");
137
- let created_at = new SortItem(table, new BaseMetaColumn(table, "created_at", "Created At", VariableType.DateTime, true), false);
138
-
139
- let ans: Order = [];
140
-
141
- if (!sorts || sorts.length == 0)
142
- sorts = [created_at];
143
-
144
- ans = sorts.filter(s => s).map(s =>
145
- {
146
- return [s.column.name, s.ascending ? "asc" : "desc"];
147
- });
148
-
149
- return ans;
150
- }
151
- public async seed<Model extends { id: string | number }>(app: BaseApplication<any>, file_path: string, seedable_table: BaseSequelizeDatabaseSeedable<Model>, preprocess?: (item: Model) => Model)
152
- {
153
- if (!preprocess)
154
- preprocess = i => i;
155
- let items = app.read<Model[]>(file_path);
156
- for (let i = 0; i < items.length; i++)
157
- {
158
- const item = preprocess(items[i]);
159
- let product = await seedable_table.getOrNull(item.id, null);
160
- if (product === null)
161
- await seedable_table.create(item, null);
162
- else
163
- await seedable_table.update(item, null);
164
- }
165
- }
1
+ import { BaseMetaColumn, BaseMetaTable, FilterItem, SortItem } from "namirasoft-core";
2
+ import { Logger } from "namirasoft-log";
3
+ import { BaseApplication, BaseDatabase, BaseFilterItemBuilder_JoinTable } from "namirasoft-node";
4
+ import { VariableType } from "namirasoft-schema";
5
+ import { Dialect, Op, Order, Sequelize, Transaction, WhereOptions } from "sequelize";
6
+ import { BaseFilterItemBuilderSequelize } from "./BaseFilterItemBuilderSequelize";
7
+ import { BaseSequelizeTable } from "./BaseSequelizeTable";
8
+
9
+ export interface BaseSequelizeDatabaseSeedable<Model>
10
+ {
11
+ getOrNull(id: string | number | undefined, trx: Transaction | null): Promise<Model | null>;
12
+ create(product: Model, trx: Transaction | null): Promise<Model>;
13
+ update(product: Model, trx: Transaction | null): Promise<number[]>;
14
+ }
15
+
16
+ export abstract class BaseSequelizeDatabase extends BaseDatabase
17
+ {
18
+ public sequelize: Sequelize;
19
+ private dialect: string;
20
+ private name: string;
21
+ constructor(dialect: Dialect, host: string, port: number, name: string, user: string, pass: string, logging: boolean = false, getConnectionString?: () => string)
22
+ {
23
+ super();
24
+ let connectionString = getConnectionString?.();
25
+ if (connectionString)
26
+ this.sequelize = new Sequelize({
27
+ dialect,
28
+ dialectOptions: {
29
+ connectionString
30
+ },
31
+ logging,
32
+ pool: { min: 2, max: 10 }
33
+ });
34
+ else
35
+ this.sequelize = new Sequelize(
36
+ name,
37
+ user,
38
+ pass,
39
+ {
40
+ dialect,
41
+ host,
42
+ port,
43
+ logging,
44
+ pool: {
45
+ min: 2,
46
+ max: 10,
47
+ }
48
+ });
49
+
50
+ this.dialect = dialect;
51
+ this.name = name;
52
+ }
53
+ override getName()
54
+ {
55
+ return this.name;
56
+ }
57
+ override async connect()
58
+ {
59
+ if (!process.env.NAMIRASOFT_MUTE)
60
+ {
61
+ await this.sequelize.authenticate();
62
+ Logger.main?.success(`Database ${this.dialect} was connected to ${this.name}`);
63
+ }
64
+ }
65
+ override async sync(force: boolean)
66
+ {
67
+ if (!process.env.NAMIRASOFT_MUTE)
68
+ await this.sequelize.sync({ force });
69
+ }
70
+ override async isHealthy()
71
+ {
72
+ try
73
+ {
74
+ this.sequelize.authenticate();
75
+ return true;
76
+ } catch (error)
77
+ {
78
+ }
79
+ return false;
80
+ }
81
+ async close()
82
+ {
83
+ this.sequelize?.close();
84
+ }
85
+ async startTransaction<T>(handler: (trx: Transaction) => Promise<T>, trx: Transaction | null): Promise<T>
86
+ {
87
+ if (trx)
88
+ return await handler(trx);
89
+ trx = await this.sequelize.transaction();
90
+ try
91
+ {
92
+ let result = await handler(trx);
93
+ await trx.commit();
94
+ return result;
95
+ }
96
+ catch (error)
97
+ {
98
+ await trx.rollback();
99
+ throw error;
100
+ }
101
+ }
102
+ async getFiltersConditions(table_main: BaseSequelizeTable<BaseSequelizeDatabase, any>, table_joins: { [table: string]: BaseFilterItemBuilder_JoinTable<WhereOptions> }, filters?: FilterItem[] | undefined): Promise<WhereOptions[]>
103
+ {
104
+ let builder = new BaseFilterItemBuilderSequelize(this);
105
+ return await builder.build(table_main, table_joins, filters);
106
+ }
107
+ getAdvancedSearchConditions(columns: string[], search: string): WhereOptions[]
108
+ {
109
+ let conditions: WhereOptions[] = [];
110
+ if (search)
111
+ if (search.split)
112
+ {
113
+ let toks = search.split(' ');
114
+ if (toks.length > 0)
115
+ {
116
+ for (let i = 0; i < toks.length; i++)
117
+ {
118
+ let rOpr = { [Op.like]: '%' + toks[i].trim() + '%' };
119
+ let lOpr;
120
+ let cs = columns.map(column =>
121
+ Sequelize.fn("IFNULL", Sequelize.col(column), '')
122
+ );
123
+ lOpr = Sequelize.fn(
124
+ "concat",
125
+ ...cs
126
+ );
127
+ let condition = Sequelize.where(lOpr, rOpr);
128
+ conditions.push(condition);
129
+ }
130
+ }
131
+ }
132
+ return conditions;
133
+ }
134
+ override getSortOptions(sorts?: SortItem[] | undefined): Order
135
+ {
136
+ let table = new BaseMetaTable(null, "", "");
137
+ let created_at = new SortItem(table, new BaseMetaColumn(table, "created_at", "Created At", VariableType.DateTime, true), false);
138
+
139
+ let ans: Order = [];
140
+
141
+ if (!sorts || sorts.length == 0)
142
+ sorts = [created_at];
143
+
144
+ ans = sorts.filter(s => s).map(s =>
145
+ {
146
+ return [s.column.name, s.ascending ? "asc" : "desc"];
147
+ });
148
+
149
+ return ans;
150
+ }
151
+ public async seed<Model extends { id: string | number }>(app: BaseApplication<any>, file_path: string, seedable_table: BaseSequelizeDatabaseSeedable<Model>, preprocess?: (item: Model) => Model)
152
+ {
153
+ if (!preprocess)
154
+ preprocess = i => i;
155
+ let items = app.read<Model[]>(file_path);
156
+ for (let i = 0; i < items.length; i++)
157
+ {
158
+ const item = preprocess(items[i]);
159
+ let product = await seedable_table.getOrNull(item.id, null);
160
+ if (product === null)
161
+ await seedable_table.create(item, null);
162
+ else
163
+ await seedable_table.update(item, null);
164
+ }
165
+ }
166
166
  }
@@ -1,5 +1,5 @@
1
- import { Model } from 'sequelize';
2
-
3
- export abstract class BaseSequelizeModel extends Model
4
- {
1
+ import { Model } from 'sequelize';
2
+
3
+ export abstract class BaseSequelizeModel extends Model
4
+ {
5
5
  }
@@ -1,11 +1,11 @@
1
- import { ModelAttributeColumnOptions, Model } from "sequelize";
2
-
3
- export interface BaseSequelizeModelAttributeColumnOptions<M extends Model = Model> extends ModelAttributeColumnOptions<M>
4
- {
5
- secure?: boolean;
6
- read_only?: boolean;
7
- searchable?: boolean;
8
- tags?: {
9
- type?: "BaseTypeSchema" | "BaseTypeSchema[]" | "BaseVariableSchema" | "BaseVariableSchema[]"
10
- }
1
+ import { ModelAttributeColumnOptions, Model } from "sequelize";
2
+
3
+ export interface BaseSequelizeModelAttributeColumnOptions<M extends Model = Model> extends ModelAttributeColumnOptions<M>
4
+ {
5
+ secure?: boolean;
6
+ read_only?: boolean;
7
+ searchable?: boolean;
8
+ tags?: {
9
+ type?: "BaseTypeSchema" | "BaseTypeSchema[]" | "BaseVariableSchema" | "BaseVariableSchema[]"
10
+ }
11
11
  };
@@ -1,6 +1,6 @@
1
- import { DataType, Model } from "sequelize";
2
- import { BaseSequelizeModelAttributeColumnOptions } from "./BaseSequelizeModelAttributeColumnOptions";
3
-
4
- export type BaseSequelizeModelAttributes<M extends Model = Model, TAttributes = any> = {
5
- [name in keyof TAttributes]: DataType | BaseSequelizeModelAttributeColumnOptions<M>;
1
+ import { DataType, Model } from "sequelize";
2
+ import { BaseSequelizeModelAttributeColumnOptions } from "./BaseSequelizeModelAttributeColumnOptions";
3
+
4
+ export type BaseSequelizeModelAttributes<M extends Model = Model, TAttributes = any> = {
5
+ [name in keyof TAttributes]: DataType | BaseSequelizeModelAttributeColumnOptions<M>;
6
6
  }