Skip to content

Commit

Permalink
break: Model classes and interace
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-smartive committed Oct 30, 2023
1 parent e1cb8b9 commit 7986b34
Show file tree
Hide file tree
Showing 56 changed files with 5,932 additions and 1,295 deletions.
12 changes: 2 additions & 10 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@
"extends": ["@smartive/eslint-config"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.jest.json"
"project": "./tsconfig.eslint.json"
},
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-floating-promises": ["error"],
"no-constant-binary-expression": "error",
"no-console": ["error", { "allow": ["info", "warn", "error", "trace", "time", "timeEnd"] }]
},
"overrides": [
{
"files": ["*.ts", "*.tsx"],
"parserOptions": {
"project": ["./tsconfig.eslint.json"]
}
}
]
}
}
6 changes: 6 additions & 0 deletions .gqmrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"modelsPath": "tests/utils/models.ts",
"generatedFolderPath": "tests/generated",
"graphqlQueriesPath": "tests",
"gqlModule": "../../../src"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ npm bootstrap
```

```
npm run lint
npm test
```

```
Expand Down
31 changes: 31 additions & 0 deletions knexfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { DateTime } from 'luxon';
import { types } from 'pg';

const dateOids = { date: 1082, timestamptz: 1184, timestamp: 1114 };
for (const oid of Object.values(dateOids)) {
types.setTypeParser(oid, (val) => DateTime.fromSQL(val));
}

const numberOids = { int8: 20, float8: 701, numeric: 1700 };
for (const oid of Object.values(numberOids)) {
types.setTypeParser(oid, Number);
}

const config = {
client: 'postgresql',
connection: {
host: process.env.DATABASE_HOST,
database: process.env.DATABASE_NAME,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
},
migrations: {
tableName: 'knex_migrations',
},
pool: {
min: 0,
max: 30,
},
} as const;

export default config;
127 changes: 127 additions & 0 deletions migrations/20230912185644_setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { Knex } from 'knex';

export const up = async (knex: Knex) => {
await knex.raw(`CREATE TYPE "someEnum" AS ENUM ('A','B','C')`);

await knex.raw(`CREATE TYPE "role" AS ENUM ('ADMIN','USER')`);

await knex.raw(`CREATE TYPE "reactionType" AS ENUM ('Review','Question','Answer')`);

await knex.schema.createTable('User', (table) => {
table.uuid('id').notNullable().primary();
table.string('username', undefined).nullable();
table.enum('role', null as any, {
useNative: true,
existingType: true,
enumName: 'role',
}).nullable();
});

await knex.schema.createTable('AnotherObject', (table) => {
table.uuid('id').notNullable().primary();
table.string('name', undefined).nullable();
table.uuid('myselfId').nullable();
table.foreign('myselfId').references('id').inTable('AnotherObject').onDelete('CASCADE');
table.boolean('deleted').notNullable().defaultTo(false);
table.timestamp('deletedAt').nullable();
table.uuid('deletedById').nullable();
table.foreign('deletedById').references('id').inTable('User').onDelete('CASCADE');
});

await knex.schema.createTable('SomeObject', (table) => {
table.uuid('id').notNullable().primary();
table.string('field', undefined).nullable();
table.uuid('anotherId').notNullable();
table.foreign('anotherId').references('id').inTable('AnotherObject').onDelete('CASCADE');
table.decimal('float', 1, 1).notNullable();
table.specificType('list', '"someEnum"[]').notNullable();
table.integer('xyz').notNullable();
table.timestamp('createdAt').notNullable();
table.uuid('createdById').notNullable();
table.foreign('createdById').references('id').inTable('User').onDelete('CASCADE');
table.timestamp('updatedAt').notNullable();
table.uuid('updatedById').notNullable();
table.foreign('updatedById').references('id').inTable('User').onDelete('CASCADE');
table.boolean('deleted').notNullable().defaultTo(false);
table.timestamp('deletedAt').nullable();
table.uuid('deletedById').nullable();
table.foreign('deletedById').references('id').inTable('User').onDelete('CASCADE');
});

await knex.schema.createTable('SomeObjectRevision', (table) => {
table.uuid('id').notNullable().primary();
table.uuid('someObjectId').notNullable();
table.uuid('createdById').notNullable();
table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now(0));
table.boolean('deleted').notNullable();
table.uuid('anotherId').notNullable();
table.foreign('anotherId').references('id').inTable('AnotherObject').onDelete('CASCADE');
table.integer('xyz').notNullable();
});

await knex.schema.createTable('Reaction', (table) => {
table.uuid('id').notNullable().primary();
table.enum('type', null as any, {
useNative: true,
existingType: true,
enumName: 'reactionType',
}).notNullable();
table.uuid('parentId').nullable();
table.foreign('parentId').references('id').inTable('Reaction').onDelete('CASCADE');
table.string('content', undefined).nullable();
table.timestamp('createdAt').notNullable();
table.uuid('createdById').notNullable();
table.foreign('createdById').references('id').inTable('User').onDelete('CASCADE');
table.timestamp('updatedAt').notNullable();
table.uuid('updatedById').notNullable();
table.foreign('updatedById').references('id').inTable('User').onDelete('CASCADE');
table.boolean('deleted').notNullable().defaultTo(false);
table.timestamp('deletedAt').nullable();
table.uuid('deletedById').nullable();
table.foreign('deletedById').references('id').inTable('User').onDelete('CASCADE');
});

await knex.schema.createTable('ReactionRevision', (table) => {
table.uuid('id').notNullable().primary();
table.uuid('reactionId').notNullable();
table.uuid('createdById').notNullable();
table.timestamp('createdAt').notNullable().defaultTo(knex.fn.now(0));
table.boolean('deleted').notNullable();
table.string('content', undefined).nullable();
});

await knex.schema.createTable('Review', (table) => {
table.uuid('id').notNullable().primary();
table.foreign('id').references('id').inTable('Reaction').onDelete('CASCADE');
table.decimal('rating', undefined, undefined).nullable();
});

await knex.schema.createTable('ReviewRevision', (table) => {
table.uuid('id').notNullable().primary();
table.decimal('rating', undefined, undefined).nullable();
});

};

export const down = async (knex: Knex) => {
await knex.schema.dropTable('ReviewRevision');

await knex.schema.dropTable('Review');

await knex.schema.dropTable('ReactionRevision');

await knex.schema.dropTable('Reaction');

await knex.schema.dropTable('SomeObjectRevision');

await knex.schema.dropTable('SomeObject');

await knex.schema.dropTable('AnotherObject');

await knex.schema.dropTable('User');

await knex.raw('DROP TYPE "reactionType"');
await knex.raw('DROP TYPE "role"');
await knex.raw('DROP TYPE "someEnum"');
};

92 changes: 91 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@
"sideEffecs": false,
"scripts": {
"bootstrap": "npm ci && npm run generate",
"generate": "npm run generate:index-files",
"generate": "npm run generate:index-files && npm run build:bin && npm run generate:gqm-stuff && npm run generate:setup-migration",
"generate:index-files": "cti create ./src --excludes bin --withoutbackup",
"generate:gqm-stuff": "npx gqm generate",
"generate:setup-migration": "npm run build:bin && npx gqm generate-migration setup 20230912185644",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"deps": "docker-compose up",
"test": "npm run lint && npm run test:all && npm run build",
"test:all": "jest tests --no-cache --no-watchman",
"test:unit": "jest tests/unit --no-cache --no-watchman",
"test:api": "jest tests/api --no-cache --no-watchman",
"generate-migration": "esbuild tests/utils/generate-migration.ts --bundle --platform=node --outdir=tmp --out-extension:.js=.cjs --format=cjs --packages=external && node tmp/generate-migration.cjs",
"test:all": "jest tests --no-cache --no-watchman --setupFiles dotenv/config",
"test:unit": "jest tests/unit --no-cache --no-watchman --setupFiles dotenv/config",
"test:api": "jest tests/api --no-cache --no-watchman --setupFiles dotenv/config",
"clean": "del-cli dist/**",
"prebuild": "npm run clean",
"build": "npm run build:esm && npm run build:cjs && npm run build:bin",
Expand Down Expand Up @@ -67,6 +68,7 @@
"@types/jest": "29.5.5",
"@types/lodash": "4.14.199",
"@types/luxon": "3.3.2",
"@types/pg": "8.10.2",
"@types/uuid": "9.0.4",
"create-ts-index": "1.14.0",
"del-cli": "5.1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/api/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const execute = async ({
additionalResolvers?: IResolvers<any, any>;
body: any;
} & Omit<Context, 'document'>) => {
const document = generate(ctx.rawModels);
const document = generate(ctx.models);

const generatedResolvers = getResolvers(ctx.models);

Expand Down
Loading

0 comments on commit 7986b34

Please sign in to comment.