Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "nothinkdb", | ||
"version": "0.1.0", | ||
"description": "ORM: Object-Rethinkdb-Mapping", | ||
"version": "0.2.0", | ||
"description": "Functional toolkit for rethinkdb", | ||
"main": "lib/nothinkdb.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# What? | ||
Simple ORM(Object-Rethinkdb-Mapping). | ||
Functional toolkit for [rethinkdb](https://www.rethinkdb.com/api/javascript/). | ||
# How? | ||
# Feature | ||
See `src/**/__tests__/*-test.js`; | ||
- __table__ | ||
- schema: handle schema with [joi](https://github.com/hapijs/joi) | ||
- validate | ||
- generate default value | ||
- __relations__ | ||
- types | ||
- one-to-one: hasOne | ||
- one-to-many: hasMany | ||
- many-to-one: belongsTo | ||
- many-to-many: belongsToMany | ||
- join | ||
- create | ||
- remove | ||
- __sync__: create table, secondary index... | ||
- __query__: handle table data with rethinkdb. | ||
# API | ||
See the [API Reference](https://github.com/ironhee/nothinkdb/blob/master/API.md). | ||
# Example | ||
See the [Examples](https://github.com/ironhee/nothinkdb/tree/master/examples) |
@@ -6,2 +6,3 @@ import r from 'rethinkdb'; | ||
import Table from '../Table'; | ||
import schema from '../schema'; | ||
import Link from '../Link'; | ||
@@ -29,12 +30,2 @@ import { hasOne, belongsTo, hasMany, belongsToMany } from '../relations'; | ||
describe('staic', () => { | ||
describe('schema', () => { | ||
it('has default property', () => { | ||
expect(Table.schema).to.have.property('id'); | ||
expect(Table.schema).to.have.property('createdAt'); | ||
expect(Table.schema).to.have.property('updatedAt'); | ||
}); | ||
}); | ||
}); | ||
describe('constructor', () => { | ||
@@ -45,3 +36,3 @@ it('schema could be extended', () => { | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
name: Joi.string().default('hello'), | ||
@@ -74,22 +65,2 @@ }), | ||
describe('attempt', () => { | ||
const fooTable = new Table({ | ||
table: 'foo', | ||
schema: () => ({ | ||
foo: Joi.string().default('foo'), | ||
bar: Joi.string().required(), | ||
}), | ||
}); | ||
it('should return with default properties', () => { | ||
const result = fooTable.attempt({ bar: 'bar' }); | ||
expect(result).to.have.property('foo', 'foo'); | ||
expect(result).to.have.property('bar', 'bar'); | ||
}); | ||
it('should throw error when invalid', () => { | ||
expect(() => fooTable.attempt({})).to.throw(Error); | ||
}); | ||
}); | ||
describe('create', () => { | ||
@@ -210,3 +181,3 @@ const fooTable = new Table({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -224,3 +195,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -240,3 +211,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
barId: barTable.getForeignKey(), | ||
@@ -248,3 +219,3 @@ }), | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -270,3 +241,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -277,3 +248,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey(), | ||
@@ -300,3 +271,3 @@ }), | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -314,3 +285,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
name: Joi.string().required(), | ||
@@ -321,4 +292,4 @@ }), | ||
await fooTable.insert(foo).run(connection); | ||
const fetchedfooTable = await fooTable.query().get(foo.id).run(connection); | ||
expect(foo).to.deep.equal(fetchedfooTable); | ||
const fetchedfoo = await fooTable.query().get(foo.id).run(connection); | ||
expect(foo).to.deep.equal(fetchedfoo); | ||
}); | ||
@@ -332,3 +303,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
name: Joi.string().required(), | ||
@@ -339,4 +310,4 @@ }), | ||
await fooTable.insert(foo).run(connection); | ||
const fetchedfooTable = await fooTable.get(foo.id).run(connection); | ||
expect(foo).to.deep.equal(fetchedfooTable); | ||
const fetchedfoo = await fooTable.get(foo.id).run(connection); | ||
expect(foo).to.deep.equal(fetchedfoo); | ||
}); | ||
@@ -350,3 +321,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
name: Joi.string().required(), | ||
@@ -358,4 +329,4 @@ }), | ||
await fooTable.update(foo.id, { name: 'bar' }).run(connection); | ||
const fetchedfooTable = await fooTable.get(foo.id).run(connection); | ||
expect(fetchedfooTable).to.have.property('name', 'bar'); | ||
const fetchedfoo = await fooTable.get(foo.id).run(connection); | ||
expect(fetchedfoo).to.have.property('name', 'bar'); | ||
}); | ||
@@ -369,3 +340,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
name: Joi.string().required(), | ||
@@ -377,4 +348,4 @@ }), | ||
await fooTable.delete(foo.id).run(connection); | ||
const fetchedfooTable = await fooTable.query().get(foo.id).run(connection); | ||
expect(fetchedfooTable).to.be.null; | ||
const fetchedfoo = await fooTable.query().get(foo.id).run(connection); | ||
expect(fetchedfoo).to.be.null; | ||
}); | ||
@@ -388,3 +359,3 @@ }); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -398,3 +369,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey(), | ||
@@ -414,4 +385,4 @@ }), | ||
query = await fooTable.withJoin(query, { bar: true }); | ||
const fetchedfooTable = await query.run(connection); | ||
expect(bar).to.deep.equal(fetchedfooTable.bar); | ||
const fetchedfoo = await query.run(connection); | ||
expect(bar).to.deep.equal(fetchedfoo.bar); | ||
}); | ||
@@ -423,3 +394,3 @@ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
barId: barTable.getForeignKey(), | ||
@@ -434,3 +405,3 @@ }), | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -449,4 +420,4 @@ }); | ||
query = fooTable.withJoin(query, { bar: true }); | ||
const fetchedfooTable = await query.run(connection); | ||
expect(bar).to.deep.equal(fetchedfooTable.bar); | ||
const fetchedfoo = await query.run(connection); | ||
expect(bar).to.deep.equal(fetchedfoo.bar); | ||
}); | ||
@@ -458,3 +429,3 @@ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -468,3 +439,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey(), | ||
@@ -484,5 +455,5 @@ }), | ||
query = fooTable.withJoin(query, { bars: true }); | ||
const fetchedfooTable = await query.run(connection); | ||
expect(fetchedfooTable.bars).to.have.length(1); | ||
expect(bar).to.deep.equal(fetchedfooTable.bars[0]); | ||
const fetchedfoo = await query.run(connection); | ||
expect(fetchedfoo.bars).to.have.length(1); | ||
expect(bar).to.deep.equal(fetchedfoo.bars[0]); | ||
}); | ||
@@ -494,3 +465,3 @@ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -504,3 +475,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -514,3 +485,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey({ isManyToMany: true }), | ||
@@ -534,5 +505,5 @@ barId: barTable.getForeignKey({ isManyToMany: true }), | ||
query = fooTable.withJoin(query, { bars: true }); | ||
const fetchedfooTable = await query.run(connection); | ||
expect(fetchedfooTable.bars).to.have.length(1); | ||
expect(bar).to.deep.equal(fetchedfooTable.bars[0]); | ||
const fetchedfoo = await query.run(connection); | ||
expect(fetchedfoo.bars).to.have.length(1); | ||
expect(bar).to.deep.equal(fetchedfoo.bars[0]); | ||
@@ -552,3 +523,3 @@ query = barTable.get(bar.id); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -562,3 +533,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey(), | ||
@@ -578,5 +549,5 @@ }), | ||
fooQuery = fooTable.withJoin(fooQuery, { bars: true }); | ||
const fetchedfooTable = await fooQuery.run(connection); | ||
expect(fetchedfooTable.bars).to.have.length(1); | ||
expect(fetchedfooTable.bars[0]).to.have.property('fooId', foo.id); | ||
const fetchedfoo = await fooQuery.run(connection); | ||
expect(fetchedfoo.bars).to.have.length(1); | ||
expect(fetchedfoo.bars[0]).to.have.property('fooId', foo.id); | ||
}); | ||
@@ -588,3 +559,3 @@ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -598,3 +569,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -608,3 +579,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey({ isManyToMany: true }), | ||
@@ -625,4 +596,4 @@ barId: barTable.getForeignKey({ isManyToMany: true }), | ||
const fooQuery = fooTable.get(foo.id); | ||
const fetchedfooTable = await fooTable.withJoin(fooQuery, { bars: true }).run(connection); | ||
expect(bar.id).to.equal(fetchedfooTable.bars[0].id); | ||
const fetchedfoo = await fooTable.withJoin(fooQuery, { bars: true }).run(connection); | ||
expect(bar.id).to.equal(fetchedfoo.bars[0].id); | ||
@@ -640,3 +611,3 @@ const barQuery = barTable.get(bar.id); | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -650,3 +621,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey(), | ||
@@ -671,5 +642,5 @@ }), | ||
fooQuery = fooTable.withJoin(fooQuery, { bars: true }); | ||
const fetchedfooTable = await fooQuery.run(connection); | ||
expect(fetchedfooTable.bars).to.have.length(1); | ||
expect(bar2.id).to.equal(fetchedfooTable.bars[0].id); | ||
const fetchedfoo = await fooQuery.run(connection); | ||
expect(fetchedfoo.bars).to.have.length(1); | ||
expect(bar2.id).to.equal(fetchedfoo.bars[0].id); | ||
}); | ||
@@ -681,3 +652,3 @@ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -691,3 +662,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
}), | ||
@@ -701,3 +672,3 @@ relations: () => ({ | ||
schema: () => ({ | ||
...Table.schema, | ||
...schema, | ||
fooId: fooTable.getForeignKey({ isManyToMany: true }), | ||
@@ -723,5 +694,5 @@ barId: barTable.getForeignKey({ isManyToMany: true }), | ||
const fooQuery = fooTable.get(foo.id); | ||
const fetchedfooTable = await fooTable.withJoin(fooQuery, { bars: true }).run(connection); | ||
expect(fetchedfooTable.bars).to.have.length(1); | ||
expect(bar2.id).to.equal(fetchedfooTable.bars[0].id); | ||
const fetchedfoo = await fooTable.withJoin(fooQuery, { bars: true }).run(connection); | ||
expect(fetchedfoo.bars).to.have.length(1); | ||
expect(bar2.id).to.equal(fetchedfoo.bars[0].id); | ||
@@ -728,0 +699,0 @@ const barQuery = barTable.get(bar2.id); |
export Table from './Table'; | ||
export * from './relations'; | ||
export schema from './schema'; |
@@ -6,3 +6,2 @@ /* eslint no-shadow: 0, no-param-reassign: 0 */ | ||
import assert from 'assert'; | ||
import uuid from 'node-uuid'; | ||
import Link from './Link'; | ||
@@ -12,13 +11,6 @@ | ||
export default class Table { | ||
static pk = 'id'; | ||
static schema = { | ||
id: Joi.string().max(36).default(() => uuid.v4(), 'primary key').meta({ index: true }), | ||
createdAt: Joi.date().default(() => new Date(), 'time of creation'), | ||
updatedAt: Joi.date().default(() => new Date(), 'time of updated'), | ||
}; | ||
constructor(options = {}) { | ||
const { table, pk, schema, relations } = Joi.attempt(options, { | ||
table: Joi.string().required(), | ||
pk: Joi.string().default(Table.pk), | ||
pk: Joi.string().default('id'), | ||
schema: Joi.func().required(), | ||
@@ -70,12 +62,12 @@ relations: Joi.func().default(() => () => ({}), 'relation'), | ||
linkTo(rightTable, leftField, options = {}) { | ||
const { index = rightTable.pk } = options; | ||
linkTo(targetTable, leftField, options = {}) { | ||
const { index = targetTable.pk } = options; | ||
return new Link({ | ||
left: { table: this, field: leftField }, | ||
right: { table: rightTable, field: index }, | ||
right: { table: targetTable, field: index }, | ||
}); | ||
} | ||
linkedBy(leftTable, leftField, options) { | ||
return leftTable.linkTo(this, leftField, options); | ||
linkedBy(targetTable, leftField, options) { | ||
return targetTable.linkTo(this, leftField, options); | ||
} | ||
@@ -113,3 +105,2 @@ | ||
query() { | ||
assert.ok(this.table, 'Table should have property \'table\'.'); | ||
return r.table(this.table); | ||
@@ -119,3 +110,3 @@ } | ||
insert(data) { | ||
return this.query().insert(this.attempt(data)); | ||
return this.query().insert(data); | ||
} | ||
@@ -122,0 +113,0 @@ |
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
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
46135
17
1035
30
0