2.0.0-beta.21 • Published 2 months ago

@mercury-js/core v2.0.0-beta.21

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@mercury-js/core

Getting started

npm install @mercury-js/core

Usage

// route.ts for nextJS
// for express you can directly use apollo server setup
import { startServerAndCreateNextHandler } from '@as-integrations/next';
import mercury from '@mercury-js/core';
import { ApolloServer } from '@apollo/server';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { applyMiddleware } from 'graphql-middleware';
import './models';
import './profiles';
import './hooks';

mercury.connect(process.env.DB_URL || 'mongodb://localhost:27017/mercury');

mercury.addGraphqlSchema(
  `
  type Query {
    hello: String
  }
`,
  {
    Query: {
      hello: (root: any, args: any, ctx: any, resolveInfo: any) => {
        return 'Hello World!';
      },
    },
  }
);

const schema = applyMiddleware(
  makeExecutableSchema({
    typeDefs: mercury.typeDefs,
    resolvers: mercury.resolvers as unknown as IResolvers<
      any,
      GraphQLResolveInfo
    >[],
  })
);

const server = new ApolloServer({
  schema,
});

const handler = startServerAndCreateNextHandler(server, {
  context: async (req, res) => ({
    ...req,
    user: {
      id: '1',
      profile: 'Admin',
    },
  }),
});

export const mercuryInstance = mercury;

export async function GET(request: any) {
  return handler(request);
}

export async function POST(request: any) {
  return handler(request);
}

Models

// User.model.ts
import mercury from '@mercury-js/core';

export const User = mercury.createModel(
  'User',
  {
    name: {
      type: 'string',
    },
    account: {
      type: 'relationship',
      ref: 'Account',
    },
    test: {
      type: 'string',
    },
    testv: {
      type: 'virtual',
      ref: 'Account',
      localField: 'account',
      foreignField: '_id',
      many: false,
    },
  },
  {}
);

// Account.model.ts
import mercury from '@mercury-js/core';
export const AccountSchema = {
  name: {
    type: 'string',
  },
  user: {
    type: 'relationship',
    ref: 'User',
  },
};

export const Account = mercury.createModel('Account', AccountSchema, {});

// index.ts

export { User } from './User.model';
export { Account } from './Account.model';

Profiles

// User.profile.ts
import mercury from '@mercury-js/core';

const rules = [
  {
    modelName: 'User',
    access: {
      create: false,
      read: true,
      update: false,
      delete: false,
    },
  },
  {
    modelName: 'Account',
    access: {
      create: false,
      read: true,
      update: false,
      delete: false,
    },
    fieldLevelAccess: true,
    fields: {
      name: {
        read: false,
      },
    },
  },
];

export const UserProfile = mercury.createProfile('User', rules);

// Admin.profile.ts
import mercury from '@mercury-js/core';

const rules = [
  {
    modelName: 'User',
    access: {
      create: true,
      read: true,
      update: true,
      delete: true,
    },
  },
  {
    modelName: 'Account',
    access: {
      create: true,
      read: true,
      update: true,
      delete: true,
    },
  },
];

export const AdminProfile = mercury.createProfile('Admin', rules);

// index.ts
export { AdminProfile } from './Admin.profile';
export { UserProfile } from './User.profile';

Hooks

// User.hook.ts
import { hook } from '@mercury-js/core';

hook.before('CREATE_USER_RECORD', async function (this: any) {
  //modify data before create
  this.data.name = 'Test 1';
  this.data.test = 'Test 3';
});

hook.after('CREATE_USER_RECORD', async function (this: any, args: any) {
  console.log('Here');
  console.log('AFTER CREATE hook', this);
});

hook.before('UPDATE_USER_RECORD', function (this: any) {
  console.log('BEFORE UPDATE hook', this);
});

hook.after('UPDATE_USER_RECORD', function (this: any) {
  console.log('Here');

  console.log('AFTER UPDATE hook', this);
});

hook.before('DELETE_USER_RECORD', function (this: any) {
  console.log('BEFORE DELETE hook', this);
});

hook.after('DELETE_USER_RECORD', function (this: any) {
  console.log('Here');

  console.log('AFTER DELETE hook', this);
});

// index.ts

export { default as UserHook } from './User.hook';

License

MIT.

2.0.0-beta.21

2 months ago

2.0.0-beta.20

3 months ago

2.0.0-beta.19

3 months ago

2.0.0-beta.18

3 months ago

1.5.16

3 months ago

1.5.15

3 months ago

2.0.0-beta.15

4 months ago

2.0.0-beta.14

4 months ago

2.0.0-beta.13

4 months ago

2.0.0-beta.17

4 months ago

2.0.0-beta.16

4 months ago

2.0.0-beta.12

4 months ago

2.0.0-beta.11

4 months ago

2.0.0-beta.10

5 months ago

2.0.0-beta.9

5 months ago

2.0.0-beta.8

5 months ago

1.5.14

5 months ago

1.5.1-beta.1

10 months ago

1.5.1-beta.2

10 months ago

1.5.1-beta.3

9 months ago

1.5.1-beta.4

9 months ago

1.5.1-beta.5

9 months ago

1.5.1-beta.6

9 months ago

1.5.1-beta.7

8 months ago

1.5.9

8 months ago

1.5.8

8 months ago

1.5.7

8 months ago

1.5.6

8 months ago

2.0.0-beta.7

6 months ago

1.5.5

8 months ago

1.5.4

8 months ago

1.5.3

8 months ago

1.5.2

8 months ago

1.5.10

7 months ago

1.5.12

6 months ago

1.5.11

7 months ago

2.0.0-beta.2

6 months ago

1.5.13

5 months ago

2.0.0-beta.1

6 months ago

2.0.0-beta.6

6 months ago

2.0.0-beta.5

6 months ago

2.0.0-beta.4

6 months ago

2.0.0-beta.3

6 months ago

1.5.1-beta.0

11 months ago

1.5.1

1 year ago

1.4.2

1 year ago

1.5.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.10

1 year ago

1.3.9

1 year ago

1.3.11

1 year ago

1.3.8

2 years ago

1.3.6-alpha.1

2 years ago

1.3.6-alpha.0

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago