Comparing version 0.0.0 to 0.0.1
{ | ||
"name": "nothinkdb", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "ORM: Object-Rethinkdb-Mapping", | ||
@@ -5,0 +5,0 @@ "main": "lib/nothinkdb.js", |
import { expect } from 'chai'; | ||
import Joi from 'joi'; | ||
import Table from '../Table'; | ||
import Link from '../Link'; | ||
import Model, { | ||
@@ -12,46 +10,3 @@ HAS_ONE, BELONGS_TO, | ||
describe('Model', () => { | ||
describe('constructor', () => { | ||
it('should throw Error if \'table\' is not overrided', () => { | ||
expect(() => new Model()).to.throw(Error); | ||
}); | ||
it('should not throw Error if \'table\' is overrided', () => { | ||
class Foo extends Model { | ||
static table = 'foo'; | ||
} | ||
expect(() => new Foo()).to.not.throw(Error); | ||
}); | ||
}); | ||
describe('static', () => { | ||
describe('schema', () => { | ||
it('has default property', () => { | ||
class Base extends Model { | ||
static table = 'base'; | ||
static schema = () => ({ | ||
...Model.schema(), | ||
name: Joi.string().default('hello'), | ||
}); | ||
} | ||
expect(Base.schema()).to.have.property('id'); | ||
expect(Base.schema()).to.have.property('createdAt'); | ||
expect(Base.schema()).to.have.property('updatedAt'); | ||
expect(Base.schema()).to.have.property('name'); | ||
}); | ||
it('could be extended', () => { | ||
class Base extends Model { | ||
static table = 'base'; | ||
static schema = () => ({ | ||
...Model.schema(), | ||
name: Joi.string().default('hello'), | ||
}); | ||
} | ||
expect(Base.schema()).to.have.property('id'); | ||
expect(Base.schema()).to.have.property('createdAt'); | ||
expect(Base.schema()).to.have.property('updatedAt'); | ||
expect(Base.schema()).to.have.property('name'); | ||
}); | ||
}); | ||
describe('hasOne', () => { | ||
@@ -72,6 +27,3 @@ it('should create hasOne relation', () => { | ||
} | ||
const bar2foo = new Link({ | ||
linker: { Table: Bar, key: 'fooId' }, | ||
linkee: { Table: Foo, key: 'id' }, | ||
}); | ||
const bar2foo = Foo.linkedBy(Bar, 'fooId'); | ||
Foo.hasOne('bar', bar2foo); | ||
@@ -101,6 +53,3 @@ | ||
} | ||
const foo2bar = new Link({ | ||
linker: { Table: Foo, key: 'barId' }, | ||
linkee: { Table: Bar, key: 'id' }, | ||
}); | ||
const foo2bar = Foo.linkTo(Bar, 'barId'); | ||
Foo.belongsTo('bar', foo2bar); | ||
@@ -130,6 +79,3 @@ | ||
} | ||
const bar2foo = new Link({ | ||
linker: { Table: Bar, key: 'fooId' }, | ||
linkee: { Table: Foo, key: 'id' }, | ||
}); | ||
const bar2foo = Foo.linkedBy(Bar, 'fooId'); | ||
Foo.hasMany('bars', bar2foo); | ||
@@ -162,25 +108,17 @@ | ||
...Model.schema(), | ||
fooId: Foo.getForeignKey(), | ||
barId: Bar.getForeignKey(), | ||
fooId: Foo.getForeignKey({ isManyToMany: true }), | ||
barId: Bar.getForeignKey({ isManyToMany: true }), | ||
}); | ||
} | ||
const foobarLinks = [ | ||
new Link({ | ||
linker: { Table: FooBar, key: 'fooId' }, | ||
linkee: { Table: Foo, key: 'id' }, | ||
}), | ||
new Link({ | ||
linker: { Table: FooBar, key: 'barId' }, | ||
linkee: { Table: Bar, key: 'id' }, | ||
}), | ||
]; | ||
Foo.belongsToMany('bars', foobarLinks); | ||
Bar.belongsToMany('foos', foobarLinks); | ||
const foo2foobar2bar = [Foo.linkedBy(FooBar, 'fooId'), FooBar.linkTo(Bar, 'barId')]; | ||
const bar2foobar2foo = [Bar.linkedBy(FooBar, 'barId'), FooBar.linkTo(Foo, 'fooId')]; | ||
Foo.belongsToMany('bars', foo2foobar2bar); | ||
Bar.belongsToMany('foos', bar2foobar2foo); | ||
expect(Foo.relations.bars).to.deep.equal({ | ||
links: foobarLinks, | ||
link: foo2foobar2bar, | ||
type: BELONGS_TO_MANY, | ||
}); | ||
expect(Bar.relations.foos).to.deep.equal({ | ||
links: foobarLinks, | ||
link: bar2foobar2foo, | ||
type: BELONGS_TO_MANY, | ||
@@ -191,27 +129,2 @@ }); | ||
}); | ||
describe('attempt', () => { | ||
it('should update default properties', () => { | ||
class Bar extends Model { | ||
static table = 'foo'; | ||
static schema = () => ({ | ||
name: Joi.string().default('bar'), | ||
}); | ||
} | ||
const bar = new Bar(); | ||
bar.attempt(); | ||
expect(bar.data).to.have.property('name', 'bar'); | ||
}); | ||
it('should throw error when invalid', () => { | ||
class Foo extends Model { | ||
static table = 'foo'; | ||
static schema = { | ||
name: Joi.string(), | ||
}; | ||
} | ||
const foo = new Foo({ name: 1 }); | ||
expect(() => foo.attempt()).to.throw(Error); | ||
}); | ||
}); | ||
}); |
@@ -6,3 +6,6 @@ import assert from 'assert'; | ||
static validateLinkData(linkData) { | ||
return !!(linkData && linkData.Table && linkData.key); | ||
return linkData && | ||
linkData.Table && | ||
linkData.key && | ||
linkData.Table.hasColumn(linkData.key); | ||
} | ||
@@ -18,5 +21,5 @@ | ||
Link.assertLinkData(linkee); | ||
linker.Table.assertColumn(linker.key); | ||
linkee.Table.assertColumn(linkee.key); | ||
this.linker = linker; | ||
this.linkee = linkee; | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import Joi from 'joi'; | ||
import assert from 'assert'; | ||
@@ -27,2 +26,3 @@ import Table from './Table'; | ||
assert.equal(link.constructor, Link); | ||
assert.equal(link.linkee.Table, this); | ||
this.addRelation(as, { link, type: HAS_ONE }); | ||
@@ -33,2 +33,3 @@ } | ||
assert.equal(link.constructor, Link); | ||
assert.equal(link.linker.Table, this); | ||
this.addRelation(as, { link, type: BELONGS_TO }); | ||
@@ -39,10 +40,13 @@ } | ||
assert.equal(link.constructor, Link); | ||
assert.equal(link.linkee.Table, this); | ||
this.addRelation(as, { link, type: HAS_MANY }); | ||
} | ||
static belongsToMany(as, links = []) { | ||
assert.equal(links.length, 2); | ||
assert.equal(links[0].constructor, Link); | ||
assert.equal(links[1].constructor, Link); | ||
this.addRelation(as, { links, type: BELONGS_TO_MANY }); | ||
static belongsToMany(as, link) { | ||
assert.equal(link.length, 2); | ||
assert.equal(link[0].constructor, Link); | ||
assert.equal(link[1].constructor, Link); | ||
assert.equal(link[0].linkee.Table, this); | ||
assert.equal(link[0].linker.Table, link[1].linker.Table); | ||
this.addRelation(as, { link, type: BELONGS_TO_MANY }); | ||
} | ||
@@ -54,6 +58,2 @@ | ||
} | ||
attempt() { | ||
this.data = Joi.attempt(this.data, this.constructor.schema()); | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import r from 'rethinkdb'; | ||
import Joi from 'joi'; | ||
@@ -6,2 +5,3 @@ import _ from 'lodash'; | ||
import uuid from 'node-uuid'; | ||
import Link from './Link'; | ||
@@ -14,3 +14,3 @@ | ||
static schema = () => ({ | ||
id: Joi.string().max(32).default(() => uuid.v4(), 'primary key'), | ||
id: Joi.string().max(32).default(() => uuid.v4(), 'primary key').meta({ index: true }), | ||
createdAt: Joi.date().default(() => new Date(), 'time of creation'), | ||
@@ -20,30 +20,42 @@ updatedAt: Joi.date().default(() => new Date(), 'time of updated'), | ||
static hasColumn(column) { | ||
return this.schema()[column]; | ||
static validate(data = null) { | ||
return !Joi.validate(data, this.schema()).error; | ||
} | ||
static assertColumn(column) { | ||
return assert.ok(this.hasColumn(column), `Column '${column}' is unspecified in table '${this.table}'.`); | ||
static attempt(data = null) { | ||
return Joi.attempt(data, this.schema()); | ||
} | ||
static getForeignKey(targetKey = this.pk, options = {}) { | ||
this.assertColumn(targetKey); | ||
const { isManyToMany = false } = options; | ||
static hasColumn(columnName) { | ||
return _.has(this.schema(), columnName); | ||
} | ||
static assertColumn(columnName) { | ||
return assert.ok(this.hasColumn(columnName), `Column '${columnName}' is unspecified in table '${this.table}'.`); | ||
} | ||
static getColumn(columnName) { | ||
this.assertColumn(columnName); | ||
return this.schema()[columnName]; | ||
} | ||
static getForeignKey(options = {}) { | ||
const { columnName = this.pk, isManyToMany = false } = options; | ||
const column = this.getColumn(columnName); | ||
if (isManyToMany) { | ||
return this.schema()[this.pk].required(); | ||
return column.required(); | ||
} | ||
return this.schema()[this.pk].default(null); | ||
return column.default(null); | ||
} | ||
static async sync(connection) { | ||
await this.ensureTable(connection); | ||
static linkTo(OtherTable, foreignKey, targetKey = OtherTable.pk) { | ||
return new Link({ | ||
linker: { Table: this, key: foreignKey }, | ||
linkee: { Table: OtherTable, key: targetKey }, | ||
}); | ||
} | ||
static async ensureTable(connection) { | ||
return await r.branch( | ||
r.tableList().contains(this.table).not(), | ||
r.tableCreate(this.table), | ||
null | ||
).run(connection); | ||
static linkedBy(OtherTable, foreignKey, targetKey = OtherTable.pk) { | ||
return OtherTable.linkTo(this, foreignKey, targetKey); | ||
} | ||
@@ -58,5 +70,9 @@ | ||
validate() { | ||
this.data = Joi.validate(this.data, this.constructor.schema()); | ||
isValid() { | ||
return this.constructor.validate(this.data); | ||
} | ||
attempt() { | ||
this.data = this.constructor.attempt(this.data); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
20626
16
603
1
8
1