schema.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { entityKind, is } from "../entity.js";
  2. import { SQL, sql } from "../sql/sql.js";
  3. import { pgEnumObjectWithSchema, pgEnumWithSchema } from "./columns/enum.js";
  4. import { pgSequenceWithSchema } from "./sequence.js";
  5. import { pgTableWithSchema } from "./table.js";
  6. import { pgMaterializedViewWithSchema, pgViewWithSchema } from "./view.js";
  7. class PgSchema {
  8. constructor(schemaName) {
  9. this.schemaName = schemaName;
  10. }
  11. static [entityKind] = "PgSchema";
  12. table = (name, columns, extraConfig) => {
  13. return pgTableWithSchema(name, columns, extraConfig, this.schemaName);
  14. };
  15. view = (name, columns) => {
  16. return pgViewWithSchema(name, columns, this.schemaName);
  17. };
  18. materializedView = (name, columns) => {
  19. return pgMaterializedViewWithSchema(name, columns, this.schemaName);
  20. };
  21. enum(enumName, input) {
  22. return Array.isArray(input) ? pgEnumWithSchema(
  23. enumName,
  24. [...input],
  25. this.schemaName
  26. ) : pgEnumObjectWithSchema(enumName, input, this.schemaName);
  27. }
  28. sequence = (name, options) => {
  29. return pgSequenceWithSchema(name, options, this.schemaName);
  30. };
  31. getSQL() {
  32. return new SQL([sql.identifier(this.schemaName)]);
  33. }
  34. shouldOmitSQLParens() {
  35. return true;
  36. }
  37. }
  38. function isPgSchema(obj) {
  39. return is(obj, PgSchema);
  40. }
  41. function pgSchema(name) {
  42. if (name === "public") {
  43. throw new Error(
  44. `You can't specify 'public' as schema name. Postgres is using public schema by default. If you want to use 'public' schema, just use pgTable() instead of creating a schema`
  45. );
  46. }
  47. return new PgSchema(name);
  48. }
  49. export {
  50. PgSchema,
  51. isPgSchema,
  52. pgSchema
  53. };
  54. //# sourceMappingURL=schema.js.map