anote-server-libs 0.2.9 → 0.2.10
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/models/ApiCall.ts +40 -40
- package/models/Migration.ts +40 -40
- package/models/repository/BaseModelRepository.ts +152 -152
- package/models/repository/MemoryCache.ts +87 -87
- package/models/repository/ModelDao.ts +259 -259
- package/package.json +38 -38
- package/services/WithBody.ts +56 -56
- package/services/WithTransaction.js +6 -0
- package/services/WithTransaction.ts +130 -130
- package/services/utils.js +16 -16
- package/services/utils.ts +180 -180
- package/tsconfig.json +30 -30
package/models/ApiCall.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { ConnectionPool, DateTimeOffset, Request } from 'mssql';
|
|
2
|
-
import {Pool} from 'pg';
|
|
3
|
-
import {Logger} from 'winston';
|
|
4
|
-
import {Model, ModelDao} from './repository/ModelDao';
|
|
5
|
-
|
|
6
|
-
export interface ApiCall extends Model<string> {
|
|
7
|
-
responseCode: number;
|
|
8
|
-
responseJson: string;
|
|
9
|
-
expiresAt: Date;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class ApiCallRepository extends ModelDao<string, ApiCall> {
|
|
13
|
-
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
14
|
-
super(pool, poolMssql, logger, 'api_call', 5, pool ? 'id=$1,"updatedOn"=$2,"responseCode"=$3,"responseJson"=$4,"expiresAt"=$5' :
|
|
15
|
-
'id=@1,"updatedOn"=@2,"responseCode"=@3,"responseJson"=@4,"expiresAt"=@5');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
buildObject(q: any): ApiCall {
|
|
19
|
-
if(!q) return undefined;
|
|
20
|
-
q.id = q.id.trim();
|
|
21
|
-
q.updatedOn = q.updatedOn && new Date(q.updatedOn);
|
|
22
|
-
q.responseCode = parseInt(q.responseCode, 10);
|
|
23
|
-
q.expiresAt = q.expiresAt && new Date(q.expiresAt);
|
|
24
|
-
return q;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
serialize(instance: ApiCall, request?: Request) {
|
|
28
|
-
if(request) {
|
|
29
|
-
request.input('1', instance.id);
|
|
30
|
-
request.input('2', DateTimeOffset, instance.updatedOn);
|
|
31
|
-
request.input('3', instance.responseCode);
|
|
32
|
-
request.input('4', instance.responseJson);
|
|
33
|
-
request.input('5', DateTimeOffset, instance.expiresAt);
|
|
34
|
-
return undefined;
|
|
35
|
-
} else {
|
|
36
|
-
return [instance.id, instance.updatedOn, instance.responseCode, instance.responseJson, instance.expiresAt];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
}
|
|
1
|
+
import { ConnectionPool, DateTimeOffset, Request } from 'mssql';
|
|
2
|
+
import {Pool} from 'pg';
|
|
3
|
+
import {Logger} from 'winston';
|
|
4
|
+
import {Model, ModelDao} from './repository/ModelDao';
|
|
5
|
+
|
|
6
|
+
export interface ApiCall extends Model<string> {
|
|
7
|
+
responseCode: number;
|
|
8
|
+
responseJson: string;
|
|
9
|
+
expiresAt: Date;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ApiCallRepository extends ModelDao<string, ApiCall> {
|
|
13
|
+
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
14
|
+
super(pool, poolMssql, logger, 'api_call', 5, pool ? 'id=$1,"updatedOn"=$2,"responseCode"=$3,"responseJson"=$4,"expiresAt"=$5' :
|
|
15
|
+
'id=@1,"updatedOn"=@2,"responseCode"=@3,"responseJson"=@4,"expiresAt"=@5');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
buildObject(q: any): ApiCall {
|
|
19
|
+
if(!q) return undefined;
|
|
20
|
+
q.id = q.id.trim();
|
|
21
|
+
q.updatedOn = q.updatedOn && new Date(q.updatedOn);
|
|
22
|
+
q.responseCode = parseInt(q.responseCode, 10);
|
|
23
|
+
q.expiresAt = q.expiresAt && new Date(q.expiresAt);
|
|
24
|
+
return q;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
serialize(instance: ApiCall, request?: Request) {
|
|
28
|
+
if(request) {
|
|
29
|
+
request.input('1', instance.id);
|
|
30
|
+
request.input('2', DateTimeOffset, instance.updatedOn);
|
|
31
|
+
request.input('3', instance.responseCode);
|
|
32
|
+
request.input('4', instance.responseJson);
|
|
33
|
+
request.input('5', DateTimeOffset, instance.expiresAt);
|
|
34
|
+
return undefined;
|
|
35
|
+
} else {
|
|
36
|
+
return [instance.id, instance.updatedOn, instance.responseCode, instance.responseJson, instance.expiresAt];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
}
|
package/models/Migration.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import { ConnectionPool, Request } from 'mssql';
|
|
2
|
-
import {Pool} from 'pg';
|
|
3
|
-
import {Logger} from 'winston';
|
|
4
|
-
import {ModelDao} from './repository/ModelDao';
|
|
5
|
-
|
|
6
|
-
export interface Migration {
|
|
7
|
-
id: number;
|
|
8
|
-
hash: string;
|
|
9
|
-
sqlUp: string;
|
|
10
|
-
sqlDown: string;
|
|
11
|
-
state: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class MigrationRepository extends ModelDao<number, Migration> {
|
|
15
|
-
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
16
|
-
super(pool, poolMssql, logger, 'migration', 5, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5' :
|
|
17
|
-
'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5');
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
buildObject(q: any): Migration {
|
|
21
|
-
if(!q) return undefined;
|
|
22
|
-
q.id = parseInt(q.id, 10);
|
|
23
|
-
q.hash = q.hash.trim();
|
|
24
|
-
q.state = parseInt(q.state, 10);
|
|
25
|
-
return q;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
serialize(instance: Migration, request?: Request) {
|
|
29
|
-
if(request) {
|
|
30
|
-
request.input('1', instance.id);
|
|
31
|
-
request.input('2', instance.hash);
|
|
32
|
-
request.input('3', instance.sqlUp);
|
|
33
|
-
request.input('4', instance.sqlDown);
|
|
34
|
-
request.input('5', instance.state);
|
|
35
|
-
return undefined;
|
|
36
|
-
} else {
|
|
37
|
-
return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
1
|
+
import { ConnectionPool, Request } from 'mssql';
|
|
2
|
+
import {Pool} from 'pg';
|
|
3
|
+
import {Logger} from 'winston';
|
|
4
|
+
import {ModelDao} from './repository/ModelDao';
|
|
5
|
+
|
|
6
|
+
export interface Migration {
|
|
7
|
+
id: number;
|
|
8
|
+
hash: string;
|
|
9
|
+
sqlUp: string;
|
|
10
|
+
sqlDown: string;
|
|
11
|
+
state: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class MigrationRepository extends ModelDao<number, Migration> {
|
|
15
|
+
constructor(protected pool: Pool, protected poolMssql: ConnectionPool, protected logger: Logger) {
|
|
16
|
+
super(pool, poolMssql, logger, 'migration', 5, pool ? 'id=$1,hash=$2,"sqlUp"=$3,"sqlDown"=$4,state=$5' :
|
|
17
|
+
'id=@1,hash=@2,"sqlUp"=@3,"sqlDown"=@4,state=@5');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
buildObject(q: any): Migration {
|
|
21
|
+
if(!q) return undefined;
|
|
22
|
+
q.id = parseInt(q.id, 10);
|
|
23
|
+
q.hash = q.hash.trim();
|
|
24
|
+
q.state = parseInt(q.state, 10);
|
|
25
|
+
return q;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
serialize(instance: Migration, request?: Request) {
|
|
29
|
+
if(request) {
|
|
30
|
+
request.input('1', instance.id);
|
|
31
|
+
request.input('2', instance.hash);
|
|
32
|
+
request.input('3', instance.sqlUp);
|
|
33
|
+
request.input('4', instance.sqlDown);
|
|
34
|
+
request.input('5', instance.state);
|
|
35
|
+
return undefined;
|
|
36
|
+
} else {
|
|
37
|
+
return [instance.id, instance.hash, instance.sqlUp, instance.sqlDown, instance.state];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -1,152 +1,152 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as Memcached from 'memcached';
|
|
3
|
-
import {ConnectionPool, Transaction} from 'mssql';
|
|
4
|
-
import {md} from 'node-forge';
|
|
5
|
-
import {ClientBase, Pool} from 'pg';
|
|
6
|
-
import {Logger} from 'winston';
|
|
7
|
-
import { ApiCallRepository } from '../ApiCall';
|
|
8
|
-
import { Migration, MigrationRepository } from '../Migration';
|
|
9
|
-
import {ModelRepr} from './ModelDao';
|
|
10
|
-
|
|
11
|
-
export class BaseModelRepository {
|
|
12
|
-
Migration: MigrationRepository;
|
|
13
|
-
ApiCall: ApiCallRepository;
|
|
14
|
-
|
|
15
|
-
constructor(public db: Pool, public dbSpare: Pool, public dbMssql: ConnectionPool, public logger: Logger, public cache: Memcached) {
|
|
16
|
-
if(db && dbSpare) {
|
|
17
|
-
const dbQuery = db.query.bind(db);
|
|
18
|
-
db.query = (function(text: any, values: any, cb: any) {
|
|
19
|
-
if((this.idleCount + this.waitingCount) >= this.totalCount && this.totalCount === this.options.max)
|
|
20
|
-
return dbSpare.query(text, values, cb);
|
|
21
|
-
return dbQuery(text, values, cb);
|
|
22
|
-
}).bind(db);
|
|
23
|
-
}
|
|
24
|
-
this.Migration = new MigrationRepository(db, dbMssql, logger);
|
|
25
|
-
this.ApiCall = new ApiCallRepository(db, dbMssql, logger);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
migrate(migrationsPath: string, callback: (() => void)) {
|
|
29
|
-
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
30
|
-
'id integer PRIMARY KEY,' +
|
|
31
|
-
'hash text NOT NULL,' +
|
|
32
|
-
'"sqlUp" text NOT NULL,' +
|
|
33
|
-
'"sqlDown" text NOT NULL,' +
|
|
34
|
-
'state integer NOT NULL' +
|
|
35
|
-
')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
|
|
36
|
-
'id int PRIMARY KEY,' +
|
|
37
|
-
'hash text NOT NULL,' +
|
|
38
|
-
'"sqlUp" text NOT NULL,' +
|
|
39
|
-
'"sqlDown" text NOT NULL,' +
|
|
40
|
-
'state int NOT NULL' +
|
|
41
|
-
')')).then(() => {
|
|
42
|
-
this.Migration.getAllBy('id').then((migrations: Migration[]) => {
|
|
43
|
-
if(migrations.find(migration => migration.state !== 0)) process.exit(4); // Have to fix manually
|
|
44
|
-
// Read the new ones
|
|
45
|
-
fs.readdir(migrationsPath, (_, files) => {
|
|
46
|
-
const migrationsAvailable = files
|
|
47
|
-
.filter(file => /[0-9]+\.sql/.test(file))
|
|
48
|
-
.map(file => parseInt(file.split('.sql')[0], 10))
|
|
49
|
-
.filter(file => file > 0)
|
|
50
|
-
.sort((a, b) => a - b)
|
|
51
|
-
.map(file => {
|
|
52
|
-
const content = fs.readFileSync(migrationsPath + file + '.sql', 'utf-8');
|
|
53
|
-
return {
|
|
54
|
-
id: file,
|
|
55
|
-
content: content,
|
|
56
|
-
hash: md.sha256.create().update(content).digest().toHex()
|
|
57
|
-
};
|
|
58
|
-
});
|
|
59
|
-
if(migrationsAvailable.length === 0
|
|
60
|
-
|| migrationsAvailable.length
|
|
61
|
-
!== migrationsAvailable[migrationsAvailable.length - 1].id) process.exit(5); // Did not use OK files
|
|
62
|
-
let highestCommon = 0;
|
|
63
|
-
while(highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
64
|
-
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
65
|
-
highestCommon++;
|
|
66
|
-
this.applyDownUntil(migrations, migrations.length, highestCommon).then(() => {
|
|
67
|
-
this.applyUpUntil(migrationsAvailable, highestCommon, migrationsAvailable.length).then(callback, process.exit);
|
|
68
|
-
}, process.exit);
|
|
69
|
-
});
|
|
70
|
-
}, () => process.exit(3));
|
|
71
|
-
}, () => process.exit(2));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
lockTables(tables: ModelRepr[], client: ClientBase | Transaction): Promise<any> {
|
|
75
|
-
if(this.db)
|
|
76
|
-
return Promise.all(tables.map(t => (<ClientBase>client).query('LOCK TABLE ' + t.table + ' IN EXCLUSIVE MODE')));
|
|
77
|
-
return Promise.all(tables.map(t => (<Transaction>client).request().query('SELECT id FROM ' + t.table + ' WITH (UPDLOCK)')));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private applyUpUntil(migrations: {id: number, content: string, hash: string}[], current: number, until: number): Promise<void> {
|
|
81
|
-
if(current < until)
|
|
82
|
-
return this.applyUp(migrations[current]).then(() => this.applyUpUntil(migrations, current + 1, until));
|
|
83
|
-
return Promise.resolve();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private applyUp(migration: {id: number, content: string, hash: string}): Promise<void> {
|
|
87
|
-
return new Promise((resolve, reject) => {
|
|
88
|
-
const sqlParts = migration.content.split('----');
|
|
89
|
-
this.Migration.create({
|
|
90
|
-
id: migration.id,
|
|
91
|
-
hash: migration.hash,
|
|
92
|
-
sqlUp: sqlParts[0],
|
|
93
|
-
sqlDown: sqlParts[1],
|
|
94
|
-
state: 2
|
|
95
|
-
}).then(() => {
|
|
96
|
-
(<any>(this.db|| this.dbMssql)).query(sqlParts[0], (err: any) => {
|
|
97
|
-
if(err) {
|
|
98
|
-
console.error(err);
|
|
99
|
-
reject(10);
|
|
100
|
-
} else {
|
|
101
|
-
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=0 WHERE "id"=' + migration.id, (err2: any) => {
|
|
102
|
-
if(err2) reject(11); // No cleanup
|
|
103
|
-
else resolve();
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
}, () => process.exit(9));
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private applyDownUntil(migrations: Migration[], current: number, until: number): Promise<void> {
|
|
112
|
-
if(current > until) {
|
|
113
|
-
current--;
|
|
114
|
-
return this.applyDown(migrations[current]).then(() => this.applyDownUntil(migrations, current, until));
|
|
115
|
-
}
|
|
116
|
-
return Promise.resolve();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
private applyDown(migration: Migration): Promise<void> {
|
|
120
|
-
return new Promise((resolve, reject) => {
|
|
121
|
-
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=1 WHERE "id"=' + migration.id, (err: any) => {
|
|
122
|
-
if(err) reject(6); // No required change
|
|
123
|
-
else (<any>(this.db|| this.dbMssql)).query(migration.sqlDown, (err2: any) => {
|
|
124
|
-
if(err2) {
|
|
125
|
-
console.error(err2);
|
|
126
|
-
reject(7);
|
|
127
|
-
} // No apply down
|
|
128
|
-
else (<any>(this.db|| this.dbMssql)).query('DELETE FROM "migration" WHERE "id"=' + migration.id, (err3: any) => {
|
|
129
|
-
if(err3 && migration.id !== 1) reject(8); // No cleanup for not base migration
|
|
130
|
-
else {
|
|
131
|
-
if(migration.id === 1) {
|
|
132
|
-
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
133
|
-
'id integer PRIMARY KEY,' +
|
|
134
|
-
'hash text NOT NULL,' +
|
|
135
|
-
'"sqlUp" text NOT NULL,' +
|
|
136
|
-
'"sqlDown" text NOT NULL,' +
|
|
137
|
-
'state integer NOT NULL' +
|
|
138
|
-
')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
|
|
139
|
-
'id int PRIMARY KEY,' +
|
|
140
|
-
'hash text NOT NULL,' +
|
|
141
|
-
'"sqlUp" text NOT NULL,' +
|
|
142
|
-
'"sqlDown" text NOT NULL,' +
|
|
143
|
-
'state int NOT NULL' +
|
|
144
|
-
')')).then(() => resolve(), reject);
|
|
145
|
-
} else resolve();
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as Memcached from 'memcached';
|
|
3
|
+
import {ConnectionPool, Transaction} from 'mssql';
|
|
4
|
+
import {md} from 'node-forge';
|
|
5
|
+
import {ClientBase, Pool} from 'pg';
|
|
6
|
+
import {Logger} from 'winston';
|
|
7
|
+
import { ApiCallRepository } from '../ApiCall';
|
|
8
|
+
import { Migration, MigrationRepository } from '../Migration';
|
|
9
|
+
import {ModelRepr} from './ModelDao';
|
|
10
|
+
|
|
11
|
+
export class BaseModelRepository {
|
|
12
|
+
Migration: MigrationRepository;
|
|
13
|
+
ApiCall: ApiCallRepository;
|
|
14
|
+
|
|
15
|
+
constructor(public db: Pool, public dbSpare: Pool, public dbMssql: ConnectionPool, public logger: Logger, public cache: Memcached) {
|
|
16
|
+
if(db && dbSpare) {
|
|
17
|
+
const dbQuery = db.query.bind(db);
|
|
18
|
+
db.query = (function(text: any, values: any, cb: any) {
|
|
19
|
+
if((this.idleCount + this.waitingCount) >= this.totalCount && this.totalCount === this.options.max)
|
|
20
|
+
return dbSpare.query(text, values, cb);
|
|
21
|
+
return dbQuery(text, values, cb);
|
|
22
|
+
}).bind(db);
|
|
23
|
+
}
|
|
24
|
+
this.Migration = new MigrationRepository(db, dbMssql, logger);
|
|
25
|
+
this.ApiCall = new ApiCallRepository(db, dbMssql, logger);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
migrate(migrationsPath: string, callback: (() => void)) {
|
|
29
|
+
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
30
|
+
'id integer PRIMARY KEY,' +
|
|
31
|
+
'hash text NOT NULL,' +
|
|
32
|
+
'"sqlUp" text NOT NULL,' +
|
|
33
|
+
'"sqlDown" text NOT NULL,' +
|
|
34
|
+
'state integer NOT NULL' +
|
|
35
|
+
')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
|
|
36
|
+
'id int PRIMARY KEY,' +
|
|
37
|
+
'hash text NOT NULL,' +
|
|
38
|
+
'"sqlUp" text NOT NULL,' +
|
|
39
|
+
'"sqlDown" text NOT NULL,' +
|
|
40
|
+
'state int NOT NULL' +
|
|
41
|
+
')')).then(() => {
|
|
42
|
+
this.Migration.getAllBy('id').then((migrations: Migration[]) => {
|
|
43
|
+
if(migrations.find(migration => migration.state !== 0)) process.exit(4); // Have to fix manually
|
|
44
|
+
// Read the new ones
|
|
45
|
+
fs.readdir(migrationsPath, (_, files) => {
|
|
46
|
+
const migrationsAvailable = files
|
|
47
|
+
.filter(file => /[0-9]+\.sql/.test(file))
|
|
48
|
+
.map(file => parseInt(file.split('.sql')[0], 10))
|
|
49
|
+
.filter(file => file > 0)
|
|
50
|
+
.sort((a, b) => a - b)
|
|
51
|
+
.map(file => {
|
|
52
|
+
const content = fs.readFileSync(migrationsPath + file + '.sql', 'utf-8');
|
|
53
|
+
return {
|
|
54
|
+
id: file,
|
|
55
|
+
content: content,
|
|
56
|
+
hash: md.sha256.create().update(content).digest().toHex()
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
if(migrationsAvailable.length === 0
|
|
60
|
+
|| migrationsAvailable.length
|
|
61
|
+
!== migrationsAvailable[migrationsAvailable.length - 1].id) process.exit(5); // Did not use OK files
|
|
62
|
+
let highestCommon = 0;
|
|
63
|
+
while(highestCommon < migrations.length && highestCommon < migrationsAvailable.length
|
|
64
|
+
&& migrations[highestCommon].hash === migrationsAvailable[highestCommon].hash)
|
|
65
|
+
highestCommon++;
|
|
66
|
+
this.applyDownUntil(migrations, migrations.length, highestCommon).then(() => {
|
|
67
|
+
this.applyUpUntil(migrationsAvailable, highestCommon, migrationsAvailable.length).then(callback, process.exit);
|
|
68
|
+
}, process.exit);
|
|
69
|
+
});
|
|
70
|
+
}, () => process.exit(3));
|
|
71
|
+
}, () => process.exit(2));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
lockTables(tables: ModelRepr[], client: ClientBase | Transaction): Promise<any> {
|
|
75
|
+
if(this.db)
|
|
76
|
+
return Promise.all(tables.map(t => (<ClientBase>client).query('LOCK TABLE ' + t.table + ' IN EXCLUSIVE MODE')));
|
|
77
|
+
return Promise.all(tables.map(t => (<Transaction>client).request().query('SELECT id FROM ' + t.table + ' WITH (UPDLOCK)')));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private applyUpUntil(migrations: {id: number, content: string, hash: string}[], current: number, until: number): Promise<void> {
|
|
81
|
+
if(current < until)
|
|
82
|
+
return this.applyUp(migrations[current]).then(() => this.applyUpUntil(migrations, current + 1, until));
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private applyUp(migration: {id: number, content: string, hash: string}): Promise<void> {
|
|
87
|
+
return new Promise((resolve, reject) => {
|
|
88
|
+
const sqlParts = migration.content.split('----');
|
|
89
|
+
this.Migration.create({
|
|
90
|
+
id: migration.id,
|
|
91
|
+
hash: migration.hash,
|
|
92
|
+
sqlUp: sqlParts[0],
|
|
93
|
+
sqlDown: sqlParts[1],
|
|
94
|
+
state: 2
|
|
95
|
+
}).then(() => {
|
|
96
|
+
(<any>(this.db|| this.dbMssql)).query(sqlParts[0], (err: any) => {
|
|
97
|
+
if(err) {
|
|
98
|
+
console.error(err);
|
|
99
|
+
reject(10);
|
|
100
|
+
} else {
|
|
101
|
+
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=0 WHERE "id"=' + migration.id, (err2: any) => {
|
|
102
|
+
if(err2) reject(11); // No cleanup
|
|
103
|
+
else resolve();
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}, () => process.exit(9));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private applyDownUntil(migrations: Migration[], current: number, until: number): Promise<void> {
|
|
112
|
+
if(current > until) {
|
|
113
|
+
current--;
|
|
114
|
+
return this.applyDown(migrations[current]).then(() => this.applyDownUntil(migrations, current, until));
|
|
115
|
+
}
|
|
116
|
+
return Promise.resolve();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private applyDown(migration: Migration): Promise<void> {
|
|
120
|
+
return new Promise((resolve, reject) => {
|
|
121
|
+
(<any>(this.db|| this.dbMssql)).query('UPDATE "migration" SET "state"=1 WHERE "id"=' + migration.id, (err: any) => {
|
|
122
|
+
if(err) reject(6); // No required change
|
|
123
|
+
else (<any>(this.db|| this.dbMssql)).query(migration.sqlDown, (err2: any) => {
|
|
124
|
+
if(err2) {
|
|
125
|
+
console.error(err2);
|
|
126
|
+
reject(7);
|
|
127
|
+
} // No apply down
|
|
128
|
+
else (<any>(this.db|| this.dbMssql)).query('DELETE FROM "migration" WHERE "id"=' + migration.id, (err3: any) => {
|
|
129
|
+
if(err3 && migration.id !== 1) reject(8); // No cleanup for not base migration
|
|
130
|
+
else {
|
|
131
|
+
if(migration.id === 1) {
|
|
132
|
+
(this.db ? this.db.query('CREATE TABLE IF NOT EXISTS migration (' +
|
|
133
|
+
'id integer PRIMARY KEY,' +
|
|
134
|
+
'hash text NOT NULL,' +
|
|
135
|
+
'"sqlUp" text NOT NULL,' +
|
|
136
|
+
'"sqlDown" text NOT NULL,' +
|
|
137
|
+
'state integer NOT NULL' +
|
|
138
|
+
')') : this.dbMssql.query('if not exists (select * from sysobjects where name=\'migration\' and xtype=\'U\') CREATE TABLE migration (' +
|
|
139
|
+
'id int PRIMARY KEY,' +
|
|
140
|
+
'hash text NOT NULL,' +
|
|
141
|
+
'"sqlUp" text NOT NULL,' +
|
|
142
|
+
'"sqlDown" text NOT NULL,' +
|
|
143
|
+
'state int NOT NULL' +
|
|
144
|
+
')')).then(() => resolve(), reject);
|
|
145
|
+
} else resolve();
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
import * as Memcached from 'memcached';
|
|
2
|
-
|
|
3
|
-
export class MemoryCache<T> {
|
|
4
|
-
|
|
5
|
-
private localCache = {};
|
|
6
|
-
|
|
7
|
-
constructor(private localKey: string, private cache?: Memcached) {
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
list(): Promise<T[]> {
|
|
11
|
-
if(this.cache) return new Promise(resolve => {
|
|
12
|
-
let pending = true;
|
|
13
|
-
setTimeout(() => {
|
|
14
|
-
if(pending) {
|
|
15
|
-
pending = false;
|
|
16
|
-
resolve([]);
|
|
17
|
-
}
|
|
18
|
-
}, 250);
|
|
19
|
-
this.cache.items((_, data) => {
|
|
20
|
-
if(pending) {
|
|
21
|
-
pending = false;
|
|
22
|
-
resolve(data.map(s => {
|
|
23
|
-
const value = <string>s[Object.getOwnPropertyNames(s).find(sname => sname.startsWith(this.localKey))];
|
|
24
|
-
return value && JSON.parse(value);
|
|
25
|
-
}).filter(x => x));
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
else return Promise.resolve(Object.getOwnPropertyNames(this.localCache).map(key => this.localCache[key]));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get(key: string | number): Promise<T> {
|
|
33
|
-
if(this.cache) return new Promise(resolve => {
|
|
34
|
-
let pending = true;
|
|
35
|
-
setTimeout(() => {
|
|
36
|
-
if(pending) {
|
|
37
|
-
pending = false;
|
|
38
|
-
resolve(undefined);
|
|
39
|
-
}
|
|
40
|
-
}, 250);
|
|
41
|
-
this.cache.get(this.localKey + key, (_, data) => {
|
|
42
|
-
if(pending) {
|
|
43
|
-
pending = false;
|
|
44
|
-
resolve(data && JSON.parse(data));
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
else return Promise.resolve(this.localCache[key]);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
set(key: string | number, val: T): Promise<void> {
|
|
52
|
-
if(this.cache) return new Promise(resolve => this.cache.add(this.localKey + key, JSON.stringify(val), 15 * 60, resolve));
|
|
53
|
-
else {
|
|
54
|
-
this.localCache[key] = val;
|
|
55
|
-
return Promise.resolve();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
delete(key: string | number): Promise<void> {
|
|
60
|
-
if(this.cache) {
|
|
61
|
-
return new Promise(resolve => {
|
|
62
|
-
let fired = false;
|
|
63
|
-
const handler = setTimeout(() => {
|
|
64
|
-
fired = true;
|
|
65
|
-
resolve();
|
|
66
|
-
}, 50);
|
|
67
|
-
this.cache.del(this.localKey + key, () => {
|
|
68
|
-
if(!fired) {
|
|
69
|
-
clearTimeout(handler);
|
|
70
|
-
resolve();
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
} else {
|
|
75
|
-
this.localCache[key] = undefined;
|
|
76
|
-
return Promise.resolve();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
clear() {
|
|
81
|
-
if(this.cache) {
|
|
82
|
-
this.localKey = this.localKey + '_';
|
|
83
|
-
if(this.localKey.length > 40)
|
|
84
|
-
this.localKey = this.localKey.replace(/_+$/, '');
|
|
85
|
-
} else this.localCache = {};
|
|
86
|
-
}
|
|
87
|
-
}
|
|
1
|
+
import * as Memcached from 'memcached';
|
|
2
|
+
|
|
3
|
+
export class MemoryCache<T> {
|
|
4
|
+
|
|
5
|
+
private localCache = {};
|
|
6
|
+
|
|
7
|
+
constructor(private localKey: string, private cache?: Memcached) {
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
list(): Promise<T[]> {
|
|
11
|
+
if(this.cache) return new Promise(resolve => {
|
|
12
|
+
let pending = true;
|
|
13
|
+
setTimeout(() => {
|
|
14
|
+
if(pending) {
|
|
15
|
+
pending = false;
|
|
16
|
+
resolve([]);
|
|
17
|
+
}
|
|
18
|
+
}, 250);
|
|
19
|
+
this.cache.items((_, data) => {
|
|
20
|
+
if(pending) {
|
|
21
|
+
pending = false;
|
|
22
|
+
resolve(data.map(s => {
|
|
23
|
+
const value = <string>s[Object.getOwnPropertyNames(s).find(sname => sname.startsWith(this.localKey))];
|
|
24
|
+
return value && JSON.parse(value);
|
|
25
|
+
}).filter(x => x));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
else return Promise.resolve(Object.getOwnPropertyNames(this.localCache).map(key => this.localCache[key]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get(key: string | number): Promise<T> {
|
|
33
|
+
if(this.cache) return new Promise(resolve => {
|
|
34
|
+
let pending = true;
|
|
35
|
+
setTimeout(() => {
|
|
36
|
+
if(pending) {
|
|
37
|
+
pending = false;
|
|
38
|
+
resolve(undefined);
|
|
39
|
+
}
|
|
40
|
+
}, 250);
|
|
41
|
+
this.cache.get(this.localKey + key, (_, data) => {
|
|
42
|
+
if(pending) {
|
|
43
|
+
pending = false;
|
|
44
|
+
resolve(data && JSON.parse(data));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
else return Promise.resolve(this.localCache[key]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
set(key: string | number, val: T): Promise<void> {
|
|
52
|
+
if(this.cache) return new Promise(resolve => this.cache.add(this.localKey + key, JSON.stringify(val), 15 * 60, resolve));
|
|
53
|
+
else {
|
|
54
|
+
this.localCache[key] = val;
|
|
55
|
+
return Promise.resolve();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
delete(key: string | number): Promise<void> {
|
|
60
|
+
if(this.cache) {
|
|
61
|
+
return new Promise(resolve => {
|
|
62
|
+
let fired = false;
|
|
63
|
+
const handler = setTimeout(() => {
|
|
64
|
+
fired = true;
|
|
65
|
+
resolve();
|
|
66
|
+
}, 50);
|
|
67
|
+
this.cache.del(this.localKey + key, () => {
|
|
68
|
+
if(!fired) {
|
|
69
|
+
clearTimeout(handler);
|
|
70
|
+
resolve();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
this.localCache[key] = undefined;
|
|
76
|
+
return Promise.resolve();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
clear() {
|
|
81
|
+
if(this.cache) {
|
|
82
|
+
this.localKey = this.localKey + '_';
|
|
83
|
+
if(this.localKey.length > 40)
|
|
84
|
+
this.localKey = this.localKey.replace(/_+$/, '');
|
|
85
|
+
} else this.localCache = {};
|
|
86
|
+
}
|
|
87
|
+
}
|