This module is a wrapper for sequelize used in our projects to handle database connection, versioning and auditory.
npm install @bowldevs/database
Open a database conexion
Param | Description |
---|---|
config | Object with the database config check sequelize documentation https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-constructor-constructor |
Returns a sequalize object https://sequelize.org/api/v6/class/src/sequelize.js~sequelize
import {
open,
} from "@bowldes/database";
const dbConfig = {
database: "test",
user: "user",
password: "password",
options: {
dialect: "sqlite",
storage: ":memory:",
logging: true,
},
};
const db = await open(dbConfig);
Define a database model (will represent a table (SQL) or a document (non-sql))
Param | Description |
---|---|
model_name | Name of the model, will generate a schema with the pluralize name in the database |
model_definition | Object with the model definition https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-method-define |
model_options | Object with the extra options |
Returns a Model object https://sequelize.org/api/v6/class/src/model.js~model
import {
DataTypes,
open,
defineModel
} from "@bowldes/database";
const dbConfig = {
database: "test",
user: "user",
password: "password",
options: {
dialect: "sqlite",
storage: ":memory:",
logging: true,
},
};
const db = await open(dbConfig);
const Stuff = defineModel("stuff", {
name: DataTypes.STRING,
});
Define a database model with created_by and updated_by auditory owner (will represent a table (SQL) or a document (non-sql)) Needs a base user model to be associated with
Param | Description |
---|---|
user_model_name | Name of the user model, will add the created_by and updated_by attributes to the model and associated with the user model |
model_name | Name of the model, will generate a schema with the pluralize name in the database |
model_definition | Object with the model definition https://sequelize.org/api/v6/class/src/sequelize.js~sequelize#instance-method-define |
model_options | Object with the extra options |
Returns a Model object https://sequelize.org/api/v6/class/src/model.js~model
import {
DataTypes,
open,
defineModel,
defineAuditedModel,
} from "@bowldes/database";
const dbConfig = {
database: "test",
user: "user",
password: "password",
options: {
dialect: "sqlite",
storage: ":memory:",
logging: true,
},
};
const db = await open(dbConfig);
const User = defineModel("user", {
"name"
});
const Stuff = defineAuditedModel("user","stuff", {
name: DataTypes.STRING,
});
Will generate or update the database schema
import {
DataTypes,
open,
defineModel,
defineAuditedModel,
sync
} from "@bowldes/database";
const dbConfig = {
database: "test",
user: "user",
password: "password",
options: {
dialect: "sqlite",
storage: ":memory:",
logging: true,
},
};
const db = await open(dbConfig);
const User = defineModel("user", {
"name"
});
const Stuff = defineAuditedModel("user", "stuff", {
name: DataTypes.STRING,
});
await sync();