namirasoft-node-sequelize 1.4.2 → 1.4.3

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