schemats icon indicating copy to clipboard operation
schemats copied to clipboard

Output information on PRIMARY KEY and UNIQUE constraints

Open frankpf opened this issue 7 years ago • 12 comments

This PR implements part of #77. It modifies schemats so it outputs interfaces like this:

interface usersMeta {
   id: { type: string, primaryKey: true, unique: false },
   email: { type: string, primaryKey: false, unique: true }
}

It also adds the --verbose flag to control the output. When verbose mode is on, the meta-interfaces are simply renamed:

export type users = usersMeta

If it's off, the meta-interfaces are simplified using the helper types in https://github.com/SweetIQ/schemats/issues/77#issuecomment-353652100:

export type users = SimpleSchema<usersMeta>

One thing to note is that the non-formatted output includes some trailing commas. I couldn't test the formatted output because I was getting the following error from 'typescript-formatter:

TypeError: ts.formatting.RulesProvider is not a constructor
    at getRuleProvider (/home/frank/Code/schemats/node_modules/typescript-formatter/lib/formatter.js:18:28)
    at Object.format [as default] (/home/frank/Code/schemats/node_modules/typescript-formatter/lib/formatter.js:12:58)
    at /home/frank/Code/schemats/node_modules/typescript-formatter/lib/index.js:110:48
    at <anonymous>

frankpf avatar Feb 10 '18 22:02 frankpf

Would it be better to change the name of "verbose" parameter into something more descriptive for example "extendedTableMeta" ?

xiamx avatar Feb 12 '18 15:02 xiamx

Looks like the RuleProvider error is being fixed by https://github.com/SweetIQ/schemats/pull/82

xiamx avatar Feb 13 '18 21:02 xiamx

I updated the flag to be --exposeConstraintInfo, as that does not require the user to understand what the "meta-interfaces" are. What do you think?

frankpf avatar Feb 19 '18 13:02 frankpf

The tests/examples/README still need to be updated, I'll try to do that this week.

frankpf avatar Feb 19 '18 13:02 frankpf

Good work ! --exposeConstraintInfo sounds ok to me. Concerning my comment https://github.com/SweetIQ/schemats/issues/77#issuecomment-353607137, if you plan to add comments and original sql types in output : on second thougth, I think they should always be included in non verbose mode, because they are convenient for any user. But it' just my opinion.

abenhamdine avatar Feb 19 '18 13:02 abenhamdine

I tested adding the original SQL type to the output ({..., originalType: 'text' }).

The reason I didn't include it in this PR is that some types (at least in Postgres) are mapped internally to something else. For example, a column with a char type will be mapped to bpchar internally. The output would be originalType: 'bpchar'. IIRC there's a way to get the actual char type from Postgres, but it's a bit more involved.

I don't know if that's a big problem though. Maybe we should add it with those internal Postgres types now and then try fix the library to use more "specific" types later?

frankpf avatar Feb 19 '18 13:02 frankpf

some types (at least in Postgres) are mapped internally to something else. For example, a column with a char type will be mapped to bpchar internally. The output would be originalType: 'bpchar'

IIRC, for most of the types, udt name is pretty clear.

Anyway, I think it's better for this PR to focus on constraints only.

abenhamdine avatar Feb 19 '18 13:02 abenhamdine

I don't know if that's a big problem though. Maybe we should add it with those internal Postgres types now and then try fix the library to use more "specific" types later?

Seems reasonable.

abenhamdine avatar Feb 19 '18 14:02 abenhamdine

All tests are passing :smiley:

frankpf avatar Feb 19 '18 14:02 frankpf

Is this totally dead? I was really hoping schemats supported exporting unique/pk constraints to aid in type safe upserts!

DiskImage avatar Jul 10 '19 15:07 DiskImage

Both of the original maintainers @crispmark and I left sweetiq and to the best of my knowledge there's no one maintaining this project at the moment.

xiamx avatar Jul 10 '19 18:07 xiamx

@DiskImage Last time I ran it, this PR was working fine. It's not in master but if you really want this you can clone the branch and use it. I can help you if you have any issues with it.

I'm working on a new project using http://www.pg-structure.com/ to do the same thing here. The difference to schemats is that pg-structure creates a data structure to introspect the database, so you can use it to create all kinds of outputs, whereas schemats is "hardcoded" to output TypeScript interfaces.

frankpf avatar Jul 26 '19 01:07 frankpf