Commit 61ee0135 authored by William Naslund's avatar William Naslund

Added uuid data type

parent aa6ca895
{
"name": "@swirl/db",
"version": "1.0.0-alpha.1",
"lockfileVersion": 1,
"requires": true,
"lockfileVersion": 1,
"dependencies": {
"@babel/code-frame": {
"version": "7.0.0",
......@@ -159,6 +158,15 @@
"moment": ">=2.14.0"
}
},
"@types/uuid": {
"version": "3.4.4",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-3.4.4.tgz",
"integrity": "sha512-tPIgT0GUmdJQNSHxp0X2jnpQfBSTfGxUMc/2CXBU2mnyTFVYVa2ojpoQ74w0U2yn2vw3jnC640+77lkFFpdVDw==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"ansi-regex": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-3.0.0.tgz",
......@@ -2141,6 +2149,12 @@
"integrity": "sha512-Y21Xqe54TBVp+VDSNbuDYdGw0BpoR/Q6wo/+35M8PAU0vipahnyduJWirxxdxjsAkS7hue53x2zp8gz7F05u0A==",
"dev": true
},
"uuid": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==",
"dev": true
},
"validate-npm-package-license": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz",
......
......@@ -29,10 +29,12 @@
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"@types/pg": "^7.4.11",
"@types/uuid": "^3.4.4",
"concurrently": "^4.1.0",
"mocha": "^5.2.0",
"module-alias": "^2.1.0",
"nyc": "^13.2.0",
"typescript": "^3.2.4"
"typescript": "^3.2.4",
"uuid": "^3.3.2"
}
}
\ No newline at end of file
}
import "reflect-metadata";
import { DBModel, DBModelConstructor } from "../model";
import { FIELD_GENERATOR } from "../internal/meta-keys";
/** Sets the default value for a field */
export function DBDefault<T extends DBModel>(generator: DBFieldGenerator<T>) {
return function(proto: T, propertyKey: string | symbol) {
Reflect.defineMetadata(FIELD_GENERATOR, generator, proto.constructor, propertyKey);
};
}
/** A function that generates a value for a field */
export interface DBFieldGenerator<T extends DBModel> {
(record: T): Promise<any>;
}
export * from './db-table';
export * from './db-field';
export * from './db-lookup';
export * from './db-constraint';
\ No newline at end of file
export * from './db-constraint';
export * from './db-default';
\ No newline at end of file
......@@ -26,6 +26,9 @@ export const TABLE_CONSTRAINTS = Symbol();
/** Identifies registered column constraints */
export const COLUMN_CONSTRAINTS = Symbol();
/** Identifies the default generator for a field */
export const FIELD_GENERATOR = Symbol();
/** Loads the table information for a constructor */
export function getTableInformation(modelType: DBModelConstructor<any>): DBTableInformation {
......
......@@ -2,8 +2,8 @@ import "reflect-metadata";
import * as os from "os";
import { MODEL_DATABASE } from "./internal/model-registry";
import { Database } from "./database";
import { getTableInformation, getTableFields, TABLE_PARENT_LOOKUPS, getFieldInformation } from "./internal/meta-keys";
import { DBLookupInformation } from "./decorators";
import { getTableInformation, getTableFields, TABLE_PARENT_LOOKUPS, getFieldInformation, FIELD_GENERATOR } from "./internal/meta-keys";
import { DBLookupInformation, DBFieldGenerator } from "./decorators";
/** A record that is backed by a table in the database */
export class DBModel {
......@@ -35,6 +35,15 @@ export class DBModel {
}
}
// Run any generators
for(const field of getTableFields(this.constructor) || []) {
const generator = Reflect.getMetadata(FIELD_GENERATOR, this.constructor, field.propertyKey) as DBFieldGenerator<this>;
if(generator != null && saveData[field.name] == undefined) {
saveData[field.name] = await generator(this);
this[field.propertyKey] = saveData[field.name];
}
}
const newId = await db.adapter.model.insert(this.constructor, saveData);
if(tableInfo.options.id) {
this[tableInfo.options.id] = newId;
......
......@@ -4,4 +4,5 @@ export * from "./char";
export * from "./text";
export * from "./integer";
export * from "./numeric";
export * from "./id-sequence";
\ No newline at end of file
export * from "./id-sequence";
export * from "./uuid";
\ No newline at end of file
import { DBType } from "./type";
export class DBUniversalUID implements DBType {
create() {
return `UUID`;
}
}
\ No newline at end of file
import * as assert from "assert";
import { Database } from "@swirl/db";
import * as uuid from "uuid/v4"
import { Database, DBTable, DBModel, DBDefault, DBColumnConstraint, DBTableConstraint, DBUniqueConstraint, DBField, DBUniversalUID } from "@swirl/db";
import { getTestAdapter } from "./db/adapter";
import { Account } from "./db/account";
import { Contact } from "./db/contact";
import { Organization } from "./db/org";
@DBTable('tests', {
id: 'guid'
})
@DBTableConstraint('unique_guid', new DBUniqueConstraint('guid'))
class Test extends DBModel {
@DBField('guid', new DBUniversalUID())
@DBDefault(async () => {
return uuid();
})
guid: string;
}
describe('DBModel', function() {
let db: Database;
......@@ -14,6 +29,25 @@ describe('DBModel', function() {
});
describe('insert()', function() {
it('Runs generators for fields that are not defined', async() => {
await db.register(Test).migrate();
let tst = new Test();
await tst.insert();
assert(tst.guid != null);
const res = await db.adapter.command.query(`
SELECT *
FROM tests
`);
assert(res[0].guid == tst.guid);
});
});
describe('update()', function() {
it('Updates field values', async () => {
......
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