Skip to content

Commit 4d8f1f9

Browse files
authored
Story - 5.1 (#20)
* initial commit * INcremental improvement in createRelation() code. Early support for circular relations * fix remote ts issue * fix store unprovisioning * public api * type fixes, simplifications, etc. * provide default for version * ts-config dist improvements * set to beta package json * tsconfig fix attempt 1 * update * find the needle in the haystack * sort out all ts configs. Tidy up, * add in jsx-a11y again * version to 5.1.0, non-beta.
1 parent 42f12f2 commit 4d8f1f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+827
-510
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"settings": {
4141
"import/resolver": {
4242
"node": {
43-
"extensions": [".ts", ".tsx", ".js"]
43+
"extensions": [".ts", ".js"]
4444
}
4545
}
4646
},

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
node-version: [12.x, 14.x, 16.x]
12+
node-version: [14.x, 16.x, 18.x]
1313
steps:
1414
- uses: actions/checkout@v3
1515
- name: Use Node.js ${{ matrix.node-version }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/build
33
/dist
44
/build-test
5+
/build-ts
56
/build-integration-test
67
.DS_Store
78
/debug.log

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ Start by viewing the [Getting Started](https://github.com/samhuk/ts-pg-orm/wiki/
3939
Define data formats, relations, and database connectivity to create type-safe auto-completing data stores:
4040

4141
```typescript
42+
import { createDataFormat, createTsPgOrm, ... } from 'ts-pg-orm'
4243
const userDF = createDataFormat(...)
43-
const orm = createTsPgOrm([userDF, ...] as const).setRelations([...] as const)
44-
await orm.initDbClient({ host: 'localhost', port: 5432, ... })
44+
const ORM = createTsPgOrm([userDF, ...] as const).setRelations([...] as const)
45+
const orm = await ORM.initDbClient({ host: 'localhost', port: 5432, ... })
4546
await orm.provisionStores()
4647
```
4748

@@ -57,6 +58,7 @@ const userFound = await orm.stores.user.get({
5758
relations: { // Recursively include related data
5859
userGroups: {
5960
query: { ... },
61+
relations: { ... }
6062
},
6163
},
6264
})
@@ -91,8 +93,8 @@ export type UserGroupRecord = ToRecord<typeof USER_GROUP_DFD>
9193
Use type-enforced sql information to create bespoke SQL statements:
9294
9395
```typescript
94-
const sql = orm.dataFormats.user.sql
95-
const customUserSql = `select ${sql.columnNames.name} from ${sql.tableName}`
96+
const userSql = ORM.dataFormats.user.sql
97+
const customUserSql = `select ${userSql.columnNames.name} from ${userSql.tableName}`
9698
```
9799

98100
## Examples

package.json

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-pg-orm",
3-
"version": "5.0.1",
3+
"version": "5.1.0",
44
"description": "Typescript PostgreSQL ORM",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -27,15 +27,14 @@
2727
"tests": "npm run clean-tests && npm run build-tests && npm run run-tests",
2828
"tests-ci": "npm run clean-tests && npm run build-tests && npm run run-tests-ci",
2929

30-
"lint": "eslint -c .eslintrc.json ./src --ext .ts,.tsx",
31-
"lint-errors-only": "eslint -c .eslintrc.json ./src --ext .ts,.tsx --quiet",
30+
"lint": "eslint -c .eslintrc.json ./src --ext .ts",
31+
"lint-errors-only": "eslint -c .eslintrc.json ./src --ext .ts --quiet",
3232

33-
"clean": "rimraf ./dist",
34-
"build-ts": "npm run clean && tsc -p ./tsconfig.dist.json",
33+
"clean-ts-dist": "rimraf ./dist",
34+
"build-ts-dist": "npm run clean-ts-dist && tsc -p ./tsconfig.dist.json",
3535

36-
"check": "npm rum lint-errors-only && npm run build-ts && npm run tests && echo Done!",
37-
38-
"start": "tsc -p ./src/tsconfig.json --watch",
36+
"clean-ts": "rimraf ./build-ts",
37+
"build-ts": "npm run clean-ts && tsc -p ./tsconfig.all.json",
3938

4039
"clean-examples": "rimraf ./build-examples",
4140
"build-examples": "tsc -p ./tsconfig.examples.json",
@@ -44,7 +43,9 @@
4443
"article-api": "npm run clean-examples && npm run build-examples && npm run run-article-api",
4544
"real-db-test": "npm run clean-examples && npm run build-examples && npm run run-real-db-test",
4645

47-
"clean-all": "npm run clean-unit-tests && npm run clean-integration-tests && npm run clean && npm run clean-examples"
46+
"clean": "npm run clean-unit-tests && npm run clean-integration-tests && npm run clean-ts && npm run clean-ts-dist && npm run clean-examples",
47+
48+
"check": "npm rum lint-errors-only && npm run build-ts && npm run tests && echo Done!"
4849
},
4950
"repository": "https://github.com/samhuk/ts-pg-orm",
5051
"author": "",
@@ -55,25 +56,25 @@
5556
"dependencies": {
5657
"@samhuk/data-filter": "^1.1.1",
5758
"@samhuk/data-query": "^1.2.1",
58-
"simple-pg-client": "^1.0.5"
59+
"simple-pg-client": "^1.1.0"
5960
},
6061
"devDependencies": {
61-
"@types/jest": "^28.1.1",
62-
"@types/node": "^17.0.19",
62+
"@types/jest": "^29.1.2",
63+
"@types/node": "^18.8.3",
6364
"@types/pg": "^8.6.5",
64-
"@typescript-eslint/eslint-plugin": "^5.12.1",
65-
"@typescript-eslint/parser": "^5.12.1",
66-
"babel-jest": "^28.1.1",
67-
"concurrently": "^7.0.0",
65+
"@typescript-eslint/eslint-plugin": "^5.40.0",
66+
"@typescript-eslint/parser": "^5.40.0",
67+
"babel-jest": "^29.1.2",
68+
"concurrently": "^7.4.0",
6869
"env-cmd": "^10.1.0",
69-
"eslint": "^8.9.0",
70+
"eslint": "^8.25.0",
7071
"eslint-config-airbnb": "^19.0.4",
71-
"eslint-plugin-import": "^2.25.4",
72-
"eslint-plugin-jsx-a11y": "^6.5.1",
73-
"eslint-plugin-react": "^7.30.1",
74-
"jest": "^28.1.1",
72+
"eslint-plugin-import": "^2.26.0",
73+
"eslint-plugin-jsx-a11y": "^6.6.1",
74+
"eslint-plugin-react": "^7.31.9",
75+
"jest": "^29.1.2",
7576
"rimraf": "^3.0.2",
76-
"ts-jest": "^28.0.4",
77-
"typescript": "^4.5.5"
77+
"ts-jest": "^29.0.3",
78+
"typescript": "^4.8.4"
7879
}
7980
}

src/dataFormat/field.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { camelCaseToSnakeCase, quote } from '../helpers/string'
22
import { Field, FieldOptions, FieldsOptions } from './types/field'
33

4+
/**
5+
* For creating a reusable group of fields that can be used across Data Formats.
6+
*
7+
* This is useful when multiple of your Data Formats share some common fields, like
8+
* "dateCreated", "dataDeleted", "name", and so on.
9+
*/
410
export const createCommonFields = <T extends FieldsOptions>(v: T): T => v
511

612
export const createField = (fieldName: string, fieldOptions: FieldOptions): Field => {

src/dataFormat/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { mapDict, toDict } from '../helpers/dict'
22
import { camelCaseToSnakeCase, capitalize, quote } from '../helpers/string'
3+
import { StringKeysOf } from '../helpers/types'
34
import { NonManyToManyRelationList } from '../relations/types'
45
import { filterForCreateRecordField } from './createRecord'
56
import { createField } from './field'
@@ -18,7 +19,7 @@ export const createDataFormat = <
1819
fieldsOptions: TFieldsOptions,
1920
pluralizedName?: TPluralizedName,
2021
tableName?: string,
21-
): DataFormat<TName, TPluralizedName, TFieldsOptions, FieldSubSetsOptions<(keyof TFieldsOptions) & string>> => {
22+
): DataFormat<TName, TPluralizedName, TFieldsOptions, FieldSubSetsOptions<StringKeysOf<TFieldsOptions>>> => {
2223
const unquotedCols = mapDict(fieldsOptions, (f, fName) => f.columnName ?? camelCaseToSnakeCase(fName))
2324
const cols = mapDict(unquotedCols, unquotedColName => quote(unquotedColName))
2425
const unquotedTableName = tableName ?? camelCaseToSnakeCase(name)
@@ -36,10 +37,10 @@ export const createDataFormat = <
3637
capitalizedPluralizedName: capitalize(_pluralizedName) as any,
3738
fields: fields as any,
3839
fieldList: fieldList as any,
39-
fieldNameList: Object.keys(fieldsOptions),
40+
fieldNameList: Object.keys(fieldsOptions) as any,
4041
fieldSubSets: {}, // TODO later
4142
fieldRefs: fieldRefs as any,
42-
createRecordFieldNameList: createRecordFields.map(f => f.name),
43+
createRecordFieldNameList: createRecordFields.map(f => f.name) as any,
4344
sql: {
4445
cols: cols as any,
4546
unquotedCols: unquotedCols as any,
@@ -48,7 +49,7 @@ export const createDataFormat = <
4849
tableName: _tableName,
4950
unquotedTableName,
5051
createRecordCols: toDict(createRecordFields, item => ({ key: item.name, value: item.sql.columnName })) as any,
51-
createRecordColumnNameList: createRecordFields.map(f => name),
52+
createRecordColumnNameList: createRecordFields.map(f => f.sql.columnName),
5253
createTableSql: (relations: NonManyToManyRelationList) => createTableSql(_tableName, fieldList, relations),
5354
dropTableSql: `drop table if exists ${_tableName};`,
5455
},

src/dataFormat/sql.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,6 @@ const createColumnsSql = (
175175
.join(',\n ')
176176
)
177177

178-
const createForeignKeysSql = (
179-
relations: Relation<RelationType.ONE_TO_MANY | RelationType.ONE_TO_ONE>[],
180-
): string => (
181-
relations
182-
.map(r => r.sql.foreignKeySql)
183-
.join(',\n')
184-
)
185-
186178
export const createTableSql = (
187179
quotedTableName: string,
188180
fieldList: FieldList,
@@ -191,11 +183,10 @@ export const createTableSql = (
191183
const oneToOneRelations = relations
192184
?.filter(r => r.type === RelationType.ONE_TO_ONE) as Relation<RelationType.ONE_TO_ONE>[] ?? []
193185
const columnsSql = createColumnsSql(fieldList, oneToOneRelations)
194-
const foreignKeysSql = relations != null ? createForeignKeysSql(relations) : ''
195186

196187
return `create table if not exists public.${quotedTableName}
197188
(
198-
${[columnsSql, foreignKeysSql].filter(s => s != null && s.length > 0).join(',\n')}
189+
${columnsSql}
199190
)
200191
201192
tablespace pg_default;

src/dataFormat/types/dataType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export type DataTypeToSubType = {
5151
[DataType.JSON]: JsonSubType
5252
}
5353

54+
export type AnySubType = StrSubType | NumSubType | EpochSubType | JsonSubType
55+
56+
export type AnyDataType = DataType.STR | DataType.NUM | DataType.BOOL | DataType.EPOCH | DataType.JSON
57+
5458
export enum ThreeStepNumberSize {
5559
SMALL,
5660
REGULAR,

src/dataFormat/types/field.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TypeDependantBaseIntersection, ValuesUnionFromDict } from '../../helpers/types'
22
import {
3+
AnySubType,
34
DataType,
45
DataTypeToDefaultValueType,
56
DataTypeToSubType,
@@ -20,7 +21,7 @@ type FieldToDefaultValueType<TField extends Field> = TField extends { type: Data
2021
: DataTypeToDefaultValueType[TField['type']]
2122

2223
type DataTypeToOptions<
23-
TSubType extends ValuesUnionFromDict<DataTypeToSubType> = ValuesUnionFromDict<DataTypeToSubType>
24+
TSubType extends AnySubType = AnySubType
2425
> = {
2526
// -- Str
2627
[DataType.STR]: TypeDependantBaseIntersection<StrSubType, {
@@ -132,7 +133,7 @@ export type Fields<TFieldsOptions extends FieldsOptions = FieldsOptions> = {
132133
? TFieldsOptions[TFieldName]['subType']
133134
: undefined,
134135
TFieldsOptions[TFieldName],
135-
TFieldName & string
136+
TFieldName
136137
>
137138
}
138139

0 commit comments

Comments
 (0)