sasat 0.21.13 → 0.21.15

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/cli/cli.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
  'use strict';
3
3
 
4
4
  const cac = require('cac');
5
- const error = require('../shared/sasat.CEngWMnG.cjs');
5
+ const error = require('../shared/sasat.DEWhnlUl.cjs');
6
6
  require('pluralize');
7
7
  const fs = require('fs');
8
8
  const path = require('path');
@@ -5027,6 +5027,10 @@ const migrate = async (options) => {
5027
5027
  if (!options.skipBuild) {
5028
5028
  await compileMigrationFiles();
5029
5029
  }
5030
+ const conf = error.config();
5031
+ if (conf.migration.db) {
5032
+ error.setConfig({ db: conf.migration.db });
5033
+ }
5030
5034
  const migration = new MigrationController();
5031
5035
  const result = await migration.migrate(options);
5032
5036
  current = result.currentMigration;
package/dist/cli/cli.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import { cac } from 'cac';
3
- import { S as SqlString, a as SasatError, c as config, r as readInitialSchema, g as getDbClient, m as mkDirIfNotExist, n as nonNullable, u as unique, C as Conditions, w as writeFileIfNotExist, b as writeCurrentSchema, d as writeYmlFile, e as defaultConf } from '../shared/sasat.CtwXwmHh.mjs';
3
+ import { S as SqlString, a as SasatError, c as config, r as readInitialSchema, g as getDbClient, m as mkDirIfNotExist, n as nonNullable, u as unique, C as Conditions, w as writeFileIfNotExist, s as setConfig, d as writeCurrentSchema, e as writeYmlFile, f as defaultConf } from '../shared/sasat.OfgCNG3F.mjs';
4
4
  import 'pluralize';
5
5
  import * as fs from 'fs';
6
6
  import fs__default from 'fs';
@@ -5002,6 +5002,10 @@ const migrate = async (options) => {
5002
5002
  if (!options.skipBuild) {
5003
5003
  await compileMigrationFiles();
5004
5004
  }
5005
+ const conf = config();
5006
+ if (conf.migration.db) {
5007
+ setConfig({ db: conf.migration.db });
5008
+ }
5005
5009
  const migration = new MigrationController();
5006
5010
  const result = await migration.migrate(options);
5007
5011
  current = result.currentMigration;
package/dist/index.cjs CHANGED
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
- const error = require('./shared/sasat.CEngWMnG.cjs');
4
- const SqlString = require('sqlstring');
5
- require('mysql2');
6
- require('util');
3
+ const error = require('./shared/sasat.DEWhnlUl.cjs');
7
4
  require('path');
8
5
  require('fs-extra');
9
6
  require('js-yaml');
7
+ const SqlString = require('sqlstring');
8
+ require('mysql2');
9
+ require('util');
10
10
 
11
11
  function _interopNamespaceCompat(e) {
12
12
  if (e && typeof e === 'object' && 'default' in e) return e;
@@ -838,29 +838,6 @@ const getCurrentDateTimeString = () => {
838
838
  return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + " " + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
839
839
  };
840
840
 
841
- const assignDeep = (base, ...objects) => {
842
- const assign = (target, key, value) => {
843
- if (key === "__proto__" || key === "constructor") {
844
- return;
845
- }
846
- if (Array.isArray(target[key]) && Array.isArray(value)) {
847
- target[key] = [...target[key], ...value];
848
- } else if (typeof target[key] === "object" && typeof value === "object") {
849
- assignDeep(target[key], value);
850
- } else {
851
- target[key] = value;
852
- }
853
- };
854
- objects.forEach((obj) => {
855
- if (typeof obj === "object") {
856
- Object.entries(obj).forEach(([key, value]) => {
857
- assign(base, key, value);
858
- });
859
- }
860
- });
861
- return base;
862
- };
863
-
864
841
  const makeArgs = (args) => {
865
842
  if (!args || args.length === 0) return "";
866
843
  return `(${args.map((arg) => `${arg.name}: ${arg.type}`).join(", ")})`;
@@ -1012,15 +989,16 @@ const pagingOption = (option) => {
1012
989
 
1013
990
  exports.Conditions = error.Conditions;
1014
991
  exports.SqlString = error.SqlString;
992
+ exports.assignDeep = error.assignDeep;
1015
993
  exports.getDbClient = error.getDbClient;
1016
994
  exports.pick = error.pick;
995
+ exports.setConfig = error.setConfig;
1017
996
  exports.CompositeCondition = CompositeCondition;
1018
997
  exports.Mutations = Mutations;
1019
998
  exports.QExpr = QExpr;
1020
999
  exports.Queries = Queries;
1021
1000
  exports.SasatDBDatasource = SasatDBDatasource;
1022
1001
  exports.Sql = Sql;
1023
- exports.assignDeep = assignDeep;
1024
1002
  exports.createTypeDef = createTypeDef;
1025
1003
  exports.dateOffset = dateOffset;
1026
1004
  exports.dateToDateString = dateToDateString;
package/dist/index.d.cts CHANGED
@@ -150,6 +150,46 @@ type Sort = {
150
150
 
151
151
  declare const queryToSql: (query: Query) => string;
152
152
 
153
+ type NestedPartial<T> = {
154
+ [K in keyof T]?: T[K] extends Array<infer R> ? Array<NestedPartial<R>> : NestedPartial<T[K]>;
155
+ };
156
+
157
+ interface SasatConfigDb {
158
+ host: string;
159
+ port: number;
160
+ user: string;
161
+ password?: string;
162
+ database: string;
163
+ ssl?: {
164
+ ca?: string[];
165
+ };
166
+ }
167
+ interface SasatConfigRedis {
168
+ host: string;
169
+ port: number;
170
+ password?: string;
171
+ }
172
+ interface SasatConfigMigration {
173
+ table: string;
174
+ dir: string;
175
+ out: string;
176
+ target?: string;
177
+ db?: SasatConfigDb;
178
+ }
179
+ interface SasatConfigGenerator {
180
+ addJsExtToImportStatement: boolean;
181
+ gql: {
182
+ subscription: boolean;
183
+ };
184
+ }
185
+ interface SasatConfig {
186
+ db: SasatConfigDb;
187
+ migration: SasatConfigMigration;
188
+ generator: SasatConfigGenerator;
189
+ redis: SasatConfigRedis;
190
+ }
191
+ declare function setConfig(update: NestedPartial<SasatConfig>): SasatConfig;
192
+
153
193
  type QueryResponse = Array<{
154
194
  [key: string]: SqlValueType;
155
195
  }>;
@@ -1092,5 +1132,5 @@ declare const pagingOption: (option: ListQueryOption) => DsPagingOption;
1092
1132
 
1093
1133
  type PagingOption = ListQueryOption;
1094
1134
 
1095
- export { CompositeCondition, Conditions, Mutations, QExpr, Queries, SQLClient, SQLTransaction, SasatDBDatasource, Sql, SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, pick, QExpr as qe, queryToSql };
1135
+ export { CompositeCondition, Conditions, Mutations, QExpr, Queries, SQLClient, SQLTransaction, SasatDBDatasource, Sql, SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, pick, QExpr as qe, queryToSql, setConfig };
1096
1136
  export type { BooleanValueExpression, CommandResponse, ComparisonOperators, CustomCondition, EntityResult, EntityType, Fields, ListQueryOption, LockMode, MigrationStore, PagingOption, Query, QueryOptions, QueryResponse, Relation, RelationMap, ResolverMiddleware, SQLExecutor, SasatMigration, TableInfo, TypeFieldDefinition };
package/dist/index.d.mts CHANGED
@@ -150,6 +150,46 @@ type Sort = {
150
150
 
151
151
  declare const queryToSql: (query: Query) => string;
152
152
 
153
+ type NestedPartial<T> = {
154
+ [K in keyof T]?: T[K] extends Array<infer R> ? Array<NestedPartial<R>> : NestedPartial<T[K]>;
155
+ };
156
+
157
+ interface SasatConfigDb {
158
+ host: string;
159
+ port: number;
160
+ user: string;
161
+ password?: string;
162
+ database: string;
163
+ ssl?: {
164
+ ca?: string[];
165
+ };
166
+ }
167
+ interface SasatConfigRedis {
168
+ host: string;
169
+ port: number;
170
+ password?: string;
171
+ }
172
+ interface SasatConfigMigration {
173
+ table: string;
174
+ dir: string;
175
+ out: string;
176
+ target?: string;
177
+ db?: SasatConfigDb;
178
+ }
179
+ interface SasatConfigGenerator {
180
+ addJsExtToImportStatement: boolean;
181
+ gql: {
182
+ subscription: boolean;
183
+ };
184
+ }
185
+ interface SasatConfig {
186
+ db: SasatConfigDb;
187
+ migration: SasatConfigMigration;
188
+ generator: SasatConfigGenerator;
189
+ redis: SasatConfigRedis;
190
+ }
191
+ declare function setConfig(update: NestedPartial<SasatConfig>): SasatConfig;
192
+
153
193
  type QueryResponse = Array<{
154
194
  [key: string]: SqlValueType;
155
195
  }>;
@@ -1092,5 +1132,5 @@ declare const pagingOption: (option: ListQueryOption) => DsPagingOption;
1092
1132
 
1093
1133
  type PagingOption = ListQueryOption;
1094
1134
 
1095
- export { CompositeCondition, Conditions, Mutations, QExpr, Queries, SQLClient, SQLTransaction, SasatDBDatasource, Sql, SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, pick, QExpr as qe, queryToSql };
1135
+ export { CompositeCondition, Conditions, Mutations, QExpr, Queries, SQLClient, SQLTransaction, SasatDBDatasource, Sql, SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, pick, QExpr as qe, queryToSql, setConfig };
1096
1136
  export type { BooleanValueExpression, CommandResponse, ComparisonOperators, CustomCondition, EntityResult, EntityType, Fields, ListQueryOption, LockMode, MigrationStore, PagingOption, Query, QueryOptions, QueryResponse, Relation, RelationMap, ResolverMiddleware, SQLExecutor, SasatMigration, TableInfo, TypeFieldDefinition };
package/dist/index.d.ts CHANGED
@@ -150,6 +150,46 @@ type Sort = {
150
150
 
151
151
  declare const queryToSql: (query: Query) => string;
152
152
 
153
+ type NestedPartial<T> = {
154
+ [K in keyof T]?: T[K] extends Array<infer R> ? Array<NestedPartial<R>> : NestedPartial<T[K]>;
155
+ };
156
+
157
+ interface SasatConfigDb {
158
+ host: string;
159
+ port: number;
160
+ user: string;
161
+ password?: string;
162
+ database: string;
163
+ ssl?: {
164
+ ca?: string[];
165
+ };
166
+ }
167
+ interface SasatConfigRedis {
168
+ host: string;
169
+ port: number;
170
+ password?: string;
171
+ }
172
+ interface SasatConfigMigration {
173
+ table: string;
174
+ dir: string;
175
+ out: string;
176
+ target?: string;
177
+ db?: SasatConfigDb;
178
+ }
179
+ interface SasatConfigGenerator {
180
+ addJsExtToImportStatement: boolean;
181
+ gql: {
182
+ subscription: boolean;
183
+ };
184
+ }
185
+ interface SasatConfig {
186
+ db: SasatConfigDb;
187
+ migration: SasatConfigMigration;
188
+ generator: SasatConfigGenerator;
189
+ redis: SasatConfigRedis;
190
+ }
191
+ declare function setConfig(update: NestedPartial<SasatConfig>): SasatConfig;
192
+
153
193
  type QueryResponse = Array<{
154
194
  [key: string]: SqlValueType;
155
195
  }>;
@@ -1092,5 +1132,5 @@ declare const pagingOption: (option: ListQueryOption) => DsPagingOption;
1092
1132
 
1093
1133
  type PagingOption = ListQueryOption;
1094
1134
 
1095
- export { CompositeCondition, Conditions, Mutations, QExpr, Queries, SQLClient, SQLTransaction, SasatDBDatasource, Sql, SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, pick, QExpr as qe, queryToSql };
1135
+ export { CompositeCondition, Conditions, Mutations, QExpr, Queries, SQLClient, SQLTransaction, SasatDBDatasource, Sql, SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, pick, QExpr as qe, queryToSql, setConfig };
1096
1136
  export type { BooleanValueExpression, CommandResponse, ComparisonOperators, CustomCondition, EntityResult, EntityType, Fields, ListQueryOption, LockMode, MigrationStore, PagingOption, Query, QueryOptions, QueryResponse, Relation, RelationMap, ResolverMiddleware, SQLExecutor, SasatMigration, TableInfo, TypeFieldDefinition };
package/dist/index.mjs CHANGED
@@ -1,11 +1,11 @@
1
- import { S as SqlString$1, n as nonNullable, u as unique, g as getDbClient, a as SasatError } from './shared/sasat.CtwXwmHh.mjs';
2
- export { C as Conditions, p as pick } from './shared/sasat.CtwXwmHh.mjs';
3
- import * as SqlString from 'sqlstring';
4
- import 'mysql2';
5
- import 'util';
1
+ import { S as SqlString$1, n as nonNullable, u as unique, g as getDbClient, a as SasatError } from './shared/sasat.OfgCNG3F.mjs';
2
+ export { C as Conditions, b as assignDeep, p as pick, s as setConfig } from './shared/sasat.OfgCNG3F.mjs';
6
3
  import 'path';
7
4
  import 'fs-extra';
8
5
  import 'js-yaml';
6
+ import * as SqlString from 'sqlstring';
7
+ import 'mysql2';
8
+ import 'util';
9
9
 
10
10
  var QueryNodeKind = /* @__PURE__ */ ((QueryNodeKind2) => {
11
11
  QueryNodeKind2[QueryNodeKind2["Field"] = 0] = "Field";
@@ -823,29 +823,6 @@ const getCurrentDateTimeString = () => {
823
823
  return date.getFullYear() + "-" + pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + " " + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds());
824
824
  };
825
825
 
826
- const assignDeep = (base, ...objects) => {
827
- const assign = (target, key, value) => {
828
- if (key === "__proto__" || key === "constructor") {
829
- return;
830
- }
831
- if (Array.isArray(target[key]) && Array.isArray(value)) {
832
- target[key] = [...target[key], ...value];
833
- } else if (typeof target[key] === "object" && typeof value === "object") {
834
- assignDeep(target[key], value);
835
- } else {
836
- target[key] = value;
837
- }
838
- };
839
- objects.forEach((obj) => {
840
- if (typeof obj === "object") {
841
- Object.entries(obj).forEach(([key, value]) => {
842
- assign(base, key, value);
843
- });
844
- }
845
- });
846
- return base;
847
- };
848
-
849
826
  const makeArgs = (args) => {
850
827
  if (!args || args.length === 0) return "";
851
828
  return `(${args.map((arg) => `${arg.name}: ${arg.type}`).join(", ")})`;
@@ -995,4 +972,4 @@ const pagingOption = (option) => {
995
972
  return { numberOfItem: option.numberOfItem, offset: option.offset, sort };
996
973
  };
997
974
 
998
- export { CompositeCondition, Mutations, QExpr, Queries, SasatDBDatasource, Sql, SqlString$1 as SqlString, assignDeep, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, QExpr as qe, queryToSql };
975
+ export { CompositeCondition, Mutations, QExpr, Queries, SasatDBDatasource, Sql, SqlString$1 as SqlString, createTypeDef, dateOffset, dateToDateString, dateToDatetimeString, getCurrentDateTimeString, getDayRange, getDayRangeQExpr, getDbClient, getTodayDateString, getTodayDateTimeString, gqlResolveInfoToField, makeNumberIdEncoder, makeParamsMiddleware, makeResolver, pagingOption, QExpr as qe, queryToSql };
@@ -33,6 +33,135 @@ const SqlString = {
33
33
  escapeId: (name) => escapeId(name)
34
34
  };
35
35
 
36
+ const assignDeep = (base, ...objects) => {
37
+ const assign = (target, key, value) => {
38
+ if (key === "__proto__" || key === "constructor") {
39
+ return;
40
+ }
41
+ if (Array.isArray(target[key]) && Array.isArray(value)) {
42
+ target[key] = [...target[key], ...value];
43
+ } else if (typeof target[key] === "object" && typeof value === "object") {
44
+ assignDeep(target[key], value);
45
+ } else {
46
+ target[key] = value;
47
+ }
48
+ };
49
+ objects.forEach((obj) => {
50
+ if (typeof obj === "object") {
51
+ Object.entries(obj).forEach(([key, value]) => {
52
+ assign(base, key, value);
53
+ });
54
+ }
55
+ });
56
+ return base;
57
+ };
58
+
59
+ const readYmlFile = (filepath) => yaml__namespace.load(fs__default.readFileSync(filepath, "utf8"));
60
+ const mkDirIfNotExist = (path) => {
61
+ if (!fs__default.pathExistsSync(path)) fs__default.mkdirpSync(path);
62
+ };
63
+ const writeFileIfNotExist = (path, data) => {
64
+ if (fs__default.existsSync(path)) return Promise.resolve();
65
+ return fs__default.writeFile(path, data);
66
+ };
67
+ const writeYmlFile = (path$1, fileName, obj) => {
68
+ mkDirIfNotExist(path$1);
69
+ fs__default.writeFileSync(
70
+ path.join(path$1, fileName),
71
+ yaml__namespace.dump(obj, {
72
+ skipInvalid: true,
73
+ noRefs: true,
74
+ sortKeys: (a, b) => {
75
+ if (b === "tableName") return 1;
76
+ if (a === "tableName") return -1;
77
+ if (a > b) return 1;
78
+ if (a < b) return -1;
79
+ return 0;
80
+ }
81
+ })
82
+ );
83
+ };
84
+ const readInitialSchema = () => {
85
+ return readYmlFile(path.join(config().migration.dir, "initialSchema.yml"));
86
+ };
87
+ const writeCurrentSchema = (schema) => {
88
+ writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
89
+ };
90
+
91
+ class SasatConfigLoader {
92
+ static loadConfig() {
93
+ const fileName = "sasat.yml";
94
+ const filepath = path__default.join(process.cwd(), fileName);
95
+ if (!fs__default.existsSync(filepath)) return defaultConf;
96
+ return readYmlFile(filepath);
97
+ }
98
+ conf;
99
+ constructor() {
100
+ const conf = this.readValue({
101
+ ...defaultConf,
102
+ ...SasatConfigLoader.loadConfig()
103
+ });
104
+ this.conf = {
105
+ ...conf
106
+ };
107
+ }
108
+ getConfig() {
109
+ return this.conf;
110
+ }
111
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
112
+ readValue(value) {
113
+ if (!value) return value;
114
+ if (Array.isArray(value)) return value.map((it) => this.readValue(it));
115
+ if (typeof value === "string" && value.startsWith("$"))
116
+ return process.env[value.slice(1)];
117
+ if (typeof value === "object") {
118
+ for (const key in value) {
119
+ if (Object.prototype.hasOwnProperty.call(value, key))
120
+ value[key] = this.readValue(value[key]);
121
+ }
122
+ return value;
123
+ }
124
+ return value;
125
+ }
126
+ }
127
+
128
+ const defaultConfDb = {
129
+ host: "127.0.0.1",
130
+ port: 3306,
131
+ user: "root",
132
+ database: "sasat",
133
+ password: ""
134
+ };
135
+ const defaultCofRedis = {
136
+ host: "127.0.0.1",
137
+ port: 6379
138
+ };
139
+ const defaultConfMigration = {
140
+ table: "__migrate__",
141
+ dir: "migrations",
142
+ out: "sasat"
143
+ };
144
+ const defaultConf = {
145
+ db: defaultConfDb,
146
+ migration: defaultConfMigration,
147
+ generator: {
148
+ addJsExtToImportStatement: false,
149
+ gql: {
150
+ subscription: true
151
+ }
152
+ },
153
+ redis: defaultCofRedis
154
+ };
155
+ let conf;
156
+ const config = () => {
157
+ if (conf === void 0) conf = new SasatConfigLoader().getConfig();
158
+ return conf;
159
+ };
160
+ function setConfig(update) {
161
+ conf = assignDeep(config(), update);
162
+ return conf;
163
+ }
164
+
36
165
  const parent = (field2) => ({
37
166
  kind: "parent",
38
167
  field: field2
@@ -240,115 +369,13 @@ class MySqlTransaction extends SQLTransaction {
240
369
  }
241
370
  }
242
371
 
243
- const readYmlFile = (filepath) => yaml__namespace.load(fs__default.readFileSync(filepath, "utf8"));
244
- const mkDirIfNotExist = (path) => {
245
- if (!fs__default.pathExistsSync(path)) fs__default.mkdirpSync(path);
246
- };
247
- const writeFileIfNotExist = (path, data) => {
248
- if (fs__default.existsSync(path)) return Promise.resolve();
249
- return fs__default.writeFile(path, data);
250
- };
251
- const writeYmlFile = (path$1, fileName, obj) => {
252
- mkDirIfNotExist(path$1);
253
- fs__default.writeFileSync(
254
- path.join(path$1, fileName),
255
- yaml__namespace.dump(obj, {
256
- skipInvalid: true,
257
- noRefs: true,
258
- sortKeys: (a, b) => {
259
- if (b === "tableName") return 1;
260
- if (a === "tableName") return -1;
261
- if (a > b) return 1;
262
- if (a < b) return -1;
263
- return 0;
264
- }
265
- })
266
- );
267
- };
268
- const readInitialSchema = () => {
269
- return readYmlFile(path.join(config().migration.dir, "initialSchema.yml"));
270
- };
271
- const writeCurrentSchema = (schema) => {
272
- writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
273
- };
274
-
275
- class SasatConfigLoader {
276
- static loadConfig() {
277
- const fileName = "sasat.yml";
278
- const filepath = path__default.join(process.cwd(), fileName);
279
- if (!fs__default.existsSync(filepath)) return defaultConf;
280
- return readYmlFile(filepath);
281
- }
282
- conf;
283
- constructor() {
284
- const conf = this.readValue({
285
- ...defaultConf,
286
- ...SasatConfigLoader.loadConfig()
287
- });
288
- this.conf = {
289
- ...conf
290
- };
291
- }
292
- getConfig() {
293
- return this.conf;
294
- }
295
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
296
- readValue(value) {
297
- if (!value) return value;
298
- if (Array.isArray(value)) return value.map((it) => this.readValue(it));
299
- if (typeof value === "string" && value.startsWith("$"))
300
- return process.env[value.slice(1)];
301
- if (typeof value === "object") {
302
- for (const key in value) {
303
- if (Object.prototype.hasOwnProperty.call(value, key))
304
- value[key] = this.readValue(value[key]);
305
- }
306
- return value;
307
- }
308
- return value;
309
- }
310
- }
311
-
312
- const defaultConfDb = {
313
- host: "127.0.0.1",
314
- port: 3306,
315
- user: "root",
316
- database: "sasat",
317
- password: ""
318
- };
319
- const defaultCofRedis = {
320
- host: "127.0.0.1",
321
- port: 6379
322
- };
323
- const defaultConfMigration = {
324
- table: "__migrate__",
325
- dir: "migrations",
326
- out: "sasat"
327
- };
328
- const defaultConf = {
329
- db: defaultConfDb,
330
- migration: defaultConfMigration,
331
- generator: {
332
- addJsExtToImportStatement: false,
333
- gql: {
334
- subscription: true
335
- }
336
- },
337
- redis: defaultCofRedis
338
- };
339
- let conf;
340
- const config = () => {
341
- if (conf === void 0) conf = new SasatConfigLoader().getConfig();
342
- return conf;
343
- };
344
-
345
- const defaultConfig = { ...config().db, dateStrings: true };
346
372
  class MysqlClient extends DBClient {
347
373
  constructor(connectionOption, poolOption) {
348
374
  super();
349
375
  this.connectionOption = connectionOption;
350
376
  this.pool = mysql__namespace.createPool({
351
- ...defaultConfig,
377
+ ...config().db,
378
+ dateStrings: true,
352
379
  ...connectionOption,
353
380
  ...poolOption
354
381
  });
@@ -359,7 +386,8 @@ class MysqlClient extends DBClient {
359
386
  };
360
387
  async transaction() {
361
388
  const connection = mysql__namespace.createConnection({
362
- ...defaultConfig,
389
+ ...config().db,
390
+ dateStrings: true,
363
391
  ...this.connectionOption
364
392
  });
365
393
  await util.promisify(connection.beginTransaction).bind(connection)();
@@ -393,6 +421,7 @@ class SasatError extends Error {
393
421
  exports.Conditions = Conditions;
394
422
  exports.SasatError = SasatError;
395
423
  exports.SqlString = SqlString;
424
+ exports.assignDeep = assignDeep;
396
425
  exports.config = config;
397
426
  exports.defaultConf = defaultConf;
398
427
  exports.getDbClient = getDbClient;
@@ -400,6 +429,7 @@ exports.mkDirIfNotExist = mkDirIfNotExist;
400
429
  exports.nonNullable = nonNullable;
401
430
  exports.pick = pick;
402
431
  exports.readInitialSchema = readInitialSchema;
432
+ exports.setConfig = setConfig;
403
433
  exports.unique = unique;
404
434
  exports.writeCurrentSchema = writeCurrentSchema;
405
435
  exports.writeFileIfNotExist = writeFileIfNotExist;
@@ -11,6 +11,135 @@ const SqlString = {
11
11
  escapeId: (name) => escapeId(name)
12
12
  };
13
13
 
14
+ const assignDeep = (base, ...objects) => {
15
+ const assign = (target, key, value) => {
16
+ if (key === "__proto__" || key === "constructor") {
17
+ return;
18
+ }
19
+ if (Array.isArray(target[key]) && Array.isArray(value)) {
20
+ target[key] = [...target[key], ...value];
21
+ } else if (typeof target[key] === "object" && typeof value === "object") {
22
+ assignDeep(target[key], value);
23
+ } else {
24
+ target[key] = value;
25
+ }
26
+ };
27
+ objects.forEach((obj) => {
28
+ if (typeof obj === "object") {
29
+ Object.entries(obj).forEach(([key, value]) => {
30
+ assign(base, key, value);
31
+ });
32
+ }
33
+ });
34
+ return base;
35
+ };
36
+
37
+ const readYmlFile = (filepath) => yaml.load(fs.readFileSync(filepath, "utf8"));
38
+ const mkDirIfNotExist = (path) => {
39
+ if (!fs.pathExistsSync(path)) fs.mkdirpSync(path);
40
+ };
41
+ const writeFileIfNotExist = (path, data) => {
42
+ if (fs.existsSync(path)) return Promise.resolve();
43
+ return fs.writeFile(path, data);
44
+ };
45
+ const writeYmlFile = (path, fileName, obj) => {
46
+ mkDirIfNotExist(path);
47
+ fs.writeFileSync(
48
+ join(path, fileName),
49
+ yaml.dump(obj, {
50
+ skipInvalid: true,
51
+ noRefs: true,
52
+ sortKeys: (a, b) => {
53
+ if (b === "tableName") return 1;
54
+ if (a === "tableName") return -1;
55
+ if (a > b) return 1;
56
+ if (a < b) return -1;
57
+ return 0;
58
+ }
59
+ })
60
+ );
61
+ };
62
+ const readInitialSchema = () => {
63
+ return readYmlFile(join(config().migration.dir, "initialSchema.yml"));
64
+ };
65
+ const writeCurrentSchema = (schema) => {
66
+ writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
67
+ };
68
+
69
+ class SasatConfigLoader {
70
+ static loadConfig() {
71
+ const fileName = "sasat.yml";
72
+ const filepath = path__default.join(process.cwd(), fileName);
73
+ if (!fs.existsSync(filepath)) return defaultConf;
74
+ return readYmlFile(filepath);
75
+ }
76
+ conf;
77
+ constructor() {
78
+ const conf = this.readValue({
79
+ ...defaultConf,
80
+ ...SasatConfigLoader.loadConfig()
81
+ });
82
+ this.conf = {
83
+ ...conf
84
+ };
85
+ }
86
+ getConfig() {
87
+ return this.conf;
88
+ }
89
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
90
+ readValue(value) {
91
+ if (!value) return value;
92
+ if (Array.isArray(value)) return value.map((it) => this.readValue(it));
93
+ if (typeof value === "string" && value.startsWith("$"))
94
+ return process.env[value.slice(1)];
95
+ if (typeof value === "object") {
96
+ for (const key in value) {
97
+ if (Object.prototype.hasOwnProperty.call(value, key))
98
+ value[key] = this.readValue(value[key]);
99
+ }
100
+ return value;
101
+ }
102
+ return value;
103
+ }
104
+ }
105
+
106
+ const defaultConfDb = {
107
+ host: "127.0.0.1",
108
+ port: 3306,
109
+ user: "root",
110
+ database: "sasat",
111
+ password: ""
112
+ };
113
+ const defaultCofRedis = {
114
+ host: "127.0.0.1",
115
+ port: 6379
116
+ };
117
+ const defaultConfMigration = {
118
+ table: "__migrate__",
119
+ dir: "migrations",
120
+ out: "sasat"
121
+ };
122
+ const defaultConf = {
123
+ db: defaultConfDb,
124
+ migration: defaultConfMigration,
125
+ generator: {
126
+ addJsExtToImportStatement: false,
127
+ gql: {
128
+ subscription: true
129
+ }
130
+ },
131
+ redis: defaultCofRedis
132
+ };
133
+ let conf;
134
+ const config = () => {
135
+ if (conf === void 0) conf = new SasatConfigLoader().getConfig();
136
+ return conf;
137
+ };
138
+ function setConfig(update) {
139
+ conf = assignDeep(config(), update);
140
+ return conf;
141
+ }
142
+
14
143
  const parent = (field2) => ({
15
144
  kind: "parent",
16
145
  field: field2
@@ -218,115 +347,13 @@ class MySqlTransaction extends SQLTransaction {
218
347
  }
219
348
  }
220
349
 
221
- const readYmlFile = (filepath) => yaml.load(fs.readFileSync(filepath, "utf8"));
222
- const mkDirIfNotExist = (path) => {
223
- if (!fs.pathExistsSync(path)) fs.mkdirpSync(path);
224
- };
225
- const writeFileIfNotExist = (path, data) => {
226
- if (fs.existsSync(path)) return Promise.resolve();
227
- return fs.writeFile(path, data);
228
- };
229
- const writeYmlFile = (path, fileName, obj) => {
230
- mkDirIfNotExist(path);
231
- fs.writeFileSync(
232
- join(path, fileName),
233
- yaml.dump(obj, {
234
- skipInvalid: true,
235
- noRefs: true,
236
- sortKeys: (a, b) => {
237
- if (b === "tableName") return 1;
238
- if (a === "tableName") return -1;
239
- if (a > b) return 1;
240
- if (a < b) return -1;
241
- return 0;
242
- }
243
- })
244
- );
245
- };
246
- const readInitialSchema = () => {
247
- return readYmlFile(join(config().migration.dir, "initialSchema.yml"));
248
- };
249
- const writeCurrentSchema = (schema) => {
250
- writeYmlFile(config().migration.dir, "currentSchema.yml", schema);
251
- };
252
-
253
- class SasatConfigLoader {
254
- static loadConfig() {
255
- const fileName = "sasat.yml";
256
- const filepath = path__default.join(process.cwd(), fileName);
257
- if (!fs.existsSync(filepath)) return defaultConf;
258
- return readYmlFile(filepath);
259
- }
260
- conf;
261
- constructor() {
262
- const conf = this.readValue({
263
- ...defaultConf,
264
- ...SasatConfigLoader.loadConfig()
265
- });
266
- this.conf = {
267
- ...conf
268
- };
269
- }
270
- getConfig() {
271
- return this.conf;
272
- }
273
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
274
- readValue(value) {
275
- if (!value) return value;
276
- if (Array.isArray(value)) return value.map((it) => this.readValue(it));
277
- if (typeof value === "string" && value.startsWith("$"))
278
- return process.env[value.slice(1)];
279
- if (typeof value === "object") {
280
- for (const key in value) {
281
- if (Object.prototype.hasOwnProperty.call(value, key))
282
- value[key] = this.readValue(value[key]);
283
- }
284
- return value;
285
- }
286
- return value;
287
- }
288
- }
289
-
290
- const defaultConfDb = {
291
- host: "127.0.0.1",
292
- port: 3306,
293
- user: "root",
294
- database: "sasat",
295
- password: ""
296
- };
297
- const defaultCofRedis = {
298
- host: "127.0.0.1",
299
- port: 6379
300
- };
301
- const defaultConfMigration = {
302
- table: "__migrate__",
303
- dir: "migrations",
304
- out: "sasat"
305
- };
306
- const defaultConf = {
307
- db: defaultConfDb,
308
- migration: defaultConfMigration,
309
- generator: {
310
- addJsExtToImportStatement: false,
311
- gql: {
312
- subscription: true
313
- }
314
- },
315
- redis: defaultCofRedis
316
- };
317
- let conf;
318
- const config = () => {
319
- if (conf === void 0) conf = new SasatConfigLoader().getConfig();
320
- return conf;
321
- };
322
-
323
- const defaultConfig = { ...config().db, dateStrings: true };
324
350
  class MysqlClient extends DBClient {
325
351
  constructor(connectionOption, poolOption) {
326
352
  super();
327
353
  this.connectionOption = connectionOption;
328
354
  this.pool = mysql.createPool({
329
- ...defaultConfig,
355
+ ...config().db,
356
+ dateStrings: true,
330
357
  ...connectionOption,
331
358
  ...poolOption
332
359
  });
@@ -337,7 +364,8 @@ class MysqlClient extends DBClient {
337
364
  };
338
365
  async transaction() {
339
366
  const connection = mysql.createConnection({
340
- ...defaultConfig,
367
+ ...config().db,
368
+ dateStrings: true,
341
369
  ...this.connectionOption
342
370
  });
343
371
  await promisify(connection.beginTransaction).bind(connection)();
@@ -368,4 +396,4 @@ class SasatError extends Error {
368
396
  }
369
397
  }
370
398
 
371
- export { Conditions as C, SqlString as S, SasatError as a, writeCurrentSchema as b, config as c, writeYmlFile as d, defaultConf as e, getDbClient as g, mkDirIfNotExist as m, nonNullable as n, pick as p, readInitialSchema as r, unique as u, writeFileIfNotExist as w };
399
+ export { Conditions as C, SqlString as S, SasatError as a, assignDeep as b, config as c, writeCurrentSchema as d, writeYmlFile as e, defaultConf as f, getDbClient as g, mkDirIfNotExist as m, nonNullable as n, pick as p, readInitialSchema as r, setConfig as s, unique as u, writeFileIfNotExist as w };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sasat",
3
- "version": "0.21.13",
3
+ "version": "0.21.15",
4
4
  "repository": "https://github.com/nin138/sasat.git",
5
5
  "author": "nin138 <ninian138@gmail.com>",
6
6
  "license": "MIT",