Commit 8d846b7d authored by William Naslund's avatar William Naslund

Added basic select functionality

parent 3fb26280
......@@ -26,5 +26,4 @@ export interface DBQueryResponse {
/** A single record from the database */
export interface DBQueryRow {
[name: string]: any;
_model: DBModelConstructor<any>;
}
......@@ -2,7 +2,8 @@ import "reflect-metadata";
import * as os from "os";
import { MODEL_DATABASE } from "./internal/model-registry";
import { Database } from "./database";
import { getTableInformation, getTableFields } from "./internal/meta-keys";
import { getTableInformation, getTableFields, TABLE_PARENT_LOOKUPS, getFieldInformation } from "./internal/meta-keys";
import { DBLookupInformation } from "./decorators";
/** A record that is backed by a table in the database */
export class DBModel {
......@@ -16,8 +17,24 @@ export class DBModel {
throw new Error(`This model (${this.constructor}) has not been registered with a database`);
}
const saveData: any = { };
for(const field in this._contents) {
saveData[field] = this._contents[field];
}
for(const lookup of Reflect.getMetadata(TABLE_PARENT_LOOKUPS, this.constructor) as DBLookupInformation[] || []) {
const lookupValue = this[lookup.propertyKey];
if(lookupValue === undefined) continue;
if(lookupValue != null) {
const lookupTableInfo = getTableInformation(lookup.model);
saveData[lookup.name] = lookupValue[lookupTableInfo.options.id];
} else {
saveData[lookup.name] = null;
}
}
const tableInfo = getTableInformation(this.constructor);
const newId = await db.adapter.model.insert(this.constructor, this._contents);
const newId = await db.adapter.model.insert(this.constructor, saveData);
if(tableInfo.options.id) {
this[tableInfo.options.id] = newId;
}
......
import "reflect-metadata";
import { DBModel, DBModelConstructor } from './model';
import { Database } from './database';
import { DBQueryRequest } from './adapter';
import { getTableFields, TABLE_PARENT_LOOKUPS } from './internal/meta-keys';
import { DBLookupInformation } from './decorators';
export class DBQuery<T extends DBModel> {
......@@ -28,13 +31,34 @@ export class DBQuery<T extends DBModel> {
/** Returns all the rows from the query */
async getAll(): Promise<T[]> {
if(this.req.fields == null) {
this.req.fields.push('*');
this.req.fields = [ '*' ];
}
const lookupFields = new Set<string>();
for(const lookup of Reflect.getMetadata(TABLE_PARENT_LOOKUPS, this.req.model) as DBLookupInformation[] || []) {
lookupFields.add(lookup.name);
}
const parsedModels: T[] = [];
for(const res of await this.db.adapter.query.run(this.req)) {
const queried = new this.req.model();
for(const field of getTableFields(this.req.model)) {
if(lookupFields.has(field.name)) {
continue;
}
const fieldValue = res.baseRecord[field.name];
if(fieldValue !== undefined) {
queried[field.propertyKey] = fieldValue;
}
}
parsedModels.push(queried);
}
const res = await this.db.adapter.query.run(this.req);
console.log(res);
return [];
return parsedModels;
}
/** Adds fields on the base model to be selected */
......
import * as assert from "assert";
import { Database } from "@swirl/db";
import { getTestAdapter } from "./db/adapter";
import { Account } from "./db/account";
......@@ -15,23 +16,31 @@ describe('DBQuery', function() {
describe('select()', function() {
it('Loads the selected fields into the model', async () => {
const test = new Account();
test.name = 'Test Account';
await test.insert();
const person = new Contact();
person.firstName = 'Tommy';
person.lastName = 'Toaster';
person.account = test;
await person.insert();
console.log(String(person));
const res = await db.query(Account)
const queriedAccount = await db.query(Account)
.select('id', 'name')
.getOne();
console.log(res);
assert.equal(queriedAccount.name, test.name);
assert.equal(queriedAccount.id, test.id);
assert(test.id != null);
});
it('Excludes non-loaded fields', async () => {
const test = new Account();
test.name = 'Another Test Account';
await test.insert();
const queriedAccount = await db.query(Account)
.select('id')
.getOne();
assert.equal(queriedAccount.id, test.id);
assert(test.id != null);
assert(queriedAccount.name == null);
});
});
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment