Skip to content

Instantly share code, notes, and snippets.

View Wonder2210's full-sized avatar
💭
Achiving new goals

Wonder Wonder2210

💭
Achiving new goals
View GitHub Profile
exports.seed = function (knex) {
// Deletes ALL existing entries
return knex("table name")
.del()
.then(function () {
// Inserts seed entries
return knex("table name").insert([
// here will be your data with the fields your database needs
// for example { id: 1, full_name: "luis", country_code: 58 },
,
exports.seed = function (knex) {
// Deletes ALL existing entries
return knex("users")
.del()
.then(function () {
// Inserts seed entries
return knex("users").insert([
{ id: 1, full_name: "luis", country_code: 58 },
{ id: 2, full_name: "jose", country_code: 59 },
{ id: 3, full_name: "raul", country_code: 39 },
import {Pet,User} from '../../database/models';
import {Resolvers} from '../../__generated__/generated-types';
import {UserInputError} from 'apollo-server-express';
const resolvers : Resolvers = {
Query:{
pet:async (parent,args,ctx)=>{
const pet:Pet= await Pet.query().findById(args.id);
@Wonder2210
Wonder2210 / config.ts
Created May 29, 2020 03:51
error free knex config with typescript
const default_config = {
client: 'pg',
connection: {
database: "db",
user: "user",
password: "password"
},
pool: {
min: 2,
max: 10
@Wonder2210
Wonder2210 / pet.ts
Last active May 29, 2020 04:36
resolvers
import {Pet,User} from '../../database/models';
import {Resolvers} from '../../__generated__/generated-types';
import {UserInputError} from 'apollo-server-express';
const resolvers : Resolvers = {
Query:{
pet:async (parent,args,ctx)=>{
const pet:Pet= await Pet.query().findById(args.id);
@Wonder2210
Wonder2210 / schema.gql
Last active May 29, 2020 04:38
Basic Graphql schema
enum Species{
BIRDS,
FISH,
MAMMALS,
REPTILES
}
type User {
id: Int!
full_name: String
@Wonder2210
Wonder2210 / Pet.ts
Created May 29, 2020 03:40
Models for PG-Graphql-TS
import {Model} from 'objection';
import {Species,Maybe} from '../../__generated__/generated-types';
import User from './User';
class Pet extends Model{
static tableName = "pets";
id! : number;
name?: Maybe<string>;
specie?: Maybe<Species>;
@Wonder2210
Wonder2210 / knexfile.ts
Created May 29, 2020 03:34
Basic knex config
require('ts-node/register');
module.exports = {
development:{
client: 'pg',
connection: {
database: "my_db",
user: "username",
password: "password"
@Wonder2210
Wonder2210 / migration.ts
Created May 29, 2020 03:30
Easy database Schema migration with knex
import * as Knex from "knex";
export async function up(knex: Knex): Promise<any> {
return knex.schema.createTable('users',(table:Knex.CreateTableBuilder)=>{
table.increments('id');
table.string('full_name',36);
table.integer('country_code');
table.timestamps(true,true);
@Wonder2210
Wonder2210 / tsconfig.json
Created May 29, 2020 03:27
Tsconfig for most cases
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
/* Concatenate and emit output to single file. */
"outDir": "dist", /* Redirect output structure to the directory. */
"rootDir": "src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */