Skip to content

Commit

Permalink
Fixes #427
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Feb 24, 2020
1 parent 2d9df2b commit b814efb
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 173 deletions.
76 changes: 75 additions & 1 deletion lib/client/database/class/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const Class = require("../../../db/class");
const DatabaseProperty = require("./property");
const Promise = require("bluebird");
const errors = require('../../../errors');
const errors = require("../../../errors");

/**
*
Expand Down Expand Up @@ -39,6 +39,80 @@ class DatabaseClass extends Class {
}
}

/**
* Find a list of records in the class.
*
* @param {Object} attributes The attributes to search with.
* @param {Integer} limit The maximum number of records to return
* @param {Integer} offset The offset to start returning records from.
* @promise {Object[]} An array of records in the class.
*/
find(attributes, limit, offset) {
var query = "SELECT * FROM " + this.name,
keys = Object.keys(attributes),
total = keys.length,
conditions = [],
params = {},
key,
sanitizedKey,
value,
i;

for (i = 0; i < total; i++) {
key = keys[i];
value = attributes[key];
sanitizedKey = key.replace(/\./g, "_");
params[sanitizedKey] = value;
conditions.push(key + " = :" + sanitizedKey);
}

if (conditions.length) {
query += " WHERE " + conditions.join(" AND ");
}

limit = +limit || 20;
offset = +offset || 0;

if (limit !== Infinity) {
query += " LIMIT " + limit + " OFFSET " + offset;
} else {
query += " OFFSET " + offset;
}

return this.db
.query(query, {
params: params
})
.all();
}

/**
* Return a list of records in the class.
*
* @param {Integer|Object} limit The maximum number of records to return, or a configuration object.
* @param {Integer} offset The offset to start returning records from.
* @promise {Object[]} An array of records in the class.
*/
list(limit, offset) {
var query = "SELECT * FROM " + this.name,
config = {};

if (limit && typeof limit === "object") {
config = limit;
limit = config.limit;
offset = config.offset;
}
limit = +limit || 20;
offset = +offset || 0;

if (limit !== Infinity) {
query += " LIMIT " + limit + " OFFSET " + offset;
} else {
query += " OFFSET " + offset;
}

return this.db.query(query, config).all();
}
/**
* Reload the class instance.
*
Expand Down
6 changes: 4 additions & 2 deletions lib/client/database/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ class ODatabaseSession extends DB {
}

exec(query, options) {
options = options || {};
options.idempotent = false;
options.operationType = OperationType.COMMAND;
let result = this.execStream(query, options);
let promise = result.toPromise(Promise);
return promise;
return result.all();
}

execStream(query, options) {
Expand Down
Loading

0 comments on commit b814efb

Please sign in to comment.