Giter Club home page Giter Club logo

Comments (7)

mkovel avatar mkovel commented on July 21, 2024 1

@m-radzikowski yeap, I've already created MR due to comas issue #29

from dynamodb-onetable.

mobsense avatar mobsense commented on July 21, 2024

Thanks for the commas and pk/sk missing the type. For JavaScript this defaults to String, but agree it is needed by TypeScript to drive the typing.

Thanks. Can you explain the "User.balance field type Number is incompatible"? Are you saying the Number type is not working? I don't think the "balance" field is used as pk/sk in any of the examples.

Thanks.

from dynamodb-onetable.

m-radzikowski avatar m-radzikowski commented on July 21, 2024

Thanks. Can you explain the "User.balance field type Number is incompatible"? Are you saying the Number type is not working? I don't think the "balance" field is used as pk/sk in any of the examples.

@mobsense yes, when schema in Table has a field with Number type, you get an error I posted:

Type '{ type: NumberConstructor; default: number; }' is not assignable to type 'OneFieldSchema'.

It does not have to be used anywhere, just the declaration alone is problematic.

from dynamodb-onetable.

mobsense avatar mobsense commented on July 21, 2024

It looks like the issue isn't with Number but with the default type for numbers.

Can you please try the following patch

type OneFieldSchema = {
    crypt?: boolean,
    default?: (() => any) | string | number | boolean | object,

This changes the default type to permit number and boolean as well.

from dynamodb-onetable.

markandrus avatar markandrus commented on July 21, 2024

Hey y'all, I do believe this is still an issue. I wanted to try out dynamodb-onetable in TypeScript an hit type errors immediately. Riffing off of one of the examples, I wrote this:

const oneFieldSchema: OneFieldSchema = { type: String, value: 'card:${id}' }
const oneModelSchema: OneModelSchema = { pk: oneFieldSchema }
new Model(table, 'Card', {
  fields: oneModelSchema
})

However, this gives a type error at fields, which is supposed to have type

{
  [key: string]: OneModelSchema
}

as defined here. It seems to me that there is one too many levels of nesting in the TypeScript definitions, and that fields should have type OneModelSchema directly.

from dynamodb-onetable.

mobsense avatar mobsense commented on July 21, 2024

Hi Mark, thanks for digging into this. I've reopened the issue and have tested and applied your pull request.

Testing around the issue, and creating some unit tests for this revealed a couple of other issues that we are addressing.

We've been migrating our internal tests to use Jest and add to the repo. I expect to push this tomorrow.

Generally, we find it easier to define schema centrally via the Table constructor. You can certainly do it per model, but I think it is easier via the schema.models in the table constructor and then use the following to create the type and the model.

type CardType = Entity<typeof DefaultSchema.models.Card>
let Card: Model<CardType> = table.getModel('Card')

I've mocked up your code sample and used it in our tests. I've defined the schema as a plain object. I've then derived the CardType from the schema. By giving to the Model constructor, the returned model "Card" is fully typed. and Card.create is fully typed.

With your patch (thank you), this works well.

import {AWS, Client, Entity, Model, Table, dump, print} from './utils/init'

test('Debug', async () => {

    const table = new Table({
        name: 'TypeScriptDebugTestTable',
        client: Client,
        schema: {
            indexes: {primary: {hash: 'pk'}},
            models: {},
        }
    })
    await table.createTable()

    const CardSchema = {
        pk:     { type: String, value: 'card:${id}' },
        id:     { type: String },
        issuer: { type: String },
    }
    type CardType = Entity<typeof CardSchema>

    let Card = new Model<CardType>(table, 'Card', { fields: CardSchema })

    let card = await Card.create({id: 'x4567', issuer: 'visa'}, {hidden: true})

    console.log(card.issuer)
    //. Will fail  console.log(card.x)

    expect(card.issuer).toBe('visa')
    expect(card.pk).toBe('card:x4567')

    await table.deleteTable('DeleteTableForever')
})

from dynamodb-onetable.

markandrus avatar markandrus commented on July 21, 2024

Thanks a lot, @mobsense!

from dynamodb-onetable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.