@alipay/faas-biz-server-sdk
Advanced tools
Comparing version 1.1.4 to 1.1.5-alpha.0
import WSBaseStorage from '../storage/BaseStorage'; | ||
import { Where } from '../types/Websocket'; | ||
interface IModel { | ||
@@ -46,8 +47,5 @@ toString(): string; | ||
static remove(id: string, namespace?: string): Promise<void>; | ||
static find<T extends typeof BaseModel>(this: T, where: { | ||
namespace?: string; | ||
id?: string; | ||
}): Promise<InstanceType<T>[]>; | ||
static find<T extends typeof BaseModel>(this: T, where: Where): Promise<InstanceType<T>[]>; | ||
static exists(id: string, namespace?: string): Promise<boolean>; | ||
} | ||
export {}; |
@@ -5,2 +5,11 @@ import WsSet from '../base/WsSet'; | ||
export default class Room extends Base { | ||
/** | ||
* | ||
* @template { | ||
* id: string; | ||
* properties: Record<string, any>; | ||
* sockets: string[]; | ||
* namespace: string; | ||
* } | ||
*/ | ||
static table: string; | ||
@@ -7,0 +16,0 @@ /** |
@@ -13,2 +13,11 @@ "use strict"; | ||
class Room extends Base_1.default { | ||
/** | ||
* | ||
* @template { | ||
* id: string; | ||
* properties: Record<string, any>; | ||
* sockets: string[]; | ||
* namespace: string; | ||
* } | ||
*/ | ||
static table = 'room'; | ||
@@ -59,5 +68,6 @@ /** | ||
// const sockets = await Promise.all(this.socketIds.map(id => Socket.get(id))); | ||
const sockets = await (0, Utils_1.pAll)(this.socketIds.map(socket => async () => { | ||
return await Socket_1.default.get(socket); | ||
}), Constants_1.DEFAULT_MAX_CONCURRENT); | ||
const sockets = await Socket_1.default.find({ | ||
id: this.socketIds, | ||
namespace: this.namespace, | ||
}); | ||
return new WsSet_1.default(sockets, Socket_1.default); | ||
@@ -64,0 +74,0 @@ } |
@@ -5,2 +5,11 @@ import Base from './Base'; | ||
export default class Socket extends Base { | ||
/** | ||
* | ||
* @template { | ||
* id: string; | ||
* properties: Record<string, any>; | ||
* rooms: string[]; | ||
* namespace: string; | ||
* } | ||
*/ | ||
roomIds: string[]; | ||
@@ -7,0 +16,0 @@ static table: string; |
@@ -13,2 +13,11 @@ "use strict"; | ||
class Socket extends Base_1.default { | ||
/** | ||
* | ||
* @template { | ||
* id: string; | ||
* properties: Record<string, any>; | ||
* rooms: string[]; | ||
* namespace: string; | ||
* } | ||
*/ | ||
roomIds = []; | ||
@@ -67,3 +76,6 @@ static table = 'socket'; | ||
} | ||
const rooms = await Promise.all(this.roomIds.map(id => Room_1.default.get(id))); | ||
const rooms = await Room_1.default.find({ | ||
id: this.roomIds, | ||
namespace: this.namespace, | ||
}); | ||
return new WsSet_1.default(rooms, Room_1.default); | ||
@@ -70,0 +82,0 @@ } |
@@ -72,2 +72,15 @@ "use strict"; | ||
const collection = await this.getCollection(table); | ||
const myWhere = { | ||
namespace: where.namespace || Constants_1.DEFAULT_NAMESPACE, | ||
}; | ||
if (where.id) { | ||
if (Array.isArray(where.id)) { | ||
myWhere.id = { | ||
$in: where.id, | ||
}; | ||
} | ||
else { | ||
myWhere.id = where.id; | ||
} | ||
} | ||
return await collection.where(where).get(); | ||
@@ -74,0 +87,0 @@ } |
@@ -12,4 +12,4 @@ import { RedisOptions } from '@alipay/faas-server-sdk/lib/Redis'; | ||
findById(table: string, id: string, namespace?: string): Promise<any>; | ||
find(table: string, where: Where): Promise<any[]>; | ||
find(table: string, where: Where): Promise<unknown[]>; | ||
private scanKeys; | ||
} |
@@ -5,3 +5,3 @@ "use strict"; | ||
const Constants_1 = require("../base/Constants"); | ||
const Utils_1 = require("../base/Utils"); | ||
// import { pAll, splitArray } from '../base/Utils'; | ||
class RedisStorage { | ||
@@ -29,3 +29,16 @@ client; | ||
// })); | ||
const updatePromises = keys.map(key => async () => { | ||
// const updatePromises = keys.map(key => async () => { | ||
// for (const k in data) { | ||
// if (typeof data[k] === 'object') { | ||
// data[k] = JSON.stringify(data[k]); | ||
// } | ||
// } | ||
// await this.client.hset(key, { | ||
// ...data, | ||
// updatedAt: new Date().getTime(), | ||
// }); | ||
// }); | ||
// await pAll(updatePromises, DEFAULT_MAX_CONCURRENT); | ||
const pipeline = this.client.pipeline(); | ||
keys.forEach(key => { | ||
for (const k in data) { | ||
@@ -36,3 +49,3 @@ if (typeof data[k] === 'object') { | ||
} | ||
await this.client.hset(key, { | ||
pipeline.hset(key, { | ||
...data, | ||
@@ -42,12 +55,30 @@ updatedAt: new Date().getTime(), | ||
}); | ||
await (0, Utils_1.pAll)(updatePromises, Constants_1.DEFAULT_MAX_CONCURRENT); | ||
await pipeline.exec(); | ||
} | ||
async remove(table, where) { | ||
const keys = await this.scanKeys(`${table}:${where.namespace || Constants_1.DEFAULT_NAMESPACE}/${where.id || ''}*`); | ||
let pattern = `${table}:${where.namespace || Constants_1.DEFAULT_NAMESPACE}/`; | ||
let keys = []; | ||
if (where.id) { | ||
if (Array.isArray(where.id) && where.id.length > 0) { | ||
keys = where.id.map((id) => `${pattern}${id}`); | ||
} | ||
else { | ||
keys.push(`${pattern}${where.id}`); | ||
} | ||
} | ||
else { | ||
pattern += '*'; | ||
keys = await this.scanKeys(pattern); | ||
} | ||
if (!keys?.length) { | ||
return; | ||
} | ||
await (0, Utils_1.pAll)((0, Utils_1.splitArray)(keys, 100).map(ks => async () => { | ||
await this.client.del(...ks); | ||
}), Constants_1.DEFAULT_MAX_CONCURRENT); | ||
const pipeline = this.client.pipeline(); | ||
keys.forEach(key => { | ||
pipeline.del(key); | ||
}); | ||
await pipeline.exec(); | ||
// await pAll(splitArray(keys, 100).map(ks => async () => { | ||
// await this.client.del(...ks); | ||
// }), DEFAULT_MAX_CONCURRENT); | ||
// const deletePromises = keys.map(async key => { | ||
@@ -79,14 +110,25 @@ // await this.client.del(key); | ||
async find(table, where) { | ||
const pattern = `${table}:${where.namespace || Constants_1.DEFAULT_NAMESPACE}/${where.id || ''}*`; | ||
const keys = await this.scanKeys(pattern); | ||
let pattern = `${table}:${where.namespace || Constants_1.DEFAULT_NAMESPACE}/`; | ||
let keys = []; | ||
if (where.id) { | ||
if (Array.isArray(where.id) && where.id.length > 0) { | ||
keys = where.id.map((id) => `${pattern}${id}`); | ||
} | ||
else { | ||
keys.push(`${pattern}${where.id}`); | ||
} | ||
} | ||
else { | ||
pattern += '*'; | ||
keys = await this.scanKeys(pattern); | ||
} | ||
if (!keys?.length) { | ||
return []; | ||
} | ||
// return await Promise.all(keys.map(async key => { | ||
// return await this.client.hgetall(key); | ||
// })); | ||
const res = await (0, Utils_1.pAll)(keys.map(key => async () => { | ||
return await this.client.hgetall(key); | ||
}), Constants_1.DEFAULT_MAX_CONCURRENT); | ||
return res; | ||
const pipeline = this.client.pipeline(); | ||
keys.forEach(key => { | ||
pipeline.hgetall(key); | ||
}); | ||
const results = await pipeline.exec(); | ||
return results.map(result => result[1]); | ||
} | ||
@@ -93,0 +135,0 @@ async scanKeys(pattern) { |
@@ -9,3 +9,3 @@ import { RedisOptions } from '@alipay/faas-server-sdk/lib/Redis'; | ||
export interface Where { | ||
id?: string; | ||
id?: string | string[]; | ||
namespace?: string; | ||
@@ -12,0 +12,0 @@ } |
{ | ||
"name": "@alipay/faas-biz-server-sdk", | ||
"version": "1.1.4", | ||
"version": "1.1.5-alpha.0", | ||
"description": "支付宝云开发业务 SDK(函数端)", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
319797
9763
2