@projectinvicta/nails 3.0.3 → 3.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import Nails from './lib/Nails.ts';
2
2
  import ControllerInternal from './lib/Controller.ts';
3
3
 
4
- export { DataTypes } from 'sequelize';
4
+ export { DataTypes, Model } from 'sequelize';
5
5
 
6
6
  export default Nails;
7
7
 
8
8
  export const Controller = ControllerInternal;
9
- export * from './lib/config.ts';
10
-
11
- export { Model } from 'sequelize';
9
+ export * from './lib/config.ts';
package/lib/Nails.ts CHANGED
@@ -97,8 +97,9 @@ export default class Nails {
97
97
 
98
98
  async initializeModels() {
99
99
  await this.loadModels(this.config.service.MODELS_ROOT);
100
- await Promise.all(this.modelFinalizations);
101
- await this.sequelize.sync();
100
+ await Promise.all(this.modelFinalizations.map((finalization) => finalization()));
101
+ // TODO: Implement a migration strategy
102
+ await this.sequelize.sync({alter: {drop: false}});
102
103
  }
103
104
 
104
105
  async startServer() {
@@ -174,7 +175,9 @@ export default class Nails {
174
175
  const schema = modelModule.schema;
175
176
  const options = modelModule.options;
176
177
  const defer = modelModule.defer as (() => Promise<void>) | undefined;
177
- this.modelFinalizations.push(modelModule.finalize as () => Promise<void>);
178
+ if (modelModule.finalize) {
179
+ this.modelFinalizations.push(modelModule.finalize as () => Promise<void>);
180
+ }
178
181
  this.modelMigrations.push(modelModule.migrate as () => Promise<void>);
179
182
 
180
183
  modelClass.init(schema, { sequelize: this.sequelize, ...options });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projectinvicta/nails",
3
- "version": "3.0.3",
3
+ "version": "3.0.5",
4
4
  "description": "A node.js webserver scaffold",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -1,4 +1,18 @@
1
1
  // TODO test all initialization here
2
- import { describe, it, expect, vi } from 'vitest';
2
+ import { describe, beforeAll, it, test, expect, vi } from 'vitest';
3
+ import Dog from './services/integration/server/models/dog.js';
4
+ import Owner from './services/integration/server/models/owner.js';
3
5
 
4
- it.todo('Should test something');
6
+ let nails;
7
+
8
+ beforeAll(async () => {
9
+ nails = (await import('./services/integration/server.js')).default;
10
+ });
11
+
12
+ test("finalize is called during model initialization", async () => {
13
+ const owner = await Owner.create({});
14
+ const dog = await owner.createDog({name: "Fido"});
15
+ const dogOwner = await dog.getOwner();
16
+ expect(dogOwner).toBeDefined();
17
+ expect(dogOwner.id).toEqual(owner.id);
18
+ })
@@ -1,4 +1,5 @@
1
1
  import {Model, DataTypes} from '../../../../../index.ts';
2
+ import Owner from './owner.js';
2
3
 
3
4
  export const schema = {
4
5
  good: DataTypes.BOOLEAN,
@@ -6,3 +7,7 @@ export const schema = {
6
7
  };
7
8
 
8
9
  export default class Dog extends Model {};
10
+
11
+ export async function finalize() {
12
+ Dog.belongsTo(Owner);
13
+ }
@@ -9,5 +9,4 @@ export default class Owner extends Model {};
9
9
 
10
10
  export async function defer() {
11
11
  Owner.hasMany(Dog);
12
- await Dog.sync({alter: {drop: false}});
13
12
  }