Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow _id as a primary key #824

Merged
merged 7 commits into from
Sep 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/plugins/error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const CODES = {

// rx-collection
COL1: 'RxDocument.insert() You cannot insert an existing document',
COL2: 'RxCollection.insert() do not provide ._id, it will be generated',
COL2: 'RxCollection.insert() do not provide ._id when it is not the primary key',
COL3: 'RxCollection.upsert() does not work without primary',
COL4: 'RxCollection.atomicUpsert() does not work without primary',
COL5: 'RxCollection.find() if you want to search by _id, use .findOne(_id)',
Expand Down Expand Up @@ -130,7 +130,6 @@ const CODES = {
SC6: 'SchemaCheck: primary can only be defined at top-level',
SC7: 'SchemaCheck: default-values can only be defined at top-level',
SC8: 'SchemaCheck: first level-fields cannot start with underscore _',
SC9: 'SchemaCheck: schema defines ._id, this will be done automatically',
SC10: 'SchemaCheck: schema defines ._rev, this will be done automatically',
SC11: 'SchemaCheck: schema need an number>=0 as version',
SC12: 'SchemaCheck: primary can only be defined once',
Expand Down
11 changes: 4 additions & 7 deletions src/plugins/schema-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
*/
export function checkFieldNameRegex(fieldName) {
if (fieldName === '') return;
if (fieldName === '_id') return;

if (['properties', 'language'].includes(fieldName)) {
throw RxError.newRxError('SC23', {
Expand Down Expand Up @@ -117,6 +118,9 @@ export function validateFieldsDeep(jsonSchema) {
if (!isNested) {
// check underscore fields
if (fieldName.charAt(0) === '_') {
if (fieldName === '_id' && schemaObj.primary) {
return;
}
throw RxError.newRxError('SC8', {
fieldName
});
Expand Down Expand Up @@ -150,13 +154,6 @@ export function validateFieldsDeep(jsonSchema) {
* @throws {Error} if something is not ok
*/
export function checkSchema(jsonID) {
// check _id
if (jsonID.properties._id) {
throw RxError.newRxError('SC9', {
schema: jsonID
});
}

// check _rev
if (jsonID.properties._rev) {
throw RxError.newRxError('SC10', {
Expand Down
2 changes: 1 addition & 1 deletion src/rx-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class RxCollection {
json = clone(json);
json = this.schema.fillObjectWithDefaults(json);

if (json._id) {
if (json._id && this.schema.primaryPath !== '_id') {
throw RxError.newRxError('COL2', {
data: json
});
Expand Down
Empty file modified src/rx-schema.js
100755 → 100644
Empty file.
7 changes: 7 additions & 0 deletions test/helper/schema-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,10 @@ export function point() {
y: faker.random.number()
};
}

export function _idPrimary() {
return {
_id: randomToken(12),
firstName: faker.name.firstName()
};
}
16 changes: 15 additions & 1 deletion test/helper/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,24 @@ export const nostringIndex = {
}
},
required: ['firstName', 'lastName']

};


export const _idPrimary = {
description: 'the primary is \'_id\'',
version: 0,
type: 'object',
properties: {
_id: {
type: 'string',
primary: true
},
firstName: {
type: 'string'
}
},
required: ['firstName']
};

export const bigHuman = {
title: 'human schema',
Expand Down
Empty file modified test/unit/key-compression.test.js
100755 → 100644
Empty file.
9 changes: 9 additions & 0 deletions test/unit/primary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ config.parallel('primary.test.js', () => {
assert.ok(schema.validate(obj));
});
});

describe('positive', () => {
it('should validate when primary key is _id', () => {
const schema = RxSchema.create(schemas._idPrimary);
const obj = schemaObjects._idPrimary();
assert.ok(schema.validate(obj));
});
});

describe('negative', () => {
it('should not validate the human without primary', () => {
const schema = RxSchema.create(schemas.primaryHuman);
Expand Down
32 changes: 29 additions & 3 deletions test/unit/rx-collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,32 @@ config.parallel('rx-collection.test.js', () => {
await collection.insert(schemaObjects.human());
db.destroy();
});
it('should insert an object with _id set', async () => {
const db = await RxDatabase.create({
name: util.randomCouchString(10),
adapter: 'memory'
});
const collection = await db.collection({
name: 'idprimary',
schema: schemas._idPrimary
});
await collection.insert(schemaObjects._idPrimary());
db.destroy();
});
it('should insert human (_id given)', async () => {
const db = await RxDatabase.create({
name: util.randomCouchString(10),
adapter: 'memory'
});
const collection = await db.collection({
name: 'human',
schema: schemas.human
});
const human = schemaObjects.human();
human._id = util.randomCouchString(20);
await collection.insert(human);
db.destroy();
});
it('should insert nested human', async () => {
const db = await RxDatabase.create({
name: util.randomCouchString(10),
Expand Down Expand Up @@ -346,14 +372,14 @@ config.parallel('rx-collection.test.js', () => {
);
db.destroy();
});
it('should not insert broken human (_id given)', async () => {
it('should not insert when _id given but _id is not primary', async () => {
const db = await RxDatabase.create({
name: util.randomCouchString(10),
adapter: 'memory'
});
const collection = await db.collection({
name: 'human',
schema: schemas.human
name: 'humanfinal',
schema: schemas.humanFinal
});
const human = schemaObjects.human();
human._id = util.randomCouchString(20);
Expand Down
36 changes: 36 additions & 0 deletions test/unit/rx-schema.test.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ config.parallel('rx-schema.test.js', () => {
it('validate point', () => {
SchemaCheck.checkSchema(schemas.point);
});
it('validate _id when primary', async () => {
SchemaCheck.checkSchema({
title: 'schema',
version: 0,
properties: {
_id: {
type: 'string',
primary: true
},
firstName: {
type: 'string'
}
},
required: ['firstName']
});
});
});
describe('negative', () => {
it('break when index is no string', () => {
Expand Down Expand Up @@ -266,6 +282,26 @@ config.parallel('rx-schema.test.js', () => {
}
}), Error);
});
it('throw when _id is not primary', async () => {
assert.throws(() => SchemaCheck.checkSchema({
title: 'schema',
version: 0,
description: 'save as fieldname',
properties: {
userId: {
type: 'string',
primary: true
},
_id: {
type: 'string',
},
firstName: {
type: 'string'
}
},
required: ['firstName']
}), Error);
});
});
});
describe('.normalize()', () => {
Expand Down