anote-server-libs 0.3.2 → 0.3.4

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,152 +1,152 @@
1
- import * as crypto from 'crypto';
2
- import * as fs from 'fs';
3
- import * as Memcached from 'memcached';
4
- import {ConnectionPool, Transaction} from 'mssql';
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), onlyAboveOrEquals = 0) {
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: crypto.createHash('sha256').update(content).digest('hex')
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 = onlyAboveOrEquals;
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, Math.min(highestCommon, migrations.length), 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 crypto from 'crypto';
2
+ import * as fs from 'fs';
3
+ import * as Memcached from 'memcached';
4
+ import {ConnectionPool, Transaction} from 'mssql';
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), onlyAboveOrEquals = 0) {
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: crypto.createHash('sha256').update(content).digest('hex')
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 = onlyAboveOrEquals;
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, Math.min(highestCommon, migrations.length), 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,92 +1,92 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MemoryCache = void 0;
4
- class MemoryCache {
5
- constructor(localKey, cache) {
6
- this.localKey = localKey;
7
- this.cache = cache;
8
- this.localCache = {};
9
- }
10
- list() {
11
- if (this.cache)
12
- return new Promise(resolve => {
13
- let pending = true;
14
- setTimeout(() => {
15
- if (pending) {
16
- pending = false;
17
- resolve([]);
18
- }
19
- }, 250);
20
- this.cache.items((_, data) => {
21
- if (pending) {
22
- pending = false;
23
- resolve(data.map(s => {
24
- const value = s[Object.getOwnPropertyNames(s).find(sname => sname.startsWith(this.localKey))];
25
- return value && JSON.parse(value);
26
- }).filter(x => x));
27
- }
28
- });
29
- });
30
- else
31
- return Promise.resolve(Object.getOwnPropertyNames(this.localCache).map(key => this.localCache[key]));
32
- }
33
- get(key) {
34
- if (this.cache)
35
- return new Promise(resolve => {
36
- let pending = true;
37
- setTimeout(() => {
38
- if (pending) {
39
- pending = false;
40
- resolve(undefined);
41
- }
42
- }, 250);
43
- this.cache.get(this.localKey + key, (_, data) => {
44
- if (pending) {
45
- pending = false;
46
- resolve(data && JSON.parse(data));
47
- }
48
- });
49
- });
50
- else
51
- return Promise.resolve(this.localCache[key]);
52
- }
53
- set(key, val) {
54
- if (this.cache)
55
- return new Promise(resolve => this.cache.add(this.localKey + key, JSON.stringify(val), 15 * 60, resolve));
56
- else {
57
- this.localCache[key] = val;
58
- return Promise.resolve();
59
- }
60
- }
61
- delete(key) {
62
- if (this.cache) {
63
- return new Promise(resolve => {
64
- let fired = false;
65
- const handler = setTimeout(() => {
66
- fired = true;
67
- resolve();
68
- }, 50);
69
- this.cache.del(this.localKey + key, () => {
70
- if (!fired) {
71
- clearTimeout(handler);
72
- resolve();
73
- }
74
- });
75
- });
76
- }
77
- else {
78
- this.localCache[key] = undefined;
79
- return Promise.resolve();
80
- }
81
- }
82
- clear() {
83
- if (this.cache) {
84
- this.localKey = this.localKey + '_';
85
- if (this.localKey.length > 40)
86
- this.localKey = this.localKey.replace(/_+$/, '');
87
- }
88
- else
89
- this.localCache = {};
90
- }
91
- }
92
- exports.MemoryCache = MemoryCache;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MemoryCache = void 0;
4
+ class MemoryCache {
5
+ constructor(localKey, cache) {
6
+ this.localKey = localKey;
7
+ this.cache = cache;
8
+ this.localCache = {};
9
+ }
10
+ list() {
11
+ if (this.cache)
12
+ return new Promise(resolve => {
13
+ let pending = true;
14
+ setTimeout(() => {
15
+ if (pending) {
16
+ pending = false;
17
+ resolve([]);
18
+ }
19
+ }, 250);
20
+ this.cache.items((_, data) => {
21
+ if (pending) {
22
+ pending = false;
23
+ resolve(data.map(s => {
24
+ const value = s[Object.getOwnPropertyNames(s).find(sname => sname.startsWith(this.localKey))];
25
+ return value && JSON.parse(value);
26
+ }).filter(x => x));
27
+ }
28
+ });
29
+ });
30
+ else
31
+ return Promise.resolve(Object.getOwnPropertyNames(this.localCache).map(key => this.localCache[key]));
32
+ }
33
+ get(key) {
34
+ if (this.cache)
35
+ return new Promise(resolve => {
36
+ let pending = true;
37
+ setTimeout(() => {
38
+ if (pending) {
39
+ pending = false;
40
+ resolve(undefined);
41
+ }
42
+ }, 250);
43
+ this.cache.get(this.localKey + key, (_, data) => {
44
+ if (pending) {
45
+ pending = false;
46
+ resolve(data && JSON.parse(data));
47
+ }
48
+ });
49
+ });
50
+ else
51
+ return Promise.resolve(this.localCache[key]);
52
+ }
53
+ set(key, val) {
54
+ if (this.cache)
55
+ return new Promise(resolve => this.cache.add(this.localKey + key, JSON.stringify(val), 15 * 60, resolve));
56
+ else {
57
+ this.localCache[key] = val;
58
+ return Promise.resolve();
59
+ }
60
+ }
61
+ delete(key) {
62
+ if (this.cache) {
63
+ return new Promise(resolve => {
64
+ let fired = false;
65
+ const handler = setTimeout(() => {
66
+ fired = true;
67
+ resolve();
68
+ }, 50);
69
+ this.cache.del(this.localKey + key, () => {
70
+ if (!fired) {
71
+ clearTimeout(handler);
72
+ resolve();
73
+ }
74
+ });
75
+ });
76
+ }
77
+ else {
78
+ this.localCache[key] = undefined;
79
+ return Promise.resolve();
80
+ }
81
+ }
82
+ clear() {
83
+ if (this.cache) {
84
+ this.localKey = this.localKey + '_';
85
+ if (this.localKey.length > 40)
86
+ this.localKey = this.localKey.replace(/_+$/, '');
87
+ }
88
+ else
89
+ this.localCache = {};
90
+ }
91
+ }
92
+ exports.MemoryCache = MemoryCache;