Skip to content

Instantly share code, notes, and snippets.

@mittalyashu
Created October 20, 2020 03:37
Show Gist options
  • Save mittalyashu/cbd1a01da02372c0386ac6d3ebd548e2 to your computer and use it in GitHub Desktop.
Save mittalyashu/cbd1a01da02372c0386ac6d3ebd548e2 to your computer and use it in GitHub Desktop.
Centinni plans and subscriptions APIs and db models
const { buildASTSchema } = require("graphql");
const { mergeTypeDefs } = require("@graphql-tools/merge");
// schema
const mutationOutput = require("./mutationOutput");
const book = require("./types/book");
const chapter = require("./types/chapter");
const genre = require("./types/genre");
const blog = require("./types/blog");
const file = require("./types/file");
const popular = require("./types/popular");
const user = require("./types/user");
const bookmark = require("./types/bookmark");
const transaction = require("./types/transaction");
const purchase = require("./types/purchase");
const subscription = require("./types/subscription");
const plan = require("./types/plan");
module.exports = buildASTSchema(
mergeTypeDefs([
mutationOutput,
book,
chapter,
genre,
blog,
file,
popular,
user,
bookmark,
transaction,
purchase,
subscription,
plan
])
);
module.exports = `
scalar GUID
scalar DateTime
type Plan {
planId: GUID!
name: String!
monthlyPlanAmount: Int!
yearlyPlanAmount: Int
createdAt: DateTime!
updatedAt: DateTime!
}
`;
module.exports = `
scalar GUID
scalar DateTime
type Subscription {
subscriptionId: GUID!
userId: GUID!
planId: GUID!
interval: SubscriptionInvterval!
periodEnd: DateTime!
createdAt: DateTime!
}
enum SubscriptionInvterval {
monthly
yearly
}
`;
const { DataTypes } = require("sequelize");
const database = require("..");
// models
const bookmark = require("./bookmark");
const transaction = require("./transaction");
const purchase = require("./purchase");
const subscription = require("./subscription");
const user = database.define("user", {
userId: {
type: DataTypes.UUID,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
primaryKey: true
},
name: {
type: DataTypes.STRING(100),
allowNull: true
},
emailAddress: {
type: DataTypes.STRING(200),
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING(300),
allowNull: false
},
username: {
type: DataTypes.STRING(40),
allowNull: false
},
avatar: {
type: DataTypes.STRING(200),
allowNull: true
},
isVerified: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
isOwner: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
isModerator: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
isBlocked: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
});
user.hasMany(bookmark, {
onDelete: "CASCADE",
foreignKey: {
name: "userId",
type: DataTypes.UUID,
allowNull: false
}
});
user.hasOne(transaction, {
onDelete: "CASCADE",
foreignKey: {
name: "userId",
type: DataTypes.UUID,
allowNull: false
}
});
user.hasMany(purchase, {
foreignKey: {
name: "userId",
type: DataTypes.UUID,
allowNull: false
}
});
user.hasMany(subscription, {
foreignKey: {
name: "userId",
type: DataTypes.UUID,
allowNull: false
}
});
module.exports = user;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment