@adaptivestone/framework 5.0.0-beta.16 → 5.0.0-beta.18
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/CHANGELOG.md +54 -0
- package/dist/commands/GenerateTypes.d.ts +5 -0
- package/dist/commands/GenerateTypes.js +48 -1
- package/dist/commands/GenerateTypes.js.map +1 -1
- package/dist/helpers/appInstance.d.ts +3 -0
- package/dist/helpers/appInstance.js +8 -0
- package/dist/helpers/appInstance.js.map +1 -0
- package/dist/helpers/crypto.d.ts +3 -0
- package/dist/helpers/crypto.js +14 -0
- package/dist/helpers/crypto.js.map +1 -0
- package/dist/helpers/redis/clearNamespace.d.ts +1 -2
- package/dist/helpers/redis/clearNamespace.js +2 -3
- package/dist/helpers/redis/clearNamespace.js.map +1 -1
- package/dist/models/Lock.d.ts +84 -57
- package/dist/models/Lock.js +87 -82
- package/dist/models/Lock.js.map +1 -1
- package/dist/models/Migration.d.ts +6 -9
- package/dist/models/Migration.js +3 -4
- package/dist/models/Migration.js.map +1 -1
- package/dist/models/Sequence.d.ts +22 -18
- package/dist/models/Sequence.js +24 -7
- package/dist/models/Sequence.js.map +1 -1
- package/dist/models/User.d.ts +2347 -153
- package/dist/models/User.js +203 -173
- package/dist/models/User.js.map +1 -1
- package/dist/models/UserOld.d.ts +179 -0
- package/dist/models/UserOld.js +230 -0
- package/dist/models/UserOld.js.map +1 -0
- package/dist/modules/AbstractModel.d.ts +1 -1
- package/dist/modules/AbstractModel.js +1 -31
- package/dist/modules/AbstractModel.js.map +1 -1
- package/dist/modules/BaseModel.d.ts +49 -0
- package/dist/modules/BaseModel.js +31 -0
- package/dist/modules/BaseModel.js.map +1 -0
- package/dist/server.d.ts +5 -3
- package/dist/server.js +44 -3
- package/dist/server.js.map +1 -1
- package/dist/tests/setupVitest.js +1 -1
- package/dist/tests/setupVitest.js.map +1 -1
- package/genTypes.d.ts +101 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
+
### 5.0.0-next
|
|
2
|
+
|
|
3
|
+
This is a big release that contains a lot of new features and breaking changes.
|
|
4
|
+
Main feature of that release is full TypeScript support insluding mongoose models.
|
|
5
|
+
|
|
6
|
+
#### New Features
|
|
7
|
+
|
|
8
|
+
[NEW] Full TypeScript support, including Mongoose models.
|
|
9
|
+
[NEW] New model type: BaseModel, simplifying work with TypeScript and based on statics.
|
|
10
|
+
[NEW] AppInstance helper to access the app instance from anywhere without passing it.
|
|
11
|
+
[NEW] GenerateTypes command added.
|
|
12
|
+
[NEW] Lock model for working with locks via MongoDB.
|
|
13
|
+
[NEW] FrameworkFolder folder added to the app for module usage.
|
|
14
|
+
[NEW] Ability to skip Mongo model initialization in CLI environments.
|
|
15
|
+
[NEW] Mongo connections in CLI now have unique names, including the command name.
|
|
16
|
+
[NEW] On shutdown event, force shutdown after a timeout.
|
|
17
|
+
[NEW] GenerateRandomBytes command added.
|
|
18
|
+
[NEW] IpDetector middleware for detecting proxies and X-Forwarded-For headers.
|
|
19
|
+
|
|
20
|
+
#### Breaking changes (please read carefully)
|
|
21
|
+
|
|
22
|
+
[BREAKING] all models now should be extended from BaseModel. This is a potencial breaking change specially for User model.
|
|
23
|
+
[BREAKING] Remove jest support for testing
|
|
24
|
+
[BREAKING] move email module to separate package @adaptivestone/framework-module-email. Please use it if you want to send emails
|
|
25
|
+
[BREAKING] remove VIEWS folders at all. Should not afffect any user as this was not used internally
|
|
26
|
+
[BREAKING] removed noidemailer-sendmail-transport. Not needed anymore and not recommended to use as well
|
|
27
|
+
[BREAKING] remove minimist CLI parsing and replace it by commandArguments parser
|
|
28
|
+
[BREAKING] vitest v3 https://vitest.dev/guide/migration.html
|
|
29
|
+
[BREAKING] i18next v24 https://www.i18next.com/misc/migration-guide#v23.x.x-to-v24.0.0
|
|
30
|
+
[BREAKING] possible breaking. Framework start using express 5 instead of express 4. Please follow express migration guide too https://expressjs.com/en/guide/migrating-5.html.
|
|
31
|
+
[BREAKING] AS part of express 5 migration _ in rotes (middlewares) should have perameter. please replace _ to \*splat
|
|
32
|
+
[BREAKING] default auth responce changed to be unified. {token, user} = > {data:{token, user}}
|
|
33
|
+
[BREAKING] RateLimiter now need to have IpDetector middleware before
|
|
34
|
+
[BREAKING] removing staticFiles middleware as it not used in projects anymore. Docs with nginx config will be provided
|
|
35
|
+
[BREAKING] remove default AUTH_SALT. It should be provided on a app level now
|
|
36
|
+
[BREAKING] Minimum node version is 20.12 as for now (process.loadEnvFile)
|
|
37
|
+
[BREAKING] ESM only. No more commonJS. That help to fix a lot of bugs with tests and provides better development expirience
|
|
38
|
+
[BREAKING] Mongoose v8. https://mongoosejs.com/docs/migrating_to_8.html
|
|
39
|
+
|
|
40
|
+
### 5.0.0-beta.19
|
|
41
|
+
|
|
42
|
+
[NEW] intruducing new model type. BaseModel. Features - simplifie works with typescript. And based on statics
|
|
43
|
+
[BREAKING] all models now should be extended from BaseModel. This is a potencial breaking change specially for User model.
|
|
44
|
+
|
|
45
|
+
### 5.0.0-beta.18
|
|
46
|
+
|
|
47
|
+
[UPDATE] move away connection from mongooseModels to server itself (preparation for different model types)
|
|
48
|
+
[BREAKING] potencial. We are removed callback from mongooseModels contrctuctor. It was not used in code.
|
|
49
|
+
[NEW] add appInstance helper to access app instance from anywhere without passing it
|
|
50
|
+
|
|
51
|
+
### 5.0.0-beta.17
|
|
52
|
+
|
|
53
|
+
[NEW] add GenerateTypes command
|
|
54
|
+
|
|
1
55
|
### 5.0.0-beta.16
|
|
2
56
|
|
|
3
57
|
[UPDATE] update deps
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import AbstractCommand from '../modules/AbstractCommand.ts';
|
|
2
2
|
declare class GenerateTypes extends AbstractCommand {
|
|
3
3
|
static get description(): string;
|
|
4
|
+
getTypesContent(): Promise<string>;
|
|
4
5
|
static isShouldInitModels: boolean;
|
|
5
6
|
run(): Promise<boolean>;
|
|
7
|
+
static getTemplate(configs: Map<string, any>, modelPaths: {
|
|
8
|
+
file: string;
|
|
9
|
+
path: string;
|
|
10
|
+
}[]): Promise<string>;
|
|
6
11
|
}
|
|
7
12
|
export default GenerateTypes;
|
|
@@ -1,13 +1,60 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
1
9
|
import AbstractCommand from "../modules/AbstractCommand.js";
|
|
10
|
+
import { BaseModel } from "../modules/BaseModel.js";
|
|
11
|
+
import * as url from 'url';
|
|
12
|
+
import fs from 'node:fs/promises';
|
|
2
13
|
class GenerateTypes extends AbstractCommand {
|
|
3
14
|
static get description() {
|
|
4
15
|
return 'Gererates TypeScript types';
|
|
5
16
|
}
|
|
17
|
+
async getTypesContent() {
|
|
18
|
+
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
|
|
19
|
+
const content = await fs.readFile(`${__dirname}/../../../src/types/types.ts`, 'utf8');
|
|
20
|
+
return content;
|
|
21
|
+
}
|
|
6
22
|
static isShouldInitModels = false;
|
|
7
23
|
async run() {
|
|
8
|
-
|
|
24
|
+
const template = await GenerateTypes.getTemplate(this.app.internalFilesCache.configs, this.app.internalFilesCache.modelPaths);
|
|
25
|
+
await fs.writeFile(`${process.cwd()}/genTypes.d.ts`, template);
|
|
26
|
+
console.log('TypeScript types generated successfully at genTypes.d.ts');
|
|
9
27
|
return Promise.resolve(true);
|
|
10
28
|
}
|
|
29
|
+
static async getTemplate(configs, modelPaths) {
|
|
30
|
+
const dir = process.cwd();
|
|
31
|
+
const configTypes = Array.from(configs)
|
|
32
|
+
.map((config) => {
|
|
33
|
+
return ` getConfig(configName: '${config[0]}'): ${JSON.stringify(config[1], null, 6)};`;
|
|
34
|
+
})
|
|
35
|
+
.join('\n');
|
|
36
|
+
const modelTypes = (await Promise.all(modelPaths.map(async (modelPath) => {
|
|
37
|
+
const modelModule = await import(__rewriteRelativeImportExtension(modelPath.path));
|
|
38
|
+
const path = modelPath.path.replace(dir, '.');
|
|
39
|
+
if (modelModule.default.prototype instanceof BaseModel) {
|
|
40
|
+
return ` getModel(modelName: '${modelPath.file}'): GetModelTypeFromClass<typeof import('${path}').default>`;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
return ` getModel(modelName: '${modelPath.file}'): import('${path}').default['mongooseModel']`;
|
|
44
|
+
}
|
|
45
|
+
}))).join('\n');
|
|
46
|
+
return `
|
|
47
|
+
import type {} from '@adaptivestone/framework/server.js';
|
|
48
|
+
import type { GetModelTypeFromClass } from './src/modules/BaseModel.ts';
|
|
49
|
+
|
|
50
|
+
declare module '@adaptivestone/framework/server.js' {
|
|
51
|
+
export interface IApp {
|
|
52
|
+
${configTypes}
|
|
53
|
+
${modelTypes}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
`;
|
|
57
|
+
}
|
|
11
58
|
}
|
|
12
59
|
export default GenerateTypes;
|
|
13
60
|
//# sourceMappingURL=GenerateTypes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenerateTypes.js","sourceRoot":"","sources":["../../src/commands/GenerateTypes.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"GenerateTypes.js","sourceRoot":"","sources":["../../src/commands/GenerateTypes.ts"],"names":[],"mappings":";;;;;;;;AAAA,OAAO,eAAe,MAAM,+BAA+B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,OAAO,KAA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C,CAAC,GAAG,CAAC;QAC7F,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,CAAC;QAEd,MAAM,UAAU,GAAG,CACjB,MAAM,OAAO,CAAC,GAAG,CACf,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE;YACjC,MAAM,WAAW,GAAG,MAAM,MAAM,kCAAC,SAAS,CAAC,IAAI,EAAC,CAAC;YACjD,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;YAC9C,IAAI,WAAW,CAAC,OAAO,CAAC,SAAS,YAAY,SAAS,EAAE,CAAC;gBACvD,OAAO,4BAA4B,SAAS,CAAC,IAAI,4CAA4C,IAAI,aAAa,CAAC;YACjH,CAAC;iBAAM,CAAC;gBACN,OAAO,4BAA4B,SAAS,CAAC,IAAI,eAAe,IAAI,6BAA6B,CAAC;YACpG,CAAC;QACH,CAAC,CAAC,CACH,CACF,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEb,OAAO;;;;;;QAMH,WAAW;QACX,UAAU;;;CAGjB,CAAC;IACA,CAAC;;AAGH,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appInstance.js","sourceRoot":"","sources":["../../src/helpers/appInstance.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,IAAI,WAAiB,CAAC;AAE7B,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,GAAS,EAAE,EAAE;IAC1C,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IACD,WAAW,GAAG,GAAG,CAAC;AACpB,CAAC,CAAC"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const scryptAsync: (arg1: string | Buffer<ArrayBufferLike> | NodeJS.TypedArray<ArrayBufferLike> | DataView<ArrayBufferLike>, arg2: string | Buffer<ArrayBufferLike> | NodeJS.TypedArray<ArrayBufferLike> | DataView<ArrayBufferLike>, arg3: number) => Promise<Buffer<ArrayBufferLike>>;
|
|
2
|
+
export declare const scryptAsyncWithSalt: (stringToHash: string) => Promise<Buffer<ArrayBufferLike>>;
|
|
3
|
+
export declare const scryptAsyncWithSaltAsString: (stringToHash: string) => Promise<string>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { scrypt } from 'node:crypto';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
import { appInstance } from './appInstance.js';
|
|
4
|
+
export const scryptAsync = promisify(scrypt);
|
|
5
|
+
export const scryptAsyncWithSalt = async (stringToHash) => {
|
|
6
|
+
const { saltSecret, hashRounds } = appInstance.getConfig('auth');
|
|
7
|
+
const res = await scryptAsync(stringToHash, saltSecret, hashRounds);
|
|
8
|
+
return res;
|
|
9
|
+
};
|
|
10
|
+
export const scryptAsyncWithSaltAsString = async (stringToHash) => {
|
|
11
|
+
const res = await scryptAsyncWithSalt(stringToHash);
|
|
12
|
+
return res.toString('base64url');
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/helpers/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,CAAC,MAAM,WAAW,GAAG,SAAS,CAKlC,MAAM,CAAC,CAAC;AAEV,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,YAAoB,EAAE,EAAE;IAChE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;IACjE,MAAM,GAAG,GAAG,MAAM,WAAW,CAAC,YAAY,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;IAEpE,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,2BAA2B,GAAG,KAAK,EAAE,YAAoB,EAAE,EAAE;IACxE,MAAM,GAAG,GAAG,MAAM,mBAAmB,CAAC,YAAY,CAAC,CAAC;IACpD,OAAO,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;AACnC,CAAC,CAAC"}
|
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
import type { RedisClientType } from '@redis/client';
|
|
2
|
-
declare
|
|
3
|
-
export default clearNamespace;
|
|
2
|
+
export declare const clearNamespace: (redisClient: RedisClientType, namespace: string) => Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export const clearNamespace = async (redisClient, namespace) => {
|
|
2
2
|
const deletedKeys = [];
|
|
3
3
|
const keys = await redisClient.sendCommand([
|
|
4
4
|
'keys',
|
|
@@ -10,6 +10,5 @@ async function clearNamespace(redisClient, namespace) {
|
|
|
10
10
|
}
|
|
11
11
|
await Promise.all(deletedKeys);
|
|
12
12
|
}
|
|
13
|
-
}
|
|
14
|
-
export default clearNamespace;
|
|
13
|
+
};
|
|
15
14
|
//# sourceMappingURL=clearNamespace.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clearNamespace.js","sourceRoot":"","sources":["../../../src/helpers/redis/clearNamespace.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"clearNamespace.js","sourceRoot":"","sources":["../../../src/helpers/redis/clearNamespace.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EACjC,WAA4B,EAC5B,SAAiB,EACjB,EAAE;IACF,MAAM,WAAW,GAAG,EAAE,CAAC;IAEvB,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,WAAW,CAAW;QACnD,MAAM;QACN,IAAI,SAAS,GAAG;KACjB,CAAC,CAAC;IAEH,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5B,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,WAAW,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QAC1D,CAAC;QACD,MAAM,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACjC,CAAC;AACH,CAAC,CAAC"}
|
package/dist/models/Lock.d.ts
CHANGED
|
@@ -1,62 +1,89 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
releaseLock(name: string): Promise<boolean>;
|
|
10
|
-
waitForUnlock(name: string): Promise<void>;
|
|
11
|
-
getLockData(name: string): Promise<{
|
|
12
|
-
ttl: number;
|
|
13
|
-
}>;
|
|
14
|
-
getLocksData(names: string[]): Promise<{
|
|
15
|
-
name: string;
|
|
16
|
-
ttl: number;
|
|
17
|
-
}[]>;
|
|
18
|
-
}
|
|
19
|
-
declare class Lock extends AbstractModel<ILock, IAbstractModelMethods<ILock>, IStatic> {
|
|
20
|
-
initHooks(): void;
|
|
21
|
-
get modelSchema(): {
|
|
22
|
-
_id: {
|
|
23
|
-
type: StringConstructor;
|
|
24
|
-
required: boolean;
|
|
1
|
+
import { BaseModel } from '../modules/BaseModel.ts';
|
|
2
|
+
import type { GetModelTypeLiteFromSchema } from '../modules/BaseModel.ts';
|
|
3
|
+
import type { Schema } from 'mongoose';
|
|
4
|
+
declare class Lock extends BaseModel {
|
|
5
|
+
static get modelSchema(): {
|
|
6
|
+
readonly _id: {
|
|
7
|
+
readonly type: StringConstructor;
|
|
8
|
+
readonly required: true;
|
|
25
9
|
};
|
|
26
|
-
expiredAt: {
|
|
27
|
-
type: DateConstructor;
|
|
10
|
+
readonly expiredAt: {
|
|
11
|
+
readonly type: DateConstructor;
|
|
28
12
|
};
|
|
29
13
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
14
|
+
static get modelStatics(): {
|
|
15
|
+
/**
|
|
16
|
+
* acquire lock based on lock name
|
|
17
|
+
* @param {string} name
|
|
18
|
+
* @param {number} [ttlSeconds=30]
|
|
19
|
+
*/
|
|
20
|
+
readonly acquireLock: (this: GetModelTypeLiteFromSchema<{
|
|
21
|
+
readonly _id: {
|
|
22
|
+
readonly type: StringConstructor;
|
|
23
|
+
readonly required: true;
|
|
24
|
+
};
|
|
25
|
+
readonly expiredAt: {
|
|
26
|
+
readonly type: DateConstructor;
|
|
27
|
+
};
|
|
28
|
+
}, {}>, name: string, ttlSeconds?: number) => Promise<boolean>;
|
|
29
|
+
/**
|
|
30
|
+
* release lock based on lock name
|
|
31
|
+
* @param {string} name
|
|
32
|
+
*/
|
|
33
|
+
readonly releaseLock: (this: GetModelTypeLiteFromSchema<{
|
|
34
|
+
readonly _id: {
|
|
35
|
+
readonly type: StringConstructor;
|
|
36
|
+
readonly required: true;
|
|
37
|
+
};
|
|
38
|
+
readonly expiredAt: {
|
|
39
|
+
readonly type: DateConstructor;
|
|
40
|
+
};
|
|
41
|
+
}, {}>, name: string) => Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* wait lock based on lock name
|
|
44
|
+
* @param {string} name
|
|
45
|
+
*/
|
|
46
|
+
readonly waitForUnlock: (this: GetModelTypeLiteFromSchema<{
|
|
47
|
+
readonly _id: {
|
|
48
|
+
readonly type: StringConstructor;
|
|
49
|
+
readonly required: true;
|
|
50
|
+
};
|
|
51
|
+
readonly expiredAt: {
|
|
52
|
+
readonly type: DateConstructor;
|
|
53
|
+
};
|
|
54
|
+
}, {}>, name: string) => Promise<unknown>;
|
|
55
|
+
/**
|
|
56
|
+
* get lock remaining time based on lock name
|
|
57
|
+
* @param {string} name
|
|
58
|
+
*/
|
|
59
|
+
readonly getLockData: (this: GetModelTypeLiteFromSchema<{
|
|
60
|
+
readonly _id: {
|
|
61
|
+
readonly type: StringConstructor;
|
|
62
|
+
readonly required: true;
|
|
63
|
+
};
|
|
64
|
+
readonly expiredAt: {
|
|
65
|
+
readonly type: DateConstructor;
|
|
66
|
+
};
|
|
67
|
+
}, {}>, name: string) => Promise<{
|
|
68
|
+
ttl: number;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* get lock remaining time based on lock name
|
|
72
|
+
* @param {string[]} names
|
|
73
|
+
*/
|
|
74
|
+
readonly getLocksData: (this: GetModelTypeLiteFromSchema<{
|
|
75
|
+
readonly _id: {
|
|
76
|
+
readonly type: StringConstructor;
|
|
77
|
+
readonly required: true;
|
|
78
|
+
};
|
|
79
|
+
readonly expiredAt: {
|
|
80
|
+
readonly type: DateConstructor;
|
|
81
|
+
};
|
|
82
|
+
}, {}>, names: string[]) => Promise<{
|
|
83
|
+
name: string;
|
|
84
|
+
ttl: number;
|
|
85
|
+
}[]>;
|
|
86
|
+
};
|
|
87
|
+
initHooks(schema: Schema): void;
|
|
61
88
|
}
|
|
62
89
|
export default Lock;
|
package/dist/models/Lock.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
class Lock extends
|
|
3
|
-
|
|
4
|
-
this.mongooseSchema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 });
|
|
5
|
-
}
|
|
6
|
-
// eslint-disable-next-line class-methods-use-this
|
|
7
|
-
get modelSchema() {
|
|
1
|
+
import { BaseModel } from "../modules/BaseModel.js";
|
|
2
|
+
class Lock extends BaseModel {
|
|
3
|
+
static get modelSchema() {
|
|
8
4
|
return {
|
|
9
5
|
_id: { type: String, required: true },
|
|
10
6
|
expiredAt: {
|
|
@@ -12,82 +8,91 @@ class Lock extends AbstractModel {
|
|
|
12
8
|
},
|
|
13
9
|
};
|
|
14
10
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
11
|
+
static get modelStatics() {
|
|
12
|
+
return {
|
|
13
|
+
/**
|
|
14
|
+
* acquire lock based on lock name
|
|
15
|
+
* @param {string} name
|
|
16
|
+
* @param {number} [ttlSeconds=30]
|
|
17
|
+
*/
|
|
18
|
+
acquireLock: async function (name, ttlSeconds = 30) {
|
|
19
|
+
try {
|
|
20
|
+
await this.create({
|
|
21
|
+
_id: name,
|
|
22
|
+
expiredAt: new Date(Date.now() + ttlSeconds * 1000),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error.code !== 11000) {
|
|
27
|
+
// not a duplicate keys
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
},
|
|
34
|
+
/**
|
|
35
|
+
* release lock based on lock name
|
|
36
|
+
* @param {string} name
|
|
37
|
+
*/
|
|
38
|
+
releaseLock: async function (name) {
|
|
39
|
+
const res = await this.deleteOne({ _id: name });
|
|
40
|
+
if (res.acknowledged && res.deletedCount) {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* wait lock based on lock name
|
|
47
|
+
* @param {string} name
|
|
48
|
+
*/
|
|
49
|
+
waitForUnlock: async function (name) {
|
|
50
|
+
const res = await this.findOne({ _id: name });
|
|
51
|
+
if (!res) {
|
|
52
|
+
return Promise.resolve();
|
|
53
|
+
}
|
|
54
|
+
return new Promise((resolve) => {
|
|
55
|
+
const stream = this.watch([
|
|
56
|
+
{ $match: { operationType: 'delete', 'documentKey._id': name } },
|
|
57
|
+
]);
|
|
58
|
+
stream.on('change', () => {
|
|
59
|
+
stream.close();
|
|
60
|
+
resolve(true);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
/**
|
|
65
|
+
* get lock remaining time based on lock name
|
|
66
|
+
* @param {string} name
|
|
67
|
+
*/
|
|
68
|
+
getLockData: async function (name) {
|
|
69
|
+
const res = await this.findOne({ _id: name });
|
|
70
|
+
if (!res || !res.expiredAt) {
|
|
71
|
+
return { ttl: 0 };
|
|
72
|
+
}
|
|
73
|
+
return { ttl: res.expiredAt.getTime() - Date.now() };
|
|
74
|
+
},
|
|
75
|
+
/**
|
|
76
|
+
* get lock remaining time based on lock name
|
|
77
|
+
* @param {string[]} names
|
|
78
|
+
*/
|
|
79
|
+
getLocksData: async function (names) {
|
|
80
|
+
const res = await this.find({ _id: { $in: names } });
|
|
81
|
+
const lockMap = new Map(res.map((lock) => [lock._id, lock]));
|
|
82
|
+
return names.map((name) => {
|
|
83
|
+
const lock = lockMap.get(name);
|
|
84
|
+
return {
|
|
85
|
+
name,
|
|
86
|
+
ttl: lock && lock.expiredAt
|
|
87
|
+
? lock.expiredAt.getTime() - Date.now()
|
|
88
|
+
: 0,
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
76
93
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
* @param {string[]} names
|
|
80
|
-
*/
|
|
81
|
-
static async getLocksData(names) {
|
|
82
|
-
const res = await this.find({ _id: { $in: names } });
|
|
83
|
-
const lockMap = new Map(res.map((lock) => [lock._id, lock]));
|
|
84
|
-
return names.map((name) => {
|
|
85
|
-
const lock = lockMap.get(name);
|
|
86
|
-
return {
|
|
87
|
-
name,
|
|
88
|
-
ttl: lock ? lock.expiredAt.getTime() - Date.now() : 0,
|
|
89
|
-
};
|
|
90
|
-
});
|
|
94
|
+
initHooks(schema) {
|
|
95
|
+
schema.index({ expiredAt: 1 }, { expireAfterSeconds: 0 });
|
|
91
96
|
}
|
|
92
97
|
}
|
|
93
98
|
export default Lock;
|
package/dist/models/Lock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Lock.js","sourceRoot":"","sources":["../../src/models/Lock.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Lock.js","sourceRoot":"","sources":["../../src/models/Lock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AASpD,MAAM,IAAK,SAAQ,SAAS;IAC1B,MAAM,KAAK,WAAW;QACpB,OAAO;YACL,GAAG,EAAE,EAAE,IAAI,EAA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EACD,IAAI,IAAI,IAAI,CAAC,SAAS;4BACpB,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,GAAG,EAAE;4BACvC,CAAC,CAAC,CAAC;qBACR,CAAC;gBACJ,CAAC,CAAC,CAAC;YACL,CAAC;SACO,CAAC;IACb,CAAC;IAED,SAAS,CAAC,MAAc;QACtB,MAAM,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE,kBAAkB,EAAE,CAAC,EAAE,CAAC,CAAC;IAC5D,CAAC;CACF;AAED,eAAe,IAAI,CAAC"}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
migrationFile: {
|
|
8
|
-
type: StringConstructor;
|
|
9
|
-
unique: boolean;
|
|
1
|
+
import { BaseModel } from '../modules/BaseModel.ts';
|
|
2
|
+
declare class Migration extends BaseModel {
|
|
3
|
+
static get modelSchema(): {
|
|
4
|
+
readonly migrationFile: {
|
|
5
|
+
readonly type: StringConstructor;
|
|
6
|
+
readonly unique: true;
|
|
10
7
|
};
|
|
11
8
|
};
|
|
12
9
|
}
|
package/dist/models/Migration.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
class Migration extends
|
|
3
|
-
|
|
4
|
-
get modelSchema() {
|
|
1
|
+
import { BaseModel } from "../modules/BaseModel.js";
|
|
2
|
+
class Migration extends BaseModel {
|
|
3
|
+
static get modelSchema() {
|
|
5
4
|
return {
|
|
6
5
|
migrationFile: {
|
|
7
6
|
type: String,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Migration.js","sourceRoot":"","sources":["../../src/models/Migration.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Migration.js","sourceRoot":"","sources":["../../src/models/Migration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD,MAAM,SAAU,SAAQ,SAAS;IAC/B,MAAM,KAAK,WAAW;QACpB,OAAO;YACL,aAAa,EAAE;gBACb,IAAI,EAAE,MAAM;gBACZ,MAAM,EAAE,IAAI;aACb;SACO,CAAC;IACb,CAAC;CACF;AAED,eAAe,SAAS,CAAC"}
|