Giter Club home page Giter Club logo

personalizer's Introduction

Getting started

Set up Apollo client with Nextjs and Apollo server GraphQL into single Express

Usage

Make sure you have Node.js 13+ as specified in package.json "engines" object.

yarn install
yarn dev

Goto http://localhost:3001/graphql to generate database structure

Run seed

Go to graphql folder and run command line

npx sequelize-cli db:seed --seed demo-role.js
npx sequelize-cli db:seed --seed demo-users.js
npx sequelize-cli db:seed --seed demo-permissions.js

Goto http://localhost:3001/api/auth/signin input Email to signin

Open http://localhost:3001 to Enjoy!

System Administrator

Goto http://localhost:3001/admin/users to manage users

SQL Sequelizer

Auto sync database

const server = new ApolloServer({
  ...// Sync database
  sequelize.sync(),
});

Graphql

Create User

mutation{
  createUser(data:{name:"Admin", email:"[email protected]", password: "1"}){
      id
      name
      jwt
    }
  }

Login user

query {
   loginUser(email:"[email protected]", password:"1") {
    name
  }
}

Query Role

query {
  getRole(where:{id:1}){
    id,
    name
  }
}

Graphql generator

Restart graphql server at http://localhoast:3001/graphql Go to packages/graphql run

yarn generate

Copy graphql.schema.json to client/services/graphql.schema.json Note documents must start by a query

Metadata

Model

Define virtual columns at model.ts

{
  @Column(DataType.VIRTUAL)
  link: string;
}

Schema

Must be include metadata at type FooModel

type Foo {
  metadata: [FooMeta]

  fooMeta1,
  fooMeta2,
  ...
}
type FooMeta {
  foo: Foo
}
type FooMetaInput {
  foo_id
  key
  value
}

Define FooMeta, FooMetaInput

metadata: [FooMeta]

Query Sequelize

before function

findOptions.include = [{ model: FooMeta }];

after function for a líst foo

const rows = foos.map(u => metadataToField(u, 'metadata'));

after function for a foo

const transferData = metadataToField(user, 'metadata');
return transferData;

Mutation sequelize

Create or update a job

// Update taxonomies
if (job && taxonomies) {
  const terms = taxonomies.map(termId => ({
    term_taxonomy_id: termId,
    ref_id: job.id,
  }));
  await JobTerm.bulkCreate(terms);
}

// Metadata
if (job && metadata) {
  const meta = metadata.map(x => ({
    ...x,
    job_id: job.id,
  }));

  await JobMeta.bulkCreate(meta);
}
findOptions.where = { id: job.id };
return findOptions;

Form

Add metadata or taxonomies at field

      <Form.Item
        name={['job', 'dueDate']}
        label={t('jobCreateform.label.dueDate')}
      >
        <DatePicker  />
      </Form.Item>

      <Form.Item
        name={['metadata', 'link']}
        label={t('jobCreateform.label.link')}
      >
        <Input type="link" />
      </Form.Item>

      <Form.Item
        name={['taxonomies', 'job_status']}
        label={t('jobCreateform.label.status')}
      >
        <ComboBoxTaxonomy type={TaxonomyType.Job_Status} />
      </Form.Item>

at submit method, pass metadata and taxonomies

upsertJob({
  variables: {
    job,
    metadata,
    taxonomies,
  },
});

load detail

const formSetFields = job => {
  form.setFields([
    // job
    { name: ['job', 'title'], value: job.title },
    { name: ['job', 'link'], value: job.link },
    { name: ['job', 'description'], value: job.description },
    { name: ['job', 'publishDate'], value: job.publishDate },
    { name: ['job', 'dueDate'], value: job.image },

    // taxonomies
    { name: ['taxonomies', 'job_priority'], value: job.job_priority },
    { name: ['taxonomies', 'job_status'], value: job.job_status },

    // metadata
    { name: ['metadata', 'link'], value: job.link },
  ]);
};

Taxonomy

Model

Follow below, must have ForeignKey for TermTaxnomy, Foo

export class JobTerm extends Model<JobTerm> {
  @BelongsTo(() => TermTaxonomy)
  termTaxonomy: TermTaxonomy;

  @ForeignKey(() => TermTaxonomy)
  @Column
  term_taxonomy_id: number;

  @Column
  order: number;

  // job
  @Column
  @ForeignKey(() => Job) // only change Job
  ref_id: number;

  @BelongsTo(() => Job) // only change Job
  job: Job;
}

Graphql

Follow below, must have termTaxnomies field

type JobTerm {
  ...
  termTaxonomies: TermTaxonomy
}

Query single

findOptions.include = [
  { model: JobMeta },
  {
    model: JobTerm,                                             // FooTerm
    require: true,
    include: [
      {
        model: TermTaxonomy,
        where: { taxonomy: ['job_priority', 'job_status'] },    // all of taxonomies fields
        require: true,
        include: [
          {
            model: Term,
            require: true,
          },
        ],
      },
    ],
  },
];

personalizer's People

Contributors

woowebsite avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.