Skip to content

Commit

Permalink
Some smart wiring to expose models to window.$Model if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Feb 2, 2020
1 parent fd83a0d commit 6197a4c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/sinon"
"url": "https://opencollective.com/memserver"
},
"license": "MIT",
"bin": {
Expand Down
31 changes: 2 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,19 @@ const CWD = process.cwd();
await setupDom();

const modelFileNames = await fs.readdir(`${CWD}/memserver/models`);
const modelRegistry = await Promise.all(
modelFileNames.map(async (fileName) => {
const modelName = fileName.slice(0, -3);
const fixturePath = `${CWD}/memserver/fixtures/${dasherize(pluralize(modelName))}`;
const fixtureExists = await fs.pathExists(`${fixturePath}.ts`);

const [model, fixtures] = await Promise.all([
import(`${CWD}/memserver/models/${fileName}`),
fixtureExists ? import(fixturePath) : { default: [] }
]);

return {
[classify(modelName)]: {
model: model.default,
fixtures: fixtures.default
}
};
}, [])
);

window.Memserver = (await import("./server")).default;

const [initializerModule, routesModule] = await Promise.all([
import(`${CWD}/memserver/initializer`),
import(`${CWD}/memserver/routes`)
]);

window.MemServer = new window.Memserver({
globalizeModels: true,
initializer: initializerModule.default,
routes: routesModule.default
});
window.modelRegistry = modelRegistry;

modelRegistry.forEach((modelRegister) => {
const modelName = Object.keys(modelRegister)[0];

window[modelName] = modelRegister[modelName].model;
window[modelName].resetDatabase(modelRegister[modelName].fixtures);
});

return window.MemServer;
})();

// import('./dist').then((memserver) => global.MemServer = memserver)
2 changes: 2 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type InternalModel = RequireOnlyOne<InternalModelShape, "id" | "uuid">;

export default abstract class MemServerModel {
static _DB = {};
static _modelDefinitions = [];
static _attributes = {};
static _defaultAttributes = {}; // NOTE: probably a decorator here in future
static _embedReferences = {}; // NOTE: serializer concern
Expand All @@ -39,6 +40,7 @@ export default abstract class MemServerModel {
static get attributes(): Array<string> {
if (!this._attributes[this.name]) {
this._attributes[this.name] = [];
this._modelDefinitions.push(this);

return this._attributes[this.name];
}
Expand Down
24 changes: 18 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,27 @@ interface MemserverOptions {
}

export default class Memserver {
models: void | Array<Model> = null;
globalizeModels: boolean = false;

constructor(options: MemserverOptions = { logging: true }) {
const initializer = options.initializer || function() {};
const initializer = options.initializer || async function() {};
const routes = options.routes || function() {};
const logging = options.hasOwnProperty("logging") ? options.logging : true;

initializer();
const initializerReturn = initializer();

if (initializerReturn instanceof Promise) {
initializerReturn.then(() => {
if (options.globalizeModels) {
Model._modelDefinitions.forEach((model) => {
window[model.name] = model;
});
}
});
} else {
if (options.globalizeModels) {
Model._modelDefinitions.forEach((model) => {
window[model.name] = model;
});
}
}

return startPretender(routes, Object.assign(options, { logging }));
}
Expand Down

0 comments on commit 6197a4c

Please sign in to comment.