id generation in utilities module

This commit is contained in:
Michael Shanks 2020-05-18 16:22:09 +01:00
parent 1c01014bdc
commit c49710c3ff
5 changed files with 13 additions and 9 deletions

View File

@ -1,7 +1,7 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb") const ClientDb = require("../../db/clientDb")
const { getPackageForBuilder } = require("../../utilities/builder") const { getPackageForBuilder } = require("../../utilities/builder")
const uuid = require("uuid") const newid = require("../../db/newid")
const env = require("../../environment") const env = require("../../environment")
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
@ -24,7 +24,7 @@ exports.create = async function(ctx) {
const db = new CouchDB(ClientDb.name(env.CLIENT_ID)) const db = new CouchDB(ClientDb.name(env.CLIENT_ID))
const newApplication = { const newApplication = {
_id: uuid.v4().replace(/-/g, ""), _id: newid(),
type: "app", type: "app",
instances: [], instances: [],
userInstanceMap: {}, userInstanceMap: {},

View File

@ -1,12 +1,11 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const client = require("../../db/clientDb") const client = require("../../db/clientDb")
const uuid = require("uuid") const newid = require("../../db/newid")
const env = require("../../environment") const env = require("../../environment")
exports.create = async function(ctx) { exports.create = async function(ctx) {
const instanceName = ctx.request.body.name const instanceName = ctx.request.body.name
const uid = uuid.v4().replace(/-/g, "") const instanceId = `inst_${ctx.params.applicationId.substring(0, 7)}_${newid()}`
const instanceId = `inst_${ctx.params.applicationId.substring(0, 7)}_${uid}`
const { applicationId } = ctx.params const { applicationId } = ctx.params
const clientId = env.CLIENT_ID const clientId = env.CLIENT_ID
const db = new CouchDB(instanceId) const db = new CouchDB(instanceId)

View File

@ -1,5 +1,5 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const uuid = require("uuid") const newid = require("../../db/newid")
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.params.instanceId) const db = new CouchDB(ctx.params.instanceId)
@ -15,7 +15,7 @@ exports.create = async function(ctx) {
const newModel = { const newModel = {
type: "model", type: "model",
...ctx.request.body, ...ctx.request.body,
_id: uuid.v4().replace(/-/g, ""), _id: newid(),
} }
const result = await db.post(newModel) const result = await db.post(newModel)

View File

@ -1,6 +1,6 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const Ajv = require("ajv") const Ajv = require("ajv")
const uuid = require("uuid") const newid = require("../../db/newid")
const ajv = new Ajv() const ajv = new Ajv()
@ -9,7 +9,7 @@ exports.save = async function(ctx) {
const record = ctx.request.body const record = ctx.request.body
if (!record._rev && !record._id) { if (!record._rev && !record._id) {
record._id = uuid.v4().replace(/-/, "") record._id = newid()
} }
// validation with ajv // validation with ajv

View File

@ -0,0 +1,5 @@
const { v4 } = require("uuid")
module.exports = function() {
return v4().replace(/-/g, "")
}