Skip to content

Commit 9ef4ae8

Browse files
committed
Starts Connection class
1 parent 66717ef commit 9ef4ae8

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

lib/connection.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import SchemaBlueprint from './schema';
2+
import Model from './model';
3+
4+
export default class Connection {
5+
constructor(path) {
6+
if (!path) {
7+
throw new Error('Database requires a path');
8+
}
9+
10+
const connectionInstance = this;
11+
12+
this.Schema = class Schema extends SchemaBlueprint {
13+
constructor(obj) {
14+
super(obj);
15+
}
16+
};
17+
18+
this.models = {};
19+
20+
this.Schema.prototype.db = connectionInstance;
21+
}
22+
23+
model(name, schema) {
24+
if (!name || typeof name !== 'string') {
25+
throw new Error('Invalid name for model');
26+
}
27+
28+
if (this.models[name]) {
29+
return this.models[name];
30+
}
31+
32+
let argSchema = schema;
33+
34+
if (argSchema.constructor !== this.Schema) {
35+
argSchema = new this.Schema(argSchema);
36+
}
37+
38+
this.models[name] = Model.compile(name, argSchema, this);
39+
40+
return this.models[name];
41+
}
42+
}

test/connection.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import assert from 'assert';
2+
import Connection from '../lib/connection';
3+
import Schema from '../lib/schema';
4+
import Model from '../lib/model';
5+
6+
describe('Connection', () => {
7+
describe('constructor', () => {
8+
it('should succeed', () => {
9+
assert.ok(new Connection('data'));
10+
});
11+
12+
it('should fail', () => {
13+
assert.throws(() => new Connection(), Error);
14+
});
15+
16+
it('should create its own db schema type', () => {
17+
const db = new Connection('data');
18+
assert.ok(Schema.isPrototypeOf(db.Schema));
19+
});
20+
});
21+
22+
describe('model method', () => {
23+
let db;
24+
25+
beforeEach(() => {
26+
db = new Connection('data');
27+
});
28+
29+
it('should create and store a model', () => {
30+
const schema = new db.Schema({ field: String });
31+
const model = db.model('TheModel', schema);
32+
33+
assert.ok(model);
34+
assert.equal(model, db.models.TheModel);
35+
});
36+
37+
it('should create a schema if an object is passed', () => {
38+
const model = db.model('TheModel', { field: String });
39+
40+
assert.ok(model);
41+
assert.ok(model.schema);
42+
assert.ok(model.schema instanceof db.Schema);
43+
});
44+
45+
it('should return an already registered model', () => {
46+
const modelOne = db.model('TheModel', { field: String });
47+
const modelTwo = db.model('TheModel', { field: String });
48+
49+
assert.equal(modelOne, modelTwo);
50+
});
51+
52+
it('should fail because name is invalid', () => {
53+
assert.throws(() => {
54+
db.model('', { field: String });
55+
}, Error, 'Invalid name for model');
56+
57+
assert.throws(() => {
58+
db.model(123, { field: String });
59+
}, Error, 'Invalid name for model');
60+
});
61+
});
62+
});

0 commit comments

Comments
 (0)