· 9 years ago · Apr 04, 2017, 05:40 PM
1```
2models = [];
3fs.readdirSync(HOMEDIR + '/lib/models/waterline').forEach(function(file) {
4 models.push(require(HOMEDIR + '/lib/models/waterline/' + file));
5});
6
7module.exports = {
8 init: function(next) {
9 models.forEach(function(model) {
10 orm.loadCollection(model);
11 });
12
13 orm.initialize(config, function(err, models) {
14 if (err) throw err;
15 global.models = models.collections;
16 global.connections = models.connections;
17 next();
18 });
19 }
20};
21
22
23//And this in my config
24localhost: {
25 migrate: 'safe',
26 adapter: 'postgres',
27 database: 'intellinote',
28 host: 'localhost',
29 user: 'postgres',
30 password: '',
31 port: 5432
32 }
33```
34
35```
36DROP TABLE IF EXISTS "intellinotedb"."external_resource";
37CREATE TABLE "intellinotedb"."external_resource" (
38 "id" int8 NOT NULL DEFAULT nextval('external_resource_id_seq'::regclass),
39 "external_id" varchar(2000) NOT NULL COLLATE "default",
40 "version_id" varchar(2000) COLLATE "default",
41 "url" varchar(5000) COLLATE "default",
42 "name" varchar(4000) COLLATE "default",
43 "size" int8,
44 "creator" varchar(50) NOT NULL COLLATE "default",
45 "created_at" timestamp(6) NOT NULL DEFAULT now(),
46 "modified_at" timestamp(6) NULL,
47 "project_id" int8 NOT NULL,
48 "note_id" int8,
49 "type" varchar(50) NOT NULL COLLATE "default",
50 "is_public" bool NOT NULL DEFAULT false,
51 "state" varchar(100) NOT NULL DEFAULT 'ACTIVE'::character varying COLLATE "default",
52 "mime_type" text COLLATE "default",
53 "internal_type" text COLLATE "default",
54 "is_template" bool NOT NULL,
55 "has_filled_data" bool NOT NULL
56)
57WITH (OIDS=FALSE);
58ALTER TABLE "intellinotedb"."external_resource" OWNER TO "intellinote";
59
60-- ----------------------------
61-- Primary key structure for table external_resource
62-- ----------------------------
63ALTER TABLE "intellinotedb"."external_resource" ADD PRIMARY KEY ("id") NOT DEFERRABLE INITIALLY IMMEDIATE;
64
65-- ----------------------------
66-- Uniques structure for table external_resource
67-- ----------------------------
68ALTER TABLE "intellinotedb"."external_resource" ADD CONSTRAINT "external_resource_note_id_key" UNIQUE ("note_id") NOT DEFERRABLE INITIALLY IMMEDIATE;
69
70-- ----------------------------
71-- Foreign keys structure for table external_resource
72-- ----------------------------
73ALTER TABLE "intellinotedb"."external_resource" ADD CONSTRAINT "external_resource_note_id_fkey" FOREIGN KEY ("note_id") REFERENCES "intellinotedb"."note" ("id") ON UPDATE NO ACTION ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE;
74ALTER TABLE "intellinotedb"."external_resource" ADD CONSTRAINT "external_resource_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "intellinotedb"."project" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE;
75```
76
77```
78Waterline = require('waterline');
79
80module.exports = Waterline.Collection.extend({
81 tableName: 'external_resource',
82 meta: {
83 schemaName: process.env.WATERLINE_SCHEMA || 'intellinotedb'
84 },
85 connection: process.env.WATERLINE_DB || 'localhost',
86 attributes: {
87 id: {
88 type: 'integer',
89 autoIncrement: true,
90 primaryKey: true,
91 unique: true,
92 size: 8
93 },
94 external_id: {
95 type: 'string',
96 required: true,
97 size: 2000
98 },
99 version_id: {
100 type: 'string',
101 size: 2000
102 },
103 url: {
104 type: 'string',
105 required: true,
106 size: 5000
107 },
108 name: {
109 type: 'string',
110 required: true,
111 size: 4000
112 },
113 size: {
114 type: 'integer',
115 required: true,
116 size: 8
117 },
118 creator: {
119 type: 'string',
120 required: true,
121 size: 50
122 },
123 createdAt: {
124 type: 'datetime',
125 columnName: 'created_at'
126 },
127 updatedAt: {
128 type: 'datetime',
129 columnName: 'modified_at'
130 },
131 project_id: {
132 type: 'integer',
133 required: true
134 },
135 note_id: {
136 type: 'integer',
137 required: true,
138 size: 8
139 },
140 type: {
141 type: 'string',
142 defaultsTo: 'FILE',
143 required: true,
144 size: 50
145 },
146 is_public: {
147 type: 'boolean',
148 defaultsTo: true,
149 required: true
150 },
151 state: {
152 type: 'string',
153 enum: ['ACTIVE', 'DELETED'],
154 defaultsTo: 'ACTIVE',
155 required: true,
156 size: 100
157 },
158 mime_type: {
159 type: 'string',
160 required: true
161 },
162 internal_type: {
163 type: 'string',
164 defaultsTo: 'REGULAR',
165 required: true
166 },
167 is_template: {
168 type: 'boolean',
169 defaultsTo: false,
170 required: false
171 },
172 has_filled_data: {
173 type: 'boolean',
174 defaultsTo: false,
175 required: false
176 }
177 }
178});
179
180```