@colyseus/redis-driver
Version:
<div align="center"> <a href="https://github.com/colyseus/colyseus"> <img src="media/logo.svg?raw=true" width="60%" height="300" /> </a> <br> <br> <a href="https://npmjs.com/package/colyseus"> <img src="https://img.shields.io/npm/dm/coly
102 lines (100 loc) • 3.12 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/drivers/redis-driver/src/Query.ts
var Query_exports = {};
__export(Query_exports, {
Query: () => Query
});
module.exports = __toCommonJS(Query_exports);
var Query = class {
constructor(rooms, conditions) {
this.order = /* @__PURE__ */ new Map();
this.conditions = conditions;
this.rooms = rooms;
}
sort(options) {
this.order.clear();
const fields = Object.entries(options);
if (fields.length) {
for (const [field, direction] of fields) {
if (direction === 1 || direction === "asc" || direction === "ascending") {
this.order.set(field, 1);
} else {
this.order.set(field, -1);
}
}
}
return this;
}
filterRooms(rooms) {
const conditions = Object.entries(this.conditions);
const withConditions = conditions.length > 0;
return rooms.filter((room) => {
if (withConditions) {
for (let [field, value] of conditions) {
if (room.hasOwnProperty(field)) {
if (room[field] !== value) {
return false;
}
} else if (room.metadata?.hasOwnProperty(field)) {
if (room.metadata[field] !== value) {
return false;
}
} else {
return false;
}
}
}
return true;
});
}
sortRooms(rooms) {
if (this.order.size) {
rooms.sort((room1, room2) => {
for (const [field, direction] of this.order) {
const val1 = room1.hasOwnProperty(field) ? room1[field] : room1.metadata?.[field];
const val2 = room2.hasOwnProperty(field) ? room2[field] : room2.metadata?.[field];
if (direction === 1) {
if (val1 > val2) return 1;
if (val1 < val2) return -1;
} else {
if (val1 > val2) return -1;
if (val1 < val2) return 1;
}
}
return 0;
});
}
return rooms;
}
all() {
return this.rooms.then((rooms) => {
return this.sortRooms(this.filterRooms(rooms));
});
}
then(resolve, reject) {
return this.rooms.then((rooms) => {
return this.sortRooms(this.filterRooms(rooms))[0];
}).then(resolve, reject);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Query
});