namirasoft-node-sequelize 1.4.0
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/dist/BaseDB2Database.d.ts +4 -0
- package/dist/BaseDB2Database.js +11 -0
- package/dist/BaseDB2Database.js.map +1 -0
- package/dist/BaseFilterItemBuilderSequelize.d.ts +54 -0
- package/dist/BaseFilterItemBuilderSequelize.js +150 -0
- package/dist/BaseFilterItemBuilderSequelize.js.map +1 -0
- package/dist/BaseMSSQLDatabase.d.ts +4 -0
- package/dist/BaseMSSQLDatabase.js +11 -0
- package/dist/BaseMSSQLDatabase.js.map +1 -0
- package/dist/BaseMariaDBDatabase.d.ts +4 -0
- package/dist/BaseMariaDBDatabase.js +11 -0
- package/dist/BaseMariaDBDatabase.js.map +1 -0
- package/dist/BaseMySqlDatabase.d.ts +4 -0
- package/dist/BaseMySqlDatabase.js +11 -0
- package/dist/BaseMySqlDatabase.js.map +1 -0
- package/dist/BaseOracleDatabase.d.ts +4 -0
- package/dist/BaseOracleDatabase.js +11 -0
- package/dist/BaseOracleDatabase.js.map +1 -0
- package/dist/BasePostgreSQLDatabase.d.ts +4 -0
- package/dist/BasePostgreSQLDatabase.js +11 -0
- package/dist/BasePostgreSQLDatabase.js.map +1 -0
- package/dist/BaseSQLiteDatabase.d.ts +4 -0
- package/dist/BaseSQLiteDatabase.js +11 -0
- package/dist/BaseSQLiteDatabase.js.map +1 -0
- package/dist/BaseSequelizeDatabase.d.ts +19 -0
- package/dist/BaseSequelizeDatabase.js +103 -0
- package/dist/BaseSequelizeDatabase.js.map +1 -0
- package/dist/BaseSequelizeModel.d.ts +3 -0
- package/dist/BaseSequelizeModel.js +8 -0
- package/dist/BaseSequelizeModel.js.map +1 -0
- package/dist/BaseSequelizeModelAttributeColumnOptions.d.ts +9 -0
- package/dist/BaseSequelizeModelAttributeColumnOptions.js +4 -0
- package/dist/BaseSequelizeModelAttributeColumnOptions.js.map +1 -0
- package/dist/BaseSequelizeModelAttributes.d.ts +5 -0
- package/dist/BaseSequelizeModelAttributes.js +3 -0
- package/dist/BaseSequelizeModelAttributes.js.map +1 -0
- package/dist/BaseSequelizeTable.d.ts +81 -0
- package/dist/BaseSequelizeTable.js +255 -0
- package/dist/BaseSequelizeTable.js.map +1 -0
- package/dist/BaseSnowFlakeDatabase.d.ts +4 -0
- package/dist/BaseSnowFlakeDatabase.js +11 -0
- package/dist/BaseSnowFlakeDatabase.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +30 -0
- package/dist/index.js.map +1 -0
- package/logo.png +0 -0
- package/package.json +25 -0
- package/src/BaseDB2Database.ts +9 -0
- package/src/BaseFilterItemBuilderSequelize.ts +120 -0
- package/src/BaseMSSQLDatabase.ts +9 -0
- package/src/BaseMariaDBDatabase.ts +9 -0
- package/src/BaseMySqlDatabase.ts +9 -0
- package/src/BaseOracleDatabase.ts +9 -0
- package/src/BasePostgreSQLDatabase.ts +9 -0
- package/src/BaseSQLiteDatabase.ts +9 -0
- package/src/BaseSequelizeDatabase.ts +115 -0
- package/src/BaseSequelizeModel.ts +5 -0
- package/src/BaseSequelizeModelAttributeColumnOptions.ts +11 -0
- package/src/BaseSequelizeModelAttributes.ts +6 -0
- package/src/BaseSequelizeTable.ts +285 -0
- package/src/BaseSnowFlakeDatabase.ts +9 -0
- package/src/index.ts +13 -0
- package/tsconfig.json +42 -0
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +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>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { ModelOptions, DataTypes, WhereOptions } from "sequelize";
|
|
2
|
+
import { BaseSequelizeDatabase } from "./BaseSequelizeDatabase";
|
|
3
|
+
import { BaseTable } from "namirasoft-node";
|
|
4
|
+
import { ModelCtor, FindOptions, Transaction, Attributes } from 'sequelize';
|
|
5
|
+
import { BaseSequelizeModel } from "./BaseSequelizeModel";
|
|
6
|
+
import { BaseMetaColumn, BaseMetaTable, BaseUUID, NamingConvention, SortItem } from "namirasoft-core";
|
|
7
|
+
import { AnySchema, ArraySchema, BaseTypeSchema, BigIntSchema, BoolSchema, DateSchema, DateTimeSchema, DecimalSchema, DoubleSchema, FloatSchema, IntegerSchema, MediumIntSchema, RealSchema, SmallIntSchema, StringSchema, TimeSchema, TinyIntSchema, TypeSchema, VariableSchema } from "namirasoft-schema";
|
|
8
|
+
import { EnumSchema } from "namirasoft-schema";
|
|
9
|
+
import { BaseSequelizeModelAttributeColumnOptions } from "./BaseSequelizeModelAttributeColumnOptions";
|
|
10
|
+
import { BaseSequelizeModelAttributes } from "./BaseSequelizeModelAttributes";
|
|
11
|
+
|
|
12
|
+
export abstract class BaseSequelizeTable<D extends BaseSequelizeDatabase, M extends BaseSequelizeModel> extends BaseTable<D, BaseSequelizeModelAttributeColumnOptions>
|
|
13
|
+
{
|
|
14
|
+
model!: ModelCtor<M>;
|
|
15
|
+
attributes!: BaseSequelizeModelAttributes<M, Attributes<M>>;
|
|
16
|
+
constructor(database: D)
|
|
17
|
+
{
|
|
18
|
+
super(database);
|
|
19
|
+
}
|
|
20
|
+
override getName(): string
|
|
21
|
+
{
|
|
22
|
+
return this.model.name;
|
|
23
|
+
}
|
|
24
|
+
protected override getExamples()
|
|
25
|
+
{
|
|
26
|
+
let ans: { [name: string]: string } = super.getExamples();
|
|
27
|
+
let add = (name: string, uuid: BaseUUID) =>
|
|
28
|
+
{
|
|
29
|
+
if (uuid.short)
|
|
30
|
+
ans[name] = uuid.new();
|
|
31
|
+
};
|
|
32
|
+
this.forEachColumn((name, column) =>
|
|
33
|
+
{
|
|
34
|
+
if (this.isChar(column.type))
|
|
35
|
+
{
|
|
36
|
+
if (name == "id")
|
|
37
|
+
add(name, this.uuid);
|
|
38
|
+
else if (name.endsWith("_id"))
|
|
39
|
+
{
|
|
40
|
+
try
|
|
41
|
+
{
|
|
42
|
+
let t = this.database.getTable(name.replace(/_id$/gm, "")) as BaseTable<D, any>;
|
|
43
|
+
add(name, t.uuid);
|
|
44
|
+
} catch (error)
|
|
45
|
+
{
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return ans;
|
|
52
|
+
}
|
|
53
|
+
override getSecureColumns(): string[]
|
|
54
|
+
{
|
|
55
|
+
let columns = super.getSecureColumns();
|
|
56
|
+
return ["deletedAt", "deleted_at", ...columns]
|
|
57
|
+
}
|
|
58
|
+
override getReadOnlyColumns(): string[]
|
|
59
|
+
{
|
|
60
|
+
let columns = super.getReadOnlyColumns();
|
|
61
|
+
return ["id", "created_at", "updated_at", "createdAt", "updatedAt", ...columns];
|
|
62
|
+
}
|
|
63
|
+
private getModelAllAttributes()
|
|
64
|
+
{
|
|
65
|
+
let dateAttributes: BaseSequelizeModelAttributes<
|
|
66
|
+
any,
|
|
67
|
+
Attributes<any>
|
|
68
|
+
> = {
|
|
69
|
+
created_at: { type: DataTypes.DATE, allowNull: false },
|
|
70
|
+
updated_at: { type: DataTypes.DATE, allowNull: false },
|
|
71
|
+
};
|
|
72
|
+
return { ... this.attributes, ...dateAttributes };
|
|
73
|
+
}
|
|
74
|
+
public override getColumnOption(name: string)
|
|
75
|
+
{
|
|
76
|
+
let att = this.getModelAllAttributes();
|
|
77
|
+
return att[name] as BaseSequelizeModelAttributeColumnOptions;
|
|
78
|
+
}
|
|
79
|
+
public override forEachColumn(handler: (name: string, column: BaseSequelizeModelAttributeColumnOptions) => void)
|
|
80
|
+
{
|
|
81
|
+
let att = this.getModelAllAttributes();
|
|
82
|
+
let keys = Object.keys(att);
|
|
83
|
+
for (let i = 0; i < keys.length; i++)
|
|
84
|
+
handler(keys[i], this.getColumnOption(keys[i]));
|
|
85
|
+
}
|
|
86
|
+
public override getColumn(name: string): BaseMetaColumn
|
|
87
|
+
{
|
|
88
|
+
let option = this.getColumnOption(name);
|
|
89
|
+
let table_name = this.getName();
|
|
90
|
+
let table = new BaseMetaTable(null, table_name, table_name);
|
|
91
|
+
let type = option.type as any;
|
|
92
|
+
try
|
|
93
|
+
{
|
|
94
|
+
if (typeof (type) == "function")
|
|
95
|
+
type = type();
|
|
96
|
+
type = type + "";
|
|
97
|
+
} catch (error)
|
|
98
|
+
{
|
|
99
|
+
type = "Enum";
|
|
100
|
+
}
|
|
101
|
+
let column = new BaseMetaColumn(table, name, name, type, option.allowNull != true);
|
|
102
|
+
return column;
|
|
103
|
+
}
|
|
104
|
+
protected override getTypeSchema(option: BaseSequelizeModelAttributeColumnOptions): BaseTypeSchema
|
|
105
|
+
{
|
|
106
|
+
let required = !(option.allowNull ?? true);
|
|
107
|
+
let type = option.type as any;
|
|
108
|
+
try
|
|
109
|
+
{
|
|
110
|
+
if (typeof (type) == "function")
|
|
111
|
+
type = type();
|
|
112
|
+
} catch (error)
|
|
113
|
+
{
|
|
114
|
+
}
|
|
115
|
+
let type_name = "";
|
|
116
|
+
try
|
|
117
|
+
{
|
|
118
|
+
type_name = type + "";
|
|
119
|
+
} catch (error)
|
|
120
|
+
{
|
|
121
|
+
type_name = "Enum";
|
|
122
|
+
}
|
|
123
|
+
let min: number | null = (option.validate?.min ?? null) as number | null;
|
|
124
|
+
let max: number | null = (option.validate?.max ?? null) as number | null;
|
|
125
|
+
/* VariableType */
|
|
126
|
+
if (type instanceof DataTypes.BOOLEAN)
|
|
127
|
+
return new BoolSchema(required);
|
|
128
|
+
else if (type instanceof DataTypes.TINYINT)
|
|
129
|
+
return new TinyIntSchema(required, min, max);
|
|
130
|
+
else if (type instanceof DataTypes.SMALLINT)
|
|
131
|
+
return new SmallIntSchema(required, min, max);
|
|
132
|
+
else if (type instanceof DataTypes.MEDIUMINT)
|
|
133
|
+
return new MediumIntSchema(required, min, max);
|
|
134
|
+
else if (type instanceof DataTypes.INTEGER)
|
|
135
|
+
return new IntegerSchema(required, min, max);
|
|
136
|
+
else if (type instanceof DataTypes.BIGINT)
|
|
137
|
+
return new BigIntSchema(required, min, max);
|
|
138
|
+
else if (this.isFloat(type))
|
|
139
|
+
return new FloatSchema(required, type.options.decimals, min, max);
|
|
140
|
+
else if (this.isDouble(type))
|
|
141
|
+
return new DoubleSchema(required, type.options.decimals, min, max);
|
|
142
|
+
else if (this.isDecimal(type))
|
|
143
|
+
return new DecimalSchema(required, type.options.scale, min, max);
|
|
144
|
+
else if (this.isReal(type))
|
|
145
|
+
return new RealSchema(required, type.options.decimals, min, max);
|
|
146
|
+
else if (this.isChar(type))
|
|
147
|
+
return new StringSchema(required, type.options.length ?? type._length, type.options.length ?? type._length);
|
|
148
|
+
else if (this.isString(type))
|
|
149
|
+
return new StringSchema(required, null, type.options.length ?? type._length);
|
|
150
|
+
else if (type instanceof DataTypes.TEXT)
|
|
151
|
+
return new StringSchema(required, null, null);
|
|
152
|
+
else if (type instanceof DataTypes.DATE || type_name == "TIMESTAMP")
|
|
153
|
+
return new DateTimeSchema(required);
|
|
154
|
+
else if (type instanceof DataTypes.DATEONLY)
|
|
155
|
+
return new DateSchema(required);
|
|
156
|
+
else if (type instanceof DataTypes.TIME)
|
|
157
|
+
return new TimeSchema(required);
|
|
158
|
+
else if (this.isEnum(type))
|
|
159
|
+
{
|
|
160
|
+
let name = NamingConvention.lower_case_underscore.convert(this.model.name + "_" + option.field, NamingConvention.Pascal_Case);
|
|
161
|
+
return new EnumSchema(name, required, type.options.values);
|
|
162
|
+
}
|
|
163
|
+
else if (type instanceof DataTypes.JSON)
|
|
164
|
+
{
|
|
165
|
+
let tags_type = option.tags?.type;
|
|
166
|
+
if (tags_type)
|
|
167
|
+
{
|
|
168
|
+
if (tags_type == "BaseTypeSchema")
|
|
169
|
+
return new TypeSchema(required);
|
|
170
|
+
if (tags_type == "BaseVariableSchema")
|
|
171
|
+
return new VariableSchema(required);
|
|
172
|
+
if (tags_type == "BaseTypeSchema[]")
|
|
173
|
+
return new ArraySchema(required, [new TypeSchema(required)]);
|
|
174
|
+
if (tags_type == "BaseVariableSchema[]")
|
|
175
|
+
return new ArraySchema(required, [new VariableSchema(required)]);
|
|
176
|
+
}
|
|
177
|
+
return new AnySchema(required);
|
|
178
|
+
}
|
|
179
|
+
throw new Error("Unsupported datatype for schema: " + option.type);
|
|
180
|
+
}
|
|
181
|
+
protected isFloat(type: any): type is { options: { length: number, decimals: number } }
|
|
182
|
+
{
|
|
183
|
+
return type instanceof DataTypes.FLOAT;
|
|
184
|
+
}
|
|
185
|
+
protected isDouble(type: any): type is { options: { length: number, decimals: number } }
|
|
186
|
+
{
|
|
187
|
+
return type instanceof DataTypes.DOUBLE;
|
|
188
|
+
}
|
|
189
|
+
protected isDecimal(type: any): type is { options: { precision: number, scale: number } }
|
|
190
|
+
{
|
|
191
|
+
return type instanceof DataTypes.DECIMAL;
|
|
192
|
+
}
|
|
193
|
+
protected isReal(type: any): type is { options: { length: number, decimals: number } }
|
|
194
|
+
{
|
|
195
|
+
return type instanceof DataTypes.REAL;
|
|
196
|
+
}
|
|
197
|
+
protected isChar(type: any): type is { _length: number, options: { length: number } }
|
|
198
|
+
{
|
|
199
|
+
return type instanceof DataTypes.CHAR;
|
|
200
|
+
}
|
|
201
|
+
protected isString(type: any): type is { _length: number, options: { length: number } }
|
|
202
|
+
{
|
|
203
|
+
return type instanceof DataTypes.STRING;
|
|
204
|
+
}
|
|
205
|
+
protected isEnum(type: any): type is { options: { values: string[] } }
|
|
206
|
+
{
|
|
207
|
+
return type instanceof DataTypes.ENUM;
|
|
208
|
+
}
|
|
209
|
+
define(modelName: string, attributes: BaseSequelizeModelAttributes<M, Attributes<M>>, options?: ModelOptions<M>): void
|
|
210
|
+
{
|
|
211
|
+
this.database.addTable(modelName, this);
|
|
212
|
+
this.attributes = attributes;
|
|
213
|
+
if (!options)
|
|
214
|
+
options = {};
|
|
215
|
+
if (options.name == undefined)
|
|
216
|
+
options.name = {
|
|
217
|
+
plural: modelName,
|
|
218
|
+
singular: modelName
|
|
219
|
+
};
|
|
220
|
+
if (options.paranoid == undefined)
|
|
221
|
+
options.paranoid = true;
|
|
222
|
+
if (options.freezeTableName == undefined)
|
|
223
|
+
options.freezeTableName = true;
|
|
224
|
+
if (options.tableName == undefined)
|
|
225
|
+
options.tableName = modelName;
|
|
226
|
+
if (options.underscored == undefined)
|
|
227
|
+
options.underscored = true;
|
|
228
|
+
if (options.timestamps == undefined)
|
|
229
|
+
options.timestamps = true;
|
|
230
|
+
if (options.paranoid == undefined)
|
|
231
|
+
options.paranoid = true;
|
|
232
|
+
if (options.createdAt == undefined)
|
|
233
|
+
options.createdAt = 'created_at';
|
|
234
|
+
if (options.updatedAt == undefined)
|
|
235
|
+
options.updatedAt = 'updated_at';
|
|
236
|
+
if (options.deletedAt == undefined)
|
|
237
|
+
options.deletedAt = 'deleted_at';
|
|
238
|
+
|
|
239
|
+
this.model = this.database.sequelize.define(modelName, attributes, options);
|
|
240
|
+
}
|
|
241
|
+
public addForeignKey(primary: BaseSequelizeTable<BaseSequelizeDatabase, BaseSequelizeModel>, name: string, allowNull?: boolean)
|
|
242
|
+
{
|
|
243
|
+
let schema = this.getSchema(true);
|
|
244
|
+
let field = schema.fields?.find(x => x.name == name);
|
|
245
|
+
if (field == null)
|
|
246
|
+
throw new Error(`Column ${name} could not be found for relation between tables ${primary.getName()} and ${this.getName()}`);
|
|
247
|
+
|
|
248
|
+
primary.model.hasMany(this.model, { foreignKey: { name, allowNull }, sourceKey: "id" });
|
|
249
|
+
this.model.belongsTo(primary.model, { foreignKey: { name, allowNull }, targetKey: "id" });
|
|
250
|
+
}
|
|
251
|
+
async _getOrNull(options: FindOptions<Attributes<M>>, trx: Transaction | null): Promise<M | null>
|
|
252
|
+
{
|
|
253
|
+
options.transaction = trx;
|
|
254
|
+
return await this.model.findOne<M>(options);
|
|
255
|
+
}
|
|
256
|
+
async _get(options: FindOptions<Attributes<M>>, trx: Transaction | null): Promise<M>
|
|
257
|
+
{
|
|
258
|
+
let value = await this._getOrNull(options, trx);
|
|
259
|
+
if (value != null)
|
|
260
|
+
return value;
|
|
261
|
+
throw this.getNotFoundError(options.where);
|
|
262
|
+
}
|
|
263
|
+
async _list(where: WhereOptions<Attributes<M>>, pagination: { dont: true } | { page: number, size: number }, sorts: { dont: true } | SortItem[], options: FindOptions<Attributes<M>>, trx: Transaction | null): Promise<{ rows: M[], count: number }>
|
|
264
|
+
{
|
|
265
|
+
let offset: number | undefined = undefined;
|
|
266
|
+
let limit: number | undefined = undefined;
|
|
267
|
+
if (!('dont' in pagination))
|
|
268
|
+
{
|
|
269
|
+
let p = this.database.paginate(pagination.page, pagination.size);
|
|
270
|
+
offset = p.offset;
|
|
271
|
+
limit = p.limit;
|
|
272
|
+
}
|
|
273
|
+
if (!('dont' in sorts))
|
|
274
|
+
{
|
|
275
|
+
if (options.order)
|
|
276
|
+
{
|
|
277
|
+
if (sorts.length > 0)
|
|
278
|
+
throw new Error("options.order and sorts must not be provided together.");
|
|
279
|
+
}
|
|
280
|
+
else
|
|
281
|
+
options.order = this.database.getSortOptions(sorts);
|
|
282
|
+
}
|
|
283
|
+
return await this.model.findAndCountAll({ where, offset, limit, ...options, distinct: true, subQuery: false, transaction: trx });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseSequelizeDatabase } from "./BaseSequelizeDatabase";
|
|
2
|
+
|
|
3
|
+
export abstract class BaseSnowFlakeDatabase extends BaseSequelizeDatabase
|
|
4
|
+
{
|
|
5
|
+
constructor(host: string, port: number, name: string, user: string, pass: string, logging: boolean = false)
|
|
6
|
+
{
|
|
7
|
+
super('db2', host, port, name, user, pass, logging);
|
|
8
|
+
}
|
|
9
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./BaseDB2Database";
|
|
2
|
+
export * from "./BaseFilterItemBuilderSequelize";
|
|
3
|
+
export * from "./BaseMariaDBDatabase";
|
|
4
|
+
export * from "./BaseMSSQLDatabase";
|
|
5
|
+
export * from "./BaseOracleDatabase";
|
|
6
|
+
export * from "./BasePostgreSQLDatabase";
|
|
7
|
+
export * from "./BaseSequelizeDatabase";
|
|
8
|
+
export * from "./BaseSequelizeModel";
|
|
9
|
+
export * from "./BaseSequelizeModelAttributeColumnOptions";
|
|
10
|
+
export * from "./BaseSequelizeModelAttributes";
|
|
11
|
+
export * from "./BaseSequelizeTable";
|
|
12
|
+
export * from "./BaseSnowFlakeDatabase";
|
|
13
|
+
export * from "./BaseSQLiteDatabase";
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES6",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"lib": [
|
|
8
|
+
"es6",
|
|
9
|
+
"dom"
|
|
10
|
+
],
|
|
11
|
+
"allowJs": false,
|
|
12
|
+
"checkJs": false,
|
|
13
|
+
"strict": true,
|
|
14
|
+
"esModuleInterop": true,
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"resolveJsonModule": true,
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"noUnusedLocals": true,
|
|
19
|
+
"noUnusedParameters": true,
|
|
20
|
+
"noImplicitAny": true,
|
|
21
|
+
"noImplicitOverride": true,
|
|
22
|
+
"noImplicitReturns": true,
|
|
23
|
+
"noImplicitThis": true,
|
|
24
|
+
"skipLibCheck": true,
|
|
25
|
+
"allowSyntheticDefaultImports": true,
|
|
26
|
+
"forceConsistentCasingInFileNames": true,
|
|
27
|
+
"noFallthroughCasesInSwitch": true,
|
|
28
|
+
"isolatedModules": false,
|
|
29
|
+
"removeComments": true
|
|
30
|
+
},
|
|
31
|
+
"include": [
|
|
32
|
+
"src",
|
|
33
|
+
"build/types/**/*.ts"
|
|
34
|
+
],
|
|
35
|
+
"exclude": [
|
|
36
|
+
"node_modules",
|
|
37
|
+
"build",
|
|
38
|
+
"dist",
|
|
39
|
+
"static",
|
|
40
|
+
"public"
|
|
41
|
+
]
|
|
42
|
+
}
|