typeorm: connected to postgres with typeorm

pull/1/head
leonnicolas 4 years ago
parent fd5c293c41
commit bb96724bcd
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -1,7 +1,6 @@
{
"env": {
"browser": true,
"es2021": true
"browser": true
},
"extends": [
"standard"
@ -15,5 +14,17 @@
"@typescript-eslint"
],
"rules": {
}
"new-parens": 1,
"no-bitwise": 1,
"brace-style": "warn",
"no-namespace": 0,
"indent": ["warn", 4],
"no-internal-module": 0,
"max-classes-per-file": 0,
"no-var-requires": 0,
"array-type": 0,
"semi": 1,
"semi-style": ["error", "last"]
}
}

@ -1,81 +1,83 @@
const {src, dest, watch, series, task} = require('gulp');
const ts = require('gulp-typescript');
const del = require('delete');
const eslint = require('gulp-eslint');
const nodemon = require('gulp-nodemon');
const { src, dest, watch, series, task } = require('gulp')
const ts = require('gulp-typescript')
const del = require('delete')
const eslint = require('gulp-eslint')
const nodemon = require('gulp-nodemon')
/**
* Clears the dist folder by deleting all files inside.
* @param cb
*/
function clearDist(cb) {
del('dist/*', cb);
function clearDist (cb) {
del('dist/*', cb)
}
/**
* Typescript compilation task.
* @returns {*}
*/
function compileTypescript() {
let tsProject = ts.createProject('tsconfig.json');
let tsResult = tsProject.src().pipe(tsProject());
function compileTypescript () {
const tsProject = ts.createProject('tsconfig.json')
const tsResult = tsProject.src().pipe(tsProject())
return tsResult
//.pipe(minify())
.pipe(dest('dist'));
// .pipe(minify())
.pipe(dest('dist'))
}
/**
* Task for moving all remaining file from source to dist that don't need procession.
* @returns {*}
*/
function moveRemaining() {
function moveRemaining () {
return src(['src/**/*', '!src/**/*.ts'])
.pipe(dest('dist'));
.pipe(dest('dist'))
}
function runEslint() {
return src(['scripts/*.js'])
function runEslint () {
return src(['src/**/*.ts'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
.pipe(eslint({ fix: true }))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
.pipe(eslint.failAfterError())
}
task('eslint', () => {
return src(['scripts/*.js'])
return src(['src/**/*.ts'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
.pipe(eslint({ fix: true }))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
.pipe(eslint.failAfterError())
})
task('default', series(clearDist, compileTypescript, moveRemaining));
task('default', series(clearDist, compileTypescript, moveRemaining))
task('watch', () => {
watch('**/*.ts', runEslint);
watch('**/*.ts', compileTypescript);
watch(['src/**/*', '!src/**/*.ts'], moveRemaining());
runEslint()
compileTypescript()
watch('**/*.ts', runEslint)
watch('**/*.ts', compileTypescript)
// watch(['src/**/*', '!src/**/*.ts'], moveRemaining());
nodemon({
script: 'dist/index.js',
watch: ['dist/**/*.js'],
ext: 'js'
});
});
})
})
task('watchnolint', () => {
watch('**/*.ts', compileTypescript);
watch(['src/**/*', '!src/**/*.ts'], moveRemaining());
watch('**/*.ts', compileTypescript)
// watch(['src/**/*', '!src/**/*.ts'], moveRemaining());
nodemon({
script: 'dist/index.js',
watch: ['dist/**/*.js'],
ext: 'js'
});
});
})
})

@ -0,0 +1,12 @@
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "apollo",
"database": "apollo_test",
"synchronize": true,
"logging": false,
"entities": [
"dist/model/**/*.js"
]
}

721
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^14.10.0",
"@typescript-eslint/eslint-plugin": "^4.1.0",
"@typescript-eslint/parser": "^4.1.0",
"delete": "^1.1.0",
@ -28,7 +29,12 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@types/bluebird": "^3.5.32",
"@types/validator": "^13.1.0",
"apollo-server": "^2.17.0",
"graphql": "^15.3.0"
"graphql": "^15.3.0",
"pg": "^8.3.3",
"reflect-metadata": "^0.1.13",
"typeorm": "^0.2.26"
}
}

@ -1,20 +1,16 @@
import { DataSource } from 'apollo-datasource';
import { DataSource } from 'apollo-datasource'
/**
* extended datasource to feed resolvers with data about cargoBikes
*/
export class CargoBikeAPI extends DataSource {
constructor() {
super();
// this.store = store;
}
/**
* Finds cargo bike by id
*/
async findCargoBikeById({ id }:{id: any}){
return {
async findCargoBikeById ({ id, token }:{id: any, token:string}) {
return {
id,
name: "hello world"
};
}
}
name: token
}
}
}

@ -0,0 +1,17 @@
/*
import {Sequelize} from 'sequelize-typescript';
export class DButils {
dbclient: any;
constructor(){
this.dbclient = new Sequelize({
database: 'apollo',
dialect: 'postgres',
username: 'postgres',
password: 'apollo',
storage: ':memory:',
models: [__dirname + '/models'], // or [Player, Team],
});
}
}
*/

@ -1,17 +1,13 @@
import { DataSource} from 'apollo-datasource';
import { DataSource } from 'apollo-datasource'
/**
* fetches datafrom user server, especially validates user tokens
*/
export class UserServerAPI extends DataSource{
constructor() {
super();
}
export class UserServerAPI extends DataSource {
/**
* validates user token
*/
async validateToken(token:string){
return true;
async validateToken (token:string) {
return true
}
}
}

@ -1,16 +1,22 @@
import { ApolloServer } from 'apollo-server';
import bikeresolver from './resolvers/cargobikeResolver';
import { CargoBikeAPI } from './datasources/db/cargobikeAPI';
import typeDefs from './schema/type-defs';
import { ApolloServer } from 'apollo-server'
import bikeresolver from './resolvers/cargobikeResolver'
import { CargoBikeAPI } from './datasources/db/cargobikeAPI'
import typeDefs from './schema/type-defs'
import 'reflect-metadata'
import { createConnection } from 'typeorm'
createConnection().then(async () => {
console.log('connected to db')
}).catch(error => console.log(error))
const server = new ApolloServer({
resolvers:[bikeresolver],
resolvers: [bikeresolver],
typeDefs,
dataSources: () => ({
cargoBikeAPI: new CargoBikeAPI(),
})
});
cargoBikeAPI: new CargoBikeAPI()
})
})
console.log(__dirname)
server.listen()
.then(({ url }) => console.log(`Server ready at ${url} `));
.then(({ url }) => console.log(`Server ready at ${url} `))

@ -0,0 +1,13 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'
@Entity()
export class CargoBike {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
}

@ -1,6 +1,6 @@
export default {
Query: {
cargobike: (_: any,{ id }:{id: any},{dataSources}:{dataSources: any}) =>
dataSources.cargoBikeAPI.findCargoBikeById( { id } )
},
};
cargobike: (_: any, { id, token }:{id: any, token: string}, { dataSources }:{dataSources: any}) =>
dataSources.cargoBikeAPI.findCargoBikeById({ id, token })
}
}

@ -1,4 +1,4 @@
import { gql } from 'apollo-server';
import { gql } from 'apollo-server'
export default gql`
@ -32,7 +32,7 @@ type CargoBike {
events: [BikeEvent]
equipment: [Equipment]
"Refers to equipment that is not unique. See kommentierte info tabelle -> Fragen -> Frage 2"
otherEquipment: [String]
otherEquipment: String
chainSwaps: [ChainSwap]
"Sticker State"
stickerBikeNameState: StickerBikeNameState
@ -75,6 +75,7 @@ type BikeModel {
name: String
dimensionsAndLoad: DimensionsAndLoad!
}
type ActiveMentor {
id: ID!
start: Date!
@ -254,8 +255,19 @@ type Organisation{
"registration number of association"
associationNo: String
}
type Query {
cargobike(token:String!,id:ID!): CargoBike
}
`;
type UpdateBikeResponse {
success: Boolean
message: String
bike: CargoBike
}
"for testing"
type Mutation {
addBike(id: ID!):UpdateBikeResponse
}
`

@ -10,7 +10,9 @@
"allowJs": true,
"moduleResolution": "node",
"module": "commonjs",
"esModuleInterop": true
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"include": [
"src/**/*"

Loading…
Cancel
Save