0

I want to create a databease schema using the TypeScript. It's pretty simple, but I can't, or don't know how, there to define the key generator. In the User Guide, for example, such a code snippet(using JavaScript):

var schema = {
  ...
indexes: [
  {
    name: 'name', // index for case in-sensitive sorting
    generator: function(obj) {
      var name = obj.first + ' ' + obj.last;
      return name.toLowerCase();
    }
  }
],

Can I define this generator using the library ydn.db-1.0.ts (downloaded from the YDN-DB server) or ydn-db.d.ts(downloaded from DefinitelyTyped)?

For example I wrote this ts file:

/// <reference path="typings/ydn-db/ydn-db.d.ts" />
module YdnDbTest
{
  module DatabaseHelper
  {
    interface Place
    {
      Name: string;
      Location: string;
    }

    var AppDbSchema: DatabaseSchemaJson =
    {
      stores:
      [
        {
          name: "AppDbStore",
          keyPath: "Name",
          indexes:
          [
            {
              name: "idxLocation",
              keyPath: "Location",
              unique: true,
            }
          ]
        }
      ]
   }
 }

, and I would like for the Location wrote a generator. I would like to write something like:

        ...
        indexes:
        [
          {
            name: "idxLocation",
            generator: (record: Place): string => 
            { return record.Location.toLowerCase() },
            unique: true,
          }
        ]
        ...

Like I wrote generator for master key(Name), but it probably is not possible.

Stefan Babos
  • 296
  • 4
  • 9
  • `ydn.db-1.0.ts` contains only a list of definitions, not the source code. So you still have to load `ydn.db-is-core-qry.js` file. – MartyIX Feb 02 '16 at 07:47
  • ydn.db-1.0.ts is the definition file, which should I use for writing the TypeScript code. I believe that file ydn.db-is-core-qry.js at this moment I don't need. – Stefan Babos Feb 02 '16 at 08:15
  • Yes, you should add to your TypeScript code at the top of a file `///` to load those TypeScript definitions and then you can use types defined in the file. However, you still need to reference the `.js` file at some point otherwise it won't work in your browser. – MartyIX Feb 02 '16 at 08:23
  • Yes, that is correct, but I believe that it is not related to the question. – Stefan Babos Feb 02 '16 at 08:28

1 Answers1

0

Yes, this can be solved using ydn-db.d.ts(from DefinitelyTyped). Library ydn.db-1.0.ts is broken(duplicate definition, instead of boolean bool is mentioned, etc.). In the case of the library ydn-db.d.ts(from DefinitelyTyped) the generator is overlooked. Using extension of the IndexSchemaJson interface I can to write this code, including the generator:

/// <reference path="typings/ydn-db/ydn-db.d.ts" />
module YdnDbTest
{
  module DatabaseHelper
  {
    interface Place
    {
      Name: string;
      Location: string;
    }

    interface IndexSchemaJsonB extends IndexSchemaJson
    {
      generator?: (row: any) => any;
    }

    var AppDatabaseSchema: DatabaseSchemaJson =
    {
      stores:
      [
        {
          name: "StoreA",
          keyPath: "Name",
          indexes:
          [
            <IndexSchemaJsonB>
            {
              name: "idxLocation",
              keyPath: "Location",
              generator: (row: Place): string =>
                { return row.Location.toLocaleUpperCase(); }
            }
          ]
        }
      ]
   }    
 }

}

Stefan Babos
  • 296
  • 4
  • 9