Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit ed9da88

Browse files
authored
fix(primaryKey): allow numeric integers as primaryKeys (#30)
* fix(primaryKey): allow numeric integers as primaryKeys * tests * docs(primaryKey): add allowed primary keys in readme
1 parent 628fd06 commit ed9da88

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ User.index('email')
4040
```
4141

4242
You might also pass a third argument to requelize.model that looks like this: `{ primaryKey: string }`
43+
Primary keys allowed are: guid and integers
4344

4445
## Querying
4546

lib/model/model.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ const hiddenProp = require('./util/hiddenProp')
1414
const parse = require('./parse')
1515
const saveAll = require('./saveAll')
1616

17+
const idType = Joi.alternatives().try(
18+
Joi.string().guid(),
19+
Joi.number().integer()
20+
)
21+
1722
/**
1823
* Bind requelize instance for createModel function
1924
* @internal
@@ -114,8 +119,8 @@ function createModel (requelize) {
114119
static _relField (foreignKey) {
115120
Model.index(foreignKey)
116121
Model._schema[foreignKey] = Joi.alternatives().try(
117-
Joi.string().guid().optional().allow(null),
118-
Joi.array().items(Joi.string().guid().optional().allow(null))
122+
idType.optional().allow(null),
123+
Joi.array().items(idType.optional().allow(null))
119124
)
120125
}
121126

@@ -172,8 +177,8 @@ function createModel (requelize) {
172177
if (!through) {
173178
const tableName = generateJoinTableName(Model._name, model)
174179
const JoinModel = bindedCreateModel(tableName, {
175-
[Model._name]: Joi.string().guid(),
176-
[model]: Joi.string().guid()
180+
[Model._name]: idType,
181+
[model]: idType
177182
})
178183

179184
JoinModel.index(Model._name)
@@ -338,7 +343,7 @@ function createModel (requelize) {
338343
}
339344

340345
// Add primaryKey to schema
341-
schema[options.primaryKey] = Joi.string().guid().optional().allow(null)
346+
schema[options.primaryKey] = idType.optional().allow(null)
342347

343348
Model._name = name
344349
Model._schema = schema

test/test.issue26.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { test, requelize } = require('./utils')
22

3-
test('Issue #26', (t) => {
3+
test('Issue #26 - instantiate Model with null as initial data', (t) => {
44
t.plan(1)
55

66
const Foo = requelize.model('foo')

test/test.issue28.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const Joi = require('joi')
2+
const { test, requelize, dropDb } = require('./utils')
3+
4+
test('Issue #28 - custom primary key type instead of guid', (t) => {
5+
t.plan(3)
6+
7+
let Foo
8+
let foo
9+
10+
dropDb()
11+
.then(() => {
12+
Foo = requelize.model('foo', { name: Joi.string() })
13+
14+
return requelize.sync()
15+
})
16+
.then(() => {
17+
foo = new Foo({ id: 1, name: 'bar' })
18+
19+
return foo.save()
20+
})
21+
.then(() => {
22+
return Foo.run()
23+
})
24+
.then((foos) => {
25+
t.equal(1, foos.length, 'found model')
26+
t.equal(1, foos[0].id, 'id is still the same')
27+
28+
return Foo.get(1)
29+
})
30+
.then((foo) => {
31+
t.equal(1, foo.id, 'can query by id')
32+
})
33+
.catch((err) => {
34+
t.fail(err)
35+
})
36+
.then(() => {
37+
t.end()
38+
})
39+
})

0 commit comments

Comments
 (0)