Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Commit

Permalink
Added simple access to models from the settee instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
rudowastaken committed Mar 4, 2017
1 parent 0ed0748 commit 030b7c9
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Settee from './settee';
import Instance from './entities/instance';
import Model from './entities/model';
import Schema from './entities/schema';
import Type from './entities/type';
declare const settee: Settee;
export { settee };
export { Instance };
export { Model };
export { Schema };
export { Type };
export * from './errors';
4 changes: 4 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ function __export(m) {
}
Object.defineProperty(exports, "__esModule", { value: true });
const settee_1 = require("./settee");
const instance_1 = require("./entities/instance");
exports.Instance = instance_1.default;
const model_1 = require("./entities/model");
exports.Model = model_1.default;
const schema_1 = require("./entities/schema");
exports.Schema = schema_1.default;
const type_1 = require("./entities/type");
Expand Down
17 changes: 17 additions & 0 deletions dist/settee.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class Settee {
* Container for registered schemas.
*/
registeredSchemas: SchemaContainer;
/**
* Container for registered models.
*/
registeredModels: Map<string, Model>;
/**
* Available consistencies for Couchbase storage actions.
*/
Expand Down Expand Up @@ -58,6 +62,19 @@ export default class Settee {
* @return {Model}
*/
registerSchema(schema: Schema): Model;
/**
* Registers a set of provided models.
*
* @param {Model[]} models
*/
registerModels(models: Model[]): void;
/**
* Provides a registered model.
*
* @param {string} name
* @return {Model}
*/
getModel(name: string): Model;
/**
* Builds deferred indexes.
*
Expand Down
26 changes: 26 additions & 0 deletions dist/settee.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Settee {
* Settee constructor.
*/
constructor() {
/**
* Container for registered models.
*/
this.registeredModels = new Map();
this.registeredSchemas = new schemaContainer_1.default();
this.consistency = {
NOT_BOUND: consistencies.NOT_BOUND,
Expand Down Expand Up @@ -72,6 +76,28 @@ class Settee {
this.registeredSchemas.add(schema, model);
return model;
}
/**
* Registers a set of provided models.
*
* @param {Model[]} models
*/
registerModels(models) {
models.forEach(model => {
this.registeredModels.set(model.name.toLowerCase(), model);
});
}
/**
* Provides a registered model.
*
* @param {string} name
* @return {Model}
*/
getModel(name) {
if (!this.registeredModels.has(name.toLowerCase())) {
throw new errors_1.SetteeError(`Model '${name}' is not available.`);
}
return this.registeredModels.get(name.toLowerCase());
}
/**
* Builds deferred indexes.
*
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Settee from './settee'
import Instance from './entities/instance'
import Model from './entities/model'
import Schema from './entities/schema'
import Type from './entities/type'

const settee: Settee = new Settee()

export { settee }
export { Instance }
export { Model }
export { Schema }
export { Type }
export * from './errors'
30 changes: 30 additions & 0 deletions src/settee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export default class Settee {
*/
public registeredSchemas: SchemaContainer

/**
* Container for registered models.
*/
public registeredModels: Map<string, Model> = new Map()

/**
* Available consistencies for Couchbase storage actions.
*/
Expand Down Expand Up @@ -107,6 +112,31 @@ export default class Settee {
return model
}

/**
* Registers a set of provided models.
*
* @param {Model[]} models
*/
public registerModels (models: Model[]): void {
models.forEach(model => {
this.registeredModels.set(model.name.toLowerCase(), model)
})
}

/**
* Provides a registered model.
*
* @param {string} name
* @return {Model}
*/
public getModel (name: string): Model {
if (!this.registeredModels.has(name.toLowerCase())) {
throw new SetteeError(`Model '${name}' is not available.`)
}

return this.registeredModels.get(name.toLowerCase())
}

/**
* Builds deferred indexes.
*
Expand Down
18 changes: 18 additions & 0 deletions test/features/general.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,21 @@ test('common queries', async () => {
await retrievedBmw.delete()
await audi.delete()
})

test('simple access to models', () => {
const CarSchema = new Schema('Car', {
brand: Type.string(),
color: Type.string()
})

const Car = settee.registerSchema(CarSchema)

// We register the models with settee ...
settee.registerModels([Car])

// ... and we can easily access them across different files.
const resolvedCar = settee.getModel('Car')

// It's the same model ...
resolvedCar.should.deep.eq(Car)
})
33 changes: 33 additions & 0 deletions test/specs/settee.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,39 @@ test.cb('it registers a schema', t => {
t.end()
})

test('it registers a set of models', t => {
settee.useBucket(bucket)

const CarSchema = new Schema('Car', {
brand: Type.string()
})

const Car = settee.registerSchema(CarSchema)

const EngineSchema = new Schema('Engine', {
brand: Type.number(150)
})

const Engine = settee.registerSchema(EngineSchema)

settee.registerModels([
Car, Engine
])

settee.getModel('Car')
.should.deep.eq(Car)

settee.getModel('Engine')
.should.deep.eq(Engine)

// error
const err = t.throws(() => {
settee.getModel('Missing')
}, SetteeError)

err.message.should.contain(`Model 'Missing' is not available.`)
})

test('it builds deferred indexes', async () => {
settee.useBucket(bucket)

Expand Down

0 comments on commit 030b7c9

Please sign in to comment.