Browse Source

chore: upgrades js-repository to 0.1.1

e22m4u 1 year ago
parent
commit
875880a88c
3 changed files with 23 additions and 3 deletions
  1. 1 1
      package.json
  2. 2 2
      src/mongodb-adapter.js
  3. 20 0
      src/mongodb-adapter.spec.js

+ 1 - 1
package.json

@@ -38,7 +38,7 @@
   "peerDependencies": {
   "peerDependencies": {
     "@e22m4u/js-format": "*",
     "@e22m4u/js-format": "*",
     "@e22m4u/js-service": "*",
     "@e22m4u/js-service": "*",
-    "@e22m4u/js-repository": "~0.1.0"
+    "@e22m4u/js-repository": "~0.1.1"
   },
   },
   "devDependencies": {
   "devDependencies": {
     "@commitlint/cli": "^18.4.3",
     "@commitlint/cli": "^18.4.3",

+ 2 - 2
src/mongodb-adapter.js

@@ -601,7 +601,7 @@ export class MongodbAdapter extends Adapter {
   async create(modelName, modelData, filter = undefined) {
   async create(modelName, modelData, filter = undefined) {
     const idPropName = this._getIdPropName(modelName);
     const idPropName = this._getIdPropName(modelName);
     const idValue = modelData[idPropName];
     const idValue = modelData[idPropName];
-    if (idValue == null || idValue === '') {
+    if (idValue == null || idValue === '' || idValue === 0) {
       const pkType = this._getIdType(modelName);
       const pkType = this._getIdType(modelName);
       if (pkType !== DataType.STRING && pkType !== DataType.ANY)
       if (pkType !== DataType.STRING && pkType !== DataType.ANY)
         throw new InvalidArgumentError(
         throw new InvalidArgumentError(
@@ -662,7 +662,7 @@ export class MongodbAdapter extends Adapter {
     const idPropName = this._getIdPropName(modelName);
     const idPropName = this._getIdPropName(modelName);
     let idValue = modelData[idPropName];
     let idValue = modelData[idPropName];
     idValue = this._coerceId(idValue);
     idValue = this._coerceId(idValue);
-    if (idValue == null || idValue === '') {
+    if (idValue == null || idValue === '' || idValue === 0) {
       const pkType = this._getIdType(modelName);
       const pkType = this._getIdType(modelName);
       if (pkType !== DataType.STRING && pkType !== DataType.ANY)
       if (pkType !== DataType.STRING && pkType !== DataType.ANY)
         throw new InvalidArgumentError(
         throw new InvalidArgumentError(

+ 20 - 0
src/mongodb-adapter.spec.js

@@ -1093,6 +1093,16 @@ describe('MongodbAdapter', function () {
       expect(result[DEF_PK]).to.have.lengthOf(24);
       expect(result[DEF_PK]).to.have.lengthOf(24);
     });
     });
 
 
+    it('generates a new identifier when a value of a primary key is zero', async function () {
+      const schema = createSchema();
+      schema.defineModel({name: 'model', datasource: 'mongodb'});
+      const rep = schema.getRepository('model');
+      const result = await rep.create({[DEF_PK]: 0, foo: 'bar'});
+      expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
+      expect(typeof result[DEF_PK]).to.be.eq('string');
+      expect(result[DEF_PK]).to.have.lengthOf(24);
+    });
+
     it('generates a new identifier for a primary key of a "string" type', async function () {
     it('generates a new identifier for a primary key of a "string" type', async function () {
       const schema = createSchema();
       const schema = createSchema();
       schema.defineModel({
       schema.defineModel({
@@ -2031,6 +2041,16 @@ describe('MongodbAdapter', function () {
       expect(result[DEF_PK]).to.have.lengthOf(24);
       expect(result[DEF_PK]).to.have.lengthOf(24);
     });
     });
 
 
+    it('generates a new identifier when a value of a primary key is zero', async function () {
+      const schema = createSchema();
+      schema.defineModel({name: 'model', datasource: 'mongodb'});
+      const rep = schema.getRepository('model');
+      const result = await rep.replaceOrCreate({[DEF_PK]: 0, foo: 'bar'});
+      expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
+      expect(typeof result[DEF_PK]).to.be.eq('string');
+      expect(result[DEF_PK]).to.have.lengthOf(24);
+    });
+
     it('generates a new identifier for a primary key of a "string" type', async function () {
     it('generates a new identifier for a primary key of a "string" type', async function () {
       const schema = createSchema();
       const schema = createSchema();
       schema.defineModel({
       schema.defineModel({