Skip to content

Commit

Permalink
feat(declarations): allow specifying declarations with the 'generateS…
Browse files Browse the repository at this point in the history
…qlSchemasFor' keyword
  • Loading branch information
uladkasach committed Jul 22, 2021
1 parent da105c9 commit 59761e0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('normalizeDeclarationContents', () => {
normalizeDeclarationContents({ contents });
throw new Error('should not reach here');
} catch (error) {
expect(error.message).toEqual('an entities array must be exported by the source file');
expect(error.message).toEqual(
'an `entities` or `generateSqlSchemasFor` array must be exported by the source file',
);
}
});
it('throw an error if entities are not all of class Entity or ValueObject', () => {
Expand Down Expand Up @@ -84,6 +86,27 @@ describe('normalizeDeclarationContents', () => {
};
const { entities } = normalizeDeclarationContents({ contents });

// check that we return everything as expected
expect(entities).toEqual([plant, vase, customer, order]);
});
it('should return the entities and value objects found in the contents - when specified with the generateSqlSchemasFor syntax', () => {
const plant = new ValueObject({ name: 'plant', properties: { genus: prop.VARCHAR(255) } });
const vase = new ValueObject({ name: 'vase', properties: { plants: prop.ARRAY_OF(prop.REFERENCES(plant)) } });
const customer = new Entity({
name: 'customer',
properties: { phone_number: prop.VARCHAR(10) },
unique: ['phone_number'], // users are unique on phone numbers
});
const order = new Entity({
name: 'order',
properties: { customer_id: prop.REFERENCES(customer), vase_id: prop.REFERENCES(vase) },
unique: ['uuid'], // logically unique on nothing - same order can be placed many times! -> we'll require the user to pass in a uuid for idempotency
});
const contents = {
generateSqlSchemasFor: [plant, vase, customer, order],
};
const { entities } = normalizeDeclarationContents({ contents });

// check that we return everything as expected
expect(entities).toEqual([plant, vase, customer, order]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { throwErrorIfNamingConventionsNotFollowed } from './throwErrorIfNamingCo
import { throwErrorIfNotUniqueOnAnything } from './throwErrorIfNotUniqueOnAnything';

export const normalizeDeclarationContents = ({ contents }: { contents: any }) => {
// 1. check that 'entities' is exported
if (!contents.entities) throw new Error('an entities array must be exported by the source file');
const entities = contents.entities as Entity[];
// 1. check that 'entities' or 'generateSqlSchemasFor' is exported
if (!contents.entities && !contents.generateSqlSchemasFor)
throw new Error('an `entities` or `generateSqlSchemasFor` array must be exported by the source file');
const entities = (contents.entities ?? contents.generateSqlSchemasFor) as Entity[];

// 2. check that each entity is of the constructor
entities.forEach((entity: any) => {
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"member-ordering": false,
"ter-arrow-parens": false,
"ter-indent": false,
"array-type": false
"array-type": false,
"curly": false
}
}

0 comments on commit 59761e0

Please sign in to comment.